70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package udp
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"io"
|
|
)
|
|
|
|
type Action int32
|
|
|
|
const (
|
|
ActionConnect Action = iota
|
|
ActionAnnounce
|
|
ActionScrape
|
|
ActionError
|
|
|
|
ConnectRequestConnectionId = 0x41727101980
|
|
|
|
// BEP 41
|
|
optionTypeEndOfOptions = 0
|
|
optionTypeNOP = 1
|
|
optionTypeURLData = 2
|
|
)
|
|
|
|
type TransactionId = int32
|
|
|
|
type ConnectionId = int64
|
|
|
|
type ConnectionRequest struct {
|
|
ConnectionId ConnectionId
|
|
Action Action
|
|
TransactionId TransactionId
|
|
}
|
|
|
|
type ConnectionResponse struct {
|
|
ConnectionId ConnectionId
|
|
}
|
|
|
|
type ResponseHeader struct {
|
|
Action Action
|
|
TransactionId TransactionId
|
|
}
|
|
|
|
type RequestHeader struct {
|
|
ConnectionId ConnectionId
|
|
Action Action
|
|
TransactionId TransactionId
|
|
} // 16 bytes
|
|
|
|
type AnnounceResponseHeader struct {
|
|
Interval int32
|
|
Leechers int32
|
|
Seeders int32
|
|
}
|
|
|
|
func marshal(data interface{}) (b []byte, err error) {
|
|
var buf bytes.Buffer
|
|
err = binary.Write(&buf, binary.BigEndian, data)
|
|
b = buf.Bytes()
|
|
return
|
|
}
|
|
|
|
func Write(w io.Writer, data interface{}) error {
|
|
return binary.Write(w, binary.BigEndian, data)
|
|
}
|
|
|
|
func Read(r io.Reader, data interface{}) error {
|
|
return binary.Read(r, binary.BigEndian, data)
|
|
}
|