Skip to content

Commit

Permalink
remove channel registering public API
Browse files Browse the repository at this point in the history
  • Loading branch information
nathhB committed Sep 24, 2023
1 parent c6091b5 commit 94531c6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 121 deletions.
127 changes: 18 additions & 109 deletions nbnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ void NBN_MeasureStream_Reset(NBN_MeasureStream *);
#pragma region NBN_Message

#define NBN_MAX_CHANNELS 8
#define NBN_LIBRARY_RESERVED_CHANNELS 3
#define NBN_MAX_MESSAGE_TYPES 255 /* Maximum value of uint8_t, see message header */
#define NBN_MESSAGE_RESEND_DELAY 0.1 /* Number of seconds before a message is resent (reliable messages redundancy) */

Expand Down Expand Up @@ -782,6 +783,8 @@ int NBN_RPC_Message_Serialize(NBN_RPC_Message *, NBN_Stream *);
#define NBN_CHANNEL_RW_CHUNK_BUFFER_INITIAL_SIZE 2048
#define NBN_CHANNEL_OUTGOING_MESSAGE_POOL_SIZE 512

/* IMPORTANT: if you add a library reserved channel below, make sure to update NBN_LIBRARY_RESERVED_CHANNELS */

/* Library reserved unreliable ordered channel */
#define NBN_CHANNEL_RESERVED_UNRELIABLE (NBN_MAX_CHANNELS - 1)

Expand Down Expand Up @@ -1004,6 +1007,7 @@ typedef struct
{
NBN_MessageInfo message_info;
NBN_Connection *connection;
NBN_ConnectionHandle connection_handle;
} data;
} NBN_Event;

Expand Down Expand Up @@ -1219,35 +1223,6 @@ void NBN_GameClient_RegisterMessage(
NBN_MessageDestructor msg_destructor,
NBN_MessageSerializer msg_serializer);

/**
* Create a new custom channel on the game client.
*
* The channel must be created on both the client and the server.
*
* @param id A unique ID between 0 and 25 (26 to 31 are reserved by nbnet, see NBN_MAX_CHANNELS for the maximum number of channels)
* @param builder A pointer to a function that builds the channel
* @param destructor A pointer to a function that destroys the channel (this method is expected to release all memory allocated by the channel builder method)
*/
void NBN_GameClient_RegisterChannel(uint8_t id, NBN_ChannelBuilder builder, NBN_ChannelDestructor destructor);

/**
* Create a new reliable ordered channel on the game client.
*
* The channel must be created on both the client and the server.
*
* @param id A unique ID between 0 and 25 (26 to 31 are reserved by nbnet, see NBN_MAX_CHANNELS for the maximum number of channels)
*/
void NBN_GameClient_RegisterReliableChannel(uint8_t id);

/**
* Create a new unreliable channel on the game client.
*
* The channel must be created on both the client and the server.
*
* @param id A unique ID between 0 and 25 (26 to 31 are reserved by nbnet, see NBN_MAX_CHANNELS for the maximum number of channels)
*/
void NBN_GameClient_RegisterUnreliableChannel(uint8_t id);

/**
* Poll game client events.
*
Expand Down Expand Up @@ -1470,35 +1445,6 @@ void NBN_GameServer_Stop(void);
void NBN_GameServer_RegisterMessage(
uint8_t msg_type, NBN_MessageBuilder msg_builder, NBN_MessageDestructor msg_destructor, NBN_MessageSerializer msg_serializer);

/**
* Create a new custom channel on the game server.
*
* The channel must be created on both the client and the server.
*
* @param id A unique ID between 0 and 25 (26 to 31 are reserved by nbnet, see NBN_MAX_CHANNELS for the maximum number of channels)
* @param builder A pointer to a function that builds the channel
* @param destructor A pointer to a function that destroys the channel (this method is expected to release all memory allocated by the channel builder method)
*/
void NBN_GameServer_RegisterChannel(uint8_t id, NBN_ChannelBuilder builder, NBN_ChannelDestructor destructor);

/**
* Create a new reliable ordered channel on the game server.
*
* The channel must be created on both the client and the server.
*
* @param id A unique ID between 0 and 25 (26 to 31 are reserved by nbnet, see NBN_MAX_CHANNELS for the maximum number of channels)
*/
void NBN_GameServer_RegisterReliableChannel(uint8_t id);

/**
* Create a new unreliable channel on the game server.
*
* The channel must be created on both the client and the server.
*
* @param id A unique ID between 0 and 25 (26 to 31 are reserved by nbnet, see NBN_MAX_CHANNELS for the maximum number of channels)
*/
void NBN_GameServer_RegisterUnreliableChannel(uint8_t id);

