callbacks.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package torrent
  2. import (
  3. "github.com/anacrolix/torrent/mse"
  4. pp "github.com/anacrolix/torrent/peer_protocol"
  5. )
  6. // These are called synchronously, and do not pass ownership of arguments (do not expect to retain
  7. // data after returning from the callback). The Client and other locks may still be held. nil
  8. // functions are not called.
  9. type Callbacks struct {
  10. // Called after a peer connection completes the BitTorrent handshake. The Client lock is not
  11. // held.
  12. CompletedHandshake func(*PeerConn, InfoHash)
  13. ReadMessage func(*PeerConn, *pp.Message)
  14. // This can be folded into the general case below.
  15. ReadExtendedHandshake func(*PeerConn, *pp.ExtendedHandshakeMessage)
  16. PeerConnClosed func(*PeerConn)
  17. // BEP 10 message. Not sure if I should call this Ltep universally. Each handler here is called
  18. // in order.
  19. PeerConnReadExtensionMessage []func(PeerConnReadExtensionMessageEvent)
  20. // Provides secret keys to be tried against incoming encrypted connections.
  21. ReceiveEncryptedHandshakeSkeys mse.SecretKeyIter
  22. ReceivedUsefulData []func(ReceivedUsefulDataEvent)
  23. ReceivedRequested []func(PeerMessageEvent)
  24. DeletedRequest []func(PeerRequestEvent)
  25. SentRequest []func(PeerRequestEvent)
  26. PeerClosed []func(*Peer)
  27. NewPeer []func(*Peer)
  28. // Called when a PeerConn has been added to a Torrent. It's finished all BitTorrent protocol
  29. // handshakes, and is about to start sending and receiving BitTorrent messages. The extended
  30. // handshake has not yet occurred. This is a good time to alter the supported extension
  31. // protocols.
  32. PeerConnAdded []func(*PeerConn)
  33. }
  34. type ReceivedUsefulDataEvent = PeerMessageEvent
  35. type PeerMessageEvent struct {
  36. Peer *Peer
  37. Message *pp.Message
  38. }
  39. type PeerRequestEvent struct {
  40. Peer *Peer
  41. Request
  42. }
  43. type PeerConnReadExtensionMessageEvent struct {
  44. PeerConn *PeerConn
  45. // You can look up what protocol this corresponds to using the PeerConn.LocalLtepProtocolMap.
  46. ExtensionNumber pp.ExtensionNumber
  47. Payload []byte
  48. }