Skip to content

Instantly share code, notes, and snippets.

@Scraft
Created October 18, 2017 11:44
Show Gist options
  • Save Scraft/c4763e111aebfb24338cc01393c224f2 to your computer and use it in GitHub Desktop.
Save Scraft/c4763e111aebfb24338cc01393c224f2 to your computer and use it in GitHub Desktop.
Example of overriding send/receive with netcode.io
/*
netcode.io reference implementation
Copyright © 2017, The Network Protocol Company, Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define NETCODE_ENABLE_TESTS 1
#include "netcode.h"
#include "netcode.c"
#include <stdio.h>
#include <assert.h>
#include <deque>
#include <vector>
struct Data
{
Data()
{
}
Data(std::vector<uint8_t> buffer, struct netcode_address_t address)
{
Buffer = buffer;
Address = address;
}
std::vector<uint8_t> Buffer;
struct netcode_address_t Address;
};
std::deque<Data> g_ToServer;
std::deque<Data> g_ToClient;
uint64_t kProtocolId = 0x1122334455667788;
uint8_t kPrivateKey[NETCODE_KEY_BYTES] = "abcdefghijklmnopqrstuvwxyz01234";
int main( int argc, char ** argv )
{
(void) argc;
(void) argv;
// --------------------------------------------
// Our tests.
// --------------------------------------------
netcode_init();
netcode_log_level(NETCODE_LOG_LEVEL_DEBUG);
// --------------------------------------------
// Server set up.
// --------------------------------------------
netcode_server_config_t serverConfig;
netcode_default_server_config(&serverConfig);
serverConfig.override_send_and_receive = 1;
serverConfig.protocol_id = kProtocolId;
memcpy(serverConfig.private_key, kPrivateKey, NETCODE_KEY_BYTES);
serverConfig.send_packet_override = [](void * context, struct netcode_address_t * addr, NETCODE_CONST uint8_t * buffer, int bufferSize)
{
g_ToClient.push_back(Data(std::vector<uint8_t>(buffer, buffer + bufferSize), *addr));
};
serverConfig.receive_packet_override = [](void * context, struct netcode_address_t * addr, uint8_t * buffer, int bufferSize)
{
if (g_ToServer.size() > 0)
{
int size = g_ToServer.front().Buffer.size();
*addr = g_ToServer.front().Address;
memcpy(buffer, &g_ToServer.front().Buffer[0], size);
g_ToServer.pop_front();
return size;
}
return 0;
};
netcode_server_t * pServer = netcode_server_create("127.0.0.1", &serverConfig, 0);
netcode_server_start(pServer, 4);
// --------------------------------------------
// Client set up.
// --------------------------------------------
netcode_client_config_t clientConfig;
netcode_default_client_config(&clientConfig);
clientConfig.override_send_and_receive = 1;
clientConfig.send_packet_override = [](void * context, struct netcode_address_t * addr, NETCODE_CONST uint8_t * buffer, int bufferSize)
{
g_ToServer.push_back(Data(std::vector<uint8_t>(buffer, buffer + bufferSize), *addr));
};
clientConfig.receive_packet_override = [](void * context, struct netcode_address_t * addr, uint8_t * buffer, int bufferSize)
{
if (g_ToClient.size() > 0)
{
int size = g_ToClient.front().Buffer.size();
*addr = g_ToClient.front().Address;
memcpy(buffer, &g_ToClient.front().Buffer[0], size);
g_ToClient.pop_front();
return size;
}
return 0;
};
netcode_client_t * pClient = netcode_client_create("127.0.0.1", &clientConfig, 0);
uint8_t connectToken[NETCODE_CONNECT_TOKEN_BYTES] = { 0 };
const char * serverAddress[1] = { "127.0.0.1" };
netcode_generate_connect_token(1, serverAddress, 45, 5, 0, kProtocolId, 0, kPrivateKey, connectToken);
netcode_client_connect(pClient, connectToken);
// --------------------------------------------
// Run test.
// --------------------------------------------
double time = 0.0;
double delta_time = 0.1;
// Get connected.
while (pClient->state != NETCODE_CLIENT_STATE_CONNECTED)
{
netcode_client_update(pClient, time);
netcode_server_update(pServer, time);
time += delta_time;
}
// Send client -> server.
netcode_client_send_packet(pClient, (const uint8_t*)"Test", 4);
netcode_client_update(pClient, time);
netcode_server_update(pServer, time);
int packetBytes = 0;
uint64_t packetSequence = 0;
uint8_t * bytes = netcode_server_receive_packet(pServer, 0, &packetBytes, &packetSequence);
netcode_server_free_packet(pServer, bytes);
// Send server -> client.
netcode_server_send_packet(pServer, 0, (const uint8_t*)"Ack", 3);
netcode_server_update(pServer, time);
netcode_client_update(pClient, time);
bytes = netcode_client_receive_packet(pClient, &packetBytes, &packetSequence);
netcode_client_free_packet(pClient, bytes);
// --------------------------------------------
// Clean up.
// --------------------------------------------
netcode_server_destroy(pServer);
netcode_client_destroy(pClient);
netcode_term();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment