Skip to content

Commit

Permalink
add more
Browse files Browse the repository at this point in the history
  • Loading branch information
gafferongames committed Dec 25, 2023
1 parent 99c308d commit 2b13eb0
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 104 deletions.
105 changes: 1 addition & 104 deletions yojimbo.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "yojimbo_adaptor.h"
#include "yojimbo_network_info.h"
#include "yojimbo_server_interface.h"
#include "yojimbo_base_server.h"

/** @file */

Expand Down Expand Up @@ -82,110 +83,6 @@ void ShutdownYojimbo();

namespace yojimbo
{
/**
Common functionality across all server implementations.
*/

class BaseServer : public ServerInterface
{
public:

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

~BaseServer();

void SetContext( void * context );

void Start( int maxClients );

void Stop();

void AdvanceTime( double time );

bool IsRunning() const { return m_running; }

int GetMaxClients() const { return m_maxClients; }

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 clientIndex, int type );

uint8_t * AllocateBlock( int clientIndex, int bytes );

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

void FreeBlock( int clientIndex, uint8_t * block );

bool CanSendMessage( int clientIndex, int channelIndex ) const;

bool HasMessagesToSend( int clientIndex, int channelIndex ) const;

void SendMessage( int clientIndex, int channelIndex, Message * message );

Message * ReceiveMessage( int clientIndex, int channelIndex );

void ReleaseMessage( int clientIndex, Message * message );

void GetNetworkInfo( int clientIndex, NetworkInfo & info ) const;

protected:

uint8_t * GetPacketBuffer() { return m_packetBuffer; }

void * GetContext() { return m_context; }

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

Allocator & GetGlobalAllocator() { yojimbo_assert( m_globalAllocator ); return *m_globalAllocator; }

MessageFactory & GetClientMessageFactory( int clientIndex );

NetworkSimulator * GetNetworkSimulator() { return m_networkSimulator; }

reliable_endpoint_t * GetClientEndpoint( int clientIndex );

Connection & GetClientConnection( int clientIndex );

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

virtual int ProcessPacketFunction( int clientIndex, 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; ///< Base client/server config.
Allocator * m_allocator; ///< Allocator passed in to constructor.
Adapter * m_adapter; ///< The adapter specifies the allocator to use, and the message factory class.
void * m_context; ///< Optional serialization context.
int m_maxClients; ///< Maximum number of clients supported.
bool m_running; ///< True if server is currently running, eg. after "Start" is called, before "Stop".
double m_time; ///< Current server time in seconds.
uint8_t * m_globalMemory; ///< The block of memory backing the global allocator. Allocated with m_allocator.
uint8_t * m_clientMemory[MaxClients]; ///< The block of memory backing the per-client allocators. Allocated with m_allocator.
Allocator * m_globalAllocator; ///< The global allocator. Used for allocations that don't belong to a specific client.
Allocator * m_clientAllocator[MaxClients]; ///< Array of per-client allocator. These are used for allocations related to connected clients.
MessageFactory * m_clientMessageFactory[MaxClients]; ///< Array of per-client message factories. This silos message allocations per-client slot.
Connection * m_clientConnection[MaxClients]; ///< Array of per-client connection classes. This is how messages are exchanged with clients.
reliable_endpoint_t * m_clientEndpoint[MaxClients]; ///< Array of per-client reliable endpoints.
NetworkSimulator * m_networkSimulator; ///< The network simulator used to simulate packet loss, latency, jitter etc. Optional.
uint8_t * m_packetBuffer; ///< Buffer used when writing packets.
};

/**
Dedicated server implementation.
*/
Expand Down
139 changes: 139 additions & 0 deletions yojimbo_base_server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
Yojimbo Client/Server Network Library.
Copyright © 2016 - 2024, Mas Bandwidth LLC.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef YOJIMBO_BASE_SERVER_H
#define YOJIMBO_BASE_SERVER_H

#include "yojimbo_config.h"

struct reliable_endpoint_t;

namespace yojimbo
{
/**
Common functionality across all server implementations.
*/

class BaseServer : public ServerInterface
{
public:

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

~BaseServer();

void SetContext( void * context );

void Start( int maxClients );

void Stop();

void AdvanceTime( double time );

bool IsRunning() const { return m_running; }

int GetMaxClients() const { return m_maxClients; }

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 clientIndex, int type );

uint8_t * AllocateBlock( int clientIndex, int bytes );

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

void FreeBlock( int clientIndex, uint8_t * block );

bool CanSendMessage( int clientIndex, int channelIndex ) const;

bool HasMessagesToSend( int clientIndex, int channelIndex ) const;

void SendMessage( int clientIndex, int channelIndex, Message * message );

Message * ReceiveMessage( int clientIndex, int channelIndex );

void ReleaseMessage( int clientIndex, Message * message );

void GetNetworkInfo( int clientIndex, NetworkInfo & info ) const;

protected:

uint8_t * GetPacketBuffer() { return m_packetBuffer; }

void * GetContext() { return m_context; }

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

Allocator & GetGlobalAllocator() { yojimbo_assert( m_globalAllocator ); return *m_globalAllocator; }

MessageFactory & GetClientMessageFactory( int clientIndex );

NetworkSimulator * GetNetworkSimulator() { return m_networkSimulator; }

reliable_endpoint_t * GetClientEndpoint( int clientIndex );

Connection & GetClientConnection( int clientIndex );

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

virtual int ProcessPacketFunction( int clientIndex, 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; ///< Base client/server config.
Allocator * m_allocator; ///< Allocator passed in to constructor.
Adapter * m_adapter; ///< The adapter specifies the allocator to use, and the message factory class.
void * m_context; ///< Optional serialization context.
int m_maxClients; ///< Maximum number of clients supported.
bool m_running; ///< True if server is currently running, eg. after "Start" is called, before "Stop".
double m_time; ///< Current server time in seconds.
uint8_t * m_globalMemory; ///< The block of memory backing the global allocator. Allocated with m_allocator.
uint8_t * m_clientMemory[MaxClients]; ///< The block of memory backing the per-client allocators. Allocated with m_allocator.
Allocator * m_globalAllocator; ///< The global allocator. Used for allocations that don't belong to a specific client.
Allocator * m_clientAllocator[MaxClients]; ///< Array of per-client allocator. These are used for allocations related to connected clients.
MessageFactory * m_clientMessageFactory[MaxClients]; ///< Array of per-client message factories. This silos message allocations per-client slot.
Connection * m_clientConnection[MaxClients]; ///< Array of per-client connection classes. This is how messages are exchanged with clients.
reliable_endpoint_t * m_clientEndpoint[MaxClients]; ///< Array of per-client reliable endpoints.
NetworkSimulator * m_networkSimulator; ///< The network simulator used to simulate packet loss, latency, jitter etc. Optional.
uint8_t * m_packetBuffer; ///< Buffer used when writing packets.
};
}

#endif // #ifndef YOJIMBO_BASE_SERVER_H

0 comments on commit 2b13eb0

Please sign in to comment.