Skip to content

Commit

Permalink
Implement functionality needed for WebRTC
Browse files Browse the repository at this point in the history
  • Loading branch information
geneotech committed Apr 14, 2024
1 parent 7d1e816 commit faeb282
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
6 changes: 5 additions & 1 deletion include/yojimbo_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "yojimbo_base_client.h"

struct netcode_client_t;
class client_adapter;

namespace yojimbo
{
Expand All @@ -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();

Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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;
};
}

Expand Down
6 changes: 5 additions & 1 deletion include/yojimbo_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "yojimbo_address.h"

struct netcode_server_t;
class server_adapter;

namespace yojimbo
{
Expand All @@ -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();

Expand Down Expand Up @@ -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:

Expand All @@ -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;
};
}

Expand Down
35 changes: 33 additions & 2 deletions netcode/netcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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 )
Expand Down
3 changes: 3 additions & 0 deletions netcode/netcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
3 changes: 2 additions & 1 deletion source/yojimbo_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion source/yojimbo_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -40,6 +40,7 @@ namespace yojimbo
m_boundAddress = address;
m_config = config;
m_server = NULL;
m_parent = parent;
}

Server::~Server()
Expand Down

0 comments on commit faeb282

Please sign in to comment.