Begin on UDP trackers

This commit is contained in:
Matt Joiner 2013-11-05 00:04:14 +11:00
parent 36b36beb3b
commit b499ab3619
2 changed files with 47 additions and 0 deletions

27
tracker/udp.go Normal file
View File

@ -0,0 +1,27 @@
package tracker
type UDPConnectionRequest struct {
ConnectionId int64
Action int32
TransctionId int32
}
type UDPAnnounceResponseHeader struct {
Action int32
TransactionId int32
Interval int32
Leechers int32
Seeders int32
}
type UDPAnnounceResponse struct {
UDPAnnounceResponseHeader
PeerAddrSlice
}
type PeerAddr struct {
IP int32
Port int16
}
type PeerAddrSlice []PeerAddr

20
tracker/udp_test.go Normal file
View File

@ -0,0 +1,20 @@
package tracker
import (
"bytes"
"encoding/binary"
"testing"
)
func TestMarshalUDPAnnounceResponse(t *testing.T) {
w := bytes.NewBuffer(nil)
if err := binary.Write(w, binary.BigEndian, &PeerAddrSlice{{1, 2}, {3, 4}}); err != nil {
t.Fatalf("error writing udp announce response addrs: %s", err)
}
if w.String() != "\x00\x00\x00\x01\x00\x02\x00\x00\x00\x03\x00\x04" {
t.FailNow()
}
if binary.Size(UDPAnnounceResponseHeader{}) != 20 {
t.FailNow()
}
}