#tokio #tcp #server #transport #message #events

tcpnet

A simple TCP-based message transport for games that runs on Tokio

1 unstable release

0.1.1 Oct 15, 2023
0.1.0 Oct 10, 2023

#21 in #network-programming

MIT license

24KB
577 lines

tcpnet

A simple tick-based TCP message transport for games that runs on Tokio. The client and server provide send() and receive() methods to send data and poll events respectively.

Server usage example:

use tcpnet::server::{Event, Server};

fn main() {
    let mut server = Server::new();
    server.start(7000);
    assert!(server.running());

    // Process events on every tick
    for event in server.received().unwrap() {
        match event {
            Event::Connect(addr) => println!("{} connected", addr),
            Event::Disconnect(addr) => println!("{} disconnected", addr),
            Event::Data(addr, data) => {
                println!("{}: {}", addr, std::str::from_utf8(&data).unwrap());
                server.send(addr, "Hello back!".as_bytes().to_vec())?;
            }
        }
    }
}

Client usage example:

use tcpnet::client::{Event, Client};

fn main() {
    let mut client = Client::new();
    client.start("127.0.0.1:7000");
    assert!(server.running());

    // Process events on every tick/frame
    for event in server.received().unwrap() {
        match event {
            Event::Disconnect => println!("Disconnected from server"),
            Event::Data(data) => println!("Server: {}", addr, std::str::from_utf8(&data).unwrap()),
        }
    }
}

Dependencies

~2.4–9MB
~57K SLoC