github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/network/p2p/protocol.go (about)

     1  package p2p
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/neatlab/neatio/network/p2p/discover"
     7  )
     8  
     9  type Protocol struct {
    10  	Name string
    11  
    12  	Version uint
    13  
    14  	Length uint64
    15  
    16  	Run func(peer *Peer, rw MsgReadWriter) error
    17  
    18  	NodeInfo func() interface{}
    19  
    20  	PeerInfo func(id discover.NodeID) interface{}
    21  }
    22  
    23  func (p Protocol) cap() Cap {
    24  	return Cap{p.Name, p.Version}
    25  }
    26  
    27  type Cap struct {
    28  	Name    string
    29  	Version uint
    30  }
    31  
    32  func (cap Cap) RlpData() interface{} {
    33  	return []interface{}{cap.Name, cap.Version}
    34  }
    35  
    36  func (cap Cap) String() string {
    37  	return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
    38  }
    39  
    40  type capsByNameAndVersion []Cap
    41  
    42  func (cs capsByNameAndVersion) Len() int      { return len(cs) }
    43  func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
    44  func (cs capsByNameAndVersion) Less(i, j int) bool {
    45  	return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version)
    46  }