pub struct Message {
pub transaction_id: Vec<u8>,
pub version: Option<Vec<u8>>,
pub requester_ip: Option<SocketAddr>,
pub message_type: MessageType,
pub read_only: Option<bool>,
}
Expand description
All packets sent and received via DHT are a serialized version of this struct.
It can be used to represent DHT messages throughout a program and has methods to serialize and deserialize DHT messages.
§Building
The easiest way to build Message structs is to use MessageBuilder.
But if you need more control/flexibility than provided by MessageBuilder, you can build Message structs directly:
use rustydht_lib::common::Id;
use rustydht_lib::packets::{Message, MessageType, RequestSpecific, FindNodeRequestArguments};
// This constructs a find_node request. It would be easier with MessageBuilder.
let msg = Message {
transaction_id: vec![1, 2, 3],
version: Some(vec![0x62, 0x61, 0x72, 0x66]),
requester_ip: None,
read_only: None,
message_type: MessageType::Request(RequestSpecific::FindNodeRequest(
FindNodeRequestArguments {
target: Id::from_hex("1234123412341234123412341234123412341234").unwrap(),
requester_id: Id::from_hex("5678567856785678567856785678567856785678").unwrap(),
},
)),
};
§Deserializing
use rustydht_lib::packets::Message;
// Imagine that this vector contains bytes from reading from a socket
let bytes: Vec<u8> = Vec::new();
match Message::from_bytes(&bytes) {
Ok(msg) => {
// Success! do something with the Message you just parsed
}
Err(e) => {
eprintln!("Oh no! I hit an error while parsing a Message: {}", e);
}
}
§Serializing
use rustydht_lib::common::Id;
use rustydht_lib::packets::{Message, MessageBuilder};
let our_id = Id::from_hex("0000000000000000000000000000000000000001").unwrap();
let target = Id::from_hex("ff00000000000000000000000000000000000002").unwrap();
let msg = MessageBuilder::new_find_node_request()
.sender_id(our_id)
.target(target)
.build()
.unwrap();
match msg.to_bytes() {
Ok(bytes) => {
// Success! You have a Vec<u8> that can be sent over a socket or whatever
}
Err(e) => {
eprintln!("Oh no! Couldn't serialize: {}", e);
}
}
Fields§
§transaction_id: Vec<u8>
§version: Option<Vec<u8>>
The version of the requester or responder.
requester_ip: Option<SocketAddr>
The IP address and port (“SocketAddr”) of the requester as seen from the responder’s point of view. This should be set only on response, but is defined at this level with the other common fields to avoid defining yet another layer on the response objects.
message_type: MessageType
§read_only: Option<bool>
For bep0043. When set true on a request, indicates that the requester can’t reply to requests and that responders should not add requester to their routing tables. Should only be set on requests - undefined behavior when set on a response.
Implementations§
Source§impl Message
impl Message
pub fn to_bytes(self) -> Result<Vec<u8>, RustyDHTError>
pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Message, RustyDHTError>
Return the Id of the sender of the Message
This is less straightforward than it seems because not all messages are sent with an Id (all are except Error messages). This is reflected in the structure of DHT Messages, and makes it a bit annoying to learn the sender’s Id without unraveling the entire message. This method is a convenience method to extract the sender (or “author”) Id from the guts of any Message.