diff --git a/p2p/adapters/rlpx.go b/p2p/adapters/rlpx.go index e1d8492dc3..ffa78664a7 100644 --- a/p2p/adapters/rlpx.go +++ b/p2p/adapters/rlpx.go @@ -60,11 +60,11 @@ func NewReportingRLPx(addr []byte, srv *p2p.Server, m Messenger, r Reporter) *RL return rlpx } -func (*RLPxMessenger) SendMsg(w p2p.MsgWriter, code uint64, msg interface{}) error { +func (RLPxMessenger) SendMsg(w p2p.MsgWriter, code uint64, msg interface{}) error { return p2p.Send(w, code, msg) } -func (*RLPxMessenger) ReadMsg(r p2p.MsgReader) (p2p.Msg, error) { +func (RLPxMessenger) ReadMsg(r p2p.MsgReader) (p2p.Msg, error) { return r.ReadMsg() } diff --git a/p2p/protocols/protocol.go b/p2p/protocols/protocol.go index aa7e0c3530..06a51272bf 100644 --- a/p2p/protocols/protocol.go +++ b/p2p/protocols/protocol.go @@ -36,6 +36,7 @@ import ( "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/p2p/adapters" ) // error codes used by this protocol scheme @@ -114,11 +115,11 @@ func errorf(code int, format string, params ...interface{}) *Error { // listing the message codes and types etc // and further metadata about the protocol type CodeMap struct { - Name string // name of the protocol - Version uint // version - MaxMsgSize int // max length of message payload size - codes []reflect.Type // index of codes to msg types - to create zero values - messages map[reflect.Type]uint // index of types to codes, for sending by type + Name string // name of the protocol + Version uint // version + MaxMsgSize int // max length of message payload size + codes []reflect.Type // index of codes to msg types - to create zero values + messages map[reflect.Type]uint64 // index of types to codes, for sending by type } func NewCodeMap(name string, version uint, maxMsgSize int, msgs ...interface{}) *CodeMap { @@ -126,18 +127,22 @@ func NewCodeMap(name string, version uint, maxMsgSize int, msgs ...interface{}) Name: name, Version: version, MaxMsgSize: maxMsgSize, - messages: make(map[reflect.Type]uint), + messages: make(map[reflect.Type]uint64), } self.Register(msgs...) return self } +func (self *CodeMap) GetCode(msg interface{}) uint64 { + return self.messages[reflect.TypeOf(msg)] +} + func (self *CodeMap) Length() uint64 { return uint64(len(self.codes)) } func (self *CodeMap) Register(msgs ...interface{}) { - code := uint(len(self.codes)) + code := uint64(len(self.codes)) for _, msg := range msgs { typ := reflect.TypeOf(msg) _, found := self.messages[typ] @@ -156,23 +161,18 @@ func (self *CodeMap) Register(msgs ...interface{}) { // a remote peer type Peer struct { ct *CodeMap // CodeMap for the protocol - m Messenger // defines senf and receive + m adapters.Messenger // defines senf and receive *p2p.Peer // the p2p.Peer object representing the remote rw p2p.MsgReadWriter // p2p.MsgReadWriter to send messages to and read messages from handlers map[reflect.Type][]func(interface{}) error // message type -> message handler callback(s) map disconnect func() // Disconnect function set differently for testing } -type Messenger interface { - SendMsg(p2p.MsgWriter, uint64, interface{}) error - ReadMsg(p2p.MsgReader) (p2p.Msg, error) -} - // NewPeer returns a new peer // this constructor is called by the p2p.Protocol#Run function // the first two arguments are comming the arguments passed to p2p.Protocol.Run function // the third argument is the CodeMap describing the protocol messages and options -func NewPeer(p *p2p.Peer, rw p2p.MsgReadWriter, ct *CodeMap, m Messenger, disconn func()) *Peer { +func NewPeer(p *p2p.Peer, rw p2p.MsgReadWriter, ct *CodeMap, m adapters.Messenger, disconn func()) *Peer { return &Peer{ ct: ct, m: m, @@ -191,7 +191,7 @@ func NewPeer(p *p2p.Peer, rw p2p.MsgReadWriter, ct *CodeMap, m Messenger, discon // handlers are assumed to be static across handshake renegotiations // i.e., a service instance either handles a message or not (irrespective of the handshake) // it panics if the message type is not defined in the CodeMap -func (self *Peer) Register(msg interface{}, handler func(interface{}) error) uint { +func (self *Peer) Register(msg interface{}, handler func(interface{}) error) uint64 { typ := reflect.TypeOf(msg) code, found := self.ct.messages[typ] if !found { diff --git a/swarm/network/protocol.go b/swarm/network/protocol.go index 40f65306f5..aea06ba54d 100644 --- a/swarm/network/protocol.go +++ b/swarm/network/protocol.go @@ -62,9 +62,9 @@ type Node interface { String() string // pretty printable the Node ID() discover.NodeID // the key that uniquely identifies the Node for the peerPool - Send(interface{}) error // can send messages - Drop() // disconnect this peer - Register(interface{}, func(interface{}) error) uint // register message-handler callbacks + Send(interface{}) error // can send messages + Drop() // disconnect this peer + Register(interface{}, func(interface{}) error) uint64 // register message-handler callbacks } // PeerPool is the interface for the connectivity manager