diff --git a/include/yojimbo_client.h b/include/yojimbo_client.h index 99236cc5..fefb317c 100644 --- a/include/yojimbo_client.h +++ b/include/yojimbo_client.h @@ -30,6 +30,7 @@ #include "yojimbo_base_client.h" struct netcode_client_t; +class client_adapter; namespace yojimbo { @@ -49,7 +50,7 @@ namespace yojimbo @param time The current time in seconds. See ClientInterface::AdvanceTime */ - explicit Client( Allocator & allocator, const Address & address, const ClientServerConfig & config, Adapter & adapter, double time ); + explicit Client( Allocator & allocator, const Address & address, const ClientServerConfig & config, Adapter & adapter, double time, client_adapter* ); ~Client(); @@ -82,6 +83,7 @@ namespace yojimbo const Address & GetAddress() const { return m_boundAddress; } netcode_client_t* GetClientDetail() const { return m_client; } + client_adapter* GetParent() const { return m_parent; } private: @@ -112,6 +114,8 @@ namespace yojimbo Address m_address; ///< Original address passed to ctor. Address m_boundAddress; ///< Address after socket bind, eg. with valid port uint64_t m_clientId; ///< The globally unique client id (set on each call to connect) + + client_adapter* m_parent; }; } diff --git a/include/yojimbo_server.h b/include/yojimbo_server.h index bf4c5c51..d6041c52 100644 --- a/include/yojimbo_server.h +++ b/include/yojimbo_server.h @@ -30,6 +30,7 @@ #include "yojimbo_address.h" struct netcode_server_t; +class server_adapter; namespace yojimbo { @@ -41,7 +42,7 @@ namespace yojimbo { public: - Server( Allocator & allocator, const uint8_t privateKey[], const Address & address, const ClientServerConfig & config, Adapter & adapter, double time ); + Server( Allocator & allocator, const uint8_t privateKey[], const Address & address, const ClientServerConfig & config, Adapter & adapter, double time, server_adapter* ); ~Server(); @@ -78,6 +79,7 @@ namespace yojimbo const Address & GetAddress() const { return m_boundAddress; } netcode_server_t* GetServerDetail() const { return m_server; } + server_adapter* GetParent() const { return m_parent; } private: @@ -98,6 +100,8 @@ namespace yojimbo Address m_address; // original address passed to ctor Address m_boundAddress; // address after socket bind, eg. valid port uint8_t m_privateKey[KeyBytes]; + + server_adapter* m_parent; }; } diff --git a/netcode/netcode.c b/netcode/netcode.c index 07e20474..b241dbbc 100755 --- a/netcode/netcode.c +++ b/netcode/netcode.c @@ -3794,6 +3794,8 @@ void netcode_default_server_config( struct netcode_server_config_t * config ) config->send_loopback_packet_callback = NULL; config->override_send_and_receive = 0; config->send_packet_override = NULL; + config->aux_receive_packet = NULL; + config->aux_send_packet = NULL; config->receive_packet_override = NULL; }; @@ -4033,7 +4035,20 @@ void netcode_server_send_global_packet( struct netcode_server_t * server, void * } else if ( to->type == NETCODE_ADDRESS_IPV4 ) { - netcode_socket_send_packet( &server->socket_holder.ipv4, to, packet_data, packet_bytes ); + bool send_real_packet = true; + + if ( server->config.aux_send_packet != NULL ) + { + if ( server->config.aux_send_packet(server->config.callback_context, to, packet_data, packet_bytes ) ) + { + send_real_packet = false; + } + } + + if ( send_real_packet ) + { + netcode_socket_send_packet( &server->socket_holder.ipv4, to, packet_data, packet_bytes ); + } } else if ( to->type == NETCODE_ADDRESS_IPV6 ) { @@ -4084,7 +4099,20 @@ void netcode_server_send_client_packet( struct netcode_server_t * server, void * { if ( server->client_address[client_index].type == NETCODE_ADDRESS_IPV4 ) { - netcode_socket_send_packet( &server->socket_holder.ipv4, &server->client_address[client_index], packet_data, packet_bytes ); + bool send_real_packet = true; + + if ( server->config.aux_send_packet != NULL ) + { + if ( server->config.aux_send_packet(server->config.callback_context, &server->client_address[client_index], packet_data, packet_bytes ) ) + { + send_real_packet = false; + } + } + + if ( send_real_packet ) + { + netcode_socket_send_packet( &server->socket_holder.ipv4, &server->client_address[client_index], packet_data, packet_bytes ); + } } else if ( server->client_address[client_index].type == NETCODE_ADDRESS_IPV6 ) { @@ -4705,6 +4733,9 @@ void netcode_server_receive_packets( struct netcode_server_t * server ) if ( packet_bytes == 0 && server->socket_holder.ipv6.handle != 0) packet_bytes = netcode_socket_receive_packet( &server->socket_holder.ipv6, &from, packet_data, NETCODE_MAX_PACKET_BYTES ); + + if ( packet_bytes == 0 && server->config.aux_receive_packet != NULL ) + packet_bytes = server->config.aux_receive_packet( server->config.callback_context, &from, packet_data, NETCODE_MAX_PACKET_BYTES ); } if ( packet_bytes == 0 ) diff --git a/netcode/netcode.h b/netcode/netcode.h index a5f7dfb7..f9a5f81b 100755 --- a/netcode/netcode.h +++ b/netcode/netcode.h @@ -208,6 +208,9 @@ struct netcode_server_config_t void (*send_packet_override)(void*,struct netcode_address_t*,NETCODE_CONST uint8_t*,int); int (*receive_packet_override)(void*,struct netcode_address_t*,uint8_t*,int); + bool (*aux_send_packet)(void*,struct netcode_address_t*,NETCODE_CONST uint8_t*,int); + int (*aux_receive_packet)(void*,struct netcode_address_t*,uint8_t*,int); + bool (*auxiliary_command_function)(void*,struct netcode_address_t*,uint8_t*,int); void * auxiliary_command_context; }; diff --git a/source/yojimbo_client.cpp b/source/yojimbo_client.cpp index 262a547b..5913c151 100644 --- a/source/yojimbo_client.cpp +++ b/source/yojimbo_client.cpp @@ -7,11 +7,12 @@ namespace yojimbo { - Client::Client( Allocator & allocator, const Address & address, const ClientServerConfig & config, Adapter & adapter, double time ) + Client::Client( Allocator & allocator, const Address & address, const ClientServerConfig & config, Adapter & adapter, double time, client_adapter* parent ) : BaseClient( allocator, config, adapter, time ), m_config( config ), m_address( address ) { m_clientId = 0; m_client = NULL; + m_parent = parent; m_boundAddress = m_address; } diff --git a/source/yojimbo_server.cpp b/source/yojimbo_server.cpp index 634bbc22..5c8652c3 100644 --- a/source/yojimbo_server.cpp +++ b/source/yojimbo_server.cpp @@ -31,7 +31,7 @@ namespace yojimbo { - Server::Server( Allocator & allocator, const uint8_t privateKey[], const Address & address, const ClientServerConfig & config, Adapter & adapter, double time ) + Server::Server( Allocator & allocator, const uint8_t privateKey[], const Address & address, const ClientServerConfig & config, Adapter & adapter, double time, server_adapter* parent ) : BaseServer( allocator, config, adapter, time ) { yojimbo_assert( KeyBytes == NETCODE_KEY_BYTES ); @@ -40,6 +40,7 @@ namespace yojimbo m_boundAddress = address; m_config = config; m_server = NULL; + m_parent = parent; } Server::~Server()