packetz

Crates.iopacketz
lib.rspacketz
version2.0.0
sourcesrc
created_at2023-06-30 11:02:03.471136
updated_at2023-07-01 06:25:39.535213
descriptionCreate async packet-based servers with ease, Built with gamedev in mind. Stay tuned for UDP support, and more!
homepage
repository
max_upload_size
id904352
size17,776
mizu (WhyNotDogie)

documentation

README

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"] }
Commit count: 0

cargo fmt