p2p: use package rlp to encode messages
Message encoding functions have been renamed to catch any uses. The switch to the new encoder can cause subtle incompatibilities. If there are any users outside of our tree, they will at least be alerted that there was a change. NewMsg no longer exists. The replacements for EncodeMsg are called Send and SendItems.
This commit is contained in:
@ -11,7 +11,6 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
@ -28,13 +27,7 @@ type Msg struct {
|
||||
Payload io.Reader
|
||||
}
|
||||
|
||||
// NewMsg creates an RLP-encoded message with the given code.
|
||||
func NewMsg(code uint64, params ...interface{}) Msg {
|
||||
p := bytes.NewReader(common.Encode(params))
|
||||
return Msg{Code: code, Size: uint32(p.Len()), Payload: p}
|
||||
}
|
||||
|
||||
// Decode parse the RLP content of a message into
|
||||
// Decode parses the RLP content of a message into
|
||||
// the given value, which must be a pointer.
|
||||
//
|
||||
// For the decoding rules, please see package rlp.
|
||||
@ -76,13 +69,30 @@ type MsgReadWriter interface {
|
||||
MsgWriter
|
||||
}
|
||||
|
||||
// EncodeMsg writes an RLP-encoded message with the given code and
|
||||
// data elements.
|
||||
func EncodeMsg(w MsgWriter, code uint64, data ...interface{}) error {
|
||||
return w.WriteMsg(NewMsg(code, data...))
|
||||
// Send writes an RLP-encoded message with the given code.
|
||||
// data should encode as an RLP list.
|
||||
func Send(w MsgWriter, msgcode uint64, data interface{}) error {
|
||||
size, r, err := rlp.EncodeToReader(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return w.WriteMsg(Msg{Code: msgcode, Size: uint32(size), Payload: r})
|
||||
}
|
||||
|
||||
// netWrapper wrapsa MsgReadWriter with locks around
|
||||
// SendItems writes an RLP with the given code and data elements.
|
||||
// For a call such as:
|
||||
//
|
||||
// SendItems(w, code, e1, e2, e3)
|
||||
//
|
||||
// the message payload will be an RLP list containing the items:
|
||||
//
|
||||
// [e1, e2, e3]
|
||||
//
|
||||
func SendItems(w MsgWriter, msgcode uint64, elems ...interface{}) error {
|
||||
return Send(w, msgcode, elems)
|
||||
}
|
||||
|
||||
// netWrapper wraps a MsgReadWriter with locks around
|
||||
// ReadMsg/WriteMsg and applies read/write deadlines.
|
||||
type netWrapper struct {
|
||||
rmu, wmu sync.Mutex
|
||||
|
Reference in New Issue
Block a user