Expand description
Netcode.io protocol to establish a connection on top of an unreliable transport
§netcode
The netcode crate implements the netcode
network protocol created by Glenn Fiedler.
netcode is a UDP-based protocol that provides secure, connection-based data transfer.
Since the protocol is meant to be used to implement multiplayer games, its API is designed to be used in a game loop, where the server and client are updated at a fixed rate (e.g., 60Hz).
§Protocol
The three main components of the netcode protocol are:
- Dedicated
Servers. Clients.- The web backend - a service that authenticates clients and generates
ConnectTokens.
The protocol does not specify how the web backend should be implemented, but it should probably be a typical HTTPS server that provides a means for clients to authenticate and request connection tokens.
The sequence of operations for a client to connect to a server is as follows:
- The
Clientauthenticates with the web backend service. (e.g., by OAuth or some other means) - The authenticated
Clientrequests a connection token from the web backend. - The web backend generates a
ConnectTokenand sends it to theClient. (e.g., as a JSON response) - The
Clientuses the token to connect to a dedicatedServer. - The
Servermakes sure the token is valid and allows theClientto connect. - The
ClientandServercan now exchange encrypted and signed UDP packets.
To learn more about the netcode protocol, see the upstream specification.
§Server
The netcode server is responsible for managing the state of the clients and sending/receiving packets.
The server should run as a part of the game loop, process incoming packets and send updates to the clients.
To create a server:
- Provide the address you intend to bind to.
- Provide the protocol id - a
u64that uniquely identifies your app. - Provide a private key - a
u8array of length 32. If you don’t have one, you can generate one withnetcode::generate_key(). - Optionally provide a
ServerConfig- a struct that allows you to customize the server’s behavior.
§Client
The netcode client connects to the server and communicates using the same protocol.
Like the server, the game client should run in a loop to process incoming data, send updates to the server, and maintain a stable connection.
To create a client:
- Provide a connect token - a
u8array of length 2048 serialized from aConnectToken. - Optionally provide a
ClientConfig- a struct that allows you to customize the client’s behavior.
Re-exports§
pub use client_plugin::NetcodeClient;pub use server_plugin::NetcodeServer;
Modules§
Structs§
- Connect
Token - A token containing all the information required for a client to connect to a server.
- Connect
Token Builder - A builder that can be used to generate a connect token.
- Server
- The
netcodeserver. - Server
Config - Configuration for a server.
Enums§
- Error
- An error that can occur in the
netcodecrate. - Invalid
Token Error - An error that can occur when de-serializing a connect token from bytes.
Constants§
- CONNECT_
TOKEN_ BYTES - The size of the connect token in bytes.
- MAX_
PACKET_ SIZE - The maximum size of a packet in bytes.
- NETCODE_
VERSION - The version of the netcode protocol implemented by this crate.
- PRIVATE_
KEY_ BYTES - The size of a private key in bytes.
- USER_
DATA_ BYTES - The size of the user data in a connect token in bytes.
Functions§
- generate_
key - Generates a random key for encrypting and decrypting packets and connect tokens.
- try_
generate_ key - The fallible version of
generate_key.