Skip to main content

Crate enxame_peer

Crate enxame_peer 

Source
Expand description

The BitTorrent peer wire protocol (BEP-3) as a typed AST.

After the TCP/uTP handshake, two peers exchange a stream of length-prefixed messages. This crate models that stream as the pleme-io idiom demands — a typed PeerMessage AST (parse-don’t-validate) with a [PeerMessage::parse] decoder and a typed-emission PeerMessage::encode (the framing is built from typed integers and byte literals, never a format!() of the wire — the ★★ TYPED EMISSION rule). It is the exact shape proven in mado’s CsiCommand dispatch: bytes → typed message → interpret.

Frame layout: <u32 length><u8 id><payload>; a zero-length frame is the PeerMessage::KeepAlive (no id byte). The Handshake that precedes the stream has its own fixed layout.

Structs§

Block
A request/cancel block region (index, begin, length) — a sub-range of a piece.
Handshake
The opening handshake: <19><"BitTorrent protocol"><8 reserved><20 info_hash><20 peer_id> — 68 bytes total.

Enums§

PeerMessage
The typed peer-wire message AST (BEP-3 + the BEP-5 Port and BEP-10 Extended messages). Unknown ids are kept (not dropped) as PeerMessage::Unknown so a forward-compatible peer round-trips.
ProtocolError
A typed framing/decoding failure.

Constants§

PROTOCOL
The fixed protocol identifier string in the handshake (pstr).

Functions§

decode_frame
Decode one complete frame from the front of a buffer: read the 4-byte big-endian length prefix, and if that many body bytes are present, return the parsed PeerMessage and the number of bytes consumed. Ok(None) means “need more bytes” (the frame isn’t complete yet) — the caller keeps buffering. This is the streaming framing seam.

Type Aliases§

InfoHash
A 20-byte info-hash (kept local so this crate has no metainfo dep).
PeerId
A peer’s 20-byte id.