Skip to content

Instantly share code, notes, and snippets.

@xemasiv
Created April 9, 2018 21:29
Show Gist options
  • Save xemasiv/5486f11cb9240eee0e685fbcfd6c2d21 to your computer and use it in GitHub Desktop.
Save xemasiv/5486f11cb9240eee0e685fbcfd6c2d21 to your computer and use it in GitHub Desktop.
initial netcode.io usage guide

netcode.io usage

netcode.io enables a client/server architecture, handling the client/server connection handshake and encrypting the traffic between them.

server setup

  1. 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)
  1. start listening for up to NETCODE_MAX_CLIENTS clients:
netcode_server_start( server, NETCODE_MAX_CLIENTS )
  1. 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 setup

  1. client generates an id for itself:
uint64_t client_id = 0
netcode_random_bytes((uint8_t*) &client_id, 8)
  1. 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)
  1. 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)
  1. 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment