Skip to content

Commit

Permalink
finished splitting out headers
Browse files Browse the repository at this point in the history
  • Loading branch information
gafferongames committed Dec 25, 2023
1 parent e272692 commit f4129e7
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 312 deletions.
90 changes: 0 additions & 90 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1893,94 +1893,6 @@ void test_client_server_message_receive_queue_overflow()
server.Stop();
}

void test_reliable_fragment_overflow_bug()
{
double time = 100.0;

ClientServerConfig config;
config.numChannels = 2;
config.channel[0].type = CHANNEL_TYPE_UNRELIABLE_UNORDERED;
config.channel[0].packetBudget = 8000;
config.channel[1].type = CHANNEL_TYPE_RELIABLE_ORDERED;
config.channel[1].packetBudget = -1;

uint8_t privateKey[KeyBytes];
memset( privateKey, 0, KeyBytes );

Server server( GetDefaultAllocator(), privateKey, Address("127.0.0.1", ServerPort), config, adapter, time );

server.Start( MaxClients );

check( server.IsRunning() );

uint64_t clientId = 0;
yojimbo_random_bytes((uint8_t*)&clientId, 8);

Client client( GetDefaultAllocator(), Address("0.0.0.0"), config, adapter, time );

Address serverAddress( "127.0.0.1", ServerPort );

client.InsecureConnect( privateKey, clientId, serverAddress );

Client * clients[] = { &client };
Server * servers[] = { &server };

while ( true )
{
PumpClientServerUpdate( time, clients, 1, servers, 1 );

if ( client.ConnectionFailed() )
break;

if ( !client.IsConnecting() && client.IsConnected() && server.GetNumConnectedClients() == 1 )
break;
}

check( !client.IsConnecting() );
check( client.IsConnected() );
check( server.GetNumConnectedClients() == 1 );
check( client.GetClientIndex() == 0 );
check( server.IsClientConnected(0) );

PumpClientServerUpdate( time, clients, 1, servers, 1 );

check( !client.IsDisconnected() );

TestBlockMessage * testBlockMessage = (TestBlockMessage *) client.CreateMessage( TEST_BLOCK_MESSAGE );
uint8_t * blockData = client.AllocateBlock(7169);
memset( blockData, 0, 7169 );
client.AttachBlockToMessage( testBlockMessage, blockData, 7169 );
client.SendMessage( 0, testBlockMessage );

testBlockMessage = (TestBlockMessage *) client.CreateMessage( TEST_BLOCK_MESSAGE );
blockData = client.AllocateBlock( 1024 );
memset( blockData, 0, 1024 );
client.AttachBlockToMessage( testBlockMessage, blockData, 1024 );
client.SendMessage( 1, testBlockMessage );

PumpClientServerUpdate( time, clients, 1, servers, 1 );

PumpClientServerUpdate( time, clients, 1, servers, 1 );

PumpClientServerUpdate( time, clients, 1, servers, 1 );

check( !client.IsDisconnected() );

Message * message = server.ReceiveMessage(0, 0);
check( message );
check( message->GetType() == TEST_BLOCK_MESSAGE );
server.ReleaseMessage( 0, message );

message = server.ReceiveMessage(0, 1);
check( message);
check( message->GetType() == TEST_BLOCK_MESSAGE );
server.ReleaseMessage( 0, message );

client.Disconnect();

server.Stop();
}

