announce.go 1.3 KB

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