const VERSION: u8 = 1;
+/// Encountered when trying to construct a [`ConnectDatagram`] with an empty message body.
+///
#[derive(Debug, Clone)]
pub struct DatagramEmptyError;
}
}
+/// A simple packet format containing a version, recipient tag, and message body.
+///
pub struct ConnectDatagram {
version: u8,
recipient: u16,
}
impl ConnectDatagram {
+ /// Creates a new [`ConnectDatagram`] based on an intended recipient and message body.
+ ///
+ /// This will return a [`DatagramEmptyError`] if the `data` parameter contains no bytes, or
+ /// in other words, when there is no message body.
+ ///
+ /// The version field is decided by the library version and used to maintain backwards
+ /// compatibility with previous datagram formats.
+ ///
pub fn new(recipient: u16, data: Vec<u8>) -> Result<Self, DatagramEmptyError> {
if data.len() > 0 {
Ok(Self {
}
}
+ /// Gets the version number of the datagram.
+ ///
pub fn version(&self) -> u8 {
self.version
}
+ /// Gets the recipient of the datagram.
+ ///
pub fn recipient(&self) -> u16 {
self.recipient
}
+ /// Gets the message body of the datagram.
+ ///
pub fn data(&self) -> Option<&Vec<u8>> {
self.data.as_ref()
}
+ /// Takes ownership of the message body of the datagram.
+ ///
pub fn take_data(&mut self) -> Option<Vec<u8>> {
self.data.take()
}
+ /// Calculates the serialized byte-size of the datagram.
+ ///
pub fn size(&self) -> usize {
let data_len = if let Some(data) = self.data() {
data.len()
3 + data_len
}
- pub fn bytes(&self) -> Vec<u8> {
+ /// Constructs a serialized representation of the datagram.
+ ///
+ pub(crate) fn bytes(&self) -> Vec<u8> {
let mut bytes = Vec::with_capacity(self.size());
bytes.extend(&self.version.to_be_bytes());
return bytes;
}
+ /// Serializes the datagram.
+ ///
pub fn encode(&self) -> Vec<u8> {
let size: u32 = (self.size()) as u32;
return bytes;
}
+ /// Deserializes the datagram from a `source`.
+ ///
pub fn decode(source: &mut (dyn Read + Send + Sync)) -> anyhow::Result<Self> {
// payload size
let mut payload_size_bytes: [u8; 4] = [0; 4];