Some tidying in tracker/udp

This commit is contained in:
Matt Joiner 2022-12-05 12:51:29 +11:00
parent bf5a22f345
commit 330d23be22
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
2 changed files with 6 additions and 3 deletions

View File

@ -125,7 +125,7 @@ func (cl *Client) requestWriter(ctx context.Context, action Action, body []byte,
return
}
buf.Reset()
err = binary.Write(&buf, binary.BigEndian, RequestHeader{
err = Write(&buf, RequestHeader{
ConnectionId: connId,
Action: action,
TransactionId: tId,

View File

@ -13,9 +13,11 @@ const (
ActionAnnounce
ActionScrape
ActionError
)
ConnectRequestConnectionId = 0x41727101980
const ConnectRequestConnectionId = 0x41727101980
const (
// BEP 41
optionTypeEndOfOptions = 0
optionTypeNOP = 1
@ -57,7 +59,7 @@ type InfoHash = [20]byte
func marshal(data interface{}) (b []byte, err error) {
var buf bytes.Buffer
err = binary.Write(&buf, binary.BigEndian, data)
err = Write(&buf, data)
b = buf.Bytes()
return
}
@ -70,6 +72,7 @@ func mustMarshal(data interface{}) []byte {
return b
}
// This is for fixed-size, builtin types only I think.
func Write(w io.Writer, data interface{}) error {
return binary.Write(w, binary.BigEndian, data)
}