2018-02-02 18:29:57 +08:00
|
|
|
package peer_protocol
|
|
|
|
|
|
|
|
import (
|
2021-09-30 07:01:10 +08:00
|
|
|
"bufio"
|
2018-02-02 18:29:57 +08:00
|
|
|
"bytes"
|
2021-09-30 07:01:10 +08:00
|
|
|
"encoding"
|
2018-02-02 18:29:57 +08:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2019-10-01 16:13:55 +08:00
|
|
|
// This is a lazy union representing all the possible fields for messages. Go doesn't have ADTs, and
|
|
|
|
// I didn't choose to use type-assertions.
|
2018-02-02 18:29:57 +08:00
|
|
|
type Message struct {
|
|
|
|
Keepalive bool
|
|
|
|
Type MessageType
|
|
|
|
Index, Begin, Length Integer
|
|
|
|
Piece []byte
|
|
|
|
Bitfield []bool
|
2018-07-10 10:20:36 +08:00
|
|
|
ExtendedID ExtensionNumber
|
2018-02-02 18:29:57 +08:00
|
|
|
ExtendedPayload []byte
|
|
|
|
Port uint16
|
|
|
|
}
|
|
|
|
|
2021-09-30 07:01:10 +08:00
|
|
|
var _ interface {
|
|
|
|
encoding.BinaryUnmarshaler
|
|
|
|
encoding.BinaryMarshaler
|
|
|
|
} = (*Message)(nil)
|
|
|
|
|
2018-02-02 18:29:57 +08:00
|
|
|
func MakeCancelMessage(piece, offset, length Integer) Message {
|
|
|
|
return Message{
|
|
|
|
Type: Cancel,
|
|
|
|
Index: piece,
|
|
|
|
Begin: offset,
|
|
|
|
Length: length,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 19:33:21 +08:00
|
|
|
func (msg Message) RequestSpec() (ret RequestSpec) {
|
|
|
|
return RequestSpec{
|
|
|
|
msg.Index,
|
|
|
|
msg.Begin,
|
|
|
|
func() Integer {
|
|
|
|
if msg.Type == Piece {
|
|
|
|
return Integer(len(msg.Piece))
|
|
|
|
} else {
|
|
|
|
return msg.Length
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
}
|
2018-07-12 07:16:17 +08:00
|
|
|
}
|
|
|
|
|
2018-02-02 18:29:57 +08:00
|
|
|
func (msg Message) MustMarshalBinary() []byte {
|
|
|
|
b, err := msg.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg Message) MarshalBinary() (data []byte, err error) {
|
2021-09-30 07:45:50 +08:00
|
|
|
var buf bytes.Buffer
|
2018-02-02 18:29:57 +08:00
|
|
|
if !msg.Keepalive {
|
|
|
|
err = buf.WriteByte(byte(msg.Type))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch msg.Type {
|
|
|
|
case Choke, Unchoke, Interested, NotInterested, HaveAll, HaveNone:
|
|
|
|
case Have:
|
2021-09-30 07:45:50 +08:00
|
|
|
err = binary.Write(&buf, binary.BigEndian, msg.Index)
|
2018-02-02 18:29:57 +08:00
|
|
|
case Request, Cancel, Reject:
|
|
|
|
for _, i := range []Integer{msg.Index, msg.Begin, msg.Length} {
|
2021-09-30 07:45:50 +08:00
|
|
|
err = binary.Write(&buf, binary.BigEndian, i)
|
2018-02-02 18:29:57 +08:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Bitfield:
|
|
|
|
_, err = buf.Write(marshalBitfield(msg.Bitfield))
|
|
|
|
case Piece:
|
|
|
|
for _, i := range []Integer{msg.Index, msg.Begin} {
|
2021-09-30 07:45:50 +08:00
|
|
|
err = binary.Write(&buf, binary.BigEndian, i)
|
2018-02-02 18:29:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n, err := buf.Write(msg.Piece)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if n != len(msg.Piece) {
|
|
|
|
panic(n)
|
|
|
|
}
|
|
|
|
case Extended:
|
2018-07-10 10:20:36 +08:00
|
|
|
err = buf.WriteByte(byte(msg.ExtendedID))
|
2018-02-02 18:29:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = buf.Write(msg.ExtendedPayload)
|
|
|
|
case Port:
|
2021-09-30 07:45:50 +08:00
|
|
|
err = binary.Write(&buf, binary.BigEndian, msg.Port)
|
2018-02-02 18:29:57 +08:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unknown message type: %v", msg.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalBitfield(bf []bool) (b []byte) {
|
|
|
|
b = make([]byte, (len(bf)+7)/8)
|
|
|
|
for i, have := range bf {
|
|
|
|
if !have {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c := b[i/8]
|
|
|
|
c |= 1 << uint(7-i%8)
|
|
|
|
b[i/8] = c
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-09-30 07:01:10 +08:00
|
|
|
|
|
|
|
func (me *Message) UnmarshalBinary(b []byte) error {
|
|
|
|
d := Decoder{
|
|
|
|
R: bufio.NewReader(bytes.NewReader(b)),
|
|
|
|
}
|
|
|
|
err := d.Decode(me)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if d.R.Buffered() != 0 {
|
|
|
|
return fmt.Errorf("%d trailing bytes", d.R.Buffered())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|