DhtMessage
The KRPC message model and codec for the BitTorrent DHT: a pure-Kotlin port of the message-building and -parsing code spread across libtorrent's src/kademlia/node.cpp (incoming_request, the per-query response builders, and the single-bucket-refresh / ping query builders), find_data.cpp, get_peers.cpp, get_item.cpp, put_data.cpp and msg.cpp.
Every DHT packet is a bencoded dictionary with three or four top-level keys (BEP-5 "KRPC Protocol"):
t: the transaction id, an opaque byte string (libtorrent uses 2 random bytes, seerpc_manager::invoke). The responder echoes it verbatim.y: the message type, either"q"query,"r"response or"e"error.v: an optional client-version string (not modelled here; libtorrent adds it at the transport layer).q+a: for a query, the query name and its argument dict.r: for a response, the response dict.e: for an error, the[code, message]list (incoming_error).
The argument/response dict for every query carries the sender's own node id under id (rpc_manager::add_our_id / node::incoming_request's add_our_id(reply)).
This file provides:
a sealed DhtMessage hierarchy (Query/Response/ErrorMessage) with the typed argument/response payloads BEP-5/44/51 define,
build*Query(...)andbuild*Response(...)helpers returning an editable Entry (ready forBencode.encode), matching thee["y"]=…; e["a"][…]=…assembly in libtorrent, andDhtMessage.parse, the dual of verifyMessage: it reads a decoded BdecodeNode envelope back into a typed DhtMessage.
Reuses NodeId/Sha1Hash, DhtEndpoint/CompactNode (compact node info), ImmutableItem/MutableItem (BEP-44 values) and DhtKeySizes.
Inheritors
Types
A KRPC error (y == "e"): {t, y:"e", e:[code, message]}, built by libtorrent's incoming_error.
A KRPC query (y == "q"): {t, y:"q", q:<name>, a:{id, …}}. The concrete argument payload is one of the nested Args types.