p2p/adapters: Removed pointer receiver for RLPxMessenger

Pointer rcvr incompatible with protocols.NewPeer

p2p: Removed duplicate declaration of Messenger interface

p2p/protocols,swarm/network: Codemap primitive type correction
Codemap was using uint for message types
But Messenger.SendMsg needs uint64
This commit is contained in:
nolash 2017-01-27 15:57:55 +01:00 committed by Lewis Marshall
parent 7a6ff56333
commit 3c84c329f9
3 changed files with 20 additions and 20 deletions

View file

@ -60,11 +60,11 @@ func NewReportingRLPx(addr []byte, srv *p2p.Server, m Messenger, r Reporter) *RL
return rlpx 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) 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() return r.ReadMsg()
} }

View file

@ -36,6 +36,7 @@ import (
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/adapters"
) )
// error codes used by this protocol scheme // error codes used by this protocol scheme
@ -118,7 +119,7 @@ type CodeMap struct {
Version uint // version Version uint // version
MaxMsgSize int // max length of message payload size MaxMsgSize int // max length of message payload size
codes []reflect.Type // index of codes to msg types - to create zero values 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 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 { 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, Name: name,
Version: version, Version: version,
MaxMsgSize: maxMsgSize, MaxMsgSize: maxMsgSize,
messages: make(map[reflect.Type]uint), messages: make(map[reflect.Type]uint64),
} }
self.Register(msgs...) self.Register(msgs...)
return self return self
} }
func (self *CodeMap) GetCode(msg interface{}) uint64 {
return self.messages[reflect.TypeOf(msg)]
}
func (self *CodeMap) Length() uint64 { func (self *CodeMap) Length() uint64 {
return uint64(len(self.codes)) return uint64(len(self.codes))
} }
func (self *CodeMap) Register(msgs ...interface{}) { func (self *CodeMap) Register(msgs ...interface{}) {
code := uint(len(self.codes)) code := uint64(len(self.codes))
for _, msg := range msgs { for _, msg := range msgs {
typ := reflect.TypeOf(msg) typ := reflect.TypeOf(msg)
_, found := self.messages[typ] _, found := self.messages[typ]
@ -156,23 +161,18 @@ func (self *CodeMap) Register(msgs ...interface{}) {
// a remote peer // a remote peer
type Peer struct { type Peer struct {
ct *CodeMap // CodeMap for the protocol 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 *p2p.Peer // the p2p.Peer object representing the remote
rw p2p.MsgReadWriter // p2p.MsgReadWriter to send messages to and read messages from 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 handlers map[reflect.Type][]func(interface{}) error // message type -> message handler callback(s) map
disconnect func() // Disconnect function set differently for testing 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 // NewPeer returns a new peer
// this constructor is called by the p2p.Protocol#Run function // 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 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 // 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{ return &Peer{
ct: ct, ct: ct,
m: m, 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 // handlers are assumed to be static across handshake renegotiations
// i.e., a service instance either handles a message or not (irrespective of the handshake) // 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 // 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) typ := reflect.TypeOf(msg)
code, found := self.ct.messages[typ] code, found := self.ct.messages[typ]
if !found { if !found {

View file

@ -64,7 +64,7 @@ type Node interface {
Send(interface{}) error // can send messages Send(interface{}) error // can send messages
Drop() // disconnect this peer Drop() // disconnect this peer
Register(interface{}, func(interface{}) error) uint // register message-handler callbacks Register(interface{}, func(interface{}) error) uint64 // register message-handler callbacks
} }
// PeerPool is the interface for the connectivity manager // PeerPool is the interface for the connectivity manager