github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/p2p/protocol.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:44</date>
    10  //</624342659875475456>
    11  
    12  
    13  package p2p
    14  
    15  import (
    16  	"fmt"
    17  
    18  	"github.com/ethereum/go-ethereum/p2p/discover"
    19  )
    20  
    21  //协议表示P2P子协议实现。
    22  type Protocol struct {
    23  //名称应包含官方协议名称,
    24  //通常是三个字母的单词。
    25  	Name string
    26  
    27  //版本应包含协议的版本号。
    28  	Version uint
    29  
    30  //长度应包含使用的消息代码数
    31  //按照协议。
    32  	Length uint64
    33  
    34  //当协议
    35  //与同行协商。它应该读写来自
    36  //RW。每个消息的有效负载必须完全消耗。
    37  //
    38  //当Start返回时,对等连接将关闭。它应该会回来
    39  //任何协议级错误(如I/O错误),即
    40  //遇到。
    41  	Run func(peer *Peer, rw MsgReadWriter) error
    42  
    43  //nodeinfo是用于检索协议特定元数据的可选助手方法
    44  //关于主机节点。
    45  	NodeInfo func() interface{}
    46  
    47  //peerinfo是一个可选的帮助器方法,用于检索协议特定的元数据
    48  //关于网络中的某个对等点。如果设置了信息检索功能,
    49  //但返回nil,假设协议握手仍在运行。
    50  	PeerInfo func(id discover.NodeID) interface{}
    51  }
    52  
    53  func (p Protocol) cap() Cap {
    54  	return Cap{p.Name, p.Version}
    55  }
    56  
    57  //cap是对等能力的结构。
    58  type Cap struct {
    59  	Name    string
    60  	Version uint
    61  }
    62  
    63  func (cap Cap) RlpData() interface{} {
    64  	return []interface{}{cap.Name, cap.Version}
    65  }
    66  
    67  func (cap Cap) String() string {
    68  	return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
    69  }
    70  
    71  type capsByNameAndVersion []Cap
    72  
    73  func (cs capsByNameAndVersion) Len() int      { return len(cs) }
    74  func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
    75  func (cs capsByNameAndVersion) Less(i, j int) bool {
    76  	return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version)
    77  }
    78