protocols

package
v0.0.0-...-b78b3a4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2019 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrMsgTooLong = iota
	ErrDecode
	ErrWrite
	ErrInvalidMsgCode
	ErrInvalidMsgType
	ErrHandshake
	ErrNoHandler
	ErrHandler
)

此协议方案使用的错误代码

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Code int
	// contains filtered or unexported fields
}

func (Error) Error

func (e Error) Error() (message string)

type Peer

type Peer struct {
	*p2p.Peer //代表远程的p2p.peer对象
	// contains filtered or unexported fields
}

对等机表示在与的对等连接上运行的远程对等机或协议实例 远程对等体

func NewPeer

func NewPeer(p *p2p.Peer, rw p2p.MsgReadWriter, spec *Spec) *Peer

new peer构造新的peer 此构造函数由p2p.protocol run函数调用 前两个参数是传递给p2p.protocol.run函数的参数 第三个参数是描述协议的规范

func (*Peer) Drop

func (p *Peer) Drop(err error)

DROP断开对等机的连接。 TODO:可能只需要实现协议删除?不想把同伴踢开 如果它们对其他协议有用

func (*Peer) Handshake

func (p *Peer) Handshake(ctx context.Context, hs interface{}, verify func(interface{}) error) (rhs interface{}, err error)

握手在对等连接上协商握手 *参数 *上下文 *要发送到远程对等机的本地握手 *远程握手时要调用的函数(可以为零) *需要相同类型的远程握手 *拨号对等端需要先发送握手,然后等待远程 *侦听对等机等待远程握手,然后发送它 返回远程握手和错误

func (*Peer) Run

func (p *Peer) Run(handler func(ctx context.Context, msg interface{}) error) error

运行启动处理传入消息的Forever循环 在p2p.protocol run函数中调用 handler参数是为接收到的每个消息调用的函数。 从远程对等机返回的错误导致循环退出 导致断开

func (*Peer) Send

func (p *Peer) Send(ctx context.Context, msg interface{}) error

send接收一条消息,将其编码为rlp,找到正确的消息代码并发送 向对等端发送消息 这个低级调用将由提供路由或广播发送的库包装。 但通常只用于转发和将消息推送到直接连接的对等端

type Spec

type Spec struct {
	//名称是协议的名称,通常是三个字母的单词
	Name string

	//version是协议的版本号
	Version uint

	//maxmsgsize是消息有效负载的最大可接受长度
	MaxMsgSize uint32

	//messages是此协议使用的消息数据类型的列表,
	//发送的每个消息类型及其数组索引作为代码(因此
	//[&foo,&bar,&baz]将发送带有代码的foo、bar和baz
	//分别为0、1和2)
	//每条消息必须有一个唯一的数据类型
	Messages []interface{}
	// contains filtered or unexported fields
}

规范是一种协议规范,包括其名称和版本以及 交换的消息类型

func (*Spec) GetCode

func (s *Spec) GetCode(msg interface{}) (uint64, bool)

getcode返回一个类型的消息代码,Boolean第二个参数是 如果未找到消息类型,则为false

func (*Spec) Length

func (s *Spec) Length() uint64

length返回协议中的消息类型数

func (*Spec) NewMsg

func (s *Spec) NewMsg(code uint64) (interface{}, bool)

newmsg构造给定代码的新消息类型

type WrappedMsg

type WrappedMsg struct {
	Context []byte
	Size    uint32
	Payload []byte
}

wrappedmsg用于在消息有效负载旁边传播已封送的上下文

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL