pub struct MessageBuilder { /* private fields */ }
Expand description
Allows building packets::Message structs in a more human-friendly way.
§Example
use rustydht_lib::common::Id;
use rustydht_lib::packets::MessageBuilder;
let client_id = Id::from_hex("0000000000000000000000000000000000000001").unwrap();
let server_id = Id::from_hex("0000000000000000000000000000000000000002").unwrap();
// To build a ping request
let ping_req = MessageBuilder::new_ping_request()
.sender_id(client_id)
.build()
.unwrap();
// To build a ping response
let ping_res = MessageBuilder::new_ping_response()
.sender_id(server_id)
.transaction_id(ping_req.transaction_id.clone())
.build()
.unwrap();
Implementations§
Source§impl MessageBuilder
impl MessageBuilder
Sourcepub fn new_ping_request() -> MessageBuilder
pub fn new_ping_request() -> MessageBuilder
Create a new MessageBuilder for a ping request
Sourcepub fn new_ping_response() -> MessageBuilder
pub fn new_ping_response() -> MessageBuilder
Create a new MessageBuilder for a ping response
Sourcepub fn new_find_node_request() -> MessageBuilder
pub fn new_find_node_request() -> MessageBuilder
Create a new MessageBuilder for a find_node request
Sourcepub fn new_find_node_response() -> MessageBuilder
pub fn new_find_node_response() -> MessageBuilder
Create a new MessageBuilder for a find_node response
Sourcepub fn new_get_peers_request() -> MessageBuilder
pub fn new_get_peers_request() -> MessageBuilder
Create a new MessageBuilder for a get_peers request
Sourcepub fn new_get_peers_response() -> MessageBuilder
pub fn new_get_peers_response() -> MessageBuilder
Create a new MessageBuilder for a get_peers response
Sourcepub fn new_announce_peer_request() -> MessageBuilder
pub fn new_announce_peer_request() -> MessageBuilder
Create a new MessageBuilder for an announce_peer request
Sourcepub fn new_announce_peer_response() -> MessageBuilder
pub fn new_announce_peer_response() -> MessageBuilder
Create a new MessageBuilder for an announce_peer response
Sourcepub fn new_sample_infohashes_request() -> MessageBuilder
pub fn new_sample_infohashes_request() -> MessageBuilder
Create a new MessageBuilder for a sample_infohashes request
Sourcepub fn new_sample_infohashes_response() -> MessageBuilder
pub fn new_sample_infohashes_response() -> MessageBuilder
Create a new MessageBuilder for a sample_infohashes response
Sourcepub fn new_error() -> MessageBuilder
pub fn new_error() -> MessageBuilder
Create a new MessageBuilder for an error
Sourcepub fn transaction_id(self, transaction_id: Vec<u8>) -> Self
pub fn transaction_id(self, transaction_id: Vec<u8>) -> Self
Set the transaction id of the packet. If one is not specified, generated requests will get a random transaction id and responses will receive an error.
Sourcepub fn version(self, version: Vec<u8>) -> Self
pub fn version(self, version: Vec<u8>) -> Self
Set the string of bytes that should be included in the packet to identify the version of the software participating on the DHT.
If one is not specified, the builder will omit the version field from the generated packet (it is optional).
Sourcepub fn requester_ip(self, remote: SocketAddr) -> Self
pub fn requester_ip(self, remote: SocketAddr) -> Self
For response packets, set the IP address and port that we saw the request come from. This is used to help other nodes on the DHT know what their external IPv4 address is.
Has no effect for request packets. If not specified on response packets, the builder will omit it from the generated response packet.
Sourcepub fn read_only(self, read_only: bool) -> Self
pub fn read_only(self, read_only: bool) -> Self
For request packets, specifies whether the read only flag should be set.
Has no effect on response packets.
Sourcepub fn sender_id(self, sender_id: Id) -> Self
pub fn sender_id(self, sender_id: Id) -> Self
Set the Id of the DHT node sending the packet (whether it’s a request or response).
Sourcepub fn target(self, target: Id) -> Self
pub fn target(self, target: Id) -> Self
Set the Id of the target node or info_hash (for get_peers, find_node, sample_infohashes, announce_peer)
Sourcepub fn port(self, port: u16) -> Self
pub fn port(self, port: u16) -> Self
Set the port field for announce_peer requests.
If not specified, 0 will be used and implied_port will automatically be implicitly set to true (unless explicitly set to false, in which case an error will occur).
Sourcepub fn implied_port(self, implied_port: bool) -> Self
pub fn implied_port(self, implied_port: bool) -> Self
Set the true/false value of implied port for announce_peer requests.
Sourcepub fn token(self, token: Vec<u8>) -> Self
pub fn token(self, token: Vec<u8>) -> Self
Set the token byte string. Used for announce_peer requests and get_peers responses.
Sourcepub fn nodes(self, nodes: Vec<Node>) -> Self
pub fn nodes(self, nodes: Vec<Node>) -> Self
Set the list of Nodes used in get_peers, find_node, and sample_infohashes responses.
nodes will be ignored for get_peers packets if peers are specified.
Sourcepub fn peers(self, peers: Vec<SocketAddr>) -> Self
pub fn peers(self, peers: Vec<SocketAddr>) -> Self
Set the list of peers used in get_peers responses.
nodes will be ignored for get_peers packets if peers are specified.
Sourcepub fn interval(self, interval: Duration) -> Self
pub fn interval(self, interval: Duration) -> Self
Set the interval used in sample_infohashes responses.
Sourcepub fn samples(self, samples: Vec<Id>) -> Self
pub fn samples(self, samples: Vec<Id>) -> Self
Set the list of info_hashes used in sample_infohashes responses.
Sourcepub fn num_infohashes(self, num: usize) -> Self
pub fn num_infohashes(self, num: usize) -> Self
Set the number of info_hashes as reported in sample_infohashes responses.
Sourcepub fn description(self, description: String) -> Self
pub fn description(self, description: String) -> Self
Set the description for error messages.
Sourcepub fn build(self) -> Result<Message, RustyDHTError>
pub fn build(self) -> Result<Message, RustyDHTError>
Build the Message, consuming this MessageBuilder in the process
Trait Implementations§
Source§impl Clone for MessageBuilder
impl Clone for MessageBuilder
Source§fn clone(&self) -> MessageBuilder
fn clone(&self) -> MessageBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more