github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/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 19:16:41</date> 10 //</624450105721753600> 11 12 13 package p2p 14 15 import ( 16 "fmt" 17 18 "github.com/ethereum/go-ethereum/p2p/enode" 19 "github.com/ethereum/go-ethereum/p2p/enr" 20 ) 21 22 //协议表示P2P子协议实现。 23 type Protocol struct { 24 //名称应包含官方协议名称, 25 //通常是三个字母的单词。 26 Name string 27 28 //版本应包含协议的版本号。 29 Version uint 30 31 //长度应包含使用的消息代码数 32 //按照协议。 33 Length uint64 34 35 //当协议 36 //与同行协商。它应该读写来自 37 //RW。每个消息的有效负载必须完全消耗。 38 // 39 //当Start返回时,对等连接将关闭。它应该会回来 40 //任何协议级错误(如I/O错误),即 41 //遇到。 42 Run func(peer *Peer, rw MsgReadWriter) error 43 44 //nodeinfo是用于检索协议特定元数据的可选助手方法 45 //关于主机节点。 46 NodeInfo func() interface{} 47 48 //peerinfo是一个可选的帮助器方法,用于检索协议特定的元数据 49 //关于网络中的某个对等点。如果设置了信息检索功能, 50 //但返回nil,假设协议握手仍在运行。 51 PeerInfo func(id enode.ID) interface{} 52 53 //属性包含节点记录的协议特定信息。 54 Attributes []enr.Entry 55 } 56 57 func (p Protocol) cap() Cap { 58 return Cap{p.Name, p.Version} 59 } 60 61 //cap是对等能力的结构。 62 type Cap struct { 63 Name string 64 Version uint 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 79 func (capsByNameAndVersion) ENRKey() string { return "cap" } 80