]> Sergey Matveev's repositories - btrtrc.git/blobdiff - peer_protocol/protocol.go
Fix request/chunk confusion, missing outgoing message prefix, protocol tests; improve...
[btrtrc.git] / peer_protocol / protocol.go
index dfef720e3a148ada07030e194b49e7c8f8d126dd..4020c5240d3f8066b1799f87993f2c973ea4db1f 100644 (file)
@@ -68,7 +68,11 @@ func (msg Message) MarshalBinary() (data []byte, err error) {
        default:
                err = errors.New("unknown message type")
        }
-       data = buf.Bytes()
+       data = make([]byte, 4+buf.Len())
+       binary.BigEndian.PutUint32(data, uint32(buf.Len()))
+       if buf.Len() != copy(data[4:], buf.Bytes()) {
+               panic("bad copy")
+       }
        return
 }
 
@@ -113,16 +117,6 @@ func (d *Decoder) Decode(msg *Message) (err error) {
        return
 }
 
-func encodeMessage(type_ MessageType, data interface{}) []byte {
-       w := &bytes.Buffer{}
-       w.WriteByte(byte(type_))
-       err := binary.Write(w, binary.BigEndian, data)
-       if err != nil {
-               panic(err)
-       }
-       return w.Bytes()
-}
-
 type Bytes []byte
 
 func (b Bytes) MarshalBinary() ([]byte, error) {