Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ set(BENCHMARK_ENABLE_WERROR OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googlebenchmark)

file(GLOB MAFIANET_BENCHMARK_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*_bench.cpp")
add_executable(MafiaNetBenchmarks ${MAFIANET_BENCHMARK_SOURCES})
# The RakPeer benchmarks run two peers on the tests' simulated network. Only
# that one file is compiled in, so benchmarks do not need GoogleTest.
add_executable(MafiaNetBenchmarks ${MAFIANET_BENCHMARK_SOURCES}
${MafiaNet_SOURCE_DIR}/Tests/Support/SimulatedNetwork.cpp)
target_include_directories(MafiaNetBenchmarks PRIVATE ${MafiaNet_SOURCE_DIR}/Tests/Support)
target_link_libraries(MafiaNetBenchmarks PRIVATE MafiaNet::MafiaNetStatic benchmark::benchmark_main)
# Must match the library: some classes carry _DEBUG-only members.
target_compile_definitions(MafiaNetBenchmarks PRIVATE $<$<CONFIG:Debug>:_DEBUG>)
Expand Down
128 changes: 128 additions & 0 deletions Benchmarks/peer_bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2026, MafiaHub
*
* This source code is licensed under the MIT-style license found in the
* license.txt file in the root directory of this source tree.
*
* Two connected RakPeers on the simulated network (Tests/Support): no
* sockets, no threads, a virtual clock. Unlike the bare ReliabilityLayer
* benchmark this covers RakPeer's own per-message work: the buffered send
* command each Send() queues for the update cycle, and the Packet each
* received message becomes and DeallocatePacket() frees.
*
* Only Send(), Receive() and DeallocatePacket() are used on the peers, so the
* file builds unchanged against older trees for A/B comparisons.
*/

#include <benchmark/benchmark.h>

#include <cstdint>
#include <vector>

#include "SimulatedNetwork.h"
#include "mafianet/message_identifiers.h"
#include "mafianet/packet_priority.h"
#include "mafianet/peer.h"

using namespace MafiaNet;

namespace
{
struct ConnectedPair
{
SimulatedNetwork net;
RakPeer *client = nullptr;
RakPeer *server = nullptr;
SystemAddress serverAddress;
bool connected = false;

ConnectedPair()
{
client = net.AddPeer(1);
server = net.AddPeer(1, 1);
serverAddress = net.AddressOf(server);
connected = net.Connect(client, server);
// The benchmark drains the server itself, so the cost measured is
// Receive() + DeallocatePacket(), not the network's inbox copies.
net.SetReceiveEnabled(server, false);
DrainServer(nullptr);
net.Inbox(client).clear();
net.Inbox(server).clear();
}

// Frees everything the server has received; counts user messages.
int64_t DrainServer(int64_t *bytes)
{
int64_t messages = 0;
for (Packet *packet = server->Receive(); packet; packet = server->Receive())
{
if (packet->length > 0 && packet->data[0] == ID_USER_PACKET_ENUM)
{
++messages;
if (bytes)
*bytes += packet->length;
}
server->DeallocatePacket(packet);
}
return messages;
}

// Steps the network 1 ms at a time until the server has received `count`
// user messages, or a simulated-time bound passes. Returns how many arrived.
int64_t DeliverAll(int64_t count, int64_t *bytes)
{
int64_t received = DrainServer(bytes);
for (int ms = 0; received < count && ms < 60000; ++ms)
{
net.Step(1);
received += DrainServer(bytes);
}
net.Inbox(client).clear();
return received;
}
};

void SendReceive(benchmark::State &state, int messageBytes, int messagesPerIteration)
{
ConnectedPair pair;
if (!pair.connected)
{
state.SkipWithError("peers did not connect");
return;
}
std::vector<char> message((size_t) messageBytes, 7);
message[0] = (char) ID_USER_PACKET_ENUM;
int64_t delivered = 0;
int64_t bytes = 0;
for (auto _ : state)
{
for (int i = 0; i < messagesPerIteration; ++i)
pair.client->Send(message.data(), messageBytes, Priority::High, Reliability::ReliableOrdered, 0,
pair.serverAddress, false);
const int64_t got = pair.DeliverAll(messagesPerIteration, &bytes);
if (got != messagesPerIteration)
{
state.SkipWithError("messages not delivered");
break;
}
delivered += got;
}
state.SetItemsProcessed(delivered);
state.SetBytesProcessed(bytes);
}
}

// Many small reliable-ordered messages per iteration: per-Send command and
// per-received-Packet allocation dominate the RakPeer share of the cost.
static void RakPeer_SendReceiveSmall(benchmark::State &state)
{
SendReceive(state, 64, (int) state.range(0));
}
BENCHMARK(RakPeer_SendReceiveSmall)->Arg(64)->Arg(512);

// Split-size messages: splitting and reassembly through the whole peer.
static void RakPeer_SendReceiveLarge(benchmark::State &state)
{
SendReceive(state, (int) state.range(0), 4);
}
BENCHMARK(RakPeer_SendReceiveLarge)->Arg(4 * 1024)->Arg(64 * 1024);
5 changes: 2 additions & 3 deletions Benchmarks/reliability_layer_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <vector>

#include "mafianet/ds_list.h"
#include "mafianet/get_time.h"
#include "mafianet/mtu_size.h"
#include "mafianet/packet_priority.h"
Expand All @@ -33,7 +32,7 @@ struct QueueSocket : public RakNetSocket2
{
std::vector<std::vector<char>> sent;

RNS2SendResult Send(RNS2_SendParameters *sendParameters, const char *, unsigned int) override
RNS2SendResult Send(RNS2_SendParameters *sendParameters) override
{
sent.emplace_back(sendParameters->data, sendParameters->data + sendParameters->length);
return sendParameters->length;
Expand All @@ -45,7 +44,7 @@ struct LayerPair
ReliabilityLayer a, b;
QueueSocket aSock, bSock;
SystemAddress aAddr{"127.0.0.1", 40001}, bAddr{"127.0.0.1", 40002};
DataStructures::List<PluginInterface2 *> handlers;
std::vector<PluginInterface2 *> handlers;
RakNetRandom rnr;
BitStream updateBitStream{MAXIMUM_MTU_SIZE};
CCTimeType now = GetTimeUS();
Expand Down
1 change: 1 addition & 0 deletions DependentExtensions/Lobby2/Lobby2Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <cstdlib>
#include "Lobby2ResultCode.h"
#include "mafianet/string.h"
#include "mafianet/ds_list.h"
#include "mafianet/assert.h"
#include <memory>
#include "mafianet/simple_mutex.h"
Expand Down
8 changes: 4 additions & 4 deletions DependentExtensions/Lobby2/Steam/Lobby2Client_Steam_Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,15 +605,15 @@ void Lobby2Client_Steam_Impl::OnAttach(void)
nextFreeSystemAddress = (uint32_t) rakPeerInterface->GetMyGUID().g;

// If this asserts, call RakPeer::Startup() before attaching Lobby2Client_Steam
DataStructures::List<RakNetSocket2 *> sockets;
std::vector<RakNetSocket2 *> sockets;
rakPeerInterface->GetSockets(sockets);
RakAssert(sockets.Size());
RakAssert(sockets.size());
((RNS2_Windows *) sockets[0])->SetSocketLayerOverride(this);
}
void Lobby2Client_Steam_Impl::OnDetach(void)
{
DataStructures::List<RakNetSocket2 *> sockets;
std::vector<RakNetSocket2 *> sockets;
rakPeerInterface->GetSockets(sockets);
RakAssert(sockets.Size());
RakAssert(sockets.size());
((RNS2_Windows *) sockets[0])->SetSocketLayerOverride(nullptr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ int main(int argc, char **argv)
MafiaNet::SocketDescriptor socketDescriptor(localPort, 0);
rakPeer->Startup(1, &socketDescriptor, 1);
rakPeer->AttachPlugin(&cloudClient);
DataStructures::List<MafiaNet::RakNetSocket2 *> sockets;
std::vector<MafiaNet::RakNetSocket2 *> sockets;
rakPeer->GetSockets(sockets);
printf("Started on port %i\n", sockets[0]->GetBoundAddress().GetPort());

Expand Down
4 changes: 2 additions & 2 deletions Samples/ChatExample/Server/Chat Example Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ _CONSOLE_2_SetSystemProcessParams
server->SetOccasionalPing(true);
server->SetUnreliableTimeout(1000);

DataStructures::List<MafiaNet::RakNetSocket2 *> sockets;
std::vector<MafiaNet::RakNetSocket2 *> sockets;
server->GetSockets(sockets);
printf("Socket addresses used by RakNet:\n");
for (unsigned int i = 0; i < sockets.Size(); i++)
for (unsigned int i = 0; i < sockets.size(); i++)
{
printf("%i. %s\n", i + 1, sockets[i]->GetBoundAddress().ToString(true));
}
Expand Down
2 changes: 1 addition & 1 deletion Samples/ComprehensivePCGame/ComprehensivePCGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ void OpenUPNP(void)
{
printf("Discovering UPNP...\n");

DataStructures::List<RakNetSocket2 *> sockets;
std::vector<RakNetSocket2 *> sockets;
rakPeer->GetSockets(sockets);
UPNPOpenAsynch(sockets[0]->GetBoundAddress().GetPort(), 2000, UPNPProgressCallback, UPNPResultCallback, 0);
}
Expand Down
14 changes: 7 additions & 7 deletions Samples/NATCompleteClient/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ struct SampleFramework

SystemAddress SelectAmongConnectedSystems(MafiaNet::RakPeerInterface *rakPeer, const char *hostName)
{
DataStructures::List<SystemAddress> addresses;
DataStructures::List<RakNetGUID> guids;
std::vector<SystemAddress> addresses;
std::vector<RakNetGUID> guids;
rakPeer->GetSystemList(addresses, guids);
if (addresses.Size() == 0)
if (addresses.size() == 0)
{
return MafiaNet::UNASSIGNED_SYSTEM_ADDRESS;
}
if (addresses.Size() > 1)
if (addresses.size() > 1)
{
printf("Select IP address for %s.\n", hostName);
char buff[64];
for (unsigned int i = 0; i < addresses.Size(); i++)
for (unsigned int i = 0; i < addresses.size(); i++)
{
addresses[i].ToString(true, buff, static_cast<size_t>(64));
printf("%i. %s\n", i + 1, buff);
Expand All @@ -99,7 +99,7 @@ SystemAddress SelectAmongConnectedSystems(MafiaNet::RakPeerInterface *rakPeer, c
return MafiaNet::UNASSIGNED_SYSTEM_ADDRESS;
}
unsigned int idx = atoi(buff);
if (idx <= 0 || idx > addresses.Size())
if (idx <= 0 || idx > addresses.size())
{
return MafiaNet::UNASSIGNED_SYSTEM_ADDRESS;
}
Expand Down Expand Up @@ -228,7 +228,7 @@ struct UPNPFramework : public SampleFramework
*/

// Use same external and internal ports
DataStructures::List<RakNetSocket2 *> sockets;
std::vector<RakNetSocket2 *> sockets;
rakPeer->GetSockets(sockets);
char iport[32];
Itoa(sockets[0]->GetBoundAddress().GetPort(), iport, 10);
Expand Down
18 changes: 9 additions & 9 deletions Samples/NATCompleteServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,18 @@ struct UDPProxyCoordinatorFramework : public SampleFramework
};
SystemAddress SelectAmongConnectedSystems(MafiaNet::RakPeerInterface *rakPeer, const char *hostName)
{
DataStructures::List<SystemAddress> addresses;
DataStructures::List<RakNetGUID> guids;
std::vector<SystemAddress> addresses;
std::vector<RakNetGUID> guids;
rakPeer->GetSystemList(addresses, guids);
if (addresses.Size() == 0)
if (addresses.size() == 0)
{
return MafiaNet::UNASSIGNED_SYSTEM_ADDRESS;
}
if (addresses.Size() > 1)
if (addresses.size() > 1)
{
printf("Select IP address for %s.\n", hostName);
char buff[64];
for (unsigned int i = 0; i < addresses.Size(); i++)
for (unsigned int i = 0; i < addresses.size(); i++)
{
addresses[i].ToString(true, buff, static_cast<size_t>(64));
printf("%i. %s\n", i + 1, buff);
Expand All @@ -273,7 +273,7 @@ SystemAddress SelectAmongConnectedSystems(MafiaNet::RakPeerInterface *rakPeer, c
return MafiaNet::UNASSIGNED_SYSTEM_ADDRESS;
}
unsigned int idx = atoi(buff);
if (idx <= 0 || idx > addresses.Size())
if (idx <= 0 || idx > addresses.size())
{
return MafiaNet::UNASSIGNED_SYSTEM_ADDRESS;
}
Expand Down Expand Up @@ -718,10 +718,10 @@ int main(int argc, char **argv)
printf("No system 0\n");
}

DataStructures::List<SystemAddress> addresses;
DataStructures::List<RakNetGUID> guids;
std::vector<SystemAddress> addresses;
std::vector<RakNetGUID> guids;
rakPeer->GetSystemList(addresses, guids);
printf("%i systems connected\n", addresses.Size());
printf("%zu systems connected\n", addresses.size());
}
}
RakSleep(30);
Expand Down
6 changes: 3 additions & 3 deletions Samples/ReadyEvent/ReadyEventSample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ int main(void)
else
printf("Completed=False\n");

DataStructures::List<SystemAddress> addresses;
DataStructures::List<RakNetGUID> guids;
std::vector<SystemAddress> addresses;
std::vector<RakNetGUID> guids;
rakPeer->GetSystemList(addresses, guids);
for (unsigned short i = 0; i < guids.Size(); i++)
for (unsigned short i = 0; i < guids.size(); i++)
{
ReadyEventSystemStatus ress = readyEventPlugin.GetReadyStatus(0, guids[i]);
printf(" Remote system %i, status = ", i);
Expand Down
2 changes: 1 addition & 1 deletion Samples/TestDLL/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main()
}
f->SetMaximumIncomingConnections(32);

DataStructures::List<RakNetSocket2 *> sockets;
std::vector<RakNetSocket2 *> sockets;
f->GetSockets(sockets);
f->ReleaseSockets(sockets);

Expand Down
6 changes: 3 additions & 3 deletions Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@ set(MAFIANET_HEADERS
include/mafianet/ds_multilist.h
include/mafianet/ds_ordered_list.h
include/mafianet/ds_queue.h
include/mafianet/ds_range_list.h
include/mafianet/range_list.h
include/mafianet/ds_table.h
include/mafianet/ds_threadsafe_allocating_queue.h
include/mafianet/dyn_dns.h
include/mafianet/email_sender.h
include/mafianet/empty_header.h
Expand Down Expand Up @@ -190,6 +189,7 @@ set(MAFIANET_HEADERS
include/mafianet/kbhit.h
include/mafianet/linux_adapter.h
include/mafianet/linux_strings.h
include/mafianet/locked_queue.h
include/mafianet/lockless_types.h
include/mafianet/log_command_parser.h
include/mafianet/message_filter.h
Expand All @@ -207,6 +207,7 @@ set(MAFIANET_HEADERS
include/mafianet/nat_type_detection_server.h
include/mafianet/network_id_manager.h
include/mafianet/network_id_object.h
include/mafianet/object_pool.h
include/mafianet/osx_adapter.h
include/mafianet/packet_console_logger.h
include/mafianet/packet_file_logger.h
Expand Down Expand Up @@ -239,7 +240,6 @@ set(MAFIANET_HEADERS
include/mafianet/signaled_event.h
include/mafianet/simple_mutex.h
include/mafianet/simple_tcp_server.h
include/mafianet/single_producer_consumer.h
include/mafianet/sleep.h
include/mafianet/mafianet.h
include/mafianet/socket2.h
Expand Down
1 change: 1 addition & 0 deletions Source/include/mafianet/bit_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "mafianet/wstring.h"
#include "mafianet/assert.h"
#include <cmath>
#include <cstring>
#include <float.h>
#include <memory>
#include <string>
Expand Down
1 change: 1 addition & 0 deletions Source/include/mafianet/cloud_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "mafianet/types.h"
#include "mafianet/string.h"
#include "mafianet/ds_list.h"

namespace MafiaNet
{
Expand Down
1 change: 1 addition & 0 deletions Source/include/mafianet/ds_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <string.h> // memmove
#include "mafianet/export.h"
#include "mafianet/string.h"
#include "mafianet/ds_list.h"

/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures
/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish.
Expand Down
Loading
Loading