void test_reliable_outbound_sequence_outdated()
{
const uint64_t clientId = 1;
Expand Down Expand Up @@ -2419,8 +2331,6 @@ int main()
RUN_TEST( test_client_server_message_exhaust_stream_allocator );
RUN_TEST( test_client_server_message_receive_queue_overflow );

RUN_TEST( test_reliable_fragment_overflow_bug );

RUN_TEST( test_single_message_type_reliable );
RUN_TEST( test_single_message_type_reliable_blocks );
RUN_TEST( test_single_message_type_unreliable );
Expand Down
224 changes: 2 additions & 222 deletions yojimbo.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,11 @@
#include "yojimbo_base_server.h"
#include "yojimbo_server.h"
#include "yojimbo_client_interface.h"
#include "yojimbo_base_client.h"
#include "yojimbo_client.h"

/** @file */

// windows =p
#ifdef SendMessage
#undef SendMessage
#endif

struct netcode_address_t;
struct netcode_server_t;
struct netcode_client_t;
struct reliable_endpoint_t;

/// The library namespace.

namespace yojimbo
Expand All @@ -81,216 +73,4 @@ bool InitializeYojimbo();

void ShutdownYojimbo();

// ---------------------------------

namespace yojimbo
{
/**
Functionality that is common across all client implementations.
*/

class BaseClient : public ClientInterface
{
public:

/**
Base client constructor.
@param allocator The allocator for all memory used by the client.
@param config The base client/server configuration.
@param time The current time in seconds. See ClientInterface::AdvanceTime
@param allocator The adapter to the game program. Specifies allocators, message factory to use etc.
*/

explicit BaseClient( Allocator & allocator, const ClientServerConfig & config, Adapter & adapter, double time );

~BaseClient();

void SetContext( void * context ) { yojimbo_assert( IsDisconnected() ); m_context = context; }

void Disconnect();

void AdvanceTime( double time );

bool IsConnecting() const { return m_clientState == CLIENT_STATE_CONNECTING; }

bool IsConnected() const { return m_clientState == CLIENT_STATE_CONNECTED; }

bool IsDisconnected() const { return m_clientState <= CLIENT_STATE_DISCONNECTED; }

bool ConnectionFailed() const { return m_clientState == CLIENT_STATE_ERROR; }

ClientState GetClientState() const { return m_clientState; }

int GetClientIndex() const { return m_clientIndex; }

double GetTime() const { return m_time; }

void SetLatency( float milliseconds );

void SetJitter( float milliseconds );

void SetPacketLoss( float percent );

void SetDuplicates( float percent );

Message * CreateMessage( int type );

uint8_t * AllocateBlock( int bytes );

void AttachBlockToMessage( Message * message, uint8_t * block, int bytes );

void FreeBlock( uint8_t * block );

bool CanSendMessage( int channelIndex ) const;

bool HasMessagesToSend( int channelIndex ) const;

void SendMessage( int channelIndex, Message * message );

Message * ReceiveMessage( int channelIndex );

void ReleaseMessage( Message * message );

void GetNetworkInfo( NetworkInfo & info ) const;

protected:

uint8_t * GetPacketBuffer() { return m_packetBuffer; }

void * GetContext() { return m_context; }

Adapter & GetAdapter() { yojimbo_assert( m_adapter ); return *m_adapter; }

void CreateInternal();

void DestroyInternal();

void SetClientState( ClientState clientState );

Allocator & GetClientAllocator() { yojimbo_assert( m_clientAllocator ); return *m_clientAllocator; }

MessageFactory & GetMessageFactory() { yojimbo_assert( m_messageFactory ); return *m_messageFactory; }

NetworkSimulator * GetNetworkSimulator() { return m_networkSimulator; }

reliable_endpoint_t * GetEndpoint() { return m_endpoint; }

Connection & GetConnection() { yojimbo_assert( m_connection ); return *m_connection; }

virtual void TransmitPacketFunction( uint16_t packetSequence, uint8_t * packetData, int packetBytes ) = 0;

virtual int ProcessPacketFunction( uint16_t packetSequence, uint8_t * packetData, int packetBytes ) = 0;

static void StaticTransmitPacketFunction( void * context, int index, uint16_t packetSequence, uint8_t * packetData, int packetBytes );

static int StaticProcessPacketFunction( void * context, int index, uint16_t packetSequence, uint8_t * packetData, int packetBytes );

static void * StaticAllocateFunction( void * context, uint64_t bytes );

static void StaticFreeFunction( void * context, void * pointer );

private:

ClientServerConfig m_config; ///< The client/server configuration.
Allocator * m_allocator; ///< The allocator passed to the client on creation.
Adapter * m_adapter; ///< The adapter specifies the allocator to use, and the message factory class.
void * m_context; ///< Context lets the user pass information to packet serialize functions.
uint8_t * m_clientMemory; ///< The memory backing the client allocator. Allocated from m_allocator.
Allocator * m_clientAllocator; ///< The client allocator. Everything allocated between connect and disconnected is allocated and freed via this allocator.
reliable_endpoint_t * m_endpoint; ///< reliable endpoint.
MessageFactory * m_messageFactory; ///< The client message factory. Created and destroyed on each connection attempt.
Connection * m_connection; ///< The client connection for exchanging messages with the server.
NetworkSimulator * m_networkSimulator; ///< The network simulator used to simulate packet loss, latency, jitter etc. Optional.
ClientState m_clientState; ///< The current client state. See ClientInterface::GetClientState
int m_clientIndex; ///< The client slot index on the server [0,maxClients-1]. -1 if not connected.
double m_time; ///< The current client time. See ClientInterface::AdvanceTime
uint8_t * m_packetBuffer; ///< Buffer used to read and write packets.

private:

BaseClient( const BaseClient & other );

const BaseClient & operator = ( const BaseClient & other );
};

/**
Implementation of client for dedicated servers.
*/

class Client : public BaseClient
{
public:

/**
The client constructor.
@param allocator The allocator for all memory used by the client.
@param address The address the client should bind to.
@param config The client/server configuration.
@param time The current time in seconds. See ClientInterface::AdvanceTime
*/

explicit Client( Allocator & allocator, const Address & address, const ClientServerConfig & config, Adapter & adapter, double time );

~Client();

void InsecureConnect( const uint8_t privateKey[], uint64_t clientId, const Address & address );

void InsecureConnect( const uint8_t privateKey[], uint64_t clientId, const Address serverAddresses[], int numServerAddresses );

void Connect( uint64_t clientId, uint8_t * connectToken );

void Disconnect();

void SendPackets();

void ReceivePackets();

void AdvanceTime( double time );

int GetClientIndex() const;

uint64_t GetClientId() const { return m_clientId; }

void ConnectLoopback( int clientIndex, uint64_t clientId, int maxClients );

void DisconnectLoopback();

bool IsLoopback() const;

void ProcessLoopbackPacket( const uint8_t * packetData, int packetBytes, uint64_t packetSequence );

const Address & GetAddress() const { return m_boundAddress; }

private:

bool GenerateInsecureConnectToken( uint8_t * connectToken,
const uint8_t privateKey[],
uint64_t clientId,
const Address serverAddresses[],
int numServerAddresses );

void CreateClient( const Address & address );

void DestroyClient();

void StateChangeCallbackFunction( int previous, int current );

static void StaticStateChangeCallbackFunction( void * context, int previous, int current );

void TransmitPacketFunction( uint16_t packetSequence, uint8_t * packetData, int packetBytes );

int ProcessPacketFunction( uint16_t packetSequence, uint8_t * packetData, int packetBytes );

void SendLoopbackPacketCallbackFunction( int clientIndex, const uint8_t * packetData, int packetBytes, uint64_t packetSequence );

static void StaticSendLoopbackPacketCallbackFunction( void * context, int clientIndex, const uint8_t * packetData, int packetBytes, uint64_t packetSequence );

ClientServerConfig m_config; ///< Client/server configuration.
netcode_client_t * m_client; ///< netcode client data.
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)
};
}

#endif // #ifndef YOJIMBO_H
Loading

0 comments on commit f4129e7

Please sign in to comment.