Skip to main content

ExtensionPlugin

Trait ExtensionPlugin 

Source
pub trait ExtensionPlugin:
    Send
    + Sync
    + 'static {
    // Required method
    fn name(&self) -> &str;

    // Provided methods
    fn on_handshake(
        &self,
        _info_hash: &Id20,
        _peer_addr: SocketAddr,
        _handshake: &ExtHandshake,
    ) -> Option<BTreeMap<String, BencodeValue>> { ... }
    fn on_message(
        &self,
        _info_hash: &Id20,
        _peer_addr: SocketAddr,
        _payload: &[u8],
    ) -> Option<Vec<u8>> { ... }
    fn on_peer_connected(&self, _info_hash: &Id20, _peer_addr: SocketAddr) { ... }
    fn on_peer_disconnected(&self, _info_hash: &Id20, _peer_addr: SocketAddr) { ... }
}
Expand description

A custom BEP 10 extension message handler.

Implement this trait to add custom extension protocol support to a torrent session. Plugins are registered via ClientBuilder::add_extension() and are immutable after session start.

§Extension ID allocation

Built-in extensions occupy IDs 1-3:

  • ut_metadata = 1
  • ut_pex = 2
  • lt_trackers = 3

Plugins are assigned IDs starting at 10, in registration order.

§Constraints

  • Plugins cannot access torrent internals (piece state, peer list).
  • Plugins cannot initiate unsolicited messages – respond only.
  • Plugins cannot override built-in extensions.
  • Callbacks are synchronous – spawn tasks internally for async work.

Required Methods§

Source

fn name(&self) -> &str

Extension name for BEP 10 handshake negotiation (e.g. "ut_comment").

This name is advertised in the extension handshake m dictionary.

Provided Methods§

Source

fn on_handshake( &self, _info_hash: &Id20, _peer_addr: SocketAddr, _handshake: &ExtHandshake, ) -> Option<BTreeMap<String, BencodeValue>>

Called when a peer’s extension handshake arrives.

Return extra key-value pairs to merge into our handshake response, or None to add nothing.

Source

fn on_message( &self, _info_hash: &Id20, _peer_addr: SocketAddr, _payload: &[u8], ) -> Option<Vec<u8>>

Called when an extension message for this plugin arrives.

Return an optional response payload to send back to the peer.

Source

fn on_peer_connected(&self, _info_hash: &Id20, _peer_addr: SocketAddr)

Peer connected (after BT handshake, before extension handshake).

Source

fn on_peer_disconnected(&self, _info_hash: &Id20, _peer_addr: SocketAddr)

Peer disconnected.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§