]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/udp/announce.go
go mod tidy
[btrtrc.git] / tracker / udp / announce.go
1 package udp
2
3 import (
4         "encoding"
5         "fmt"
6
7         "github.com/anacrolix/dht/v2/krpc"
8 )
9
10 // Marshalled as binary by the UDP client, so be careful making changes.
11 type AnnounceRequest struct {
12         InfoHash   [20]byte
13         PeerId     [20]byte
14         Downloaded int64
15         Left       int64 // If less than 0, math.MaxInt64 will be used for HTTP trackers instead.
16         Uploaded   int64
17         // Apparently this is optional. None can be used for announces done at
18         // regular intervals.
19         Event     AnnounceEvent
20         IPAddress uint32
21         Key       int32
22         NumWant   int32 // How many peer addresses are desired. -1 for default.
23         Port      uint16
24 } // 82 bytes
25
26 type AnnounceEvent int32
27
28 func (me *AnnounceEvent) UnmarshalText(text []byte) error {
29         for key, str := range announceEventStrings {
30                 if string(text) == str {
31                         *me = AnnounceEvent(key)
32                         return nil
33                 }
34         }
35         return fmt.Errorf("unknown event")
36 }
37
38 var announceEventStrings = []string{"", "completed", "started", "stopped"}
39
40 func (e AnnounceEvent) String() string {
41         // See BEP 3, "event", and
42         // https://github.com/anacrolix/torrent/issues/416#issuecomment-751427001. Return a safe default
43         // in case event values are not sanitized.
44         if e < 0 || int(e) >= len(announceEventStrings) {
45                 return ""
46         }
47         return announceEventStrings[e]
48 }
49
50 type AnnounceResponsePeers interface {
51         encoding.BinaryUnmarshaler
52         NodeAddrs() []krpc.NodeAddr
53 }