github.com/anacrolix/torrent@v1.61.0/callbacks.go (about)

     1  package torrent
     2  
     3  import (
     4  	"github.com/anacrolix/torrent/mse"
     5  	pp "github.com/anacrolix/torrent/peer_protocol"
     6  )
     7  
     8  // These are called synchronously, and do not pass ownership of arguments (do not expect to retain
     9  // data after returning from the callback). The Client and other locks may still be held. nil
    10  // functions are not called.
    11  type Callbacks struct {
    12  	// Called after a peer connection completes the BitTorrent handshake. The Client lock is not
    13  	// held.
    14  	CompletedHandshake func(*PeerConn, InfoHash)
    15  	ReadMessage        func(*PeerConn, *pp.Message)
    16  	// This can be folded into the general case below.
    17  	ReadExtendedHandshake func(*PeerConn, *pp.ExtendedHandshakeMessage)
    18  	PeerConnClosed        func(*PeerConn)
    19  	// BEP 10 message. Not sure if I should call this Ltep universally. Each handler here is called
    20  	// in order.
    21  	PeerConnReadExtensionMessage []func(PeerConnReadExtensionMessageEvent)
    22  
    23  	// Provides secret keys to be tried against incoming encrypted connections.
    24  	ReceiveEncryptedHandshakeSkeys mse.SecretKeyIter
    25  
    26  	ReceivedUsefulData []func(ReceivedUsefulDataEvent)
    27  	ReceivedRequested  []func(PeerMessageEvent)
    28  	DeletedRequest     []func(PeerRequestEvent)
    29  	SentRequest        []func(PeerRequestEvent)
    30  	PeerClosed         []func(*Peer)
    31  	NewPeer            []func(*Peer)
    32  	// Called when a PeerConn has been added to a Torrent. It's finished all BitTorrent protocol
    33  	// handshakes, and is about to start sending and receiving BitTorrent messages. The extended
    34  	// handshake has not yet occurred. This is a good time to alter the supported extension
    35  	// protocols.
    36  	PeerConnAdded []func(*PeerConn)
    37  
    38  	// Sends status event updates. Useful to inform the user of specific events as they happen,
    39  	// for logging or to action on.
    40  	StatusUpdated []func(StatusUpdatedEvent)
    41  }
    42  
    43  type ReceivedUsefulDataEvent = PeerMessageEvent
    44  
    45  type PeerMessageEvent struct {
    46  	Peer    *Peer
    47  	Message *pp.Message
    48  }
    49  
    50  type PeerRequestEvent struct {
    51  	Peer *Peer
    52  	Request
    53  }
    54  
    55  type PeerConnReadExtensionMessageEvent struct {
    56  	PeerConn *PeerConn
    57  	// You can look up what protocol this corresponds to using the PeerConn.LocalLtepProtocolMap.
    58  	ExtensionNumber pp.ExtensionNumber
    59  	Payload         []byte
    60  }
    61  
    62  type StatusUpdatedEvent struct {
    63  	Event StatusEvent `json:"event"`
    64  	Error error       `json:"error"`
    65  	// The following fields may or may not be populated depending on the event.
    66  	PeerId   PeerID `json:"peer_id"`
    67  	Url      string `json:"url"`
    68  	InfoHash string `json:"info_hash"`
    69  }
    70  
    71  type StatusEvent string
    72  
    73  const (
    74  	PeerConnected             StatusEvent = "peer_connected"
    75  	PeerDisconnected          StatusEvent = "peer_disconnected"
    76  	TrackerConnected          StatusEvent = "tracker_connected"
    77  	TrackerDisconnected       StatusEvent = "tracker_disconnected"
    78  	TrackerAnnounceSuccessful StatusEvent = "tracker_announce_successful"
    79  	TrackerAnnounceError      StatusEvent = "tracker_announce_error"
    80  )