/**
* Poll game server events.
*
Expand Down Expand Up @@ -4652,11 +4598,17 @@ static void Endpoint_Init(NBN_Endpoint *endpoint, bool is_server)

NBN_EventQueue_Init(&endpoint->event_queue);

/* Register library reserved channels */
/* Register library reserved reliable channel */
Endpoint_RegisterChannel(endpoint, NBN_CHANNEL_RESERVED_UNRELIABLE, (NBN_ChannelBuilder)NBN_UnreliableOrderedChannel_Create, (NBN_ChannelDestructor)NBN_Channel_Destroy);
Endpoint_RegisterChannel(endpoint, NBN_CHANNEL_RESERVED_RELIABLE, (NBN_ChannelBuilder)NBN_ReliableOrderedChannel_Create, (NBN_ChannelDestructor)NBN_Channel_Destroy);
Endpoint_RegisterChannel(endpoint, NBN_CHANNEL_RESERVED_LIBRARY_MESSAGES, (NBN_ChannelBuilder)NBN_ReliableOrderedChannel_Create, (NBN_ChannelDestructor)NBN_Channel_Destroy);

/* Register general purposes reliable channels */
for (int i = 0; i < NBN_MAX_CHANNELS - NBN_LIBRARY_RESERVED_CHANNELS; i++)
{
Endpoint_RegisterChannel(endpoint, i, (NBN_ChannelBuilder)NBN_ReliableOrderedChannel_Create, (NBN_ChannelDestructor)NBN_Channel_Destroy);
}

