netcode.io enables a client/server architecture, handling the client/server connection handshake and encrypting the traffic between them.
- create a server instance:
char * server_address = "127.0.0.1:40000"
struct netcode_server_config_t server_config
netcode_default_server_config( &server_config)
server_config.protocol_id = TEST_PROTOCOL_ID
memcpy(&server_config.private_key, private_key, NETCODE_KEY_BYTES)
struct netcode_server_t * server = netcode_server_create(server_address, &server_config, time)
- start listening for up to NETCODE_MAX_CLIENTS clients:
netcode_server_start( server, NETCODE_MAX_CLIENTS )
- the server enters it's regular update loop, which looks like this:
double time = 0.0 // global system time
double delta_time = 1.0 / 60.0 // frame time (1/60 of a second)
while(!quit) {
netcode_server_update(client, time)
// an example of sending data to the client;
// if client in slot 0 is connected, send it a packet
if (netcode_server_client_connected(server, 0))
netcode_server_send_packet(server, 0, packet_data, NETCODE_MAX_PACKET_SIZE)
// receive packets from all connected clients
for (int client_index = 0; client_index < NETCODE_MAX_CLIENTS; ++client_index) {
void * packet
do {
int packet_bytes
uint64_t packet_sequence
packet = netcode_server_receive_packet( server, client_index, &packet_bytes, &packet_sequence );
if(packet) {
// do something with the packet you've received here
// free/cleanup the packet
netcode_server_free_packet(server, packet)
}
} while(packet)
}
netcode_sleep(delta_time)
time += delta_time
}
- client generates an id for itself:
uint64_t client_id = 0
netcode_random_bytes((uint8_t*) &client_id, 8)
- client generates a connect token for a given server, also passing EXPIRY and TIMEOUT settings
// expressed in seconds
#define CONNECT_TOKEN_EXPIRY 30
#define CONNECT_TOKEN_TIMEOUT 5
char * server_address = "127.0.0.1:40000"
uint8_t connect_token[NETCODE_CONNECT_TOKEN_BYTES]
netcode_generate_connect_token(1, (NETCODE_CONST char**) &server_address, (NETCODE_CONST char**) &server_address, CONNECT_TOKEN_EXPIRY, CONNECT_TOKEN_TIMEOUT, client_id, PROTOCOL_ID, 0, private_key, connect_token)
- client initiates connection to server. This puts the client into a connecting state while it handles all of the server handshaking.
netcode_client_connect(client, connect_token)
- client enters it's regular update loop, which looks like this:
double time = 0.0 // global system time
double delta_time = 1.0 / 60.0 // frame time (1/60 of a second)
while(!quit) {
netcode_client_update(client, time)
if (netcode_client_state(client) == NETCODE_CLIENT_STATE_CONNECTED) {
// at this point the connection is established and encrypted data is sent
// between the client and the server.
//
// send any data to the server from the client that needs to go out
netcode_client_send_packet(client, packet_data, NETCODE_MAX_PACKET_SIZE)
}
void * packet
do {
int packet_bytes
uint64_t packet_sequence
packet = netcode_client_receive_packet(client, &packet_bytes, &packet_sequence)
if(packet) {
// do something with the packet you've received here
// free/cleanup the packet
netcode_client_free_packet(client, packet)
}
} while(packet)
if (netcode_client_state(client) <= NETCODE_CLIENT_STATE_DISCONNECTED)
break
netcode_sleep(delta_time)
time += delta_time
}