#udp #raknet #networking

nightly raknet-rs

Raknet protocol implementation by rust

2 releases

0.1.1 Feb 10, 2024
0.1.0 Feb 9, 2024

#3 in #raknet

Apache-2.0

215KB
5.5K SLoC

Header

Crates.io CI Status Coverage License

Yet another project rewritten in Rust.

Features

  • Stream/Sink/Future based async API.
  • Support Unreliable, Reliable and ReliableOrdered packets.
  • Support multiple order channels.
  • Support ACK/NACK mechanism.
  • Low level API but easy to use.

Roadmap

  • Add sliding window congestion control

Getting Started

See examples for usage.

Server

IO is a hidden type that implements the traits Stream and Sink. Never stop polling incoming because it also serves as the router to every IOs. Apply Sink::poll_flush to IO will trigger to flush all pending packets, ACK/NACK, and stale packets.

use bytes::Bytes;
use futures::{SinkExt, StreamExt};
use raknet_rs::server::{self, MakeIncoming};

let socket = tokio::net::UdpSocket::bind("127.0.0.1:0").await?;
let config = server::ConfigBuilder::default()
    .send_buf_cap(1024)
    .sever_guid(114514)
    .advertisement(Bytes::from_static(b"Hello, I am server"))
    ...
    .build()
    .unwrap();
let mut incoming = socket.make_incoming(config);
let mut io = incoming.next().await.unwrap();
let data: Bytes = io.next().await.unwrap();
io.send(data).await.unwrap();

Client

The current version of the client only has the most basic handshake implementation, and it is not recommended to use it directly.

use bytes::Bytes;
use futures::{SinkExt, StreamExt};
use raknet_rs::client::{self, ConnectTo};

let socket = tokio::net::UdpSocket::bind("0.0.0.0:0").await?;
let config = client::ConfigBuilder::default()
    .send_buf_cap(1024)
    .client_guid(1919810)
    ...
    .build()
    .unwrap();
let mut conn = socket.connect_to(<addr>, config).await?;
conn.send(Bytes::from_static(b"Hello, Anyone there?"))
    .await?;
let res: Bytes = conn.next().await.unwrap();

Dependencies

~6–18MB
~197K SLoC