pub enum Message<B = Bytes> {
Show 20 variants
KeepAlive,
Choke,
Unchoke,
Interested,
NotInterested,
Have {
index: u32,
},
Bitfield(B),
Request {
index: u32,
begin: u32,
length: u32,
},
Piece {
index: u32,
begin: u32,
data_0: B,
data_1: B,
},
Cancel {
index: u32,
begin: u32,
length: u32,
},
Port(u16),
Extended {
ext_id: u8,
payload: B,
},
SuggestPiece(u32),
HaveAll,
HaveNone,
RejectRequest {
index: u32,
begin: u32,
length: u32,
},
AllowedFast(u32),
HashRequest {
pieces_root: Id32,
base: u32,
index: u32,
count: u32,
proof_layers: u32,
},
Hashes {
pieces_root: Id32,
base: u32,
index: u32,
count: u32,
proof_layers: u32,
hashes: Vec<Id32>,
},
HashReject {
pieces_root: Id32,
base: u32,
index: u32,
count: u32,
proof_layers: u32,
},
}Expand description
Standard BitTorrent peer wire messages (BEP 3).
Generic over buffer type B to support both owned (Bytes) and borrowed
(&[u8]) payloads. Data-carrying variants (Piece, Bitfield,
Extended) use B; fixed-field variants are buffer-agnostic.
The Piece variant carries two buffer fields (data_0, data_1) to
support ring-buffer wrap-around: when a block spans the ring boundary the
two non-contiguous slices are stored separately. In the common (no-wrap)
case data_1 is empty.
Variants§
KeepAlive
Keep connection alive (no payload, length=0).
Choke
Peer is choking us.
Unchoke
Peer is unchoking us.
Interested
We’re interested in the peer’s data.
NotInterested
We’re not interested.
Have
Peer has piece index.
Bitfield(B)
Peer’s complete bitfield.
Request
Request a block: piece index, byte offset within piece, length.
Fields
Piece
A data block: piece index, byte offset, data.
Two buffer fields support ring-buffer wrap-around. When the block does
not straddle the ring boundary, data_1 is empty.
Fields
data_0: BFirst (or only) contiguous slice of block payload.
data_1: BSecond contiguous slice when the ring buffer wraps; empty otherwise.
Cancel
Cancel a previously sent request.
Fields
Port(u16)
DHT port (BEP 5).
Extended
Extension message (BEP 10). ext_id=0 is handshake.
SuggestPiece(u32)
BEP 6: Suggest a piece for the peer to download.
HaveAll
BEP 6: We have all pieces.
HaveNone
BEP 6: We have no pieces.
RejectRequest
BEP 6: Reject a request from the peer.
Fields
AllowedFast(u32)
BEP 6: Piece index the peer is allowed to request while choked.
HashRequest
BEP 52: Request hashes from a file’s Merkle tree.
Fields
Hashes
BEP 52: Response with hashes and uncle proof.
Fields
HashReject
BEP 52: Reject a hash request.
Implementations§
Source§impl<B: AsRef<[u8]>> Message<B>
impl<B: AsRef<[u8]>> Message<B>
Sourcepub fn to_bytes(&self) -> Bytes
pub fn to_bytes(&self) -> Bytes
Serialize a message to bytes (length-prefix + id + payload).
The returned bytes include the 4-byte length prefix.
Sourcepub fn encode_into(&self, dst: &mut BytesMut)
pub fn encode_into(&self, dst: &mut BytesMut)
Encode this message (with length prefix) directly into a buffer.
Unlike to_bytes, this writes directly into dst
without allocating an intermediate Bytes, avoiding a double-copy
when used with tokio_util::codec::Encoder.
Sourcepub fn wire_len(&self) -> usize
pub fn wire_len(&self) -> usize
Return the exact encoded wire length in bytes, including the 4-byte length prefix.
This is a pure computation — no allocation, no encoding. Use it to
check whether a message fits in a fixed-size buffer before calling
encode_to_slice.
Sourcepub fn encode_to_slice(&self, dst: &mut [u8]) -> usize
pub fn encode_to_slice(&self, dst: &mut [u8]) -> usize
Encode this message (with length prefix) into a raw byte slice.
Returns the number of bytes written. The caller must ensure dst is
large enough to hold the encoded message. For peer wire messages,
MAX_MSG_LEN (16397) is always sufficient.
Uses std::io::Cursor<&mut [u8]> + std::io::Write instead of
BufMut, producing identical bytes to encode_into.