10 "github.com/anacrolix/dht/v2/krpc"
13 // Marshalled as binary by the UDP client, so be careful making changes.
14 type AnnounceRequest struct {
18 Left int64 // If less than 0, math.MaxInt64 will be used for HTTP trackers instead.
20 // Apparently this is optional. None can be used for announces done at
25 NumWant int32 // How many peer addresses are desired. -1 for default.
29 type AnnounceResponse struct {
30 Interval int32 // Minimum seconds the local peer should wait before next announce.
36 type AnnounceEvent int32
38 func (e AnnounceEvent) String() string {
39 // See BEP 3, "event", and https://github.com/anacrolix/torrent/issues/416#issuecomment-751427001.
40 return []string{"", "completed", "started", "stopped"}[e]
44 None AnnounceEvent = iota
45 Completed // The local peer just completed the torrent.
46 Started // The local peer has just resumed this torrent.
47 Stopped // The local peer is leaving the swarm.
51 ErrBadScheme = errors.New("unknown scheme")
54 type Announce struct {
56 Request AnnounceRequest
58 HTTPProxy func(*http.Request) (*url.URL, error)
62 // If the port is zero, it's assumed to be the same as the Request.Port.
63 ClientIp4 krpc.NodeAddr
64 // If the port is zero, it's assumed to be the same as the Request.Port.
65 ClientIp6 krpc.NodeAddr
66 Context context.Context
69 // The code *is* the documentation.
70 const DefaultTrackerAnnounceTimeout = 15 * time.Second
72 func (me Announce) Do() (res AnnounceResponse, err error) {
73 _url, err := url.Parse(me.TrackerUrl)
77 if me.Context == nil {
78 // This is just to maintain the old behaviour that should be a timeout of 15s. Users can
79 // override it by providing their own Context. See comments elsewhere about longer timeouts
80 // acting as rate limiting overloaded trackers.
81 ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout)
87 return announceHTTP(me, _url)
88 case "udp", "udp4", "udp6":
89 return announceUDP(me, _url)