Skip to main content

Message

Enum Message 

Source
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.

Fields

§index: u32

Piece index.

§

Bitfield(B)

Peer’s complete bitfield.

§

Request

Request a block: piece index, byte offset within piece, length.

Fields

§index: u32

Piece index.

§begin: u32

Byte offset within the piece.

§length: u32

Requested block length in bytes.

§

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

§index: u32

Piece index.

§begin: u32

Byte offset within the piece.

§data_0: B

First (or only) contiguous slice of block payload.

§data_1: B

Second contiguous slice when the ring buffer wraps; empty otherwise.

§

Cancel

Cancel a previously sent request.

Fields

§index: u32

Piece index.

§begin: u32

Byte offset within the piece.

§length: u32

Block length in bytes.

§

Port(u16)

DHT port (BEP 5).

§

Extended

Extension message (BEP 10). ext_id=0 is handshake.

Fields

§ext_id: u8

Extension message ID (0 = handshake).

§payload: B

Bencoded extension payload.

§

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

§index: u32

Piece index.

§begin: u32

Byte offset within the piece.

§length: u32

Block length in bytes.

§

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

§pieces_root: Id32

File root hash identifying the Merkle tree.

§base: u32

Tree layer (0 = leaf/block layer).

§index: u32

Starting node index within the layer.

§count: u32

Number of consecutive hashes requested.

§proof_layers: u32

Number of uncle proof layers to include.

§

Hashes

BEP 52: Response with hashes and uncle proof.

Fields

§pieces_root: Id32

File root hash identifying the Merkle tree.

§base: u32

Tree layer (0 = leaf/block layer).

§index: u32

Starting node index within the layer.

§count: u32

Number of consecutive hashes in the response.

§proof_layers: u32

Number of uncle proof layers included.

§hashes: Vec<Id32>

Hash values followed by uncle proof hashes.

§

HashReject

BEP 52: Reject a hash request.

Fields

§pieces_root: Id32

File root hash identifying the Merkle tree.

§base: u32

Tree layer that was requested.

§index: u32

Starting node index that was requested.

§count: u32

Number of hashes that was requested.

§proof_layers: u32

Number of proof layers that was requested.

Implementations§

Source§

impl<B: AsRef<[u8]>> Message<B>

Source

pub fn to_bytes(&self) -> Bytes

Serialize a message to bytes (length-prefix + id + payload).

The returned bytes include the 4-byte length prefix.

Source

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.

Source

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.

Source

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.

Source§

impl Message<&[u8]>

Source

pub fn to_owned_bytes(&self) -> Message<Bytes>

Convert a borrowed message to an owned Message<Bytes>.

Fixed-field variants are zero-cost (no data to copy). Data-carrying variants (Piece, Bitfield, Extended) copy their slices into fresh Bytes allocations.

Source§

impl Message<Bytes>

Source

pub fn from_payload(payload: Bytes) -> Result<Self>

Parse a message from its payload (after the 4-byte length prefix has been consumed). payload is everything after the length prefix.

§Errors

Returns an error if the message ID is unknown or the payload is malformed.

Trait Implementations§

Source§

impl<B: Clone> Clone for Message<B>

Source§

fn clone(&self) -> Message<B>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<B: Debug> Debug for Message<B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Encoder<Message> for MessageCodec

Source§

type Error = Error

The type of encoding errors. Read more
Source§

fn encode( &mut self, msg: Message<Bytes>, dst: &mut BytesMut, ) -> Result<(), Error>

Encodes a frame into the buffer provided. Read more
Source§

impl<B: PartialEq> PartialEq for Message<B>

Source§

fn eq(&self, other: &Message<B>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<B: Eq> Eq for Message<B>

Source§

impl<B> StructuralPartialEq for Message<B>

Auto Trait Implementations§

§

impl<B> Freeze for Message<B>
where B: Freeze,

§

impl<B> RefUnwindSafe for Message<B>
where B: RefUnwindSafe,

§

impl<B> Send for Message<B>
where B: Send,

§

impl<B> Sync for Message<B>
where B: Sync,

§

impl<B> Unpin for Message<B>
where B: Unpin,

§

impl<B> UnsafeUnpin for Message<B>
where B: UnsafeUnpin,

§

impl<B> UnwindSafe for Message<B>
where B: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.