Skip to content

Commit

Permalink
generalize reliable so it doesn't depend on client indices. it now us…
Browse files Browse the repository at this point in the history
…es 64bit client ids
  • Loading branch information
gafferongames committed Dec 27, 2023
1 parent 2f936b3 commit d6a9d6b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 52 deletions.
8 changes: 4 additions & 4 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ struct connection_t
static struct connection_t client;
static struct connection_t server;

static void transmit_packet( void * context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
static void transmit_packet( void * context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
(void) index;
(void) id;
(void) sequence;
(void) packet_data;
(void) packet_bytes;
Expand All @@ -60,10 +60,10 @@ static void transmit_packet( void * context, int index, uint16_t sequence, uint8
}
}

static int process_packet( void * context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
static int process_packet( void * context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
(void) context;
(void) index;
(void) id;
(void) sequence;
(void) packet_data;
(void) packet_bytes;
Expand Down
10 changes: 5 additions & 5 deletions fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ double global_time = 100.0;

struct reliable_endpoint_t * endpoint;

void test_transmit_packet_function( void * context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
void test_transmit_packet_function( void * context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
(void) context;
(void) index;
(void) id;
(void) sequence;
(void) packet_data;
(void) packet_bytes;
}

int test_process_packet_function( void * context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
int test_process_packet_function( void * context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
(void) context;
(void) index;
(void) id;
(void) sequence;
(void) packet_data;
(void) packet_bytes;
Expand All @@ -80,8 +80,8 @@ void fuzz_initialize()

reliable_default_config( &config );

config.index = 0;
config.transmit_packet_function = &test_transmit_packet_function;

config.process_packet_function = &test_process_packet_function;

endpoint = reliable_endpoint_create( &config, global_time );
Expand Down
52 changes: 26 additions & 26 deletions reliable.c
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ void reliable_endpoint_send_packet( struct reliable_endpoint_t * endpoint, uint8

memcpy( transmit_packet_data + packet_header_bytes, packet_data, packet_bytes );

endpoint->config.transmit_packet_function( endpoint->config.context, endpoint->config.index, sequence, transmit_packet_data, packet_header_bytes + packet_bytes );
endpoint->config.transmit_packet_function( endpoint->config.context, endpoint->config.id, sequence, transmit_packet_data, packet_header_bytes + packet_bytes );

endpoint->free_function( endpoint->allocator_context, transmit_packet_data );
}
Expand Down Expand Up @@ -831,7 +831,7 @@ void reliable_endpoint_send_packet( struct reliable_endpoint_t * endpoint, uint8

int fragment_packet_bytes = (int) ( p - fragment_packet_data );

endpoint->config.transmit_packet_function( endpoint->config.context, endpoint->config.index, sequence, fragment_packet_data, fragment_packet_bytes );
endpoint->config.transmit_packet_function( endpoint->config.context, endpoint->config.id, sequence, fragment_packet_data, fragment_packet_bytes );

endpoint->counters[RELIABLE_ENDPOINT_COUNTER_NUM_FRAGMENTS_SENT]++;
}
Expand Down Expand Up @@ -1107,7 +1107,7 @@ void reliable_endpoint_receive_packet( struct reliable_endpoint_t * endpoint, ui
reliable_printf( RELIABLE_LOG_LEVEL_DEBUG, "[%s] processing packet %d\n", endpoint->config.name, sequence );

if ( endpoint->config.process_packet_function( endpoint->config.context,
endpoint->config.index,
endpoint->config.id,
sequence,
packet_data + packet_header_bytes,
packet_bytes - packet_header_bytes ) )
Expand Down Expand Up @@ -1776,7 +1776,7 @@ void test_default_context( struct test_context_t * context )
context->allow_packets = -1;
}

static void test_transmit_packet_function( void * _context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
static void test_transmit_packet_function( void * _context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
(void) sequence;

Expand All @@ -1797,22 +1797,22 @@ static void test_transmit_packet_function( void * _context, int index, uint16_t
context->allow_packets--;
}

if ( index == 0 )
if ( id == 0 )
{
reliable_endpoint_receive_packet( context->receiver, packet_data, packet_bytes );
}
else if ( index == 1 )
else if ( id == 1 )
{
reliable_endpoint_receive_packet( context->sender, packet_data, packet_bytes );
}
}

static int test_process_packet_function( void * _context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
static int test_process_packet_function( void * _context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
struct test_context_t * context = (struct test_context_t*) _context;

(void) context;
(void) index;
(void) id;
(void) sequence;
(void) packet_data;
(void) packet_bytes;
Expand All @@ -1836,12 +1836,12 @@ static void test_acks()
reliable_default_config( &receiver_config );

sender_config.context = &context;
sender_config.index = 0;
sender_config.id = 0;
sender_config.transmit_packet_function = &test_transmit_packet_function;
sender_config.process_packet_function = &test_process_packet_function;

receiver_config.context = &context;
receiver_config.index = 1;
receiver_config.id = 1;
receiver_config.transmit_packet_function = &test_transmit_packet_function;
receiver_config.process_packet_function = &test_process_packet_function;

Expand Down Expand Up @@ -1913,12 +1913,12 @@ static void test_acks_packet_loss()
reliable_default_config( &receiver_config );

sender_config.context = &context;
sender_config.index = 0;
sender_config.id = 0;
sender_config.transmit_packet_function = &test_transmit_packet_function;
sender_config.process_packet_function = &test_process_packet_function;

receiver_config.context = &context;
receiver_config.index = 1;
receiver_config.id = 1;
receiver_config.transmit_packet_function = &test_transmit_packet_function;
receiver_config.process_packet_function = &test_process_packet_function;

Expand Down Expand Up @@ -2018,14 +2018,14 @@ static void validate_packet_data( uint8_t * packet_data, int packet_bytes )
}
}

static int test_process_packet_function_validate( void * context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
static int test_process_packet_function_validate( void * context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
reliable_assert( packet_data );
reliable_assert( packet_bytes > 0 );
reliable_assert( packet_bytes <= TEST_MAX_PACKET_BYTES );

(void) context;
(void) index;
(void) id;
(void) sequence;

validate_packet_data( packet_data, packet_bytes );
Expand All @@ -2049,15 +2049,15 @@ static int generate_packet_data_large( uint8_t* packet_data )
return data_bytes + 2;
}

static int test_process_packet_function_validate_large( void * context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
static int test_process_packet_function_validate_large( void * context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
reliable_assert( packet_data );
reliable_assert( packet_bytes >= 2 );
reliable_assert( packet_bytes <= TEST_MAX_PACKET_BYTES );

(void)context;
(void)index;
(void)sequence;
(void) context;
(void) id;
(void) sequence;

uint16_t data_bytes = 0;
data_bytes |= (uint16_t) packet_data[0];
Expand Down Expand Up @@ -2094,7 +2094,7 @@ void test_packets()
strcpy( sender_config.name, "sender" );
#endif
sender_config.context = &context;
sender_config.index = 0;
sender_config.id = 0;
sender_config.transmit_packet_function = &test_transmit_packet_function;
sender_config.process_packet_function = &test_process_packet_function_validate;

Expand All @@ -2104,7 +2104,7 @@ void test_packets()
strcpy( receiver_config.name, "receiver" );
#endif
receiver_config.context = &context;
receiver_config.index = 1;
receiver_config.id = 1;
receiver_config.transmit_packet_function = &test_transmit_packet_function;
receiver_config.process_packet_function = &test_process_packet_function_validate;

Expand Down Expand Up @@ -2168,7 +2168,7 @@ void test_large_packets()
strcpy( sender_config.name, "sender" );
#endif
sender_config.context = &context;
sender_config.index = 0;
sender_config.id = 0;
sender_config.transmit_packet_function = &test_transmit_packet_function;
sender_config.process_packet_function = &test_process_packet_function_validate_large;

Expand All @@ -2178,7 +2178,7 @@ void test_large_packets()
strcpy( receiver_config.name, "receiver" );
#endif
receiver_config.context = &context;
receiver_config.index = 1;
receiver_config.id = 1;
receiver_config.transmit_packet_function = &test_transmit_packet_function;
receiver_config.process_packet_function = &test_process_packet_function_validate_large;

Expand Down Expand Up @@ -2228,7 +2228,7 @@ void test_sequence_buffer_rollover()
strcpy( sender_config.name, "sender" );
#endif
sender_config.context = &context;
sender_config.index = 0;
sender_config.id = 0;
sender_config.transmit_packet_function = &test_transmit_packet_function;
sender_config.process_packet_function = &test_process_packet_function;

Expand All @@ -2238,7 +2238,7 @@ void test_sequence_buffer_rollover()
strcpy( receiver_config.name, "receiver" );
#endif
receiver_config.context = &context;
receiver_config.index = 1;
receiver_config.id = 1;
receiver_config.transmit_packet_function = &test_transmit_packet_function;
receiver_config.process_packet_function = &test_process_packet_function;

Expand Down Expand Up @@ -2341,7 +2341,7 @@ void test_fragment_cleanup()
strcpy( sender_config.name, "sender" );
#endif
sender_config.context = &context;
sender_config.index = 0;
sender_config.id = 0;
sender_config.transmit_packet_function = &test_transmit_packet_function;
sender_config.process_packet_function = &test_process_packet_function;

Expand All @@ -2351,7 +2351,7 @@ void test_fragment_cleanup()
strcpy( receiver_config.name, "receiver" );
#endif
receiver_config.context = &context;
receiver_config.index = 1;
receiver_config.id = 1;
receiver_config.transmit_packet_function = &test_transmit_packet_function;
receiver_config.process_packet_function = &test_process_packet_function;

Expand Down
6 changes: 3 additions & 3 deletions reliable.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct reliable_config_t
{
char name[256];
void * context;
int index;
uint64_t id;
int max_packet_size;
int fragment_above;
int max_fragments;
Expand All @@ -110,8 +110,8 @@ struct reliable_config_t
float packet_loss_smoothing_factor;
float bandwidth_smoothing_factor;
int packet_header_size;
void (*transmit_packet_function)(void*,int,uint16_t,uint8_t*,int);
int (*process_packet_function)(void*,int,uint16_t,uint8_t*,int);
void (*transmit_packet_function)(void*,uint64_t,uint16_t,uint8_t*,int);
int (*process_packet_function)(void*,uint64_t,uint16_t,uint8_t*,int);
void * allocator_context;
void * (*allocate_function)(void*,size_t);
void (*free_function)(void*,void*);
Expand Down
14 changes: 7 additions & 7 deletions soak.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ double global_time = 100.0;

struct test_context_t global_context;

void test_transmit_packet_function( void * _context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
void test_transmit_packet_function( void * _context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
(void) sequence;

Expand All @@ -69,11 +69,11 @@ void test_transmit_packet_function( void * _context, int index, uint16_t sequenc
if ( random_int(0,100) < 5 )
return;

if ( index == 0 )
if ( id == 0 )
{
reliable_endpoint_receive_packet( context->server, packet_data, packet_bytes );
}
else if ( index == 1 )
else if ( id == 1 )
{
reliable_endpoint_receive_packet( context->client, packet_data, packet_bytes );
}
Expand Down Expand Up @@ -134,14 +134,14 @@ void check_packet_data( uint8_t * packet_data, int packet_bytes )
}
}

int test_process_packet_function( void * context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
int test_process_packet_function( void * context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
assert( packet_data );
assert( packet_bytes > 0 );
assert( packet_bytes <= MAX_PACKET_BYTES );

(void) context;
(void) index;
(void) id;
(void) sequence;

check_packet_data( packet_data, packet_bytes );
Expand Down Expand Up @@ -174,7 +174,7 @@ void soak_initialize()
strcpy( client_config.name, "client" );
#endif
client_config.context = &global_context;
client_config.index = 0;
client_config.id = 0;
client_config.transmit_packet_function = &test_transmit_packet_function;
client_config.process_packet_function = &test_process_packet_function;

Expand All @@ -184,7 +184,7 @@ void soak_initialize()
strcpy( server_config.name, "server" );
#endif
server_config.context = &global_context;
server_config.index = 1;
server_config.id = 1;
server_config.transmit_packet_function = &test_transmit_packet_function;
server_config.process_packet_function = &test_process_packet_function;

Expand Down
14 changes: 7 additions & 7 deletions stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ double global_time = 100.0;

struct test_context_t global_context;

void test_transmit_packet_function( void * _context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
void test_transmit_packet_function( void * _context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
(void) sequence;

Expand All @@ -70,11 +70,11 @@ void test_transmit_packet_function( void * _context, int index, uint16_t sequenc
if ( ( sequence % 5 ) == 0 )
return;

if ( index == 0 )
if ( id == 0 )
{
reliable_endpoint_receive_packet( context->server, packet_data, packet_bytes );
}
else if ( index == 1 )
else if ( id == 1 )
{
reliable_endpoint_receive_packet( context->client, packet_data, packet_bytes );
}
Expand Down Expand Up @@ -133,14 +133,14 @@ void check_packet_data( uint8_t * packet_data, int packet_bytes )
}
}

int test_process_packet_function( void * context, int index, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
int test_process_packet_function( void * context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
assert( packet_data );
assert( packet_bytes > 0 );
assert( packet_bytes <= MAX_PACKET_BYTES );

(void) context;
(void) index;
(void) id;
(void) sequence;

check_packet_data( packet_data, packet_bytes );
Expand Down Expand Up @@ -171,7 +171,7 @@ void stats_initialize()
strcpy( client_config.name, "client" );
#endif
client_config.context = &global_context;
client_config.index = 0;
client_config.id = 0;
client_config.transmit_packet_function = &test_transmit_packet_function;
client_config.process_packet_function = &test_process_packet_function;

Expand All @@ -181,7 +181,7 @@ void stats_initialize()
strcpy( server_config.name, "server" );
#endif
server_config.context = &global_context;
server_config.index = 1;
server_config.id = 1;
server_config.transmit_packet_function = &test_transmit_packet_function;
server_config.process_packet_function = &test_process_packet_function;

Expand Down

0 comments on commit d6a9d6b

Please sign in to comment.