PeerMessage
One message of the BitTorrent peer wire protocol. This is the pure-Kotlin port of the write_* senders and on_* parsers in bt_peer_connection.cpp, plus the message id enum bt_peer_connection::message_type (include/libtorrent/bt_peer_connection.hpp).
Every non-keepalive message on the wire is length-prefixed:
<4-byte big-endian length N><1-byte message id><N-1 bytes payload>A keep-alive is the degenerate case N == 0: just four zero bytes, no id, no body (write_keepalive() sends {0,0,0,0}).
The message set spans three specs:
BEP-3: choke, unchoke, interested, not-interested, have, bitfield, request, piece, cancel.
BEP-5: the DHT
portmessage.BEP-6 Fast extension: suggest, have-all, have-none, reject-request, allowed-fast.
BEP-10: the extension-protocol
extendedcarrier message.
encode returns the full frame including the 4-byte length prefix, ready to write to a socket. Decoding is split: PeerMessage.decode parses ONE already-deframed body (id byte + payload, the length prefix already stripped), while PeerMessage.tryReadMessage does length-prefixed stream framing.
Inheritors
Types
allowed_fast (id 17, BEP-6): the receiver may request piece even while choked.
bitfield (id 5): the complete set of pieces the sender has. The payload is the bitfield's raw bytes (MSB-first, exactly Bitfield.data()).
choke (id 0): sender will not honour the receiver's requests.
hashes (id 22, BEP-52): the response to a HashRequest, carrying the requested range of 32-byte SHA-256 Merkle hashes. The payload is the same 48-byte header as HashRequest followed by N concatenated 32-byte hashes. On the wire: [len=49+32*N][22][header][hash...]. Mirrors write_hashes / on_hashes.
hash_reject (id 23, BEP-52): refuse a HashRequest. The payload is the identical 48-byte header as HashRequest, with no trailing hashes. On the wire: [len=49][23][header]. Mirrors write_hash_reject / on_hash_reject.
hash_request (id 21, BEP-52): ask the peer for a range of v2 Merkle-tree hashes. The payload is a fixed 48-byte header: [pieces_root 32 bytes][int32 base][int32 index][int32 length][int32 proof_layers]. On the wire: [len=49][21][header]. Mirrors write_hash_request / on_hash_request in bt_peer_connection.cpp (the C++ hash_request carries file/base/index/count/proof_layers; the file is identified on the wire by its pieces_root, and count is named length here).
have (id 4): the sender now has piece piece.
have_all (id 14, BEP-6): the sender has every piece (a seed).
have_none (id 15, BEP-6): the sender has no pieces.
interested (id 2): sender wants to request pieces.
Keep-alive: four zero bytes, length 0, no id, no payload.
not_interested (id 3): sender no longer wants to request pieces.
port (id 9, BEP-5): the sender's DHT UDP port. On the wire the port is a 16-bit big-endian value (write_dht_port emits {0,0,0,3, 9, hi, lo}).
reject_request (id 16, BEP-6): refuse a Request. Same body as request.
suggest (id 13, BEP-6): a hint that the receiver may want piece.
unchoke (id 1): sender will now honour the receiver's requests.