From b8a7f0374877c72977f200e08da4133c2e25134d Mon Sep 17 00:00:00 2001 From: Glenn Fiedler Date: Wed, 18 Sep 2024 17:19:47 -0400 Subject: [PATCH] the problem was the new netcode address map. i removed that and went back to a simple linear search for now --- include/yojimbo_config.h | 3 - netcode/netcode.c | 293 ++------------------------------- source/yojimbo_base_client.cpp | 20 +-- source/yojimbo_client.cpp | 21 --- source/yojimbo_platform.cpp | 3 +- test.cpp | 17 -- 6 files changed, 18 insertions(+), 339 deletions(-) diff --git a/include/yojimbo_config.h b/include/yojimbo_config.h index e3fabc08..3b770f19 100644 --- a/include/yojimbo_config.h +++ b/include/yojimbo_config.h @@ -66,9 +66,6 @@ #define YOJIMBO_PLATFORM YOJIMBO_PLATFORM_UNIX #endif -// todo -#define YOJIMBO_DEBUG 1 - #ifdef YOJIMBO_DEBUG #define YOJIMBO_DEBUG_MEMORY_LEAKS 1 diff --git a/netcode/netcode.c b/netcode/netcode.c index 78849b94..00690fae 100755 --- a/netcode/netcode.c +++ b/netcode/netcode.c @@ -121,10 +121,8 @@ void netcode_set_assert_function( void (*function)( NETCODE_CONST char *, NETCOD void netcode_printf( int level, NETCODE_CONST char * format, ... ) { - // todo - (void) level; - // if ( level > log_level ) - // return; + if ( level > log_level ) + return; va_list args; va_start( args, format ); char buffer[4*1024]; @@ -3083,9 +3081,6 @@ void netcode_client_send_packet_to_server_internal( struct netcode_client_t * cl if ( client->config.network_simulator ) { - // todo - printf( "sent packet through network simulator\n" ); - netcode_network_simulator_send_packet( client->config.network_simulator, &client->address, &client->server_address, packet_data, packet_bytes ); } else @@ -3703,66 +3698,6 @@ int netcode_connect_token_entries_find_or_add( struct netcode_connect_token_entr return 0; } -// ---------------------------------------------------------------- - -struct netcode_address_map_element_t -{ - int client_index; - struct netcode_address_t address; -}; - -struct netcode_address_map_bucket_t -{ - int size; - struct netcode_address_map_element_t elements[NETCODE_MAX_CLIENTS]; -}; - -struct netcode_address_map_t -{ - void * allocator_context; - void * (*allocate_function)(void*,size_t); - void (*free_function)(void*,void*); - int size; - struct netcode_address_map_bucket_t buckets[NETCODE_ADDRESS_MAP_BUCKETS]; -}; - -static void netcode_address_map_reset( struct netcode_address_map_t * map ); - -struct netcode_address_map_t * netcode_address_map_create( void * allocator_context, - void * (*allocate_function)(void*,size_t), - void (*free_function)(void*,void*) ) -{ - if ( allocate_function == NULL ) - { - allocate_function = netcode_default_allocate_function; - } - - if ( free_function == NULL ) - { - free_function = netcode_default_free_function; - } - - struct netcode_address_map_t * map = (struct netcode_address_map_t*) - allocate_function( allocator_context, sizeof( struct netcode_address_map_t ) ); - - netcode_assert( map ); - - netcode_address_map_reset( map ); - - map->allocator_context = allocator_context; - map->allocate_function = allocate_function; - map->free_function = free_function; - - return map; -} - -void netcode_address_map_destroy( struct netcode_address_map_t * map ) -{ - netcode_assert( map ); - netcode_assert( map->free_function ); - map->free_function( map->allocator_context, map ); -} - typedef uint64_t netcode_fnv_t; void netcode_fnv_init( netcode_fnv_t * fnv ) @@ -3800,114 +3735,6 @@ uint64_t netcode_hash_data( NETCODE_CONST uint8_t * data, size_t size ) return netcode_fnv_finalize( &fnv ); } -static int netcode_address_hash( struct netcode_address_t * address ) -{ - return netcode_hash_data( (NETCODE_CONST uint8_t*) address, sizeof(struct netcode_address_t) ) % NETCODE_ADDRESS_MAP_BUCKETS; -} - -static void netcode_address_map_element_reset( struct netcode_address_map_element_t * element ) -{ - element->client_index = -1; - memset( &element->address, 0, sizeof( element->address ) ); -} - -static void netcode_address_map_bucket_reset( struct netcode_address_map_bucket_t * bucket ) -{ - int i; - bucket->size = 0; - for ( i = 0; i < NETCODE_MAX_CLIENTS; i++ ) - { - struct netcode_address_map_element_t * element = bucket->elements + i; - netcode_address_map_element_reset( element ); - } -} - -static void netcode_address_map_reset( struct netcode_address_map_t * map ) -{ - int i; - map->size = 0; - for ( i = 0; i < NETCODE_ADDRESS_MAP_BUCKETS; i++ ) - { - struct netcode_address_map_bucket_t * bucket = map->buckets + i; - netcode_address_map_bucket_reset(bucket); - } -} - -static int netcode_address_map_set( struct netcode_address_map_t * map, - struct netcode_address_t * address, - int client_index ) -{ - int bucket_index = netcode_address_hash( address ); - struct netcode_address_map_bucket_t * bucket = map->buckets + bucket_index; - if ( bucket->size == NETCODE_MAX_CLIENTS ) - { - return 0; - } - - struct netcode_address_map_element_t * element = bucket->elements + bucket->size; - element->client_index = client_index; - element->address = *address; - - ++bucket->size; - ++map->size; - - return 1; -} - -static struct netcode_address_map_element_t * netcode_address_map_bucket_find( - struct netcode_address_map_bucket_t * bucket, - struct netcode_address_t * address ) -{ - int i; - for ( i = 0; i < bucket->size; i++ ) - { - struct netcode_address_map_element_t * element = bucket->elements + i; - if ( netcode_address_equal( address, &element->address ) ) - { - return element; - } - } - - return NULL; -} - -static int netcode_address_map_get( struct netcode_address_map_t * map, - struct netcode_address_t * address ) -{ - int bucket_index = netcode_address_hash( address ); - struct netcode_address_map_bucket_t * bucket = map->buckets + bucket_index; - struct netcode_address_map_element_t * element = netcode_address_map_bucket_find( bucket, address ); - - if ( !element ) - { - return -1; - } - - return element->client_index; -} - -static int netcode_address_map_delete( struct netcode_address_map_t * map, - struct netcode_address_t * address ) -{ - int bucket_index = netcode_address_hash( address ); - struct netcode_address_map_bucket_t * bucket = map->buckets + bucket_index; - struct netcode_address_map_element_t * element = netcode_address_map_bucket_find( bucket, address ); - - if ( !element ) - { - return 0; - } - - struct netcode_address_map_element_t * last = bucket->elements + (bucket->size - 1); - *element = *last; - netcode_address_map_element_reset(last); - - --bucket->size; - --map->size; - - return 1; -} - // ---------------------------------------------------------------- #define NETCODE_SERVER_FLAG_IGNORE_CONNECTION_REQUEST_PACKETS 1 @@ -3954,7 +3781,6 @@ struct netcode_server_t struct netcode_replay_protection_t client_replay_protection[NETCODE_MAX_CLIENTS]; struct netcode_packet_queue_t client_packet_queue[NETCODE_MAX_CLIENTS]; struct netcode_address_t client_address[NETCODE_MAX_CLIENTS]; - struct netcode_address_map_t client_address_map; struct netcode_connect_token_entry_t connect_token_entries[NETCODE_MAX_CONNECT_TOKEN_ENTRIES]; struct netcode_encryption_manager_t encryption_manager; uint8_t * receive_packet_data[NETCODE_SERVER_MAX_RECEIVE_PACKETS]; @@ -4051,6 +3877,8 @@ struct netcode_server_t * netcode_server_create_overload( NETCODE_CONST char * s return NULL; } + memset( server, 0, sizeof(struct netcode_server_t) ); + if ( !config->network_simulator ) { netcode_printf( NETCODE_LOG_LEVEL_INFO, "server listening on %s\n", server_address1_string ); @@ -4064,26 +3892,9 @@ struct netcode_server_t * netcode_server_create_overload( NETCODE_CONST char * s server->socket_holder.ipv4 = socket_ipv4; server->socket_holder.ipv6 = socket_ipv6; server->address = server_address1; - server->flags = 0; server->time = time; - server->running = 0; - server->max_clients = 0; - server->num_connected_clients = 0; server->global_sequence = 1ULL << 63; - memset( server->client_connected, 0, sizeof( server->client_connected ) ); - memset( server->client_loopback, 0, sizeof( server->client_loopback ) ); - memset( server->client_confirmed, 0, sizeof( server->client_confirmed ) ); - memset( server->client_id, 0, sizeof( server->client_id ) ); - memset( server->client_sequence, 0, sizeof( server->client_sequence ) ); - - memset( server->client_address, 0, sizeof( server->client_address ) ); - netcode_address_map_reset( &server->client_address_map ); - memset( server->client_user_data, 0, sizeof( server->client_user_data ) ); - - memset( server->client_last_packet_send_time, 0, sizeof( server->client_last_packet_send_time ) ); - memset( server->client_last_packet_receive_time, 0, sizeof( server->client_last_packet_receive_time ) ); - int i; for ( i = 0; i < NETCODE_MAX_CLIENTS; i++ ) { @@ -4099,8 +3910,6 @@ struct netcode_server_t * netcode_server_create_overload( NETCODE_CONST char * s netcode_replay_protection_reset( &server->client_replay_protection[i] ); } - memset( &server->client_packet_queue, 0, sizeof( server->client_packet_queue ) ); - return server; } @@ -4130,7 +3939,9 @@ void netcode_server_start( struct netcode_server_t * server, int max_clients ) netcode_assert( max_clients <= NETCODE_MAX_CLIENTS ); if ( server->running ) + { netcode_server_stop( server ); + } netcode_printf( NETCODE_LOG_LEVEL_INFO, "server started with %d client slots\n", max_clients ); @@ -4292,7 +4103,6 @@ void netcode_server_disconnect_client_internal( struct netcode_server_t * server server->client_sequence[client_index] = 0; server->client_last_packet_send_time[client_index] = 0.0; server->client_last_packet_receive_time[client_index] = 0.0; - netcode_address_map_delete( &server->client_address_map, &server->client_address[client_index] ); memset( &server->client_address[client_index], 0, sizeof( struct netcode_address_t ) ); server->client_encryption_index[client_index] = -1; memset( server->client_user_data[client_index], 0, NETCODE_USER_DATA_BYTES ); @@ -4382,20 +4192,14 @@ int netcode_server_find_client_index_by_address( struct netcode_server_t * serve netcode_assert( server ); netcode_assert( address ); - if ( address->type == 0 ) - { - // todo - printf( "address type is zero?!\n" ); - return -1; + int i; + for ( i = 0; i < server->max_clients; i++ ) + { + if ( server->client_connected[i] && netcode_address_equal( &server->client_address[i], address ) ) + return i; } - int client_index = netcode_address_map_get( &server->client_address_map, address ); - - // todo - char address_string[NETCODE_MAX_ADDRESS_STRING_LENGTH]; - printf( "netcode_server_find_client_index_by_address: %s -> %d\n", netcode_address_to_string( address, address_string ), client_index ); - - return client_index; + return -1; } void netcode_server_process_connection_request_packet( struct netcode_server_t * server, @@ -4547,8 +4351,6 @@ void netcode_server_connect_client( struct netcode_server_t * server, server->client_sequence[client_index] = 0; server->client_address[client_index] = *address; - netcode_address_map_set( &server->client_address_map, address, client_index ); - netcode_assert( netcode_server_find_client_index_by_id( server, client_id ) == client_index ); netcode_assert( netcode_server_find_client_index_by_address( server, address ) == client_index ); @@ -4561,9 +4363,6 @@ void netcode_server_connect_client( struct netcode_server_t * server, netcode_printf( NETCODE_LOG_LEVEL_INFO, "server accepted client %s %.16" PRIx64 " in slot %d\n", netcode_address_to_string( address, address_string ), client_id, client_index ); - // todo - exit(1); - struct netcode_connection_keep_alive_packet_t packet; packet.packet_type = NETCODE_CONNECTION_KEEP_ALIVE_PACKET; packet.client_index = client_index; @@ -4656,9 +4455,6 @@ void netcode_server_process_packet_internal( struct netcode_server_t * server, uint8_t packet_type = ( (uint8_t*) packet ) [0]; - // todo - printf( "server process packet type %d\n", packet_type ); - switch ( packet_type ) { case NETCODE_CONNECTION_REQUEST_PACKET: @@ -4695,11 +4491,6 @@ void netcode_server_process_packet_internal( struct netcode_server_t * server, server->client_confirmed[client_index] = 1; } } - else - { - // todo - printf( "*** ignoring keep alive because client index is -1 ***\n" ); - } } break; @@ -4959,7 +4750,6 @@ void netcode_server_check_for_timeouts( struct netcode_server_t * server ) if ( server->client_loopback[i] ) continue; - // todo if ( ( server->time - server->client_last_packet_receive_time[i] ) >= 1.0f ) { netcode_printf( NETCODE_LOG_LEVEL_DEBUG, "server has not received a packet from client %d for %.2f seconds\n", i, server->time - server->client_last_packet_receive_time[i] ); @@ -4969,10 +4759,6 @@ void netcode_server_check_for_timeouts( struct netcode_server_t * server ) { netcode_printf( NETCODE_LOG_LEVEL_INFO, "server timed out client %d\n", i ); netcode_server_disconnect_client_internal( server, i, 0 ); - - // todo - printf( "*** BROKEN ***\n" ); - exit(1); } } } @@ -5168,7 +4954,6 @@ void netcode_server_connect_loopback_client( struct netcode_server_t * server, i server->client_id[client_index] = client_id; server->client_sequence[client_index] = 0; memset( &server->client_address[client_index], 0, sizeof( struct netcode_address_t ) ); - netcode_address_map_set( &server->client_address_map, &server->client_address[client_index], client_index ); server->client_last_packet_send_time[client_index] = server->time; server->client_last_packet_receive_time[client_index] = server->time; @@ -5222,7 +5007,6 @@ void netcode_server_disconnect_loopback_client( struct netcode_server_t * server server->client_sequence[client_index] = 0; server->client_last_packet_send_time[client_index] = 0.0; server->client_last_packet_receive_time[client_index] = 0.0; - netcode_address_map_delete( &server->client_address_map, &server->client_address[client_index] ); memset( &server->client_address[client_index], 0, sizeof( struct netcode_address_t ) ); server->client_encryption_index[client_index] = -1; memset( server->client_user_data[client_index], 0, NETCODE_USER_DATA_BYTES ); @@ -8734,58 +8518,6 @@ void test_loopback() netcode_network_simulator_destroy( network_simulator ); } -void test_address_map() -{ - NETCODE_CONST char * str_address_1 = "107.77.207.77:40000"; - NETCODE_CONST char * str_address_2 = "127.0.0.1:23650"; - NETCODE_CONST char * str_address_3 = "fe80::202:b3ff:fe1e:8329"; - NETCODE_CONST char * str_address_4 = "fe80::202:b3ff:fe1e:8330"; - - struct netcode_address_map_t * map = netcode_address_map_create( NULL, NULL, NULL ); - - struct netcode_address_t address_set; - struct netcode_address_t address_get; - struct netcode_address_t address_delete; - - netcode_address_map_reset( map ); - - // Set ipv4 - netcode_parse_address( str_address_1, &address_set ); - check( netcode_address_map_set( map, &address_set, 0 ) == 1 ); - - // Set ipv6 - netcode_parse_address( str_address_3, &address_set ); - check( netcode_address_map_set( map, &address_set, 1 ) == 1 ); - - // Get ipv4 - netcode_parse_address( str_address_1, &address_get ); - check( netcode_address_map_get( map, &address_get) == 0 ); - - // Get ipv6 - netcode_parse_address( str_address_3, &address_get ); - check( netcode_address_map_get( map, &address_get) == 1 ); - - // Get non-existent ipv4 - netcode_parse_address( str_address_2, &address_get ); - check( netcode_address_map_get( map, &address_get ) == -1); - - // Get non-existent ipv6 - netcode_parse_address( str_address_4, &address_get ); - check( netcode_address_map_get( map, &address_get ) == -1); - - // Try to delete key, after that, the key should disappear - netcode_parse_address( str_address_1, &address_delete ); - netcode_parse_address( str_address_1, &address_get ); - check( netcode_address_map_delete( map, &address_delete ) == 1 ); - check( netcode_address_map_get( map, &address_get ) == -1 ); - - // Try to delete non-existent key - netcode_parse_address( str_address_2, &address_delete ); - check ( netcode_address_map_delete( map, &address_delete ) == 0 ); - - netcode_address_map_destroy( map ); -} - #if NETCODE_PACKET_TAGGING void test_packet_tagging() @@ -8915,7 +8647,6 @@ void netcode_test() RUN_TEST( test_client_reconnect ); RUN_TEST( test_disable_timeout ); RUN_TEST( test_loopback ); - RUN_TEST( test_address_map ); #if NETCODE_PACKET_TAGGING RUN_TEST( test_packet_tagging ); #endif // #if NETCODE_PACKET_TAGGING diff --git a/source/yojimbo_base_client.cpp b/source/yojimbo_base_client.cpp index 0b9ec1be..2c0c1ca6 100644 --- a/source/yojimbo_base_client.cpp +++ b/source/yojimbo_base_client.cpp @@ -34,9 +34,6 @@ namespace yojimbo { BaseClient::BaseClient( Allocator & allocator, const ClientServerConfig & config, Adapter & adapter, double time ) : m_config( config ) { - // todo - printf( "BaseClient::BaseClient\n" ); - m_allocator = &allocator; m_adapter = &adapter; m_time = time; @@ -54,9 +51,6 @@ namespace yojimbo BaseClient::~BaseClient() { - // todo - printf( "BaseClient::~BaseClient\n" ); - // IMPORTANT: Please disconnect the client before destroying it yojimbo_assert( m_clientState <= CLIENT_STATE_DISCONNECTED ); YOJIMBO_FREE( *m_allocator, m_packetBuffer ); @@ -65,9 +59,6 @@ namespace yojimbo void BaseClient::Disconnect() { - // todo - printf( "BaseClient::Disconnect\n" ); - SetClientState( CLIENT_STATE_DISCONNECTED ); Reset(); } @@ -136,27 +127,24 @@ namespace yojimbo void BaseClient::CreateInternal() { - // todo - printf( "BaseClient::CreateInternal\n" ); - yojimbo_assert( m_allocator ); yojimbo_assert( m_adapter ); yojimbo_assert( m_clientMemory == NULL ); yojimbo_assert( m_clientAllocator == NULL ); yojimbo_assert( m_messageFactory == NULL ); + m_clientMemory = (uint8_t*) YOJIMBO_ALLOCATE( *m_allocator, m_config.clientMemory ); m_clientAllocator = m_adapter->CreateAllocator( *m_allocator, m_clientMemory, m_config.clientMemory ); m_messageFactory = m_adapter->CreateMessageFactory( *m_clientAllocator ); m_connection = YOJIMBO_NEW( *m_clientAllocator, Connection, *m_clientAllocator, *m_messageFactory, m_config, m_time ); - yojimbo_assert( m_connection ); - // todo - printf( "m_connection = %p\n", m_connection ); + yojimbo_assert( m_connection ); if ( m_config.networkSimulator ) { m_networkSimulator = YOJIMBO_NEW( *m_clientAllocator, NetworkSimulator, *m_clientAllocator, m_config.maxSimulatorPackets, m_time ); } + reliable_config_t reliable_config; reliable_default_config( &reliable_config ); yojimbo_copy_string( reliable_config.name, "client endpoint", sizeof( reliable_config.name ) ); @@ -174,7 +162,9 @@ namespace yojimbo reliable_config.allocator_context = m_clientAllocator; reliable_config.allocate_function = BaseClient::StaticAllocateFunction; reliable_config.free_function = BaseClient::StaticFreeFunction; + m_endpoint = reliable_endpoint_create( &reliable_config, m_time ); + reliable_endpoint_reset( m_endpoint ); } diff --git a/source/yojimbo_client.cpp b/source/yojimbo_client.cpp index ccf65de3..67223b1d 100644 --- a/source/yojimbo_client.cpp +++ b/source/yojimbo_client.cpp @@ -10,9 +10,6 @@ namespace yojimbo Client::Client( Allocator & allocator, const Address & address, const ClientServerConfig & config, Adapter & adapter, double time ) : BaseClient( allocator, config, adapter, time ), m_config( config ), m_address( address ) { - // todo - printf( "Client::Client\n" ); - m_clientId = 0; m_client = NULL; m_boundAddress = m_address; @@ -20,9 +17,6 @@ namespace yojimbo Client::~Client() { - // todo - printf( "Client::~Client\n" ); - // IMPORTANT: Please disconnect the client before destroying it yojimbo_assert( m_client == NULL ); } @@ -34,9 +28,6 @@ namespace yojimbo void Client::InsecureConnect( const uint8_t privateKey[], uint64_t clientId, const Address serverAddresses[], int numServerAddresses ) { - // todo - printf( "Client::InsecureConnect\n" ); - yojimbo_assert( serverAddresses ); yojimbo_assert( numServerAddresses > 0 ); yojimbo_assert( numServerAddresses <= NETCODE_MAX_SERVERS_PER_CONNECT ); @@ -91,9 +82,6 @@ namespace yojimbo void Client::Connect( uint64_t clientId, uint8_t * connectToken ) { - // todo - printf( "Client::Connect\n" ); - yojimbo_assert( connectToken ); Disconnect(); CreateInternal(); @@ -112,9 +100,6 @@ namespace yojimbo void Client::Disconnect() { - // todo - printf( "Client::Disconnect\n" ); - BaseClient::Disconnect(); DestroyClient(); DestroyInternal(); @@ -161,17 +146,11 @@ namespace yojimbo const int state = netcode_client_state( m_client ); if ( state < NETCODE_CLIENT_STATE_DISCONNECTED ) { - // todo - printf( "error state %d\n", state ); - Disconnect(); SetClientState( CLIENT_STATE_ERROR ); } else if ( state == NETCODE_CLIENT_STATE_DISCONNECTED ) { - // todo - printf( "set disconnected state %d\n", state ); - Disconnect(); SetClientState( CLIENT_STATE_DISCONNECTED ); } diff --git a/source/yojimbo_platform.cpp b/source/yojimbo_platform.cpp index 85551c6a..ba285376 100644 --- a/source/yojimbo_platform.cpp +++ b/source/yojimbo_platform.cpp @@ -40,8 +40,7 @@ static void default_assert_handler( const char * condition, const char * functio #endif } -// todo -static int log_level = 1000; //0; +static int log_level = 0; static int (*printf_function)( const char *, ... ) = printf; diff --git a/test.cpp b/test.cpp index 8772e113..2902279c 100644 --- a/test.cpp +++ b/test.cpp @@ -1370,14 +1370,8 @@ void test_client_server_messages() { // connect and wait until connection completes - // todo - printf( "iteration %d\n", iteration ); - client.InsecureConnect( privateKey, clientId, serverAddress ); - // todo - printf( "A\n" ); - const int NumIterations = 10000; for ( int i = 0; i < NumIterations; ++i ) @@ -1394,9 +1388,6 @@ void test_client_server_messages() break; } - // todo - printf( "B\n" ); - // verify connection has completed successfully check( !client.IsConnecting() ); @@ -1424,11 +1415,7 @@ void test_client_server_messages() PumpClientServerUpdate( time, clients, 1, servers, 1 ); if ( !client.IsConnected() ) - { - // todo - printf( "client is not connected\n" ); break; - } ProcessServerToClientMessages( client, numMessagesReceivedFromServer ); @@ -2487,7 +2474,6 @@ int main() while ( true ) #endif // #if SOAK { - /* { printf( "[serialize]\n\n" ); @@ -2532,10 +2518,8 @@ int main() RUN_TEST( test_connection_reliable_ordered_messages_and_blocks_multiple_channels ); RUN_TEST( test_connection_unreliable_unordered_messages ); RUN_TEST( test_connection_unreliable_unordered_blocks ); - */ RUN_TEST( test_client_server_messages ); - /* RUN_TEST( test_client_server_start_stop_restart ); RUN_TEST( test_client_server_message_failed_to_serialize_reliable_ordered ); RUN_TEST( test_client_server_message_failed_to_serialize_unreliable_unordered ); @@ -2547,7 +2531,6 @@ int main() RUN_TEST( test_single_message_type_unreliable ); RUN_TEST( test_client_server_messages_network_sim_leak ); - */ #if SOAK if ( quit )