#udp-server #async #gamedev #built #mind #ease #packet

packetz

Create async packet-based servers with ease, Built with gamedev in mind. Stay tuned for UDP support, and more!

4 releases (2 stable)

2.0.0 Jul 1, 2023
1.0.0 Jun 30, 2023
0.1.1 Jun 30, 2023
0.1.0 Jun 30, 2023

#14 in #mind

34 downloads per month

ISC license

17KB
330 lines

Packetz

Create async packet-based servers with ease, Built with gamedev in mind. Stay tuned for UDP support, and more!

Basic usage

Server

use packetz::{server::*, packet::*};

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
    let server: Server<&str> = Server::bind("0.0.0.0:5515"); // The extra &str is the type of the argumet in `server::bind()`, so that server::bind is able to be a const fn.
    let listener: ServerListener = server.listen().await?;

    loop {
        let (
            mut connection,
            addr
        ) = listener.accept().await?;
        let _: tokio::task::JoinHandle<Result<(), std::io::Error>> = tokio::spawn(async move {
            'l: loop { // In a real world scenario we would check for errors on the `send` and `recv` methods, and break the loop if one is found, and disconnect the client without disconnecting all other clients.
                let msg = connection.recv().await?;
                connection.send(msg.body).await?;
                connection.disconnect(); // This is optional, as all it does is drop the PacketStream, and breaking the loop should automaticall drop it.
                break 'l;
            }
            Ok(())
        });
    }
}

Client

use packetz::{client::*, packet::*};

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
    let mut client = packetz::client::connect("0.0.0.0:5515").await?;
    client.send(b"Hello, Packetz!").await?;
    println!("{}", String::from_utf8(
        client.recv().await?
    )?);
    Ok(())
}

Dependencies

Dependencies for these examples:

[dependencies]
packetz = "0.1.0" # Replace this with the latest version, if it's not already the latest version.
tokio = { version = "1.29.0", features = ["net", "io-util", "time", "rt", "rt-multi-thread"] }

Dependencies

~2–11MB
~120K SLoC