Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion netcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -3758,7 +3758,7 @@ void netcode_client_connect_loopback( struct netcode_client_t * client, int clie
}

netcode_printf( NETCODE_LOG_LEVEL_INFO, "client connected to server via loopback as client %d\n", client_index );
client->state = NETCODE_CLIENT_STATE_CONNECTED;
netcode_client_set_state( client, NETCODE_CLIENT_STATE_CONNECTED );
client->client_index = client_index;
client->max_clients = max_clients;
client->loopback = 1;
Expand Down Expand Up @@ -11736,6 +11736,68 @@ void test_packet_tagging()

#endif // #if NETCODE_PACKET_TAGGING

struct test_loopback_state_change_context_t
{
int num_state_changes;
int from_state;
int to_state;
};

void test_loopback_state_change_callback( void * _context, int from_state, int to_state )
{
struct test_loopback_state_change_context_t * context = (struct test_loopback_state_change_context_t*) _context;
context->num_state_changes++;
context->from_state = from_state;
context->to_state = to_state;
}

void test_loopback_noop_packet_callback( void * _context, int client_index, NETCODE_CONST uint8_t * packet_data, int packet_bytes, uint64_t packet_sequence )
{
(void) _context;
(void) client_index;
(void) packet_data;
(void) packet_bytes;
(void) packet_sequence;
}

void test_loopback_state_change()
{
struct test_loopback_state_change_context_t context;
memset( &context, 0, sizeof( context ) );

struct netcode_client_config_t client_config;
netcode_default_client_config( &client_config );
client_config.callback_context = &context;
client_config.state_change_callback = test_loopback_state_change_callback;
client_config.send_loopback_packet_callback = test_loopback_noop_packet_callback;
client_config.override_send_and_receive = 1;
client_config.send_packet_override = test_override_send_packet;
client_config.receive_packet_override = test_override_receive_packet;

struct netcode_client_t * client = netcode_client_create( "0.0.0.0:50000", &client_config, 0.0 );

check( client );

check( netcode_client_state( client ) == NETCODE_CLIENT_STATE_DISCONNECTED );
check( context.num_state_changes == 0 );

netcode_client_connect_loopback( client, 0, 1 );

check( netcode_client_state( client ) == NETCODE_CLIENT_STATE_CONNECTED );
check( context.num_state_changes == 1 );
check( context.from_state == NETCODE_CLIENT_STATE_DISCONNECTED );
check( context.to_state == NETCODE_CLIENT_STATE_CONNECTED );

netcode_client_disconnect_loopback( client );

check( netcode_client_state( client ) == NETCODE_CLIENT_STATE_DISCONNECTED );
check( context.num_state_changes == 2 );
check( context.from_state == NETCODE_CLIENT_STATE_CONNECTED );
check( context.to_state == NETCODE_CLIENT_STATE_DISCONNECTED );

netcode_client_destroy( client );
}

void test_loopback_callback_required()
{
// entering loopback with send_loopback_packet_callback unset would call a null pointer
Expand Down Expand Up @@ -11941,6 +12003,7 @@ void netcode_test()
RUN_TEST( test_client_error_connect_token_at_server_start_floor );
RUN_TEST( test_disable_timeout );
RUN_TEST( test_loopback );
RUN_TEST( test_loopback_state_change );
RUN_TEST( test_loopback_callback_required );
#if NETCODE_ENABLE_NONCE_AUDIT
RUN_TEST( test_nonce_audit );
Expand Down
Loading