mirror of
https://github.com/bdnugget/nbnet.git
synced 2026-11-07 05:27:27 +00:00
* Fix rpc build with other examples
CPP_COMPILE is an option, a cache variable, thus regular unset() is not
working for it since it tries to unset the scope variable.
This causing cmake to skip compilation of rpc.
Unset CPP_COMPILE from CACHE to make it work as intended.
* Fix compiler flags with build targets
Currently tests, soak and examples are build without extra flags,
because add_compile_options is specified after defining build targets.
From add_compile_options() docs:
Adds options to the compiler command line for targets in the current
directory and below that are added *after* this command is invoked.
* Pass debug callbacks as struct
Fixes:
warning: ISO C forbids conversion of function pointer to object pointer
type
* Move global variables under NBNET_IMPL
Nb net could be included multiple times, to prevent creation of global
variables in each file move them under NBNET_IMPL.
Also use designated initializer to initiate a driver structure to
remove warnings about partial initialization. Designated initializer
would initialize other members to zero.
* Resolve unused variables warnings.
Cast unused function arguments to (void) to suspend warnings.
* Resolve signed vs unsigned warnings
Remove unnecessary lt 0 checks and sync types to remove signed vs
unsigned warnings.
* Resolve string related warnings
Fixes:
nbnet/tests/string_tests.c:18:9: error: ‘strncpy’
output truncated before terminating nul copying 5 bytes from a string of
the same length
[-Werror=stringop-truncation]
18 | strncpy(res + pos, b, len_b);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
nbnet/tests/string_tests.c:24:9: error: ‘strncpy’
specified bound depends on the length of the source argument
[-Werror=stringop-truncation]
* Resolve function defined but not used warnings
Mark GameServer_GetClientCount() as inline.
* Attempt to fix warning in ASSERT_VALUE_IN_RANGE
In case when value in unsigned type and min is 0, this commit just
obfuscates types, maybe there's better solution.
Fixes:
warning: comparison of unsigned expression in ‘>= 0’ is always true
[-Wtype-limits]
* Ensure that read len is less than word size
In release mode(-DCMAKE_BUILD_TYPE=Release) tests returning following
error:
nbnet/tests/../nbnet.h:2272:9: error: ‘memcpy’ forming offset [4, 7] is
out of the bound s [0, 4] of object ‘word’ with type ‘Word’ {aka
‘unsigned int’} [-Werror=array-bounds=]
2272 | memcpy(bytes, &word, length); |
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Add check to verify that length is less than Word size.
* add -Wno-unknown-pragmas flag
* fix examples compilation on windows
* fix examples compilation on windows
* fix examples compilation on windows
* fix unit tests compilation on windows
* fix soak test compilation on windows
* fix unit tests compilation on windows
* fix unit tests compilation on windows
---------
Co-authored-by: BIAGINI Nathan <nath.biagini@gmail.com>
156 lines
4.0 KiB
C
156 lines
4.0 KiB
C
/*
|
|
|
|
Copyright (C) 2024 BIAGINI Nathan
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
warranty. In no event will the authors be held liable for any damages
|
|
arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
including commercial applications, and to alter it and redistribute it
|
|
freely, subject to the following restrictions:
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
claim that you wrote the original software. If you use this software
|
|
in a product, an acknowledgment in the product documentation would be
|
|
appreciated but is not required.
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
misrepresented as being the original software.
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
|
|
// Has to be defined in exactly *one* source file before including the nbnet header
|
|
#define NBNET_IMPL
|
|
|
|
#include "shared.h"
|
|
|
|
static bool running = true;
|
|
static bool connected = false;
|
|
static bool disconnected = false;
|
|
|
|
static void TestRPC2(unsigned int param_count, NBN_RPC_Param params[NBN_RPC_MAX_PARAM_COUNT], NBN_ConnectionHandle sender)
|
|
{
|
|
(void) sender;
|
|
TEST_VERIFY(param_count == 2);
|
|
Log(LOG_INFO, "TestRPC2 called !");
|
|
Log(LOG_INFO, "Parameter 1 (float): %f", NBN_RPC_GetFloat(params, 0));
|
|
Log(LOG_INFO, "Parameter 2 (string): %s", NBN_RPC_GetString(params, 1));
|
|
}
|
|
|
|
void OnConnected(void)
|
|
{
|
|
Log(LOG_INFO, "Connected");
|
|
|
|
connected = true;
|
|
|
|
int ret = NBN_GameClient_CallRPC(TEST_RPC_ID, 4242, -1234.5678f, true);
|
|
|
|
TEST_VERIFY(ret == 0);
|
|
}
|
|
|
|
void OnDisconnected(void)
|
|
{
|
|
Log(LOG_INFO, "Disconnected");
|
|
|
|
// Stop the main loop
|
|
disconnected = true;
|
|
running = false;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
(void) argc;
|
|
(void) argv;
|
|
#ifdef __EMSCRIPTEN__
|
|
NBN_WebRTC_Register(); // Register the WebRTC driver
|
|
#else
|
|
NBN_UDP_Register(); // Register the UDP driver
|
|
#endif // __EMSCRIPTEN__
|
|
|
|
// Initialize the client with a protocol name (must be the same than the one used by the server), the server ip address and port
|
|
|
|
if (NBN_GameClient_StartEx(RPC_PROTOCOL_NAME, "127.0.0.1", RPC_EXAMPLE_PORT, NULL, 0) < 0)
|
|
{
|
|
Log(LOG_ERROR, "Failed to start client");
|
|
|
|
// Error, quit the client application
|
|
#ifdef __EMSCRIPTEN__
|
|
emscripten_force_exit(1);
|
|
#else
|
|
return 1;
|
|
#endif
|
|
}
|
|
|
|
int ret = NBN_GameClient_RegisterRPC(TEST_RPC_ID, TEST_RPC_SIGNATURE, NULL);
|
|
|
|
TEST_VERIFY(ret == 0);
|
|
|
|
ret = NBN_GameClient_RegisterRPC(TEST_RPC_2_ID, TEST_RPC_2_SIGNATURE, TestRPC2);
|
|
|
|
TEST_VERIFY(ret == 0);
|
|
|
|
// Number of seconds between client ticks
|
|
double dt = 1.0 / RPC_TICK_RATE;
|
|
|
|
while (running)
|
|
{
|
|
int ev;
|
|
|
|
// Poll for client events
|
|
while ((ev = NBN_GameClient_Poll()) != NBN_NO_EVENT)
|
|
{
|
|
if (ev < 0)
|
|
{
|
|
Log(LOG_ERROR, "An error occured while polling client events. Exit");
|
|
|
|
// Stop main loop
|
|
running = false;
|
|
break;
|
|
}
|
|
|
|
switch (ev)
|
|
{
|
|
// Client is connected to the server
|
|
case NBN_CONNECTED:
|
|
OnConnected();
|
|
break;
|
|
|
|
// Client has disconnected from the server
|
|
case NBN_DISCONNECTED:
|
|
OnDisconnected();
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (disconnected)
|
|
break;
|
|
|
|
// Pack all enqueued messages as packets and send them
|
|
if (NBN_GameClient_SendPackets() < 0)
|
|
{
|
|
Log(LOG_ERROR, "Failed to send packets. Exit");
|
|
|
|
// Stop main loop
|
|
running = false;
|
|
break;
|
|
}
|
|
|
|
// Cap the client tick rate
|
|
ExampleSleep(dt);
|
|
}
|
|
|
|
// Stop the client
|
|
NBN_GameClient_Stop();
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
emscripten_force_exit(0);
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|