/* Register NBN_MessageChunk library message */
Endpoint_RegisterMessageBuilder(
endpoint, (NBN_MessageBuilder)NBN_MessageChunk_Create, NBN_MESSAGE_CHUNK_TYPE);
Expand Down Expand Up @@ -5152,6 +5104,9 @@ int NBN_GameClient_StartEx(const char *protocol_name, const char *host, uint16_t

void NBN_GameClient_Stop(void)
{
// Poll remaining events to clear the event queue
while (NBN_GameClient_Poll() != NBN_NO_EVENT) {}

if (nbn_game_client.server_connection)
{
if (!nbn_game_client.server_connection->is_closed && !nbn_game_client.server_connection->is_stale)
Expand All @@ -5175,10 +5130,7 @@ void NBN_GameClient_Stop(void)

NBN_Connection_Destroy(nbn_game_client.server_connection);
nbn_game_client.server_connection = NULL;
}

// Poll remaining events to clear the event queue
while (NBN_GameClient_Poll() != NBN_NO_EVENT) {}
}

NBN_LogInfo("Stopping all drivers...");

Expand Down Expand Up @@ -5225,27 +5177,6 @@ void NBN_GameClient_RegisterMessage(
Endpoint_RegisterMessageSerializer(&nbn_game_client.endpoint, msg_serializer, msg_type);
}

void NBN_GameClient_RegisterChannel(uint8_t id, NBN_ChannelBuilder builder, NBN_ChannelDestructor destructor)
{
if (id == NBN_CHANNEL_RESERVED_UNRELIABLE || id == NBN_CHANNEL_RESERVED_RELIABLE || id == NBN_CHANNEL_RESERVED_LIBRARY_MESSAGES)
{
NBN_LogError("Channel id %d is reserved by the library", id);
NBN_Abort();
}

Endpoint_RegisterChannel(&nbn_game_client.endpoint, id, builder, destructor);
}

void NBN_GameClient_RegisterReliableChannel(uint8_t id)
{
NBN_GameClient_RegisterChannel(id, (NBN_ChannelBuilder)NBN_ReliableOrderedChannel_Create, (NBN_ChannelDestructor)NBN_Channel_Destroy);
}

void NBN_GameClient_RegisterUnreliableChannel(uint8_t id)
{
NBN_GameClient_RegisterChannel(id, (NBN_ChannelBuilder)NBN_UnreliableOrderedChannel_Create, (NBN_ChannelDestructor)NBN_Channel_Destroy);
}

int NBN_GameClient_Poll(void)
{
Endpoint_UpdateTime(&nbn_game_client.endpoint);
Expand Down Expand Up @@ -5712,8 +5643,6 @@ void NBN_GameServer_Stop(void)
// Poll remaning events to clear the event queue
while (NBN_GameServer_Poll() != NBN_NO_EVENT) {}

Endpoint_Deinit(&nbn_game_server.endpoint);

for (unsigned int i = 0; i < nbn_game_server.clients->count; i++)
{
NBN_Connection_Destroy(nbn_game_server.clients->connections[i]);
Expand Down Expand Up @@ -5744,6 +5673,7 @@ void NBN_GameServer_Stop(void)
}

nbn_game_server.closed_clients_head = NULL;
Endpoint_Deinit(&nbn_game_server.endpoint);

NBN_LogInfo("Stopped");
}
Expand All @@ -5765,27 +5695,6 @@ void NBN_GameServer_RegisterMessage(
Endpoint_RegisterMessageSerializer(&nbn_game_server.endpoint, msg_serializer, msg_type);
}

void NBN_GameServer_RegisterChannel(uint8_t id, NBN_ChannelBuilder builder, NBN_ChannelDestructor destructor)
{
if (id == NBN_CHANNEL_RESERVED_UNRELIABLE || id == NBN_CHANNEL_RESERVED_RELIABLE || id == NBN_CHANNEL_RESERVED_LIBRARY_MESSAGES)
{
NBN_LogError("Channel id %d is reserved by the library", id);
NBN_Abort();
}

Endpoint_RegisterChannel(&nbn_game_server.endpoint, id, builder, destructor);
}

void NBN_GameServer_RegisterReliableChannel(uint8_t id)
{
NBN_GameServer_RegisterChannel(id, (NBN_ChannelBuilder)NBN_ReliableOrderedChannel_Create, (NBN_ChannelDestructor)NBN_Channel_Destroy);
}

void NBN_GameServer_RegisterUnreliableChannel(uint8_t id)
{
NBN_GameServer_RegisterChannel(id, (NBN_ChannelBuilder)NBN_UnreliableOrderedChannel_Create, (NBN_ChannelDestructor)NBN_Channel_Destroy);
}

int NBN_GameServer_Poll(void)
{
Endpoint_UpdateTime(&nbn_game_server.endpoint);
Expand Down Expand Up @@ -6043,7 +5952,7 @@ NBN_ConnectionHandle NBN_GameServer_GetDisconnectedClient(void)
{
assert(nbn_game_server.last_event.type == NBN_CLIENT_DISCONNECTED);

return nbn_game_server.last_event.data.connection->id;
return nbn_game_server.last_event.data.connection_handle;
}

NBN_MessageInfo NBN_GameServer_GetMessageInfo(void)
Expand Down Expand Up @@ -6400,7 +6309,7 @@ static int GameServer_HandleMessageReceivedEvent(void)
sender->is_stale = true;

nbn_game_server.last_event.type = NBN_CLIENT_DISCONNECTED;
nbn_game_server.last_event.data.connection = sender;
nbn_game_server.last_event.data.connection_handle = sender->id;

GameServer_RemoveClosedClientConnections();

Expand Down
21 changes: 9 additions & 12 deletions soak/soak.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ int Soak_Init(int argc, char *argv[])

#ifdef SOAK_CLIENT

for (int i = 0; i < options.channel_count; i++)
{
NBN_GameClient_RegisterReliableChannel(i);
}

NBN_GameClient_RegisterMessage(SOAK_MESSAGE,
(NBN_MessageBuilder)SoakMessage_CreateIncoming,
(NBN_MessageDestructor)SoakMessage_Destroy,
Expand All @@ -83,11 +78,6 @@ int Soak_Init(int argc, char *argv[])

#ifdef SOAK_SERVER

for (int i = 0; i < options.channel_count; i++)
{
NBN_GameServer_RegisterReliableChannel(i);
}

NBN_GameServer_RegisterMessage(SOAK_MESSAGE,
(NBN_MessageBuilder)SoakMessage_CreateIncoming,
(NBN_MessageDestructor)SoakMessage_Destroy,
Expand Down Expand Up @@ -187,6 +177,12 @@ int Soak_ReadCommandLine(int argc, char *argv[])
return -1;
}

if (soak_options.channel_count > NBN_MAX_CHANNELS - NBN_LIBRARY_RESERVED_CHANNELS)
{
Soak_LogError("Channel count cannot exceed %d", NBN_MAX_CHANNELS - NBN_LIBRARY_RESERVED_CHANNELS);
return -1;
}

#ifdef SOAK_CLIENT
if (soak_options.message_count <= 0)
{
Expand Down Expand Up @@ -245,7 +241,8 @@ SoakOptions Soak_GetOptions(void)

void Soak_Debug_PrintAddedToRecvQueue(NBN_Connection *conn, NBN_Message *msg)
{
if (msg->header.type == NBN_MESSAGE_CHUNK_TYPE)
// FIXME
/*if (msg->header.type == NBN_MESSAGE_CHUNK_TYPE)
{
NBN_MessageChunk *chunk = (NBN_MessageChunk *)msg->data;
Expand All @@ -258,7 +255,7 @@ void Soak_Debug_PrintAddedToRecvQueue(NBN_Connection *conn, NBN_Message *msg)
Soak_LogDebug("Soak message added to recv queue (conn id: %d, msg id: %d, soak msg id: %d)",
conn->id, msg->header.id, soak_message->id);
}
}*/
}

unsigned int Soak_GetCreatedOutgoingSoakMessageCount(void)
Expand Down

0 comments on commit 94531c6

Please sign in to comment.