Protocol
This crate implements the netcode network protocol created by Glenn Fiedler.
netcode
is a simple connection based client/server protocol built on top of UDP.
See the official upstream specification to learn more about how the protocol works.
Install
Run the following Cargo command in your project directory:
Or add the following line to your Cargo.toml:
[]
= "1.4.0"
NOTE: While the crate name is netcode-rs
, the library name is actually netcode
.
This means that even though you used cargo add netcode-rs
to add the dependency, you will need to write use netcode::*
(and not use netcode_rs::*
) to use it.
I know this is confusing, and I know that -rs
crate names are frowned upon.
Unfortunately the original netcode
crate is unmaintained and its maintainer is unreachable, so I could not get the ownership for the crate name.
I didn't want to use names like netcode2
to avoid confusion with the netcode protocol versions.
Examples
server
use ;
use ;
// Create a server
let protocol_id = 0x11223344u64; // a unique number that must match the client's protocol id
let private_key = generate_key; // you can also provide your own key
let mut server = new.unwrap;
// Run the server at 60Hz
let start = now;
let tick_rate = from_secs_f64;
loop
client
use ;
use ;
// Generate a connection token for the client
let protocol_id = 0x11223344u64; // a unique number that must match the server's protocol id
let private_key = generate_key; // you can also provide your own key
let client_id = 123u64; // globally unique identifier for an authenticated client
let server_address = "my-domain.com:5555"; // the server's public address (can also be multiple addresses)
let connect_token = build.generate.unwrap;
// Start the client
let token_bytes = connect_token.try_into_bytes.unwrap;
let mut client = new.unwrap;
client.connect;
// Run the client at 60Hz
let start = now;
let tick_rate = from_secs_f64;
loop
See examples for more.