github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/p2p/protocol.go (about)

     1  package p2p
     2  
     3  import "fmt"
     4  
     5  // Protocol represents a P2P subprotocol implementation.
     6  type Protocol struct {
     7  	// Name should contain the official protocol name,
     8  	// often a three-letter word.
     9  	Name string
    10  
    11  	// Version should contain the version number of the protocol.
    12  	Version uint
    13  
    14  	// Length should contain the number of message codes used
    15  	// by the protocol.
    16  	Length uint64
    17  
    18  	// Run is called in a new groutine when the protocol has been
    19  	// negotiated with a peer. It should read and write messages from
    20  	// rw. The Payload for each message must be fully consumed.
    21  	//
    22  	// The peer connection is closed when Start returns. It should return
    23  	// any protocol-level error (such as an I/O error) that is
    24  	// encountered.
    25  	Run func(peer *Peer, rw MsgReadWriter) error
    26  }
    27  
    28  func (p Protocol) cap() Cap {
    29  	return Cap{p.Name, p.Version}
    30  }
    31  
    32  // Cap is the structure of a peer capability.
    33  type Cap struct {
    34  	Name    string
    35  	Version uint
    36  }
    37  
    38  func (cap Cap) RlpData() interface{} {
    39  	return []interface{}{cap.Name, cap.Version}
    40  }
    41  
    42  func (cap Cap) String() string {
    43  	return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
    44  }
    45  
    46  type capsByName []Cap
    47  
    48  func (cs capsByName) Len() int           { return len(cs) }
    49  func (cs capsByName) Less(i, j int) bool { return cs[i].Name < cs[j].Name }
    50  func (cs capsByName) Swap(i, j int)      { cs[i], cs[j] = cs[j], cs[i] }