Crate sockit

Source
Expand description

A high-level UDP Socket that allows for writing and reading (de)serializable values

§Example

use sockit::UdpSocket;
use serde::{Deserialize, Serialize};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
 // Create UDP Sockets
 let (mut a, mut b): (UdpSocket, UdpSocket) = setup().await;

 let message = TestMessage {
    id: 123,
    name: "Test Message".to_string(),
    payload: vec![1, 2, 3, 4, 5]
 };

 // Write a message to b
 a.write(&message, b.local_addr()?).await?;

 // Read a message from b's socket
 let (parsed_message, from) = b.read::<TestMessage>().await?;

 // Assert that the message was sent from a and that it matches the original message
 assert_eq!(from, a.local_addr()?);
 assert_eq!(message, parsed_message);

 Ok(())
}

// ... boilerplate

#[derive(Serialize, Deserialize, Debug, PartialOrd, PartialEq)]
struct TestMessage { id: u32, name: String, payload: Vec<u8> }

async fn setup() -> (UdpSocket, UdpSocket) {
   let a = UdpSocket::bind("127.0.0.1:0").await.unwrap();
   let b = UdpSocket::bind("127.0.0.1:0").await.unwrap();
   (a, b)
}

Structs§

UdpSocket
A high-level UDP Socket that allows for writing and reading (de)serializable values

Enums§

UdpSocketError