p2p: use package rlp for baseProtocol

This commit is contained in:
Felix Lange
2014-11-25 12:25:31 +01:00
parent c1fca72552
commit 6049fcd52a
4 changed files with 71 additions and 58 deletions

View File

@ -41,14 +41,22 @@ func encodePayload(params ...interface{}) []byte {
return buf.Bytes()
}
// Data returns the decoded RLP payload items in a message.
func (msg Msg) Data() (*ethutil.Value, error) {
s := rlp.NewListStream(msg.Payload, uint64(msg.Size))
// Value returns the decoded RLP payload items in a message.
func (msg Msg) Value() (*ethutil.Value, error) {
var v []interface{}
err := s.Decode(&v)
err := msg.Decode(&v)
return ethutil.NewValue(v), err
}
// Decode parse the RLP content of a message into
// the given value, which must be a pointer.
//
// For the decoding rules, please see package rlp.
func (msg Msg) Decode(val interface{}) error {
s := rlp.NewListStream(msg.Payload, uint64(msg.Size))
return s.Decode(val)
}
// Discard reads any remaining payload data into a black hole.
func (msg Msg) Discard() error {
_, err := io.Copy(ioutil.Discard, msg.Payload)
@ -91,7 +99,7 @@ func MsgLoop(r MsgReader, maxsize uint32, f func(code uint64, data *ethutil.Valu
if msg.Size > maxsize {
return newPeerError(errInvalidMsg, "size %d exceeds maximum size of %d", msg.Size, maxsize)
}
value, err := msg.Data()
value, err := msg.Value()
if err != nil {
return err
}