#udp #serialization #io

sockit

A UDP socket that can read and write serializable data

3 unstable releases

0.2.1 Mar 30, 2023
0.2.0 Mar 27, 2023
0.1.0 Mar 20, 2023

#130 in #udp

31 downloads per month

MIT license

11KB
86 lines

Sockit 🧦

github crates.io docs.rs build status codecov

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

Usage

Add this to your Cargo.toml:

[dependencies]
sockit = "0.2.1"

You can create a Socket by binding it to an address like so:

#[tokio::main]
async fn main() {
  let socket = sockit::UdpSocket::bind("127.0.0.1:0").await?;
}

You can use the Socket to send and receive serializable objects:

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

/// A (de)serializable type shared between client and server
#[derive(Serialize, Deserialize)]
struct Message {
  id: u32,
  data: String,
}

/// Code running client side
async fn client_side(mut client_socket: UdpSocket) {
  let message = Message {
    id: 1,
    data: "Hello, world!".to_string(),
  };

  client_socket.write::<Message>(&message).await.unwrap();
}

/// Code running server side
async fn server_side(mut server_socket: UdpSocket) {
  let message: Message = server_socket.read::<Message>().await.unwrap().unwrap();
}

Dependencies

~3–11MB
~89K SLoC