diff --git a/Benchmarks/CMakeLists.txt b/Benchmarks/CMakeLists.txt index 2d4f18e54..a87bcfcb7 100644 --- a/Benchmarks/CMakeLists.txt +++ b/Benchmarks/CMakeLists.txt @@ -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 $<$:_DEBUG>) diff --git a/Benchmarks/peer_bench.cpp b/Benchmarks/peer_bench.cpp new file mode 100644 index 000000000..f10f28770 --- /dev/null +++ b/Benchmarks/peer_bench.cpp @@ -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 + +#include +#include + +#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 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); diff --git a/Benchmarks/reliability_layer_bench.cpp b/Benchmarks/reliability_layer_bench.cpp index 77fefe5cc..c67008c66 100644 --- a/Benchmarks/reliability_layer_bench.cpp +++ b/Benchmarks/reliability_layer_bench.cpp @@ -15,7 +15,6 @@ #include -#include "mafianet/ds_list.h" #include "mafianet/get_time.h" #include "mafianet/mtu_size.h" #include "mafianet/packet_priority.h" @@ -33,7 +32,7 @@ struct QueueSocket : public RakNetSocket2 { std::vector> 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; @@ -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 handlers; + std::vector handlers; RakNetRandom rnr; BitStream updateBitStream{MAXIMUM_MTU_SIZE}; CCTimeType now = GetTimeUS(); diff --git a/DependentExtensions/Lobby2/Lobby2Message.h b/DependentExtensions/Lobby2/Lobby2Message.h index 0040a06b0..19dc3d964 100644 --- a/DependentExtensions/Lobby2/Lobby2Message.h +++ b/DependentExtensions/Lobby2/Lobby2Message.h @@ -20,6 +20,7 @@ #include #include "Lobby2ResultCode.h" #include "mafianet/string.h" +#include "mafianet/ds_list.h" #include "mafianet/assert.h" #include #include "mafianet/simple_mutex.h" diff --git a/DependentExtensions/Lobby2/Steam/Lobby2Client_Steam_Impl.cpp b/DependentExtensions/Lobby2/Steam/Lobby2Client_Steam_Impl.cpp index 891dae097..ccf10d5dc 100644 --- a/DependentExtensions/Lobby2/Steam/Lobby2Client_Steam_Impl.cpp +++ b/DependentExtensions/Lobby2/Steam/Lobby2Client_Steam_Impl.cpp @@ -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 sockets; + std::vector sockets; rakPeerInterface->GetSockets(sockets); - RakAssert(sockets.Size()); + RakAssert(sockets.size()); ((RNS2_Windows *) sockets[0])->SetSocketLayerOverride(this); } void Lobby2Client_Steam_Impl::OnDetach(void) { - DataStructures::List sockets; + std::vector sockets; rakPeerInterface->GetSockets(sockets); - RakAssert(sockets.Size()); + RakAssert(sockets.size()); ((RNS2_Windows *) sockets[0])->SetSocketLayerOverride(nullptr); } diff --git a/Samples/AutopatcherClient_SelfScaling/AutopatcherClientTest.cpp b/Samples/AutopatcherClient_SelfScaling/AutopatcherClientTest.cpp index 44075b97e..75c512a14 100644 --- a/Samples/AutopatcherClient_SelfScaling/AutopatcherClientTest.cpp +++ b/Samples/AutopatcherClient_SelfScaling/AutopatcherClientTest.cpp @@ -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 sockets; + std::vector sockets; rakPeer->GetSockets(sockets); printf("Started on port %i\n", sockets[0]->GetBoundAddress().GetPort()); diff --git a/Samples/ChatExample/Server/Chat Example Server.cpp b/Samples/ChatExample/Server/Chat Example Server.cpp index 454083754..c76e6f7a3 100644 --- a/Samples/ChatExample/Server/Chat Example Server.cpp +++ b/Samples/ChatExample/Server/Chat Example Server.cpp @@ -121,10 +121,10 @@ _CONSOLE_2_SetSystemProcessParams server->SetOccasionalPing(true); server->SetUnreliableTimeout(1000); - DataStructures::List sockets; + std::vector 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)); } diff --git a/Samples/ComprehensivePCGame/ComprehensivePCGame.cpp b/Samples/ComprehensivePCGame/ComprehensivePCGame.cpp index dabc20056..7cb51198a 100644 --- a/Samples/ComprehensivePCGame/ComprehensivePCGame.cpp +++ b/Samples/ComprehensivePCGame/ComprehensivePCGame.cpp @@ -986,7 +986,7 @@ void OpenUPNP(void) { printf("Discovering UPNP...\n"); - DataStructures::List sockets; + std::vector sockets; rakPeer->GetSockets(sockets); UPNPOpenAsynch(sockets[0]->GetBoundAddress().GetPort(), 2000, UPNPProgressCallback, UPNPResultCallback, 0); } diff --git a/Samples/NATCompleteClient/main.cpp b/Samples/NATCompleteClient/main.cpp index 21561430c..3b256e199 100644 --- a/Samples/NATCompleteClient/main.cpp +++ b/Samples/NATCompleteClient/main.cpp @@ -77,18 +77,18 @@ struct SampleFramework SystemAddress SelectAmongConnectedSystems(MafiaNet::RakPeerInterface *rakPeer, const char *hostName) { - DataStructures::List addresses; - DataStructures::List guids; + std::vector addresses; + std::vector 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(64)); printf("%i. %s\n", i + 1, buff); @@ -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; } @@ -228,7 +228,7 @@ struct UPNPFramework : public SampleFramework */ // Use same external and internal ports - DataStructures::List sockets; + std::vector sockets; rakPeer->GetSockets(sockets); char iport[32]; Itoa(sockets[0]->GetBoundAddress().GetPort(), iport, 10); diff --git a/Samples/NATCompleteServer/main.cpp b/Samples/NATCompleteServer/main.cpp index 508123873..ff94eacc1 100644 --- a/Samples/NATCompleteServer/main.cpp +++ b/Samples/NATCompleteServer/main.cpp @@ -251,18 +251,18 @@ struct UDPProxyCoordinatorFramework : public SampleFramework }; SystemAddress SelectAmongConnectedSystems(MafiaNet::RakPeerInterface *rakPeer, const char *hostName) { - DataStructures::List addresses; - DataStructures::List guids; + std::vector addresses; + std::vector 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(64)); printf("%i. %s\n", i + 1, buff); @@ -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; } @@ -718,10 +718,10 @@ int main(int argc, char **argv) printf("No system 0\n"); } - DataStructures::List addresses; - DataStructures::List guids; + std::vector addresses; + std::vector guids; rakPeer->GetSystemList(addresses, guids); - printf("%i systems connected\n", addresses.Size()); + printf("%zu systems connected\n", addresses.size()); } } RakSleep(30); diff --git a/Samples/ReadyEvent/ReadyEventSample.cpp b/Samples/ReadyEvent/ReadyEventSample.cpp index 7b9759628..ec3e1130e 100644 --- a/Samples/ReadyEvent/ReadyEventSample.cpp +++ b/Samples/ReadyEvent/ReadyEventSample.cpp @@ -159,10 +159,10 @@ int main(void) else printf("Completed=False\n"); - DataStructures::List addresses; - DataStructures::List guids; + std::vector addresses; + std::vector 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); diff --git a/Samples/TestDLL/main.cpp b/Samples/TestDLL/main.cpp index f348f3706..d5d69656c 100644 --- a/Samples/TestDLL/main.cpp +++ b/Samples/TestDLL/main.cpp @@ -46,7 +46,7 @@ int main() } f->SetMaximumIncomingConnections(32); - DataStructures::List sockets; + std::vector sockets; f->GetSockets(sockets); f->ReleaseSockets(sockets); diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index c55c04cf8..9fdc0633f 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/Source/include/mafianet/bit_stream.h b/Source/include/mafianet/bit_stream.h index 79a12010f..cbd164176 100644 --- a/Source/include/mafianet/bit_stream.h +++ b/Source/include/mafianet/bit_stream.h @@ -28,6 +28,7 @@ #include "mafianet/wstring.h" #include "mafianet/assert.h" #include +#include #include #include #include diff --git a/Source/include/mafianet/cloud_common.h b/Source/include/mafianet/cloud_common.h index c45e033b1..25b692f39 100644 --- a/Source/include/mafianet/cloud_common.h +++ b/Source/include/mafianet/cloud_common.h @@ -21,6 +21,7 @@ #include "mafianet/types.h" #include "mafianet/string.h" +#include "mafianet/ds_list.h" namespace MafiaNet { diff --git a/Source/include/mafianet/ds_hash.h b/Source/include/mafianet/ds_hash.h index b8e1edcdc..075bee568 100644 --- a/Source/include/mafianet/ds_hash.h +++ b/Source/include/mafianet/ds_hash.h @@ -25,6 +25,7 @@ #include // 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. diff --git a/Source/include/mafianet/ds_range_list.h b/Source/include/mafianet/ds_range_list.h deleted file mode 100644 index 8917108ef..000000000 --- a/Source/include/mafianet/ds_range_list.h +++ /dev/null @@ -1,324 +0,0 @@ -/* - * Original work: Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * RakNet License.txt file in the licenses directory of this source tree. An additional grant - * of patent rights can be found in the RakNet Patents.txt file in the same directory. - * - * - * Modified work: Copyright (c) 2017-2018, SLikeSoft UG (haftungsbeschränkt) - * - * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style - * license found in the license.txt file in the root directory of this source tree. - */ - -/// \file ds_range_list.h -/// \internal -/// \brief A queue implemented as a linked list. -/// - - -#ifndef __RANGE_LIST_H -#define __RANGE_LIST_H - -#include "mafianet/ds_ordered_list.h" -#include "mafianet/bit_stream.h" -#include "mafianet/export.h" -#include "mafianet/assert.h" - -namespace DataStructures -{ -template -struct RangeNode -{ - RangeNode() {} - ~RangeNode() {} - RangeNode(range_type min, range_type max) - { - minIndex = min; - maxIndex = max; - } - range_type minIndex; - range_type maxIndex; -}; - - -template -int RangeNodeComp(const range_type &a, const RangeNode &b) -{ - if (a < b.minIndex) - { - return -1; - } - if (a > b.maxIndex) - { - return 1; - } - return 0; -} - -template -class RAK_DLL_EXPORT RangeList -{ -public: - RangeList(); - ~RangeList(); - void Insert(range_type index); - void Clear(void); - bool IsWithinRange(range_type value) const; - unsigned Size(void) const; - unsigned RangeSum(void) const; - MafiaNet::BitSize_t Serialize(MafiaNet::BitStream *in, MafiaNet::BitSize_t maxBits, bool clearSerialized); - bool Deserialize(MafiaNet::BitStream *out); - - DataStructures::OrderedList, RangeNodeComp> ranges; - - // internal helpers -private: - static bool DeserializeSingleRange(MafiaNet::BitStream *out, range_type &min, range_type &max); -}; - -template -MafiaNet::BitSize_t RangeList::Serialize(MafiaNet::BitStream *in, MafiaNet::BitSize_t maxBits, bool clearSerialized) -{ - RakAssert(ranges.Size() < (unsigned short) -1); - MafiaNet::BitStream tempBS; - MafiaNet::BitSize_t bitsWritten; - unsigned short countWritten; - unsigned i; - countWritten = 0; - bitsWritten = 0; - for (i = 0; i < ranges.Size(); i++) - { - // #med - review this calculation --- shouldn't this be +8 rather than +1 due to minEqualsMax being 1 byte? - if ((int) sizeof(unsigned short) * 8 + bitsWritten + (int) sizeof(range_type) * 8 * 2 + 1 > maxBits) - { - break; - } - unsigned char minEqualsMax; - if (ranges[i].minIndex == ranges[i].maxIndex) - { - minEqualsMax = 1; - } - else - { - minEqualsMax = 0; - } - tempBS.Write(minEqualsMax); // Use one byte, instead of one bit, for speed, as this is done a lot - tempBS.Write(ranges[i].minIndex); - bitsWritten += sizeof(range_type) * 8 + 8; - if (ranges[i].minIndex != ranges[i].maxIndex) - { - tempBS.Write(ranges[i].maxIndex); - bitsWritten += sizeof(range_type) * 8; - } - countWritten++; - } - - in->AlignWriteToByteBoundary(); - MafiaNet::BitSize_t before = in->GetWriteOffset(); - in->Write(countWritten); - bitsWritten += in->GetWriteOffset() - before; - // RAKNET_DEBUG_PRINTF("%i ", in->GetNumberOfBitsUsed()); - in->Write(&tempBS, tempBS.GetNumberOfBitsUsed()); - // RAKNET_DEBUG_PRINTF("%i %i \n", tempBS.GetNumberOfBitsUsed(),in->GetNumberOfBitsUsed()); - - if (clearSerialized && countWritten) - { - unsigned rangeSize = ranges.Size(); - for (i = 0; i < rangeSize - countWritten; i++) - { - ranges[i] = ranges[i + countWritten]; - } - ranges.RemoveFromEnd(countWritten); - } - - return bitsWritten; -} - -template -bool RangeList::Deserialize(MafiaNet::BitStream *out) -{ - ranges.Clear(true, _FILE_AND_LINE_); - unsigned short count; - out->AlignReadToByteBoundary(); - if (!out->Read(count)) - { - return false; - } - range_type absMin; - range_type min, max; - - if (count == 0) - { - return true; - } - - if (!DeserializeSingleRange(out, min, max)) - { - return false; - } - ranges.InsertAtEnd(RangeNode(min, max), _FILE_AND_LINE_); - - for (unsigned short i = 1; i < count; i++) - { - absMin = max; - - if (!DeserializeSingleRange(out, min, max)) - { - return false; - } - if (min <= absMin) - { - return false; - } - ranges.InsertAtEnd(RangeNode(min, max), _FILE_AND_LINE_); - } - return true; -} - -template -bool RangeList::DeserializeSingleRange(MafiaNet::BitStream *out, range_type &min, range_type &max) -{ - unsigned char maxEqualToMin; - - if (!out->Read(maxEqualToMin)) - { - return false; - } - if (!out->Read(min)) - { - return false; - } - if (maxEqualToMin == 0) - { - if (!out->Read(max)) - { - return false; - } - if (max <= min) - { - return false; - } - } - else - { - max = min; - } - - return true; -} - -template -RangeList::RangeList() -{ - RangeNodeComp(0, RangeNode()); -} - -template -RangeList::~RangeList() -{ - Clear(); -} - -template -void RangeList::Insert(range_type index) -{ - if (ranges.Size() == 0) - { - ranges.Insert(index, RangeNode(index, index), true, _FILE_AND_LINE_); - return; - } - - bool objectExists; - unsigned insertionIndex = ranges.GetIndexFromKey(index, &objectExists); - if (objectExists) - { - return; // index already covered by a range entry - do not create a duplicated entry - } - - // index > maxIndex on entire range list - if (insertionIndex == ranges.Size()) - { - if (index == ranges[insertionIndex - 1].maxIndex + (range_type) 1) - { - ranges[insertionIndex - 1].maxIndex++; - } - else if (index > ranges[insertionIndex - 1].maxIndex + (range_type) 1) - { - // Insert at end - ranges.Insert(index, RangeNode(index, index), true, _FILE_AND_LINE_); - } - return; - } - - // verify it's really not within the current range (otherwise objectExists should have been true) - RakAssert(index < ranges[insertionIndex].minIndex || index > ranges[insertionIndex].maxIndex); - - if (index < ranges[insertionIndex].minIndex - (range_type) 1) - { - // Insert here - ranges.InsertAtIndex(RangeNode(index, index), insertionIndex, _FILE_AND_LINE_); - return; - } - - if (index == ranges[insertionIndex].minIndex - (range_type) 1) - { - // Decrease minIndex and join left - ranges[insertionIndex].minIndex--; - if (insertionIndex > 0 && ranges[insertionIndex - 1].maxIndex + (range_type) 1 == ranges[insertionIndex].minIndex) - { - ranges[insertionIndex - 1].maxIndex = ranges[insertionIndex].maxIndex; - ranges.RemoveAtIndex(insertionIndex); - } - return; - } - - if (index == ranges[insertionIndex].maxIndex + (range_type) 1) - { - // Increase maxIndex and join right - ranges[insertionIndex].maxIndex++; - if (insertionIndex < ranges.Size() - 1 && ranges[insertionIndex + (range_type) 1].minIndex == ranges[insertionIndex].maxIndex + (range_type) 1) - { - ranges[insertionIndex + 1].minIndex = ranges[insertionIndex].minIndex; - ranges.RemoveAtIndex(insertionIndex); - } - } -} - -template -void RangeList::Clear(void) -{ - ranges.Clear(true, _FILE_AND_LINE_); -} - -template -bool RangeList::IsWithinRange(range_type value) const -{ - bool objectExists; - // not interested in the return value - (void) ranges.GetIndexFromKey(value, &objectExists); - return objectExists; -} - -template -unsigned RangeList::Size(void) const -{ - return ranges.Size(); -} - -template -unsigned RangeList::RangeSum(void) const -{ - unsigned sum = 0, i; - for (i = 0; i < ranges.Size(); i++) - { - sum += ranges[i].maxIndex - ranges[i].minIndex + 1; - } - return sum; -} - -} - -#endif diff --git a/Source/include/mafianet/ds_threadsafe_allocating_queue.h b/Source/include/mafianet/ds_threadsafe_allocating_queue.h deleted file mode 100644 index 31e722a1b..000000000 --- a/Source/include/mafianet/ds_threadsafe_allocating_queue.h +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Original work: Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * RakNet License.txt file in the licenses directory of this source tree. An additional grant - * of patent rights can be found in the RakNet Patents.txt file in the same directory. - * - * - * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt) - * - * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style - * license found in the license.txt file in the root directory of this source tree. - */ - -/// \file ds_threadsafe_allocating_queue.h -/// \internal -/// A threadsafe queue, that also uses a memory pool for allocation - -#ifndef __THREADSAFE_ALLOCATING_QUEUE -#define __THREADSAFE_ALLOCATING_QUEUE - -#include -#include "mafianet/ds_queue.h" -#include "mafianet/simple_mutex.h" -#include "mafianet/ds_memory_pool.h" - -// #if defined(new) -// #pragma push_macro("new") -// #undef new -// #define RMO_NEW_UNDEF_ALLOCATING_QUEUE -// #endif - -namespace DataStructures -{ - -template -class RAK_DLL_EXPORT ThreadsafeAllocatingQueue -{ -public: - // Queue operations - void Push(structureType *s); - structureType *PopInaccurate(void); - structureType *Pop(void); - void SetPageSize(int size); - bool IsEmpty(void); - structureType *operator[](unsigned int position); - void RemoveAtIndex(unsigned int position); - unsigned int Size(void); - - // Memory pool operations - structureType *Allocate(const char *file, unsigned int line); - void Deallocate(structureType *s, const char *file, unsigned int line); - void Clear(const char *file, unsigned int line); - -protected: - mutable MemoryPool memoryPool; - MafiaNet::SimpleMutex memoryPoolMutex; - Queue queue; - MafiaNet::SimpleMutex queueMutex; -}; - -template -void ThreadsafeAllocatingQueue::Push(structureType *s) -{ - queueMutex.Lock(); - queue.Push(s, _FILE_AND_LINE_); - queueMutex.Unlock(); -} - -template -structureType *ThreadsafeAllocatingQueue::PopInaccurate(void) -{ - structureType *s; - if (queue.IsEmpty()) - return 0; - queueMutex.Lock(); - if (queue.IsEmpty() == false) - s = queue.Pop(); - else - s = 0; - queueMutex.Unlock(); - return s; -} - -template -structureType *ThreadsafeAllocatingQueue::Pop(void) -{ - structureType *s; - queueMutex.Lock(); - if (queue.IsEmpty()) - { - queueMutex.Unlock(); - return 0; - } - s = queue.Pop(); - queueMutex.Unlock(); - return s; -} - -template -structureType *ThreadsafeAllocatingQueue::Allocate(const char *file, unsigned int line) -{ - structureType *s; - memoryPoolMutex.Lock(); - s = memoryPool.Allocate(file, line); - memoryPoolMutex.Unlock(); - // Call new operator, memoryPool doesn't do this - s = new ((void *) s) structureType; - return s; -} -template -void ThreadsafeAllocatingQueue::Deallocate(structureType *s, const char *file, unsigned int line) -{ - // Call delete operator, memory pool doesn't do this - s->~structureType(); - memoryPoolMutex.Lock(); - memoryPool.Release(s, file, line); - memoryPoolMutex.Unlock(); -} - -template -void ThreadsafeAllocatingQueue::Clear(const char *file, unsigned int line) -{ - memoryPoolMutex.Lock(); - for (unsigned int i = 0; i < queue.Size(); i++) - { - queue[i]->~structureType(); - memoryPool.Release(queue[i], file, line); - } - queue.Clear(file, line); - memoryPoolMutex.Unlock(); - memoryPoolMutex.Lock(); - memoryPool.Clear(file, line); - memoryPoolMutex.Unlock(); -} - -template -void ThreadsafeAllocatingQueue::SetPageSize(int size) -{ - memoryPool.SetPageSize(size); -} - -template -bool ThreadsafeAllocatingQueue::IsEmpty(void) -{ - bool isEmpty; - queueMutex.Lock(); - isEmpty = queue.IsEmpty(); - queueMutex.Unlock(); - return isEmpty; -} - -template -structureType *ThreadsafeAllocatingQueue::operator[](unsigned int position) -{ - structureType *s; - queueMutex.Lock(); - s = queue[position]; - queueMutex.Unlock(); - return s; -} - -template -void ThreadsafeAllocatingQueue::RemoveAtIndex(unsigned int position) -{ - queueMutex.Lock(); - queue.RemoveAtIndex(position); - queueMutex.Unlock(); -} - -template -unsigned int ThreadsafeAllocatingQueue::Size(void) -{ - unsigned int s; - queueMutex.Lock(); - s = queue.Size(); - queueMutex.Unlock(); - return s; -} - -} - - -// #if defined(RMO_NEW_UNDEF_ALLOCATING_QUEUE) -// #pragma pop_macro("new") -// #undef RMO_NEW_UNDEF_ALLOCATING_QUEUE -// #endif - -#endif diff --git a/Source/include/mafianet/locked_queue.h b/Source/include/mafianet/locked_queue.h new file mode 100644 index 000000000..3d4bcb558 --- /dev/null +++ b/Source/include/mafianet/locked_queue.h @@ -0,0 +1,91 @@ +/* + * 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. + */ + +/// \file +/// \brief A FIFO queue guarded by one mutex, for handing items between threads. +/// + +#ifndef MAFIANET_LOCKED_QUEUE_H +#define MAFIANET_LOCKED_QUEUE_H + +#include +#include +#include +#include +#include + +namespace MafiaNet +{ + +/// A FIFO queue whose every operation takes one mutex, so any number of producer and +/// consumer threads may share it. There is deliberately no unlocked peek: a stale emptiness +/// check would be a data race. +/// +/// Items are owned by the queue until popped. To pass heap objects between threads use +/// T = std::unique_ptr: producers Push(std::make_unique(...)), consumers +/// `while (auto item = queue.TryPop()) { ... }`, and whatever is still queued when the queue +/// is cleared or destroyed is freed by its unique_ptr. +template +class LockedQueue +{ +public: + LockedQueue() = default; + LockedQueue(const LockedQueue &) = delete; + LockedQueue &operator=(const LockedQueue &) = delete; + + /// Appends \a item at the back. + void Push(T item) + { + std::lock_guard lock(mutex); + items.push_back(std::move(item)); + } + + /// Removes and returns the front item, or std::nullopt when the queue is empty. + std::optional TryPop() + { + std::lock_guard lock(mutex); + if (items.empty()) + return std::nullopt; + std::optional front(std::move(items.front())); + items.pop_front(); + return front; + } + + /// Whether the queue was empty at the moment of the call. Takes the lock. + bool IsEmpty() const + { + std::lock_guard lock(mutex); + return items.empty(); + } + + /// The number of queued items at the moment of the call. Takes the lock. + std::size_t Size() const + { + std::lock_guard lock(mutex); + return items.size(); + } + + /// Destroys every queued item. + void Clear() + { + // Destroy the items outside the lock: an item's destructor must not be able to deadlock + // on (or be slowed by) this queue. + std::deque discarded; + { + std::lock_guard lock(mutex); + discarded.swap(items); + } + } + +private: + mutable std::mutex mutex; + std::deque items; +}; + +} // namespace MafiaNet + +#endif diff --git a/Source/include/mafianet/mmsg_batch.h b/Source/include/mafianet/mmsg_batch.h index db133ccc9..71c9f0f2c 100644 --- a/Source/include/mafianet/mmsg_batch.h +++ b/Source/include/mafianet/mmsg_batch.h @@ -285,7 +285,7 @@ class RNS2SendBatch // SendBatch returns a datagram count, or a negative error when nothing at // all went out. Either shortfall means datagrams were dropped; the scalar // path reports its sendto failures the same way, so don't lose the signal. - const RNS2SendResult sent = socket->SendBatch(sends, count, _FILE_AND_LINE_); + const RNS2SendResult sent = socket->SendBatch(sends, count); // A return above `count` means an override handed back a byte total // instead of a datagram count -- the one contract the RNS2_Linux sendmmsg // override cannot be unit-tested against, so check it at runtime here. diff --git a/Source/include/mafianet/nat_type_detection_client.h b/Source/include/mafianet/nat_type_detection_client.h index cfe483151..aef20f7e3 100644 --- a/Source/include/mafianet/nat_type_detection_client.h +++ b/Source/include/mafianet/nat_type_detection_client.h @@ -30,6 +30,8 @@ #include "mafianet/packet_priority.h" #include "mafianet/socket_includes.h" #include "mafianet/ds_ordered_list.h" +#include "mafianet/ds_queue.h" +#include "mafianet/simple_mutex.h" #include "mafianet/string.h" #include "mafianet/nat_type_detection_common.h" @@ -76,8 +78,8 @@ class RAK_DLL_EXPORT NatTypeDetectionClient : public PluginInterface2, public RN virtual void OnDetach(void); virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct); - virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line); - virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line); + virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s); + virtual RNS2RecvStruct *AllocRNS2RecvStruct(); protected: DataStructures::Queue bufferedPackets; diff --git a/Source/include/mafianet/nat_type_detection_server.h b/Source/include/mafianet/nat_type_detection_server.h index d2aef6c21..f72121236 100644 --- a/Source/include/mafianet/nat_type_detection_server.h +++ b/Source/include/mafianet/nat_type_detection_server.h @@ -29,6 +29,8 @@ #include "mafianet/packet_priority.h" #include "mafianet/socket_includes.h" #include "mafianet/ds_ordered_list.h" +#include "mafianet/ds_queue.h" +#include "mafianet/simple_mutex.h" #include "mafianet/string.h" #include "mafianet/nat_type_detection_common.h" @@ -113,8 +115,8 @@ class RAK_DLL_EXPORT NatTypeDetectionServer : public PluginInterface2, public RN }; virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct); - virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line); - virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line); + virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s); + virtual RNS2RecvStruct *AllocRNS2RecvStruct(); protected: DataStructures::Queue bufferedPackets; diff --git a/Source/include/mafianet/object_pool.h b/Source/include/mafianet/object_pool.h new file mode 100644 index 000000000..bb1107eaa --- /dev/null +++ b/Source/include/mafianet/object_pool.h @@ -0,0 +1,74 @@ +/* + * 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. + */ + +/// \file object_pool.h +/// \brief A free list of reusable heap objects for allocation-heavy hot paths. +/// +/// Only for objects whose allocation rate showed up in the benchmarks (the +/// reliability layer's per-message bookkeeping). Elsewhere use plain new/delete +/// or std::unique_ptr. + +#pragma once + +#include // std::size_t +#include // std::unique_ptr +#include // std::move +#include // std::vector + +namespace MafiaNet +{ + +/// \brief Recycles heap objects of type T instead of freeing them. +/// +/// Acquire() hands out a raw pointer: the objects this pool serves live in +/// intrusive lists, heaps and fixed arrays whose owner changes as a message +/// moves through the layer, so a smart pointer would be released at almost +/// every step. The caller owns an acquired object until it passes it back to +/// Release() exactly once (or deletes it; the pool never tracks objects it has +/// handed out). The pool owns only the released objects, and frees them in +/// Clear() and its destructor. +/// +/// Objects are not reconstructed on reuse: Acquire() returns either a +/// default-initialized new T or a released object as it was left. Callers must +/// set every field they read. Not thread-safe. +template +class ObjectPool +{ +public: + ObjectPool() = default; + ObjectPool(const ObjectPool &) = delete; + ObjectPool &operator=(const ObjectPool &) = delete; + + /// A released object if there is one, otherwise a newly allocated one. + T *Acquire() + { + if (freeList.empty()) + return new T; + T *object = freeList.back().release(); + freeList.pop_back(); + return object; + } + + /// Takes back an object Acquire() returned, for reuse. Null is ignored. + void Release(T *object) + { + std::unique_ptr owned(object); + if (owned) + freeList.push_back(std::move(owned)); + } + + /// Frees every released object. Objects still acquired are unaffected. + void Clear() { freeList.clear(); } + + /// The number of released objects waiting for reuse. + std::size_t FreeCount() const { return freeList.size(); } + +private: + std::vector> freeList; +}; + +} // namespace MafiaNet diff --git a/Source/include/mafianet/peer.h b/Source/include/mafianet/peer.h index f740a68c6..5b1f57f52 100644 --- a/Source/include/mafianet/peer.h +++ b/Source/include/mafianet/peer.h @@ -27,23 +27,24 @@ #include "mafianet/reliability_layer.h" #include "mafianet/peer_interface.h" #include "mafianet/bit_stream.h" -#include "mafianet/single_producer_consumer.h" +#include "mafianet/locked_queue.h" #include "mafianet/simple_mutex.h" -#include "mafianet/ds_ordered_list.h" #include "mafianet/export.h" #include "mafianet/string.h" #include "mafianet/thread.h" //#include "socket.h" -#include "mafianet/ds_threadsafe_allocating_queue.h" #include "mafianet/signaled_event.h" #include "mafianet/native_feature_includes.h" #include "mafianet/handshake_challenge.h" #include "mafianet/session_crypto.h" #include "mafianet/lockless_types.h" -#include "mafianet/ds_queue.h" #include #include +#include +#include +#include #include +#include namespace MafiaNet { @@ -369,7 +370,7 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler /// Indices match each other, so \a addresses[0] and \a guids[0] refer to the same system /// \param[out] addresses All system addresses. Size of the list is the number of connections. Size of the \a addresses list will match the size of the \a guids list. /// \param[out] guids All guids. Size of the list is the number of connections. Size of the list will match the size of the \a addresses list. - void GetSystemList(DataStructures::List &addresses, DataStructures::List &guids) const; + void GetSystemList(std::vector &addresses, std::vector &guids) const; /// \brief Bans an IP from connecting. /// \details Banned IPs persist between connections but are not saved on shutdown nor loaded on startup. @@ -657,8 +658,8 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler /// \brief Gets all sockets in use. /// \note This sends a query to the thread and blocks on the return value for up to one second. In practice it should only take a millisecond or so. /// \param[out] sockets List of RakNetSocket structures in use. - virtual void GetSockets(DataStructures::List &sockets); - virtual void ReleaseSockets(DataStructures::List &sockets); + virtual void GetSockets(std::vector &sockets); + virtual void ReleaseSockets(std::vector &sockets); /// \internal virtual void WriteOutOfBandHeader(MafiaNet::BitStream *bitStream); @@ -729,7 +730,7 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler /// \param[out] addresses SystemAddress for each connected system /// \param[out] guids RakNetGUID for each connected system /// \param[out] statistics Calculated RakNetStatistics for each connected system - virtual void GetStatisticsList(DataStructures::List &addresses, DataStructures::List &guids, DataStructures::List &statistics); + virtual void GetStatisticsList(std::vector &addresses, std::vector &guids, std::vector &statistics); /// \Returns how many messages are waiting when you call Receive() virtual unsigned int GetReceiveBufferSize(void); @@ -752,7 +753,7 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler /// \internal bool SendOutOfBand(const char *host, unsigned short remotePort, const char *data, BitSize_t dataLength, unsigned connectionSocketIndex = 0); - // static Packet *AllocPacket(unsigned dataSize, const char *file, unsigned int line); + // static Packet *AllocPacket(unsigned dataSize); /// \internal /// \brief Holds the clock differences between systems, along with the ping @@ -862,12 +863,16 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler // after the connection packet; freed, never delivered, by ClearSessionConfig() when the connection // ends before that. Bounded by MAXIMUM_HELD_SESSION_MESSAGES and MAXIMUM_HELD_SESSION_BYTES. // Touched only by the network thread (and by Shutdown() once it has stopped). + struct HeldBytesFree + { + void operator()(unsigned char *bytes) const { std::free(bytes); } + }; struct HeldApplicationMessage { - unsigned char *data; // owned; allocated by the reliability layer + std::unique_ptr data; // std::malloc'd by the reliability layer BitSize_t bitSize; }; - DataStructures::Queue heldApplicationMessages; + std::deque heldApplicationMessages; unsigned int heldApplicationBytes; // EXCHANGING_SESSION_DATA sits between HANDLING_CONNECTION_REQUEST and CONNECTED: the reliability @@ -886,7 +891,10 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler UNVERIFIED_SENDER, EXCHANGING_SESSION_DATA, CONNECTED - } connectMode; + }; + // Written only by the network thread, but read lock-free from user threads (Send()'s + // session-handshake gate, GetConnectionState, GetSystemList...), hence atomic. + std::atomic connectMode; }; // DS_APR @@ -1006,8 +1014,9 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler RemoteSystemStruct *GetRemoteSystem(const SystemAddress &sa) const; unsigned int GetRemoteSystemIndex(const SystemAddress &sa) const; void ClearRemoteSystemLookup(void); - DataStructures::MemoryPool remoteSystemIndexPool; - // Entries taken from remoteSystemIndexPool and not yet released. GetLiveRemoteSystemIndexEntriesForTesting(). + // Bucket count of remoteSystemLookup; each bucket is a singly linked chain of new'd RemoteSystemIndex. + unsigned int remoteSystemLookupSize; + // RemoteSystemIndex entries allocated and not yet freed. GetLiveRemoteSystemIndexEntriesForTesting(). unsigned int liveRemoteSystemIndexEntries; void AddToActiveSystemList(unsigned int remoteSystemListIndex); @@ -1031,7 +1040,6 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler bool updateCycleIsRunning; ///The list of people we have tried to connect to recently - //DataStructures::Queue requestedConnectionsList; ///Data that both the client and the server needs unsigned int bytesSentPerSecond, bytesReceivedPerSecond; @@ -1039,7 +1047,6 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler // bool continualPing,isRecvfromThreadActive,isMainLoopThreadActive, endThreads, isSocketLayerBlocking; unsigned int validationInteger; SimpleMutex incomingQueueMutex, banListMutex; //,synchronizedMemoryQueueMutex, automaticVariableSynchronizationMutex; - //DataStructures::Queue incomingpacketSingleProducerConsumer; //, synchronizedMemorypacketSingleProducerConsumer; // BitStream enumerationData; struct BanStruct @@ -1096,13 +1103,12 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler uint8_t pinnedServerIdentity[32] = {}; }; - //DataStructures::List* > automaticVariableSynchronizationList; - DataStructures::List banList; + std::vector banList; // Threadsafe, and not thread safe - DataStructures::List pluginListTS, pluginListNTS; + std::vector pluginListTS, pluginListNTS; // Every attached plugin again, for OnReceiveNetworkThread. The mutex lets AttachPlugin and // DetachPlugin run while the update thread is dispatching. - DataStructures::List pluginListNetworkThread; + std::vector pluginListNetworkThread; SimpleMutex pluginListNetworkThreadMutex; // The thread inside DispatchNetworkThreadReceive. AttachPlugin/DetachPlugin called from a hook // on it are ignored: they would deadlock on the lock that thread holds, and they also change @@ -1114,7 +1120,7 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler bool IsInsideNetworkThreadReceive(void) const; void SetNetworkThreadReceiveStopped(bool stopped); - DataStructures::Queue requestedConnectionQueue; + std::deque requestedConnectionQueue; SimpleMutex requestedConnectionQueueMutex; // The next Happy Eyeballs raceGroup to hand out (never 0). Guarded by requestedConnectionQueueMutex. uint32_t nextRaceGroup; @@ -1136,8 +1142,16 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler // void RunMutexedUpdateCycle(void); + // A command from a user thread to the network thread. It owns \a data (std::malloc'd) and frees it + // when destroyed, so a command that is never processed (dropped at shutdown) does not leak it. A + // consumer that hands \a data to someone else must set it to nullptr first. struct BufferedCommandStruct { + BufferedCommandStruct() = default; + ~BufferedCommandStruct() { std::free(data); } + BufferedCommandStruct(const BufferedCommandStruct &) = delete; + BufferedCommandStruct &operator=(const BufferedCommandStruct &) = delete; + BitSize_t numberOfBitsToSend; MafiaNet::Priority priority; MafiaNet::Reliability reliability; @@ -1147,7 +1161,7 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler RemoteSystemStruct::ConnectMode connectionMode; NetworkID networkID; bool blockingCommand; // Only used for RPC - char *data; + char *data = nullptr; bool haveRakNetCloseSocket; unsigned connectionSocketIndex; unsigned int extraSocketOptions; @@ -1171,33 +1185,29 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler } command; }; - // Single producer single consumer queue using a linked list - //BufferedCommandStruct* bufferedCommandReadIndex, bufferedCommandWriteIndex; - - DataStructures::ThreadsafeAllocatingQueue bufferedCommands; + // User threads push, the network thread pops (UpdateInternal). + LockedQueue> bufferedCommands; - // DataStructures::ThreadsafeAllocatingQueue bufferedPackets; - - DataStructures::Queue bufferedPacketsFreePool; + // Recycled receive buffers; order does not matter, so the most recently freed is reused first. + std::vector bufferedPacketsFreePool; MafiaNet::SimpleMutex bufferedPacketsFreePoolMutex; - DataStructures::Queue bufferedPacketsQueue; + std::deque bufferedPacketsQueue; MafiaNet::SimpleMutex bufferedPacketsQueueMutex; - virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line); - virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line); + virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s); + virtual RNS2RecvStruct *AllocRNS2RecvStruct(); void SetupBufferedPackets(void); void PushBufferedPacket(RNS2RecvStruct *p); RNS2RecvStruct *PopBufferedPacket(void); struct SocketQueryOutput { - SocketQueryOutput() {} - ~SocketQueryOutput() {} - DataStructures::List sockets; + std::vector sockets; }; - DataStructures::ThreadsafeAllocatingQueue socketQueryOutput; + // The network thread pushes the answer to a BCS_GET_SOCKET command; GetSocket(s) pops it. + LockedQueue> socketQueryOutput; bool AllowIncomingConnections(void) const; @@ -1225,7 +1235,7 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler bool trackFrequencyTable; // Smart pointer so I can return the object to the user - DataStructures::List socketList; + std::vector socketList; void DerefAllSockets(void); unsigned int GetRakNetSocketFromUserConnectionSocketIndex(unsigned int userIndex) const; // Used for RPC replies @@ -1259,6 +1269,7 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler bool (*incomingDatagramEventHandler)(RNS2RecvStruct *); + SystemAddress ipList[MAXIMUM_NUMBER_OF_INTERNAL_IDS]; bool allowInternalRouting; @@ -1272,21 +1283,17 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler // Key exchanges run in the ID_OPEN_CONNECTION_REQUEST_2 handler (network thread). See GetServerKeyExchangeCountForTesting. uint64_t serverKeyExchangeCount = 0; - SimpleMutex packetAllocationPoolMutex; - DataStructures::MemoryPool packetAllocationPool; - // Pool packets not yet given back through DeallocatePacket, under packetAllocationPoolMutex. - // Shutdown() keeps the pool while any are out: Receive may be dispatching one, and the - // application may still hold one it is handling. - unsigned int packetsOutstanding; + // Packets AllocPacket handed out and DeallocatePacket has not freed yet (leak accounting). + std::atomic packetsOutstanding; // Bumped by every Shutdown(), so Receive can tell its dispatch was cut short by one. unsigned int shutdownGeneration; SimpleMutex packetReturnMutex; - DataStructures::Queue packetReturnQueue; + std::deque packetReturnQueue; // Wakes WaitForPackets. Notified under packetReturnMutex at every push and by Shutdown(). std::condition_variable_any packetReturnCondition; - Packet *AllocPacket(unsigned dataSize, const char *file, unsigned int line); - Packet *AllocPacket(unsigned dataSize, unsigned char *data, const char *file, unsigned int line); + Packet *AllocPacket(unsigned dataSize); + Packet *AllocPacket(unsigned dataSize, unsigned char *data); /// This is used to return a number to the user when they call Send identifying the message /// This number will be returned back with ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS and is only returned @@ -1295,7 +1302,7 @@ class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler uint32_t sendReceiptSerial; void ResetSendReceipt(void); void OnConnectedPong(MafiaNet::Time sendPingTime, MafiaNet::Time sendPongTime, RemoteSystemStruct *remoteSystem); - void CallPluginCallbacks(DataStructures::List &pluginList, Packet *packet); + void CallPluginCallbacks(std::vector &pluginList, Packet *packet); // Offers a message to the network-thread plugins; true if one of them took it. bool DispatchNetworkThreadReceive(Packet *packet); // Ticks every plugin's UpdateNetworkThread(), under the same rules as DispatchNetworkThreadReceive. diff --git a/Source/include/mafianet/peer_interface.h b/Source/include/mafianet/peer_interface.h index 91b00cf8d..b90eb97ae 100644 --- a/Source/include/mafianet/peer_interface.h +++ b/Source/include/mafianet/peer_interface.h @@ -25,7 +25,7 @@ #include "mafianet/types.h" #include "mafianet/session_crypto.h" #include "mafianet/export.h" -#include "mafianet/ds_list.h" +#include #include "mafianet/socket2.h" namespace MafiaNet @@ -334,7 +334,7 @@ class RAK_DLL_EXPORT RakPeerInterface /// Indices match each other, so \a addresses[0] and \a guids[0] refer to the same system /// \param[out] addresses All system addresses. Size of the list is the number of connections. Size of the list will match the size of the \a guids list. /// \param[out] guids All guids. Size of the list is the number of connections. Size of the list will match the size of the \a addresses list. - virtual void GetSystemList(DataStructures::List &addresses, DataStructures::List &guids) const = 0; + virtual void GetSystemList(std::vector &addresses, std::vector &guids) const = 0; /// Bans an IP from connecting. Banned IPs persist between connections but are not saved on shutdown nor loaded on startup. /// param[in] IP Dotted IP address. Can use * as a wildcard, such as 128.0.0.* will ban all IP addresses starting with 128.0.0 @@ -641,8 +641,8 @@ class RAK_DLL_EXPORT RakPeerInterface /// Get all sockets in use /// \note This sends a query to the thread and blocks on the return value for up to one second. In practice it should only take a millisecond or so. /// \param[out] sockets List of RakNetSocket2 structures in use. Sockets will not be closed until \a sockets goes out of scope - virtual void GetSockets(DataStructures::List &sockets) = 0; - virtual void ReleaseSockets(DataStructures::List &sockets) = 0; + virtual void GetSockets(std::vector &sockets) = 0; + virtual void ReleaseSockets(std::vector &sockets) = 0; virtual void WriteOutOfBandHeader(MafiaNet::BitStream *bitStream) = 0; @@ -698,7 +698,7 @@ class RAK_DLL_EXPORT RakPeerInterface /// \param[out] addresses SystemAddress for each connected system /// \param[out] guids RakNetGUID for each connected system /// \param[out] statistics Calculated RakNetStatistics for each connected system - virtual void GetStatisticsList(DataStructures::List &addresses, DataStructures::List &guids, DataStructures::List &statistics) = 0; + virtual void GetStatisticsList(std::vector &addresses, std::vector &guids, std::vector &statistics) = 0; /// \Returns how many messages are waiting when you call Receive() virtual unsigned int GetReceiveBufferSize(void) = 0; diff --git a/Source/include/mafianet/range_list.h b/Source/include/mafianet/range_list.h new file mode 100644 index 000000000..f001d15e2 --- /dev/null +++ b/Source/include/mafianet/range_list.h @@ -0,0 +1,210 @@ +/* + * Original work: Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * RakNet License.txt file in the licenses directory of this source tree. An additional grant + * of patent rights can be found in the RakNet Patents.txt file in the same directory. + * + * + * Modified work: Copyright (c) 2017-2018, SLikeSoft UG (haftungsbeschränkt) + * + * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style + * license found in the license.txt file in the root directory of this source tree. + */ + +/// \file range_list.h +/// \internal +/// \brief Sorted list of inclusive index ranges; the ACK/NAK wire codec. +/// + +#ifndef __RANGE_LIST_H +#define __RANGE_LIST_H + +#include "mafianet/assert.h" +#include "mafianet/bit_stream.h" + +#include +#include +#include + +namespace MafiaNet +{ +template +struct RangeNode +{ + RangeNode() {} + RangeNode(range_type min, range_type max) + : minIndex(min), maxIndex(max) {} + range_type minIndex; + range_type maxIndex; +}; + +/// Stores indices as sorted, non-overlapping, inclusive ranges. Serialize and +/// Deserialize define the ACK/NAK wire format and must stay bit-exact. +template +class RangeList +{ +public: + /// Insert an index; adjacent ranges are merged. Duplicates are ignored. + void Insert(range_type index) + { + // First range whose maxIndex >= index: either it contains index or it + // is the first range after it. + const size_t ins = FirstNotBelow(index); + if (ins < ranges.size() && !(index < ranges[ins].minIndex)) + { + return; // already covered by a range + } + + // index > maxIndex of the entire list + if (ins == ranges.size()) + { + if (!ranges.empty() && index == ranges[ins - 1].maxIndex + (range_type) 1) + { + ranges[ins - 1].maxIndex++; + } + else + { + ranges.push_back(RangeNode(index, index)); + } + return; + } + + if (index < ranges[ins].minIndex - (range_type) 1) + { + ranges.insert(ranges.begin() + (std::ptrdiff_t) ins, RangeNode(index, index)); + return; + } + + // index == minIndex - 1: extend downwards and join left + ranges[ins].minIndex--; + if (ins > 0 && ranges[ins - 1].maxIndex + (range_type) 1 == ranges[ins].minIndex) + { + ranges[ins - 1].maxIndex = ranges[ins].maxIndex; + ranges.erase(ranges.begin() + (std::ptrdiff_t) ins); + } + } + + void Clear() { ranges.clear(); } + + bool IsWithinRange(range_type value) const + { + const size_t i = FirstNotBelow(value); + return i < ranges.size() && !(value < ranges[i].minIndex); + } + + unsigned Size() const { return (unsigned) ranges.size(); } + + unsigned RangeSum() const + { + unsigned sum = 0; + for (const RangeNode &r : ranges) + { + sum += r.maxIndex - r.minIndex + 1; + } + return sum; + } + + /// Write as many ranges as fit in maxBits. With clearSerialized the written + /// ranges (always a prefix) are removed. Returns the bits written. + BitSize_t Serialize(BitStream *out, BitSize_t maxBits, bool clearSerialized) + { + RakAssert(ranges.size() < (unsigned short) -1); + BitStream tempBS; + BitSize_t bitsWritten = 0; + unsigned short countWritten = 0; + for (size_t i = 0; i < ranges.size(); i++) + { + if ((int) sizeof(unsigned short) * 8 + bitsWritten + (int) sizeof(range_type) * 8 * 2 + 1 > maxBits) + { + break; + } + unsigned char minEqualsMax = ranges[i].minIndex == ranges[i].maxIndex ? 1 : 0; + tempBS.Write(minEqualsMax); // one byte instead of one bit, for speed + tempBS.Write(ranges[i].minIndex); + bitsWritten += sizeof(range_type) * 8 + 8; + if (ranges[i].minIndex != ranges[i].maxIndex) + { + tempBS.Write(ranges[i].maxIndex); + bitsWritten += sizeof(range_type) * 8; + } + countWritten++; + } + + out->AlignWriteToByteBoundary(); + BitSize_t before = out->GetWriteOffset(); + out->Write(countWritten); + bitsWritten += out->GetWriteOffset() - before; + out->Write(&tempBS, tempBS.GetNumberOfBitsUsed()); + + if (clearSerialized && countWritten) + { + ranges.erase(ranges.begin(), ranges.begin() + countWritten); + } + return bitsWritten; + } + + /// Replace the contents with ranges read from the stream. Returns false on + /// truncated or non-increasing input (the list may then be partial). + bool Deserialize(BitStream *in) + { + ranges.clear(); + unsigned short count; + in->AlignReadToByteBoundary(); + if (!in->Read(count)) + { + return false; + } + // Grow as ranges are actually read; never trust the declared count. + range_type min, max; + for (unsigned short i = 0; i < count; i++) + { + if (!DeserializeSingleRange(in, min, max)) + { + return false; + } + if (!ranges.empty() && min <= ranges.back().maxIndex) + { + return false; + } + ranges.push_back(RangeNode(min, max)); + } + return true; + } + + std::vector> ranges; + +private: + size_t FirstNotBelow(const range_type &value) const + { + auto it = std::lower_bound(ranges.begin(), ranges.end(), value, [](const RangeNode &n, const range_type &v) + { return n.maxIndex < v; }); + return (size_t) (it - ranges.begin()); + } + + static bool DeserializeSingleRange(BitStream *in, range_type &min, range_type &max) + { + unsigned char maxEqualToMin; + if (!in->Read(maxEqualToMin) || !in->Read(min)) + { + return false; + } + if (maxEqualToMin == 0) + { + if (!in->Read(max) || max <= min) + { + return false; + } + } + else + { + max = min; + } + return true; + } +}; + +} // namespace MafiaNet + +#endif diff --git a/Source/include/mafianet/reliability_layer.h b/Source/include/mafianet/reliability_layer.h index 928d4176c..ddb07e57c 100644 --- a/Source/include/mafianet/reliability_layer.h +++ b/Source/include/mafianet/reliability_layer.h @@ -23,21 +23,15 @@ #include "mafianet/export.h" #include "mafianet/mtu_size.h" -#include "mafianet/ds_linked_list.h" -#include "mafianet/ds_list.h" #include "mafianet/socket_layer.h" #include "mafianet/packet_priority.h" -#include "mafianet/ds_queue.h" #include "mafianet/bit_stream.h" #include "mafianet/internal_packet.h" #include "mafianet/statistics.h" #include "mafianet/dr_sha1.h" -#include "mafianet/ds_ordered_list.h" -#include "mafianet/ds_range_list.h" -#include "mafianet/ds_bplus_tree.h" -#include "mafianet/ds_memory_pool.h" +#include "mafianet/range_list.h" +#include "mafianet/object_pool.h" #include "mafianet/defines.h" -#include "mafianet/ds_heap.h" #include "mafianet/bit_stream.h" #include "mafianet/native_feature_includes.h" #include "mafianet/session_crypto.h" @@ -45,6 +39,9 @@ #include "mafianet/plugin_interface2.h" #include "mafianet/rand.h" #include "mafianet/socket2.h" +#include +#include +#include #include #include @@ -59,8 +56,6 @@ /// Number of ordered streams available. You can use up to 32 ordered streams #define NUMBER_OF_ORDERED_STREAMS 32 // 2^5 -#define RESEND_TREE_ORDER 32 - namespace MafiaNet { @@ -119,17 +114,16 @@ struct SplitPacketChannel // InternalPacket *firstPacket; #endif }; -int RAK_DLL_EXPORT SplitPacketChannelComp(SplitPacketIdType const &key, SplitPacketChannel *const &data); // Helper class struct BPSTracker { BPSTracker(); ~BPSTracker(); - void Reset(const char *file, unsigned int line); + void Reset(); inline void Push1(CCTimeType time, uint64_t value1) { - dataQueue.Push(TimeAndValue2(time, value1), _FILE_AND_LINE_); + dataQueue.push_back(TimeAndValue2(time, value1)); total1 += value1; lastSec1 += value1; } @@ -162,7 +156,7 @@ struct BPSTracker uint64_t total1, lastSec1; // uint64_t total2, lastSec2; - DataStructures::Queue dataQueue; + std::deque dataQueue; void ClearExpired1(CCTimeType time); // void ClearExpired2(MafiaNet::TimeUS time); }; @@ -250,7 +244,7 @@ class ReliabilityLayer // /// \retval true Success /// \retval false Modified packet bool HandleSocketReceiveFromConnectedPlayer( - const char *buffer, unsigned int length, SystemAddress &systemAddress, DataStructures::List &messageHandlerList, int mtuSize, + const char *buffer, unsigned int length, SystemAddress &systemAddress, std::vector &messageHandlerList, int mtuSize, RakNetSocket2 *s, RakNetRandom *rnr, CCTimeType timeRead, BitStream &updateBitStream); /// This allocates bytes and writes a user-level message to those bytes. @@ -280,7 +274,7 @@ class ReliabilityLayer // /// \param[in] messageHandlerList A list of registered plugins void Update(RakNetSocket2 *s, SystemAddress &systemAddress, int MTUSize, CCTimeType time, unsigned bitsPerSecondLimit, - DataStructures::List &messageHandlerList, + std::vector &messageHandlerList, RakNetRandom *rnr, BitStream &updateBitStream); // #med 0.2.0 - review whether we'd rather have this defined as a private method and declare RakPeer a friend of ReliabilityLayer @@ -288,7 +282,7 @@ class ReliabilityLayer // /// Same as \see Update() except that outstanding ACKs are ensured to be sent. void UpdateAndForceACKs(RakNetSocket2 *s, SystemAddress &systemAddress, int MTUSize, CCTimeType time, unsigned bitsPerSecondLimit, - DataStructures::List &messageHandlerList, + std::vector &messageHandlerList, RakNetRandom *rnr, BitStream &updateBitStream); /// Were you ever unable to deliver a packet despite retries? @@ -330,6 +324,40 @@ class ReliabilityLayer // // Encoded as numMessages[unsigned int], message1BitLength[unsigned int], message1Data (aligned), ... //void GetUndeliveredMessages(MafiaNet::BitStream *messages, int MTUSize); + /// \internal One entry of the layer's binary min-heaps (send scheduling and + /// out-of-order buffering), kept in a std::vector by HeapPush/HeapPop. + /// Entries pop by ascending weight; equal weights pop in push order, by seq, + /// which comes from a per-heap counter bumped on every push. + struct HeapEntry + { + reliabilityHeapWeightType weight; + std::uint64_t seq; + InternalPacket *packet; + }; + /// \internal Heap comparator for std::push_heap/std::pop_heap: true when + /// \a a pops after \a b, which keeps the smallest (weight, seq) at front(). + struct HeapEntryPopsLater + { + bool operator()(const HeapEntry &a, const HeapEntry &b) const + { + return a.weight > b.weight || (a.weight == b.weight && a.seq > b.seq); + } + }; + /// \internal Adds \a packet to \a heap under \a weight, stamped with nextSeq++. + static void HeapPush(std::vector &heap, std::uint64_t &nextSeq, reliabilityHeapWeightType weight, InternalPacket *packet) + { + heap.push_back(HeapEntry{weight, nextSeq++, packet}); + std::push_heap(heap.begin(), heap.end(), HeapEntryPopsLater()); + } + /// \internal Removes and returns the packet at front(). \pre !heap.empty() + static InternalPacket *HeapPop(std::vector &heap) + { + std::pop_heap(heap.begin(), heap.end(), HeapEntryPopsLater()); + InternalPacket *packet = heap.back().packet; + heap.pop_back(); + return packet; + } + private: /// Send the contents of a bitstream to the socket /// \param[in] s The socket used for sending data @@ -351,7 +379,7 @@ class ReliabilityLayer // InternalPacket *CreateInternalPacketFromBitStream(MafiaNet::BitStream *bitStream, CCTimeType time); /// Does what the function name says - unsigned RemovePacketFromResendListAndDeleteOlderReliableSequenced(const MessageNumberType messageNumber, CCTimeType time, DataStructures::List &messageHandlerList, const SystemAddress &systemAddress); + unsigned RemovePacketFromResendListAndDeleteOlderReliableSequenced(const MessageNumberType messageNumber, CCTimeType time, std::vector &messageHandlerList, const SystemAddress &systemAddress); /// Acknowledge receipt of the packet with the specified messageNumber void SendAcknowledgementPacket(const DatagramSequenceNumberType messageNumber, CCTimeType time); @@ -375,12 +403,6 @@ class ReliabilityLayer // /// Check the SHA1 code bool CheckSHA1(char code[SHA1_LENGTH], unsigned char *const buffer, unsigned int nbytes); - /// Search the specified list for sequenced packets on the specified ordering channel, optionally skipping those with splitPacketId, and delete them - // void DeleteSequencedPacketsInList( unsigned char orderingChannel, DataStructures::List&theList, int splitPacketId = -1 ); - - /// Search the specified list for sequenced packets with a value less than orderingIndex and delete them - // void DeleteSequencedPacketsInList( unsigned char orderingChannel, DataStructures::Queue&theList ); - /// Returns true if newPacketOrderingIndex is older than the waitingForPacketOrderingIndex bool IsOlderOrderedPacket(OrderingIndexType newPacketOrderingIndex, OrderingIndexType waitingForPacketOrderingIndex); @@ -463,9 +485,6 @@ class ReliabilityLayer // /// Does not copy any split data parameters as that information is always generated does not have any reason to be copied InternalPacket *CreateInternalPacketCopy(InternalPacket *original, int dataByteOffset, int dataByteLength, CCTimeType time); - /// Get the specified ordering list - // DataStructures::LinkedList *GetOrderingListAtOrderingStream( unsigned char orderingChannel ); - /// Add the internal packet to the ordering list in order based on order index // void AddToOrderingList( InternalPacket * internalPacket ); @@ -489,7 +508,7 @@ class ReliabilityLayer // void UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAddress, int MTUSize, CCTimeType time, unsigned bitsPerSecondLimit, - DataStructures::List &messageHandlerList, + std::vector &messageHandlerList, RakNetRandom *rnr, BitStream &updateBitStream, bool forceSendACKs); /// Does this packet number represent a packet that was skipped (out of order?) @@ -508,8 +527,7 @@ class ReliabilityLayer // // Used ONLY for MafiaNet::Reliability::ReliableOrdered // MafiaNet::Reliability::ReliableSequenced just returns the newest one - // DataStructures::List*> orderingList; - DataStructures::Queue outputQueue; + std::deque outputQueue; // Every this many fragments of a split message, ID_DOWNLOAD_PROGRESS; 0 (until SetSplitMessageProgressInterval) for none. int splitMessageProgressInterval = 0; CCTimeType unreliableTimeout; @@ -537,8 +555,7 @@ class ReliabilityLayer // // Queue length is programmatically restricted to DATAGRAM_MESSAGE_ID_ARRAY_LENGTH // This is essentially an O(1) lookup to get a DatagramHistoryNode given an index // datagramHistory holds a linked list of MessageNumberNode. Each MessageNumberNode refers to one element in resendList which can be cleared on an ack. - DataStructures::Queue datagramHistory; - DataStructures::MemoryPool datagramHistoryMessagePool; + std::deque datagramHistory; struct UnreliableWithAckReceiptNode { @@ -551,7 +568,7 @@ class ReliabilityLayer // uint32_t sendReceiptSerial; MafiaNet::TimeUS nextActionTime; }; - DataStructures::List unreliableWithAckReceiptHistory; + std::vector unreliableWithAckReceiptHistory; void RemoveFromDatagramHistory(DatagramSequenceNumberType index); MessageNumberNode *GetMessageNumberNodeByDatagramIndex(DatagramSequenceNumberType index, CCTimeType *timeSent, int *wireBytes = nullptr); @@ -560,8 +577,9 @@ class ReliabilityLayer // MessageNumberNode *AddSubsequentToDatagramHistory(MessageNumberNode *messageNumberNode, DatagramSequenceNumberType messageNumber); DatagramSequenceNumberType datagramHistoryPopCount; - DataStructures::MemoryPool internalPacketPool; - // DataStructures::BPlusTree resendTree; + // Every message sent, received or split takes one: plain new/delete cost up + // to 20% in the ReliabilityLayer_* benchmarks, so they are recycled. + ObjectPool internalPacketPool; InternalPacket *resendBuffer[RESEND_BUFFER_ARRAY_LENGTH]; InternalPacket *resendLinkedListHead; InternalPacket *unreliableLinkedListHead; @@ -569,7 +587,7 @@ class ReliabilityLayer // void AddToUnreliableLinkedList(InternalPacket *internalPacket); // unsigned int numPacketsOnResendBuffer; //unsigned int blockWindowIncreaseUntilTime; - // DataStructures::RangeList acknowlegements; + // RangeList acknowlegements; // Resend list is a tree of packets we need to resend // Set to the current time when the resend queue is no longer empty @@ -586,8 +604,8 @@ class ReliabilityLayer // // unsigned packetlossThisSampleResendCount; // CCTimeType lastPacketlossTime; - //DataStructures::Queue sendPacketSet[ MafiaNet::NUMBER_OF_PRIORITIES ]; - DataStructures::Heap outgoingPacketBuffer; + std::vector outgoingPacketBuffer; // min-heap, see HeapPush/HeapPop + std::uint64_t outgoingPacketBufferNextSeq = 0; // never reset: ties stay FIFO across Reset() reliabilityHeapWeightType outgoingPacketBufferNextWeights[MafiaNet::NUMBER_OF_PRIORITIES]; void InitHeapWeights(void); reliabilityHeapWeightType GetNextWeight(int priorityLevel); @@ -595,7 +613,8 @@ class ReliabilityLayer // // double bytesInSendBuffer[MafiaNet::NUMBER_OF_PRIORITIES]; - DataStructures::OrderedList splitPacketChannelList; + // Partially reassembled split messages, keyed by splitPacketId. Owns the channels. + std::map splitPacketChannelList; MessageNumberType sendReliableMessageNumberIndex; MessageNumberType internalOrderIndex; @@ -624,6 +643,7 @@ class ReliabilityLayer // // Messages in buffer are put in a minheap. The value of each node is calculated such that messages are returned: // A. (lowest ordering index, lowest sequence index) // B. (lowest ordering index, no sequence index) + // C. equal weights (copies of one ordered message, repeated sequenced indices) in arrival order // Messages are pushed off until the heap is empty, or the next message to be returned does not preserve the ordered index // For an empty heap, the heap weight should start at the lowest value based on the next expected ordering index, to avoid variable overflow @@ -635,7 +655,8 @@ class ReliabilityLayer // OrderingIndexType orderedReadIndex[NUMBER_OF_ORDERED_STREAMS]; // Highest value received for sequencedWriteIndex for the current value of orderedReadIndex on the same channel. OrderingIndexType highestSequencedReadIndex[NUMBER_OF_ORDERED_STREAMS]; - DataStructures::Heap orderingHeaps[NUMBER_OF_ORDERED_STREAMS]; + std::vector orderingHeaps[NUMBER_OF_ORDERED_STREAMS]; // min-heaps, see HeapPush/HeapPop + std::uint64_t orderingHeapNextSeq[NUMBER_OF_ORDERED_STREAMS] = {}; // never reset: ties stay FIFO across Reset() OrderingIndexType heapIndexOffsets[NUMBER_OF_ORDERED_STREAMS]; @@ -651,8 +672,7 @@ class ReliabilityLayer // /// If 0, we got got that packet. Otherwise, the time to give up waiting for that packet. /// If we get a packet number where (receivedPacketsBaseIndex-packetNumber) is less than half the range of receivedPacketsBaseIndex then it is a duplicate /// Otherwise, it is a duplicate packet (and ignore it). - // DataStructures::Queue hasReceivedPacketQueue; - DataStructures::Queue hasReceivedPacketQueue; + std::deque hasReceivedPacketQueue; DatagramSequenceNumberType receivedPacketsBaseIndex; bool resetReceivedPackets; @@ -689,7 +709,7 @@ class ReliabilityLayer // // SystemAddress systemAddress; unsigned int extraSocketOptions; }; - DataStructures::Queue delayList; + std::deque delayList; // Internet simulator double packetloss; @@ -736,15 +756,14 @@ class ReliabilityLayer // void AddToListTail(InternalPacket *internalPacket, bool modifyUnacknowledgedBytes); void PopListHead(bool modifyUnacknowledgedBytes); bool IsResendQueueEmpty(void) const; - void SortSplitPacketList(DataStructures::List &data, unsigned int leftEdge, unsigned int rightEdge) const; void SendACKs(RakNetSocket2 *s, SystemAddress &systemAddress, CCTimeType time, RakNetRandom *rnr, BitStream &updateBitStream); - DataStructures::List packetsToSendThisUpdate; - DataStructures::List packetsToDeallocThisUpdate; + std::vector packetsToSendThisUpdate; + std::vector packetsToDeallocThisUpdate; // boundary is in packetsToSendThisUpdate, inclusive - DataStructures::List packetsToSendThisUpdateDatagramBoundaries; - DataStructures::List datagramsToSendThisUpdateIsPair; - DataStructures::List datagramSizesInBytes; + std::vector packetsToSendThisUpdateDatagramBoundaries; + std::vector datagramsToSendThisUpdateIsPair; + std::vector datagramSizesInBytes; BitSize_t datagramSizeSoFar; BitSize_t allDatagramSizesSoFar; double totalUserDataBytesAcked; @@ -752,15 +771,15 @@ class ReliabilityLayer // CCTimeType timeToNextUnreliableCull; // This doesn't need to be a member, but I do it to avoid reallocations - DataStructures::RangeList incomingAcks; + RangeList incomingAcks; // Every 16 datagrams, we make sure the 17th datagram goes out the same update tick, and is the same size as the 16th int countdownToNextPacketPair; InternalPacket *AllocateFromInternalPacketPool(void); void ReleaseToInternalPacketPool(InternalPacket *ip); - DataStructures::RangeList acknowlegements; - DataStructures::RangeList NAKs; + RangeList acknowlegements; + RangeList NAKs; bool remoteSystemNeedsBAndAS; unsigned int GetMaxDatagramSizeExcludingMessageHeaderBytes(void); @@ -773,7 +792,6 @@ class ReliabilityLayer // // Allocate new void AllocInternalPacketData(InternalPacket *internalPacket, unsigned int numBytes, bool allowStack, const char *file, unsigned int line); void FreeInternalPacketData(InternalPacket *internalPacket, const char *file, unsigned int line); - DataStructures::MemoryPool refCountedDataPool; BPSTracker bpsMetrics[RNS_PER_SECOND_METRICS_COUNT]; CCTimeType lastBpsClear; diff --git a/Source/include/mafianet/send_to_thread.h b/Source/include/mafianet/send_to_thread.h index b73acbdd4..64df89be6 100644 --- a/Source/include/mafianet/send_to_thread.h +++ b/Source/include/mafianet/send_to_thread.h @@ -22,9 +22,11 @@ #include "mafianet/internal_packet.h" #include "mafianet/socket_layer.h" -#include "mafianet/ds_threadsafe_allocating_queue.h" +#include "mafianet/locked_queue.h" #include "mafianet/thread_pool.h" +#include + namespace MafiaNet { class SendToThread @@ -47,7 +49,8 @@ class SendToThread static void AddRef(void); static void Deref(void); - static DataStructures::ThreadsafeAllocatingQueue objectQueue; + // Blocks the worker has sent, kept for AllocateBlock to reuse. + static LockedQueue> objectQueue; protected: static int refCount; diff --git a/Source/include/mafianet/single_producer_consumer.h b/Source/include/mafianet/single_producer_consumer.h deleted file mode 100644 index dc720ce8c..000000000 --- a/Source/include/mafianet/single_producer_consumer.h +++ /dev/null @@ -1,270 +0,0 @@ -/* - * Original work: Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * RakNet License.txt file in the licenses directory of this source tree. An additional grant - * of patent rights can be found in the RakNet Patents.txt file in the same directory. - * - * - * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt) - * - * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style - * license found in the license.txt file in the root directory of this source tree. - */ - -/// \file -/// \brief \b [Internal] Passes queued data between threads using a circular buffer with read and write pointers -/// - - -#ifndef __SINGLE_PRODUCER_CONSUMER_H -#define __SINGLE_PRODUCER_CONSUMER_H - -#include "mafianet/assert.h" - -static const int MINIMUM_LIST_SIZE = 8; - -#include "mafianet/export.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. -namespace DataStructures -{ -/// \brief A single producer consumer implementation without critical sections. -template -class RAK_DLL_EXPORT SingleProducerConsumer -{ -public: - // Constructor - SingleProducerConsumer(); - - // Destructor - ~SingleProducerConsumer(); - - /// WriteLock must be immediately followed by WriteUnlock. These two functions must be called in the same thread. - /// \return A pointer to a block of data you can write to. - SingleProducerConsumerType *WriteLock(void); - - /// Call if you don't want to write to a block of data from WriteLock() after all. - /// Cancelling locks cancels all locks back up to the data passed. So if you lock twice and cancel using the first lock, the second lock is ignored - /// \param[in] cancelToLocation Which WriteLock() to cancel. - void CancelWriteLock(SingleProducerConsumerType *cancelToLocation); - - /// Call when you are done writing to a block of memory returned by WriteLock() - void WriteUnlock(void); - - /// ReadLock must be immediately followed by ReadUnlock. These two functions must be called in the same thread. - /// \retval 0 No data is availble to read - /// \retval Non-zero The data previously written to, in another thread, by WriteLock followed by WriteUnlock. - SingleProducerConsumerType *ReadLock(void); - - // Cancelling locks cancels all locks back up to the data passed. So if you lock twice and cancel using the first lock, the second lock is ignored - /// param[in] Which ReadLock() to cancel. - void CancelReadLock(SingleProducerConsumerType *cancelToLocation); - - /// Signals that we are done reading the the data from the least recent call of ReadLock. - /// At this point that pointer is no longer valid, and should no longer be read. - void ReadUnlock(void); - - /// Clear is not thread-safe and none of the lock or unlock functions should be called while it is running. - void Clear(void); - - /// This function will estimate how many elements are waiting to be read. It's threadsafe enough that the value returned is stable, but not threadsafe enough to give accurate results. - /// \return An ESTIMATE of how many data elements are waiting to be read - int Size(void) const; - - /// Make sure that the pointer we done reading for the call to ReadUnlock is the right pointer. - /// param[in] A previous pointer returned by ReadLock() - bool CheckReadUnlockOrder(const SingleProducerConsumerType *data) const; - - /// Returns if ReadUnlock was called before ReadLock - /// \return If the read is locked - bool ReadIsLocked(void) const; - -private: - struct DataPlusPtr - { - DataPlusPtr() { readyToRead = false; } - SingleProducerConsumerType object; - - // Ready to read is so we can use an equality boolean comparison, in case the writePointer var is trashed while context switching. - volatile bool readyToRead; - volatile DataPlusPtr *next; - }; - volatile DataPlusPtr *readAheadPointer; - volatile DataPlusPtr *writeAheadPointer; - volatile DataPlusPtr *readPointer; - volatile DataPlusPtr *writePointer; - unsigned readCount, writeCount; -}; - -template -SingleProducerConsumer::SingleProducerConsumer() -{ - // Preallocate - readPointer = new DataPlusPtr; - writePointer = readPointer; - readPointer->next = new DataPlusPtr; - int listSize; -#ifdef _DEBUG - RakAssert(MINIMUM_LIST_SIZE >= 3); -#endif - for (listSize = 2; listSize < MINIMUM_LIST_SIZE; listSize++) - { - readPointer = readPointer->next; - readPointer->next = new DataPlusPtr; - } - readPointer->next->next = writePointer; // last to next = start - readPointer = writePointer; - readAheadPointer = readPointer; - writeAheadPointer = writePointer; - readCount = writeCount = 0; -} - -template -SingleProducerConsumer::~SingleProducerConsumer() -{ - volatile DataPlusPtr *next; - readPointer = writeAheadPointer->next; - while (readPointer != writeAheadPointer) - { - next = readPointer->next; - delete (char *) readPointer; - readPointer = next; - } - delete (char *) readPointer; -} - -template -SingleProducerConsumerType *SingleProducerConsumer::WriteLock(void) -{ - if (writeAheadPointer->next == readPointer || - writeAheadPointer->next->readyToRead == true) - { - volatile DataPlusPtr *originalNext = writeAheadPointer->next; - writeAheadPointer->next = new DataPlusPtr; - RakAssert(writeAheadPointer->next); - writeAheadPointer->next->next = originalNext; - } - - volatile DataPlusPtr *last; - last = writeAheadPointer; - writeAheadPointer = writeAheadPointer->next; - - return (SingleProducerConsumerType *) last; -} - -template -void SingleProducerConsumer::CancelWriteLock(SingleProducerConsumerType *cancelToLocation) -{ - writeAheadPointer = (DataPlusPtr *) cancelToLocation; -} - -template -void SingleProducerConsumer::WriteUnlock(void) -{ - // DataPlusPtr *dataContainer = (DataPlusPtr *)structure; - -#ifdef _DEBUG - RakAssert(writePointer->next != readPointer); - RakAssert(writePointer != writeAheadPointer); -#endif - - writeCount++; - // User is done with the data, allow send by updating the write pointer - writePointer->readyToRead = true; - writePointer = writePointer->next; -} - -template -SingleProducerConsumerType *SingleProducerConsumer::ReadLock(void) -{ - if (readAheadPointer == writePointer || - readAheadPointer->readyToRead == false) - { - return 0; - } - - volatile DataPlusPtr *last; - last = readAheadPointer; - readAheadPointer = readAheadPointer->next; - return (SingleProducerConsumerType *) last; -} - -template -void SingleProducerConsumer::CancelReadLock(SingleProducerConsumerType *cancelToLocation) -{ -#ifdef _DEBUG - RakAssert(readPointer != writePointer); -#endif - readAheadPointer = (DataPlusPtr *) cancelToLocation; -} - -template -void SingleProducerConsumer::ReadUnlock(void) -{ -#ifdef _DEBUG - RakAssert(readAheadPointer != readPointer); // If hits, then called ReadUnlock before ReadLock - RakAssert(readPointer != writePointer); // If hits, then called ReadUnlock when Read returns 0 -#endif - readCount++; - - // Allow writes to this memory block - readPointer->readyToRead = false; - readPointer = readPointer->next; -} - -template -void SingleProducerConsumer::Clear(void) -{ - // Shrink the list down to MINIMUM_LIST_SIZE elements - volatile DataPlusPtr *next; - writePointer = readPointer->next; - - int listSize = 1; - next = readPointer->next; - while (next != readPointer) - { - listSize++; - next = next->next; - } - - while (listSize-- > MINIMUM_LIST_SIZE) - { - next = writePointer->next; -#ifdef _DEBUG - RakAssert(writePointer != readPointer); -#endif - delete (char *) writePointer; - writePointer = next; - } - - readPointer->next = writePointer; - writePointer = readPointer; - readAheadPointer = readPointer; - writeAheadPointer = writePointer; - readCount = writeCount = 0; -} - -template -int SingleProducerConsumer::Size(void) const -{ - return writeCount - readCount; -} - -template -bool SingleProducerConsumer::CheckReadUnlockOrder(const SingleProducerConsumerType *data) const -{ - return const_cast(&readPointer->object) == data; -} - - -template -bool SingleProducerConsumer::ReadIsLocked(void) const -{ - return readAheadPointer != readPointer; -} -} - -#endif diff --git a/Source/include/mafianet/socket2.h b/Source/include/mafianet/socket2.h index 41f4a7b73..3a8054042 100644 --- a/Source/include/mafianet/socket2.h +++ b/Source/include/mafianet/socket2.h @@ -20,7 +20,6 @@ #include "mafianet/mtu_size.h" #include "mafianet/lockless_types.h" #include "mafianet/thread.h" -#include "mafianet/ds_threadsafe_allocating_queue.h" #include "mafianet/export.h" #include @@ -167,11 +166,8 @@ class RAK_DLL_EXPORT RNS2EventHandler // bufferedPackets.Push(recvFromStruct); // quitAndDataEvents.SetEvent(); virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct) = 0; - virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line) = 0; - virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line) = 0; - - // recvFromStruct=bufferedPackets.Allocate( _FILE_AND_LINE_ ); - // DataStructures::ThreadsafeAllocatingQueue bufferedPackets; + virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s) = 0; + virtual RNS2RecvStruct *AllocRNS2RecvStruct() = 0; }; class RakNetSocket2 @@ -182,7 +178,7 @@ class RakNetSocket2 // In order for the handler to trigger, some platforms must call PollRecvFrom, some platforms this create an internal thread. void SetRecvEventHandler(RNS2EventHandler *_eventHandler); - virtual RNS2SendResult Send(RNS2_SendParameters *sendParameters, const char *file, unsigned int line) = 0; + virtual RNS2SendResult Send(RNS2_SendParameters *sendParameters) = 0; // Batched send. The base implementation simply loops Send() so every socket // type has a working default; RNS2_Linux overrides it with sendmmsg where the // syscall exists (Linux). @@ -205,7 +201,7 @@ class RakNetSocket2 // did not go out"; only the diagnostics differ. See MmsgBatchTests. // RNS2_SendParameters::ttl is honoured either way: sendmmsg has no per-message // TTL, so the override defers a batch carrying one to this base loop. - virtual RNS2SendResult SendBatch(RNS2_SendParameters *sends, unsigned count, const char *file, unsigned int line); + virtual RNS2SendResult SendBatch(RNS2_SendParameters *sends, unsigned count); RNS2Type GetSocketType(void) const; void SetSocketType(RNS2Type t); bool IsBerkleySocket(void) const; @@ -293,7 +289,7 @@ class IRNS2_Berkley : public RakNetSocket2 static bool IsPortInUse(unsigned short port, const char *hostAddress, unsigned short addressFamily, int type); // ----------- MEMBERS ------------ - virtual RNS2BindResult Bind(RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line) = 0; + virtual RNS2BindResult Bind(RNS2_BerkleyBindParameters *bindParameters) = 0; }; // Common Berkeley socket implementation for Windows and Linux class RNS2_Berkley : public IRNS2_Berkley @@ -310,8 +306,8 @@ class RNS2_Berkley : public IRNS2_Berkley protected: // Used by other classes - RNS2BindResult BindShared(RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line); - RNS2BindResult BindSharedIPV4And6(RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line); + RNS2BindResult BindShared(RNS2_BerkleyBindParameters *bindParameters); + RNS2BindResult BindSharedIPV4And6(RNS2_BerkleyBindParameters *bindParameters); // Binds the first usable address getaddrinfo returns for \a family. With // \a wantDualStack, AF_INET6 sockets get IPV6_V6ONLY=0; \a *dualStackUnavailable // is then set when the host cannot provide that (no IPv6 address for the @@ -359,7 +355,7 @@ class RNS2_Windows_Linux public: protected: // \a socketFamily and \a dualStack describe rns2Socket; see SystemAddressToSockaddr. - static RNS2SendResult Send_Windows_Linux(RNS2Socket rns2Socket, int socketFamily, bool dualStack, RNS2_SendParameters *sendParameters, const char *file, unsigned int line); + static RNS2SendResult Send_Windows_Linux(RNS2Socket rns2Socket, int socketFamily, bool dualStack, RNS2_SendParameters *sendParameters); }; #endif @@ -384,8 +380,8 @@ class RNS2_Windows : public RNS2_Berkley, public RNS2_Windows_Linux public: RNS2_Windows(); virtual ~RNS2_Windows(); - RNS2BindResult Bind(RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line); - RNS2SendResult Send(RNS2_SendParameters *sendParameters, const char *file, unsigned int line); + RNS2BindResult Bind(RNS2_BerkleyBindParameters *bindParameters); + RNS2SendResult Send(RNS2_SendParameters *sendParameters); void SetSocketLayerOverride(SocketLayerOverride *_slo); SocketLayerOverride *GetSocketLayerOverride(void); // ----------- STATICS ------------ @@ -399,13 +395,13 @@ class RNS2_Windows : public RNS2_Berkley, public RNS2_Windows_Linux class RNS2_Linux : public RNS2_Berkley, public RNS2_Windows_Linux { public: - RNS2BindResult Bind(RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line); - RNS2SendResult Send(RNS2_SendParameters *sendParameters, const char *file, unsigned int line); + RNS2BindResult Bind(RNS2_BerkleyBindParameters *bindParameters); + RNS2SendResult Send(RNS2_SendParameters *sendParameters); // Guarded because RNS2_Linux is the non-Windows socket class: macOS and the // BSDs compile it too, and sendmmsg/mmsghdr do not exist there. Those // platforms inherit the portable base SendBatch (a Send() loop). #if defined(__linux__) - RNS2SendResult SendBatch(RNS2_SendParameters *sends, unsigned count, const char *file, unsigned int line); + RNS2SendResult SendBatch(RNS2_SendParameters *sends, unsigned count); #endif // ----------- STATICS ------------ diff --git a/Source/include/mafianet/string.h b/Source/include/mafianet/string.h index 200b3c3bb..f74d1a24a 100644 --- a/Source/include/mafianet/string.h +++ b/Source/include/mafianet/string.h @@ -17,10 +17,10 @@ #define __RAK_STRING_H #include "mafianet/export.h" -#include "mafianet/ds_list.h" #include "mafianet/types.h" // int64_t #include #include +#include #ifdef _WIN32 #include "mafianet/windows_includes.h" @@ -322,8 +322,6 @@ class RAK_DLL_EXPORT RakString /// \internal SharedString *sharedString; - // static SimpleMutex poolMutex; - // static DataStructures::MemoryPool pool; /// \internal static SharedString emptyString; @@ -331,7 +329,7 @@ class RAK_DLL_EXPORT RakString //static unsigned int sharedStringFreeListAllocationCount; /// \internal /// List of free objects to reduce memory reallocations - static DataStructures::List freeList; + static std::vector freeList; static int RakStringComp(RakString const &key, RakString const &data); diff --git a/Source/include/mafianet/tcp_interface.h b/Source/include/mafianet/tcp_interface.h index c5bc7df1b..fc18d8efa 100644 --- a/Source/include/mafianet/tcp_interface.h +++ b/Source/include/mafianet/tcp_interface.h @@ -33,10 +33,12 @@ #include "mafianet/defines.h" #include "mafianet/socket_includes.h" #include "mafianet/ds_byte_queue.h" -#include "mafianet/ds_threadsafe_allocating_queue.h" +#include "mafianet/locked_queue.h" #include "mafianet/lockless_types.h" #include "mafianet/plugin_interface2.h" +#include +#include #include #include @@ -157,14 +159,19 @@ class RAK_DLL_EXPORT TCPInterface unsigned int length; }; */ - // DataStructures::SingleProducerConsumer outgoingMessages; - // DataStructures::SingleProducerConsumer incomingMessages; - // DataStructures::SingleProducerConsumer newIncomingConnections, lostConnections, requestedCloseConnections; - // DataStructures::SingleProducerConsumer newRemoteClients; - // DataStructures::ThreadsafeAllocatingQueue outgoingMessages; - DataStructures::ThreadsafeAllocatingQueue incomingMessages; - DataStructures::ThreadsafeAllocatingQueue newIncomingConnections, lostConnections, requestedCloseConnections; - DataStructures::ThreadsafeAllocatingQueue newRemoteClients; + // Frees a packet the TCP thread queued: its std::malloc'd data, then the packet itself. A packet popped + // for the user is released from its unique_ptr and freed by DeallocatePacket the same way. + struct IncomingPacketDeleter + { + void operator()(Packet *packet) const + { + std::free(packet->data); + delete packet; + } + }; + // Filled by the TCP thread, drained by the user thread (Receive, HasNewIncomingConnection, HasLostConnection). + LockedQueue> incomingMessages; + LockedQueue newIncomingConnections, lostConnections; SimpleMutex completedConnectionAttemptMutex, failedConnectionAttemptMutex; DataStructures::Queue completedConnectionAttempts, failedConnectionAttempts; diff --git a/Source/include/mafianet/threadsafe_packet_logger.h b/Source/include/mafianet/threadsafe_packet_logger.h index 2981e80c1..66e67e6c9 100644 --- a/Source/include/mafianet/threadsafe_packet_logger.h +++ b/Source/include/mafianet/threadsafe_packet_logger.h @@ -25,7 +25,9 @@ #define __THREADSAFE_PACKET_LOGGER_H #include "mafianet/packet_logger.h" -#include "mafianet/single_producer_consumer.h" +#include "mafianet/locked_queue.h" + +#include namespace MafiaNet { @@ -43,7 +45,8 @@ class RAK_DLL_EXPORT ThreadsafePacketLogger : public PacketLogger protected: virtual void AddToLog(const char *str); - DataStructures::SingleProducerConsumer logMessages; + // Lines logged on the network thread, written out by Update on the user thread. + LockedQueue logMessages; }; } // namespace MafiaNet diff --git a/Source/include/mafianet/udp_forwarder.h b/Source/include/mafianet/udp_forwarder.h index a8b71b513..cc043b5b6 100644 --- a/Source/include/mafianet/udp_forwarder.h +++ b/Source/include/mafianet/udp_forwarder.h @@ -34,7 +34,7 @@ #include "mafianet/ds_queue.h" #include "mafianet/ds_ordered_list.h" #include "mafianet/lockless_types.h" -#include "mafianet/ds_threadsafe_allocating_queue.h" +#include "mafianet/locked_queue.h" namespace MafiaNet { @@ -129,7 +129,8 @@ class RAK_DLL_EXPORT UDPForwarder unsigned int inputId; }; - DataStructures::ThreadsafeAllocatingQueue startForwardingInput; + // User thread (StartForwarding) to the forwarder thread. + LockedQueue startForwardingInput; struct StartForwardingOutputStruct { @@ -146,7 +147,8 @@ class RAK_DLL_EXPORT UDPForwarder SystemAddress source; SystemAddress destination; }; - DataStructures::ThreadsafeAllocatingQueue stopForwardingCommands; + // User thread (StopForwarding) to the forwarder thread. + LockedQueue stopForwardingCommands; unsigned int nextInputId; // New entries are added to forwardListNotUpdated diff --git a/Source/src/connection_graph2.cpp b/Source/src/connection_graph2.cpp index e37788fdf..0cc848c20 100644 --- a/Source/src/connection_graph2.cpp +++ b/Source/src/connection_graph2.cpp @@ -213,17 +213,17 @@ void ConnectionGraph2::AddParticipant(const SystemAddress &systemAddress, RakNet SendUnified(&bs, MafiaNet::Priority::High, MafiaNet::Reliability::ReliableOrdered, 0, systemAddress, true); // Send everyone to the new guy - DataStructures::List addresses; - DataStructures::List guids; + std::vector addresses; + std::vector guids; rakPeerInterface->GetSystemList(addresses, guids); bs.Reset(); bs.Write((MessageID) ID_REMOTE_NEW_INCOMING_CONNECTION); BitSize_t writeOffset = bs.GetWriteOffset(); - bs.Write((uint32_t) addresses.Size()); + bs.Write((uint32_t) addresses.size()); unsigned int i; uint32_t count = 0; - for (i = 0; i < addresses.Size(); i++) + for (i = 0; i < addresses.size(); i++) { if (addresses[i] == systemAddress) continue; diff --git a/Source/src/mmsg_batch.cpp b/Source/src/mmsg_batch.cpp index 4d7d8d5ac..e3d585341 100644 --- a/Source/src/mmsg_batch.cpp +++ b/Source/src/mmsg_batch.cpp @@ -85,13 +85,13 @@ void DispatchRecvBatch(RNS2EventHandler *handler, // Zero-length read (same as the scalar bytesRead<=0 branch) or an // undecodable source address -- free the struct rather than // surfacing a packet we cannot attribute to a peer. - handler->DeallocRNS2RecvStruct(s, _FILE_AND_LINE_); + handler->DeallocRNS2RecvStruct(s); } } // Hand back the unused tail so no preallocated buffer leaks. for (unsigned i = received; i < allocated; ++i) - handler->DeallocRNS2RecvStruct(slots[i], _FILE_AND_LINE_); + handler->DeallocRNS2RecvStruct(slots[i]); } } // namespace MafiaNet diff --git a/Source/src/nat_punchthrough_client.cpp b/Source/src/nat_punchthrough_client.cpp index be574d757..f81e5f881 100644 --- a/Source/src/nat_punchthrough_client.cpp +++ b/Source/src/nat_punchthrough_client.cpp @@ -940,7 +940,7 @@ void NatPunchthroughClient::OnClosedConnection(const SystemAddress &systemAddres } void NatPunchthroughClient::GetUPNPPortMappings(char *externalPort, char *internalPort, const SystemAddress &natPunchthroughServerAddress) { - DataStructures::List sockets; + std::vector sockets; rakPeerInterface->GetSockets(sockets); Itoa(sockets[0]->GetBoundAddress().GetPort(), internalPort, 10); if (mostRecentExternalPort == 0) diff --git a/Source/src/nat_punchthrough_server.cpp b/Source/src/nat_punchthrough_server.cpp index a5da04da4..f08f05a5a 100644 --- a/Source/src/nat_punchthrough_server.cpp +++ b/Source/src/nat_punchthrough_server.cpp @@ -248,9 +248,9 @@ PluginReceiveResult NatPunchthroughServer::OnReceive(Packet *packet) if (boundAddresses[0] == UNASSIGNED_SYSTEM_ADDRESS) { - DataStructures::List sockets; + std::vector sockets; rakPeerInterface->GetSockets(sockets); - for (unsigned i = 0; i < sockets.Size() && i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) + for (unsigned i = 0; i < sockets.size() && i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) { boundAddresses[i] = sockets[i]->GetBoundAddress(); boundAddressCount++; diff --git a/Source/src/nat_type_detection_client.cpp b/Source/src/nat_type_detection_client.cpp index 3a8b3d0a2..42daf9e0b 100644 --- a/Source/src/nat_type_detection_client.cpp +++ b/Source/src/nat_type_detection_client.cpp @@ -47,7 +47,7 @@ void NatTypeDetectionClient::DetectNATType(SystemAddress _serverAddress) if (c2 == 0) { - DataStructures::List sockets; + std::vector sockets; rakPeerInterface->GetSockets(sockets); //SystemAddress sockAddr; //SocketLayer::GetSystemAddress(sockets[0], &sockAddr); @@ -115,7 +115,7 @@ void NatTypeDetectionClient::Update(void) OnCompletion(NAT_TYPE_NONE); RakAssert(IsInProgress() == false); } - DeallocRNS2RecvStruct(recvStruct, _FILE_AND_LINE_); + DeallocRNS2RecvStruct(recvStruct); bufferedPacketsMutex.Lock(); if (bufferedPackets.Size() > 0) @@ -182,7 +182,7 @@ void NatTypeDetectionClient::OnTestPortRestricted(Packet *packet) unsigned short s3p4Port; bsIn.Read(s3p4Port); - DataStructures::List sockets; + std::vector sockets; rakPeerInterface->GetSockets(sockets); SystemAddress s3p4Addr = sockets[0]->GetBoundAddress(); s3p4Addr.FromStringExplicitPort(s3p4StrAddress.C_String(), s3p4Port); @@ -198,7 +198,7 @@ void NatTypeDetectionClient::OnTestPortRestricted(Packet *packet) bsp.data = (char *) bsOut.GetData(); bsp.length = bsOut.GetNumberOfBytesUsed(); bsp.systemAddress = s3p4Addr; - sockets[0]->Send(&bsp, _FILE_AND_LINE_); + sockets[0]->Send(&bsp); } void NatTypeDetectionClient::Shutdown(void) { @@ -219,12 +219,12 @@ void NatTypeDetectionClient::Shutdown(void) } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void NatTypeDetectionClient::DeallocRNS2RecvStruct(RNS2RecvStruct *s, [[maybe_unused]] const char *file, [[maybe_unused]] unsigned int line) +void NatTypeDetectionClient::DeallocRNS2RecvStruct(RNS2RecvStruct *s) { delete s; } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RNS2RecvStruct *NatTypeDetectionClient::AllocRNS2RecvStruct([[maybe_unused]] const char *file, [[maybe_unused]] unsigned int line) +RNS2RecvStruct *NatTypeDetectionClient::AllocRNS2RecvStruct() { return new RNS2RecvStruct; } diff --git a/Source/src/nat_type_detection_common.cpp b/Source/src/nat_type_detection_common.cpp index 67d436663..1c1defe18 100644 --- a/Source/src/nat_type_detection_common.cpp +++ b/Source/src/nat_type_detection_common.cpp @@ -20,6 +20,7 @@ #include "mafianet/socket_layer.h" #include "mafianet/socket_includes.h" #include "mafianet/socket_defines.h" +#include "mafianet/assert.h" using namespace MafiaNet; @@ -116,7 +117,7 @@ RakNetSocket2 *MafiaNet::CreateNonblockingBoundSocket(const char *bindAddr, RNS2 bbp.doNotFragment = false; bbp.pollingThreadPriority = 0; bbp.eventHandler = eventHandler; - RNS2BindResult br = ((RNS2_Berkley *) r2)->Bind(&bbp, _FILE_AND_LINE_); + RNS2BindResult br = ((RNS2_Berkley *) r2)->Bind(&bbp); if (br == BR_FAILED_TO_BIND_SOCKET || br == BR_FAMILY_NOT_SUPPORTED) { diff --git a/Source/src/nat_type_detection_server.cpp b/Source/src/nat_type_detection_server.cpp index 62d937daf..7c0ab2a56 100644 --- a/Source/src/nat_type_detection_server.cpp +++ b/Source/src/nat_type_detection_server.cpp @@ -46,7 +46,7 @@ void NatTypeDetectionServer::Startup( const char *nonRakNetIP3, const char *nonRakNetIP4) { - DataStructures::List sockets; + std::vector sockets; rakPeerInterface->GetSockets(sockets); char str[64]; sockets[0]->GetBoundAddress().ToString(false, str, static_cast(64)); @@ -170,7 +170,7 @@ void NatTypeDetectionServer::Update(void) } } - DeallocRNS2RecvStruct(recvStruct, _FILE_AND_LINE_); + DeallocRNS2RecvStruct(recvStruct); bufferedPacketsMutex.Lock(); if (bufferedPackets.Size() > 0) recvStruct = bufferedPackets.Pop(); @@ -266,7 +266,7 @@ void NatTypeDetectionServer::Update(void) bsp.data = (char *) &c; bsp.length = 1; bsp.systemAddress = saOut; - s4p5->Send(&bsp, _FILE_AND_LINE_); + s4p5->Send(&bsp); break; case STATE_TESTING_FULL_CONE_1: case STATE_TESTING_FULL_CONE_2: @@ -284,7 +284,7 @@ void NatTypeDetectionServer::Update(void) bsp.data = (char *) bs.GetData(); bsp.length = bs.GetNumberOfBytesUsed(); bsp.systemAddress = saOut; - s2p3->Send(&bsp, _FILE_AND_LINE_); + s2p3->Send(&bsp); break; case STATE_TESTING_ADDRESS_RESTRICTED_1: case STATE_TESTING_ADDRESS_RESTRICTED_2: @@ -302,7 +302,7 @@ void NatTypeDetectionServer::Update(void) bsp.data = (char *) bs.GetData(); bsp.length = bs.GetNumberOfBytesUsed(); bsp.systemAddress = saOut; - s1p2->Send(&bsp, _FILE_AND_LINE_); + s1p2->Send(&bsp); break; case STATE_TESTING_PORT_RESTRICTED_1: case STATE_TESTING_PORT_RESTRICTED_2: @@ -402,12 +402,12 @@ unsigned int NatTypeDetectionServer::GetDetectionAttemptIndex(RakNetGUID guid) } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void NatTypeDetectionServer::DeallocRNS2RecvStruct(RNS2RecvStruct *s, [[maybe_unused]] const char *file, [[maybe_unused]] unsigned int line) +void NatTypeDetectionServer::DeallocRNS2RecvStruct(RNS2RecvStruct *s) { delete s; } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RNS2RecvStruct *NatTypeDetectionServer::AllocRNS2RecvStruct([[maybe_unused]] const char *file, [[maybe_unused]] unsigned int line) +RNS2RecvStruct *NatTypeDetectionServer::AllocRNS2RecvStruct() { return new RNS2RecvStruct; } diff --git a/Source/src/network_id_manager.cpp b/Source/src/network_id_manager.cpp index ff6304173..c92f1eb7f 100644 --- a/Source/src/network_id_manager.cpp +++ b/Source/src/network_id_manager.cpp @@ -24,6 +24,7 @@ #include "mafianet/sleep.h" #include "mafianet/super_fast_hash.h" #include "mafianet/peer_interface.h" +#include using namespace MafiaNet; diff --git a/Source/src/peer.cpp b/Source/src/peer.cpp index 8acba2bc0..1506c8ed0 100644 --- a/Source/src/peer.cpp +++ b/Source/src/peer.cpp @@ -42,12 +42,14 @@ #include // toupper #include #include "mafianet/get_time.h" +#include #include #include #include #include #include "mafianet/message_identifiers.h" #include "mafianet/mtu_black_hole.h" +#include "mafianet/vector_util.h" #include "mafianet/ds_huffman_encoding_tree.h" #include "mafianet/rand.h" #include "mafianet/plugin_interface2.h" @@ -193,6 +195,17 @@ static bool ResolveOneShotTarget(const char *host, unsigned short port, const Ra return true; } +// A message ReliabilityLayer::Receive handed over: a std::malloc'd buffer that RakPeer now owns. It is either +// passed on to a Packet (AllocPacket takes the same malloc'd buffer, freed by DeallocatePacket) or freed here. +namespace +{ +struct ReceivedMessageFree +{ + void operator()(unsigned char *bytes) const { std::free(bytes); } +}; +using ReceivedMessage = std::unique_ptr; +} // namespace + // Whether a message a connected remote sent, and that RakPeer did not consume itself, may be handed to // the plugins and the application. // @@ -260,7 +273,7 @@ struct PacketFollowedByData unsigned char data[1]; }; -Packet *RakPeer::AllocPacket(unsigned dataSize, const char *file, unsigned int line) +Packet *RakPeer::AllocPacket(unsigned dataSize) { // Crashes when dataSize is 4 bytes - not sure why // unsigned char *data = (unsigned char *) std::malloc(sizeof(PacketFollowedByData)+dataSize); @@ -272,12 +285,8 @@ Packet *RakPeer::AllocPacket(unsigned dataSize, const char *file, unsigned int l // p->guid=UNASSIGNED_RAKNET_GUID; // return p; - MafiaNet::Packet *p; - packetAllocationPoolMutex.Lock(); - p = packetAllocationPool.Allocate(file, line); + MafiaNet::Packet *p = new Packet; ++packetsOutstanding; - packetAllocationPoolMutex.Unlock(); - p = new ((void *) p) Packet; p->data = (unsigned char *) std::malloc(dataSize); p->length = dataSize; p->bitSize = BYTES_TO_BITS(dataSize); @@ -287,16 +296,10 @@ Packet *RakPeer::AllocPacket(unsigned dataSize, const char *file, unsigned int l return p; } -Packet *RakPeer::AllocPacket(unsigned dataSize, unsigned char *data, const char *file, unsigned int line) +Packet *RakPeer::AllocPacket(unsigned dataSize, unsigned char *data) { - // Packet *p = (Packet *)std::malloc(sizeof(Packet)); - MafiaNet::Packet *p; - packetAllocationPoolMutex.Lock(); - p = packetAllocationPool.Allocate(file, line); + MafiaNet::Packet *p = new Packet; ++packetsOutstanding; - packetAllocationPoolMutex.Unlock(); - p = new ((void *) p) Packet; - RakAssert(p); p->data = data; p->length = dataSize; p->bitSize = BYTES_TO_BITS(dataSize); @@ -374,16 +377,9 @@ RakPeer::RakPeer() _extraPingVariance = 0; #endif - bufferedCommands.SetPageSize(sizeof(BufferedCommandStruct) * 16); - socketQueryOutput.SetPageSize(sizeof(SocketQueryOutput) * 8); - - packetAllocationPoolMutex.Lock(); - packetAllocationPool.SetPageSize(sizeof(DataStructures::MemoryPool::MemoryWithPage) * 32); packetsOutstanding = 0; - packetAllocationPoolMutex.Unlock(); shutdownGeneration = 0; - - remoteSystemIndexPool.SetPageSize(sizeof(DataStructures::MemoryPool::MemoryWithPage) * 32); + remoteSystemLookupSize = 0; liveRemoteSystemIndexEntries = 0; @@ -413,9 +409,9 @@ RakPeer::~RakPeer() quitAndDataEvents.CloseEvent(); - // for (unsigned int i=0; i < pluginListTS.Size(); i++) + // for (unsigned int i=0; i < pluginListTS.size(); i++) // pluginListTS[i]->SetRakPeerInterface(0); - // for (unsigned int i=0; i < pluginListNTS.Size(); i++) + // for (unsigned int i=0; i < pluginListNTS.size(); i++) // pluginListNTS[i]->SetRakPeerInterface(0); } @@ -531,7 +527,7 @@ StartupResult RakPeer::Startup(unsigned int maxConnections, SocketDescriptor *so bbp.doNotFragment = false; bbp.pollingThreadPriority = threadPriority; bbp.eventHandler = this; - RNS2BindResult br = ((RNS2_Berkley *) r2)->Bind(&bbp, _FILE_AND_LINE_); + RNS2BindResult br = ((RNS2_Berkley *) r2)->Bind(&bbp); if (br == BR_FAMILY_NOT_SUPPORTED) { @@ -562,7 +558,7 @@ StartupResult RakPeer::Startup(unsigned int maxConnections, SocketDescriptor *so r2->SetRecvEventHandler(this); } - socketList.Push(r2, _FILE_AND_LINE_); + socketList.push_back(r2); } for (i = 0; i < socketDescriptorCount; i++) @@ -604,7 +600,8 @@ StartupResult RakPeer::Startup(unsigned int maxConnections, SocketDescriptor *so //remoteSystemList = new RemoteSystemStruct[ remoteSystemListSize ]; remoteSystemList = new RemoteSystemStruct[maximumNumberOfPeers]; - remoteSystemLookup = new RemoteSystemIndex *[(unsigned int) maximumNumberOfPeers * REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE]; + remoteSystemLookupSize = (unsigned int) maximumNumberOfPeers * REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE; + remoteSystemLookup = new RemoteSystemIndex *[remoteSystemLookupSize]; activeSystemList = new RemoteSystemStruct *[maximumNumberOfPeers]; @@ -642,7 +639,7 @@ StartupResult RakPeer::Startup(unsigned int maxConnections, SocketDescriptor *so activeSystemList[i] = &remoteSystemList[i]; } - for (i = 0; i < (unsigned int) maximumNumberOfPeers * REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE; i++) + for (i = 0; i < remoteSystemLookupSize; i++) { remoteSystemLookup[i] = 0; } @@ -717,12 +714,12 @@ StartupResult RakPeer::Startup(unsigned int maxConnections, SocketDescriptor *so #endif // RAKPEER_USER_THREADED!=1 } - for (i = 0; i < pluginListTS.Size(); i++) + for (i = 0; i < pluginListTS.size(); i++) { pluginListTS[i]->OnRakPeerStartup(); } - for (i = 0; i < pluginListNTS.Size(); i++) + for (i = 0; i < pluginListNTS.size(); i++) { pluginListNTS[i]->OnRakPeerStartup(); } @@ -829,7 +826,7 @@ bool RakPeer::GetPendingHandshakeNonceForTesting(const SystemAddress &remote, ui { bool found = false; requestedConnectionQueueMutex.Lock(); - for (unsigned i = 0; i < requestedConnectionQueue.Size(); i++) + for (unsigned i = 0; i < requestedConnectionQueue.size(); i++) { const RequestedConnectionStruct *rcs = requestedConnectionQueue[i]; if (rcs->systemAddress == remote && !rcs->cancelled) @@ -893,10 +890,10 @@ unsigned int RakPeer::GetMaximumIncomingConnections(void) const // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- unsigned short RakPeer::NumberOfConnections(void) const { - DataStructures::List addresses; - DataStructures::List guids; + std::vector addresses; + std::vector guids; GetSystemList(addresses, guids); - return (unsigned short) addresses.Size(); + return (unsigned short) addresses.size(); } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -964,7 +961,7 @@ void RakPeer::GetIncomingPassword(char *passwordData, int *passwordDataLength) ConnectionAttemptResult RakPeer::Connect(const char *host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, MafiaNet::TimeMS timeoutTime) { // If endThreads is true here you didn't call Startup() first. - if (host == 0 || endThreads || connectionSocketIndex >= socketList.Size()) + if (host == 0 || endThreads || connectionSocketIndex >= socketList.size()) return INVALID_PARAMETER; RakAssert(remotePort != 0); @@ -1068,11 +1065,11 @@ void RakPeer::Shutdown(unsigned int blockDuration, unsigned char orderingChannel // offering them messages there first, so no hook is mid-call or starts after this point. SetNetworkThreadReceiveStopped(true); - for (i = 0; i < pluginListTS.Size(); i++) + for (i = 0; i < pluginListTS.size(); i++) { pluginListTS[i]->OnRakPeerShutdown(); } - for (i = 0; i < pluginListNTS.Size(); i++) + for (i = 0; i < pluginListNTS.size(); i++) { pluginListNTS[i]->OnRakPeerShutdown(); } @@ -1089,7 +1086,7 @@ void RakPeer::Shutdown(unsigned int blockDuration, unsigned char orderingChannel // MafiaNet::TimeMS timeout; #if RAKPEER_USER_THREADED != 1 - for (i = 0; i < socketList.Size(); i++) + for (i = 0; i < socketList.size(); i++) { if (socketList[i]->IsBerkleySocket()) { @@ -1099,7 +1096,7 @@ void RakPeer::Shutdown(unsigned int blockDuration, unsigned char orderingChannel /* // Get recvfrom to unblock - for (i=0; i < socketList.Size(); i++) + for (i=0; i < socketList.size(); i++) { if (SocketLayer::SendTo(socketList[i], (const char*) &i,1,socketList[i]->GetBoundAddress(), _FILE_AND_LINE_)!=0) break; @@ -1123,7 +1120,7 @@ void RakPeer::Shutdown(unsigned int blockDuration, unsigned char orderingChannel while ( isRecvFromLoopThreadActive.GetValue()>0 && MafiaNet::GetTimeMS()GetBoundAddress(), _FILE_AND_LINE_); } @@ -1132,7 +1129,7 @@ void RakPeer::Shutdown(unsigned int blockDuration, unsigned char orderingChannel } */ - for (i = 0; i < socketList.Size(); i++) + for (i = 0; i < socketList.size(); i++) { if (socketList[i]->IsBerkleySocket()) { @@ -1168,17 +1165,13 @@ void RakPeer::Shutdown(unsigned int blockDuration, unsigned char orderingChannel // Free any packets the user didn't deallocate packetReturnMutex.Lock(); - for (i = 0; i < packetReturnQueue.Size(); i++) - DeallocatePacket(packetReturnQueue[i]); - packetReturnQueue.Clear(_FILE_AND_LINE_); + for (Packet *queued : packetReturnQueue) + DeallocatePacket(queued); + packetReturnQueue.clear(); packetReturnMutex.Unlock(); - // Packets still out live in the pool's pages: a Shutdown() from inside Receive's dispatch, - // or from code handling a packet Receive returned, must not free the packet under it. The - // pages are reused by the next session and freed by a later Shutdown() or the destructor. - packetAllocationPoolMutex.Lock(); - if (packetsOutstanding == 0) - packetAllocationPool.Clear(_FILE_AND_LINE_); - packetAllocationPoolMutex.Unlock(); + // Packets still out (a Shutdown() from inside Receive's dispatch, or from code handling a + // packet Receive returned) are individually allocated, so they stay valid until + // DeallocatePacket frees them. /* if (isRecvFromLoopThreadActive.GetValue()>0) @@ -1247,19 +1240,19 @@ bool RakPeer::GetConnectionList(SystemAddress *remoteSystems, unsigned short *nu return false; } - DataStructures::List addresses; - DataStructures::List guids; + std::vector addresses; + std::vector guids; GetSystemList(addresses, guids); if (remoteSystems) { unsigned short i; - for (i = 0; i < *numberOfSystems && i < addresses.Size(); i++) + for (i = 0; i < *numberOfSystems && i < addresses.size(); i++) remoteSystems[i] = addresses[i]; *numberOfSystems = i; } else { - *numberOfSystems = (unsigned short) addresses.Size(); + *numberOfSystems = (unsigned short) addresses.size(); } return true; } @@ -1375,7 +1368,7 @@ void RakPeer::SendLoopback(const char *data, const int length) if (data == 0 || length < 0) return; - Packet *packet = AllocPacket(length, _FILE_AND_LINE_); + Packet *packet = AllocPacket(length); memcpy(packet->data, data, length); packet->systemAddress = GetLoopbackAddress(); packet->guid = myGuid; @@ -1514,7 +1507,7 @@ Packet *RakPeer::Receive(void) /* #if RAKPEER_SINGLE_THREADED==1 RakPeer::RecvFromStruct *recvFromStruct; - for (i=0; i < socketList.Size(); i++) + for (i=0; i < socketList.size(); i++) { for(;;) { @@ -1541,11 +1534,11 @@ Packet *RakPeer::Receive(void) #endif */ - for (i = 0; i < pluginListTS.Size(); i++) + for (i = 0; i < pluginListTS.size(); i++) { pluginListTS[i]->Update(); } - for (i = 0; i < pluginListNTS.Size(); i++) + for (i = 0; i < pluginListNTS.size(); i++) { pluginListNTS[i]->Update(); } @@ -1553,10 +1546,13 @@ Packet *RakPeer::Receive(void) do { packetReturnMutex.Lock(); - if (packetReturnQueue.IsEmpty()) + if (packetReturnQueue.empty()) packet = 0; else - packet = packetReturnQueue.Pop(); + { + packet = packetReturnQueue.front(); + packetReturnQueue.pop_front(); + } packetReturnMutex.Unlock(); if (packet == 0) return 0; @@ -1590,7 +1586,7 @@ Packet *RakPeer::Receive(void) return 0; } - for (i = 0; i < pluginListTS.Size(); i++) + for (i = 0; i < pluginListTS.size(); i++) { pluginResult = pluginListTS[i]->OnReceive(packet); if (shutdownGeneration != generation) @@ -1612,7 +1608,7 @@ Packet *RakPeer::Receive(void) } } - for (i = 0; i < pluginListNTS.Size(); i++) + for (i = 0; i < pluginListNTS.size(); i++) { pluginResult = pluginListNTS[i]->OnReceive(packet); if (shutdownGeneration != generation) @@ -1655,11 +1651,8 @@ void RakPeer::DeallocatePacket(Packet *packet) if (packet->deleteData) { std::free(packet->data); - packet->~Packet(); - packetAllocationPoolMutex.Lock(); - packetAllocationPool.Release(packet, _FILE_AND_LINE_); + delete packet; --packetsOutstanding; - packetAllocationPoolMutex.Unlock(); } else { @@ -1712,7 +1705,7 @@ void RakPeer::CloseConnection(const AddressOrGUID target, bool sendDisconnection // the BCS_CLOSE_CONNECTION path below. With no socket at all there is nothing to // close, so bail out. RakNetSocket2 *closeSocket = (remoteSystemListIndex != -1) ? remoteSystemList[remoteSystemListIndex].rakNetSocket : nullptr; - if (closeSocket == nullptr && socketList.Size() > 0) + if (closeSocket == nullptr && socketList.size() > 0) closeSocket = socketList[0]; if (closeSocket == nullptr) return; @@ -1721,7 +1714,7 @@ void RakPeer::CloseConnection(const AddressOrGUID target, bool sendDisconnection // 12/14/09 Return ID_CONNECTION_LOST when calling CloseConnection with sendDisconnectionNotification==false, elsewise it is never returned if (sendDisconnectionNotification == false && GetConnectionState(target) == IS_CONNECTED) { - Packet *packet = AllocPacket(sizeof(char), _FILE_AND_LINE_); + Packet *packet = AllocPacket(sizeof(char)); packet->data[0] = ID_CONNECTION_LOST; // DeadConnection packet->guid = target.rakNetGuid == UNASSIGNED_RAKNET_GUID ? GetGuidFromSystemAddress(target.systemAddress) : target.rakNetGuid; packet->systemAddress = address; @@ -1748,7 +1741,7 @@ void RakPeer::CancelConnectionAttempt(const SystemAddress target) // with the queue unlocked, so nothing is freed here: the requests are only // flagged, and the network thread drops them (see RunUpdateCycle). requestedConnectionQueueMutex.Lock(); - for (i = 0; i < requestedConnectionQueue.Size(); i++) + for (i = 0; i < requestedConnectionQueue.size(); i++) { RequestedConnectionStruct *rcs = requestedConnectionQueue[i]; if (RequestMatches(rcs, target)) @@ -1756,7 +1749,7 @@ void RakPeer::CancelConnectionAttempt(const SystemAddress target) rcs->cancelled = true; if (rcs->raceGroup != 0) { - for (unsigned int k = 0; k < requestedConnectionQueue.Size(); k++) + for (unsigned int k = 0; k < requestedConnectionQueue.size(); k++) { if (requestedConnectionQueue[k]->raceGroup == rcs->raceGroup) requestedConnectionQueue[k]->cancelled = true; @@ -1794,11 +1787,11 @@ void RakPeer::DeleteRequestedConnection(RequestedConnectionStruct *rcs) void RakPeer::RemoveRequestedConnection_NoLock(const RequestedConnectionStruct *rcs) { - for (unsigned int i = 0; i < requestedConnectionQueue.Size(); i++) + for (unsigned int i = 0; i < requestedConnectionQueue.size(); i++) { if (requestedConnectionQueue[i] == rcs) { - requestedConnectionQueue.RemoveAtIndex(i); + requestedConnectionQueue.erase(requestedConnectionQueue.begin() + i); return; } } @@ -1809,12 +1802,12 @@ void RakPeer::CancelRaceSiblings_NoLock(const RequestedConnectionStruct *keep) if (keep->raceGroup == 0) return; unsigned int i = 0; - while (i < requestedConnectionQueue.Size()) + while (i < requestedConnectionQueue.size()) { RequestedConnectionStruct *rcs = requestedConnectionQueue[i]; if (rcs != keep && rcs->raceGroup == keep->raceGroup) { - requestedConnectionQueue.RemoveAtIndex(i); + requestedConnectionQueue.erase(requestedConnectionQueue.begin() + i); DeleteRequestedConnection(rcs); } else @@ -1826,7 +1819,7 @@ bool RakPeer::DropConnectionAttemptSupersededBy(const SystemAddress &address) { RequestedConnectionStruct *superseded = nullptr; requestedConnectionQueueMutex.Lock(); - for (unsigned int i = 0; i < requestedConnectionQueue.Size(); i++) + for (unsigned int i = 0; i < requestedConnectionQueue.size(); i++) { RequestedConnectionStruct *rcs = requestedConnectionQueue[i]; if (rcs->systemAddress == address && !rcs->cancelled && rcs->actionToTake == RequestedConnectionStruct::CONNECT) @@ -1856,7 +1849,7 @@ bool RakPeer::AdvanceRace_NoLock(uint32_t raceGroup, MafiaNet::Time now) bool anyLeft = false; bool anyUnstarted = false; MafiaNet::Time earliest = 0; - for (unsigned int i = 0; i < requestedConnectionQueue.Size(); i++) + for (unsigned int i = 0; i < requestedConnectionQueue.size(); i++) { const RequestedConnectionStruct *rcs = requestedConnectionQueue[i]; if (rcs->raceGroup != raceGroup || rcs->cancelled) @@ -1872,7 +1865,7 @@ bool RakPeer::AdvanceRace_NoLock(uint32_t raceGroup, MafiaNet::Time now) if (anyUnstarted && earliest > now) { const MafiaNet::Time shift = earliest - now; - for (unsigned int i = 0; i < requestedConnectionQueue.Size(); i++) + for (unsigned int i = 0; i < requestedConnectionQueue.size(); i++) { RequestedConnectionStruct *rcs = requestedConnectionQueue[i]; if (rcs->raceGroup == raceGroup && rcs->requestsMade == 0) @@ -1890,7 +1883,7 @@ ConnectionState RakPeer::GetConnectionState(const AddressOrGUID systemIdentifier { unsigned int i = 0; requestedConnectionQueueMutex.Lock(); - for (; i < requestedConnectionQueue.Size(); i++) + for (; i < requestedConnectionQueue.size(); i++) { if (RequestMatches(requestedConnectionQueue[i], systemIdentifier.systemAddress)) { @@ -2014,10 +2007,10 @@ RakNetGUID RakPeer::GetGUIDFromIndex(unsigned int index) // \param[out] addresses All system addresses. Size of the list is the number of connections. Size of the list will match the size of the \a guids list. // \param[out] guids All guids. Size of the list is the number of connections. Size of the list will match the size of the \a addresses list. // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::GetSystemList(DataStructures::List &addresses, DataStructures::List &guids) const +void RakPeer::GetSystemList(std::vector &addresses, std::vector &guids) const { - addresses.Clear(false, _FILE_AND_LINE_); - guids.Clear(false, _FILE_AND_LINE_); + addresses.clear(); + guids.clear(); if (remoteSystemList == 0 || endThreads == true) return; @@ -2028,8 +2021,8 @@ void RakPeer::GetSystemList(DataStructures::List &addresses, Data if ((activeSystemList[i])->isActive && (activeSystemList[i])->connectMode == RakPeer::RemoteSystemStruct::CONNECTED) { - addresses.Push((activeSystemList[i])->systemAddress, _FILE_AND_LINE_); - guids.Push((activeSystemList[i])->guid, _FILE_AND_LINE_); + addresses.push_back((activeSystemList[i])->systemAddress); + guids.push_back((activeSystemList[i])->guid); } } } @@ -2056,7 +2049,7 @@ void RakPeer::AddToBanList(const char *IP, MafiaNet::TimeMS milliseconds) banListMutex.Lock(); - for (; index < banList.Size(); index++) + for (; index < banList.size(); index++) { if (strcmp(IP, banList[index]->IP) == 0) { @@ -2080,7 +2073,7 @@ void RakPeer::AddToBanList(const char *IP, MafiaNet::TimeMS milliseconds) banStruct->timeout = time + milliseconds; strcpy_s(banStruct->IP, 16, IP); banListMutex.Lock(); - banList.Insert(banStruct, _FILE_AND_LINE_); + banList.push_back(banStruct); banListMutex.Unlock(); } @@ -2105,13 +2098,12 @@ void RakPeer::RemoveFromBanList(const char *IP) banListMutex.Lock(); - for (; index < banList.Size(); index++) + for (; index < banList.size(); index++) { if (strcmp(IP, banList[index]->IP) == 0) { temp = banList[index]; - banList[index] = banList[banList.Size() - 1]; - banList.RemoveAtIndex(banList.Size() - 1); + SwapRemove(banList, index); break; } } @@ -2135,13 +2127,13 @@ void RakPeer::ClearBanList(void) index = 0; banListMutex.Lock(); - for (; index < banList.Size(); index++) + for (; index < banList.size(); index++) { std::free(banList[index]->IP); delete banList[index]; } - banList.Clear(false, _FILE_AND_LINE_); + banList.clear(); banListMutex.Unlock(); } @@ -2173,21 +2165,20 @@ bool RakPeer::IsBanned(const char *IP) banListIndex = 0; - if (banList.Size() == 0) + if (banList.size() == 0) return false; // Skip the mutex if possible time = MafiaNet::GetTimeMS(); banListMutex.Lock(); - while (banListIndex < banList.Size()) + while (banListIndex < banList.size()) { if (banList[banListIndex]->timeout > 0 && banList[banListIndex]->timeout < time) { // Delete expired ban temp = banList[banListIndex]; - banList[banListIndex] = banList[banList.Size() - 1]; - banList.RemoveAtIndex(banList.Size() - 1); + SwapRemove(banList, banListIndex); std::free(temp->IP); delete temp; } @@ -2274,7 +2265,7 @@ bool RakPeer::Ping(const char *host, unsigned short remotePort, bool onlyReplyOn return false; // If this assert hits then Startup wasn't called or the call failed. - RakAssert(connectionSocketIndex < socketList.Size()); + RakAssert(connectionSocketIndex < socketList.size()); // if ( IsActive() == false ) // return; @@ -2300,7 +2291,7 @@ bool RakPeer::Ping(const char *host, unsigned short remotePort, bool onlyReplyOn systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); unsigned i; - for (i=0; i < pluginListNTS.Size(); i++) + for (i=0; i < pluginListNTS.size(); i++) pluginListNTS[i]->OnDirectSocketSend((const char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), systemAddress); SocketLayer::SendTo( socketList[realIndex], (const char*)bitStream.GetData(), (int) bitStream.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); */ @@ -2312,9 +2303,9 @@ bool RakPeer::Ping(const char *host, unsigned short remotePort, bool onlyReplyOn bsp.systemAddress == UNASSIGNED_SYSTEM_ADDRESS) return false; unsigned i; - for (i = 0; i < pluginListNTS.Size(); i++) + for (i = 0; i < pluginListNTS.size(); i++) pluginListNTS[i]->OnDirectSocketSend((const char *) bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), bsp.systemAddress); - socketList[realIndex]->Send(&bsp, _FILE_AND_LINE_); + socketList[realIndex]->Send(&bsp); return true; } @@ -2580,8 +2571,7 @@ void RakPeer::QueueSessionDecision(const AddressOrGUID systemIdentifier, bool ac // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::QueueSessionCommand(const AddressOrGUID systemIdentifier, int command, const char *data, unsigned int length) { - BufferedCommandStruct *bcs; - bcs = bufferedCommands.Allocate(_FILE_AND_LINE_); + auto bcs = std::make_unique(); bcs->command = (decltype(bcs->command)) command; bcs->systemIdentifier = systemIdentifier; bcs->numberOfBitsToSend = BYTES_TO_BITS(length); @@ -2599,7 +2589,7 @@ void RakPeer::QueueSessionCommand(const AddressOrGUID systemIdentifier, int comm else bcs->numberOfBitsToSend = 0; } - bufferedCommands.Push(bcs); + bufferedCommands.Push(std::move(bcs)); quitAndDataEvents.SetEvent(); } @@ -2689,9 +2679,9 @@ const RakNetGUID RakPeer::GetMyGUID(void) const // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SystemAddress RakPeer::GetMyBoundAddress(const int socketIndex) { - DataStructures::List sockets; + std::vector sockets; GetSockets(sockets); - if (sockets.Size() > 0) + if (sockets.size() > 0) return sockets[socketIndex]->GetBoundAddress(); else return UNASSIGNED_SYSTEM_ADDRESS; @@ -2991,9 +2981,9 @@ void RakPeer::SendTTL(const char *host, unsigned short remotePort, int ttl, unsi return; bsp.ttl = ttl; unsigned i; - for (i = 0; i < pluginListNTS.Size(); i++) + for (i = 0; i < pluginListNTS.size(); i++) pluginListNTS[i]->OnDirectSocketSend((const char *) bsp.data, BYTES_TO_BITS(bsp.length), bsp.systemAddress); - socketList[realIndex]->Send(&bsp, _FILE_AND_LINE_); + socketList[realIndex]->Send(&bsp); } } @@ -3011,26 +3001,26 @@ void RakPeer::AttachPlugin(PluginInterface2 *plugin) bool isNotThreadsafe = plugin->UsesReliabilityLayer(); if (isNotThreadsafe) { - if (pluginListNTS.GetIndexOf(plugin) == MAX_UNSIGNED_LONG) + if (std::find(pluginListNTS.begin(), pluginListNTS.end(), plugin) == pluginListNTS.end()) { plugin->SetRakPeerInterface(this); plugin->OnAttach(); - pluginListNTS.Insert(plugin, _FILE_AND_LINE_); + pluginListNTS.push_back(plugin); } } else { - if (pluginListTS.GetIndexOf(plugin) == MAX_UNSIGNED_LONG) + if (std::find(pluginListTS.begin(), pluginListTS.end(), plugin) == pluginListTS.end()) { plugin->SetRakPeerInterface(this); plugin->OnAttach(); - pluginListTS.Insert(plugin, _FILE_AND_LINE_); + pluginListTS.push_back(plugin); } } pluginListNetworkThreadMutex.Lock(); - if (pluginListNetworkThread.GetIndexOf(plugin) == MAX_UNSIGNED_LONG) - pluginListNetworkThread.Insert(plugin, _FILE_AND_LINE_); + if (std::find(pluginListNetworkThread.begin(), pluginListNetworkThread.end(), plugin) == pluginListNetworkThread.end()) + pluginListNetworkThread.push_back(plugin); pluginListNetworkThreadMutex.Unlock(); } @@ -3044,38 +3034,31 @@ void RakPeer::DetachPlugin(PluginInterface2 *plugin) if (plugin == 0 || IsInsideNetworkThreadReceive()) return; - unsigned int index; - bool isNotThreadsafe = plugin->UsesReliabilityLayer(); if (isNotThreadsafe) { - index = pluginListNTS.GetIndexOf(plugin); - if (index != MAX_UNSIGNED_LONG) + const auto it = std::find(pluginListNTS.begin(), pluginListNTS.end(), plugin); + if (it != pluginListNTS.end()) { // Unordered list so delete from end for speed - pluginListNTS[index] = pluginListNTS[pluginListNTS.Size() - 1]; - pluginListNTS.RemoveFromEnd(); + SwapRemove(pluginListNTS, (std::size_t)(it - pluginListNTS.begin())); } } else { - index = pluginListTS.GetIndexOf(plugin); - if (index != MAX_UNSIGNED_LONG) + const auto it = std::find(pluginListTS.begin(), pluginListTS.end(), plugin); + if (it != pluginListTS.end()) { // Unordered list so delete from end for speed - pluginListTS[index] = pluginListTS[pluginListTS.Size() - 1]; - pluginListTS.RemoveFromEnd(); + SwapRemove(pluginListTS, (std::size_t)(it - pluginListTS.begin())); } } // Removed under the lock the update thread dispatches under, so once this returns the plugin // is not inside OnReceiveNetworkThread and will not be called again. pluginListNetworkThreadMutex.Lock(); - index = pluginListNetworkThread.GetIndexOf(plugin); - if (index != MAX_UNSIGNED_LONG) - { - pluginListNetworkThread[index] = pluginListNetworkThread[pluginListNetworkThread.Size() - 1]; - pluginListNetworkThread.RemoveFromEnd(); - } + const auto networkThreadIt = std::find(pluginListNetworkThread.begin(), pluginListNetworkThread.end(), plugin); + if (networkThreadIt != pluginListNetworkThread.end()) + SwapRemove(pluginListNetworkThread, (std::size_t)(networkThreadIt - pluginListNetworkThread.begin())); pluginListNetworkThreadMutex.Unlock(); plugin->OnDetach(); @@ -3094,16 +3077,16 @@ void RakPeer::PushBackPacket(Packet *packet, bool pushAtHead) return; unsigned i; - for (i = 0; i < pluginListTS.Size(); i++) + for (i = 0; i < pluginListTS.size(); i++) pluginListTS[i]->OnPushBackPacket((const char *) packet->data, packet->bitSize, packet->systemAddress); - for (i = 0; i < pluginListNTS.Size(); i++) + for (i = 0; i < pluginListNTS.size(); i++) pluginListNTS[i]->OnPushBackPacket((const char *) packet->data, packet->bitSize, packet->systemAddress); packetReturnMutex.Lock(); if (pushAtHead) - packetReturnQueue.PushAtHead(packet, 0, _FILE_AND_LINE_); + packetReturnQueue.push_front(packet); else - packetReturnQueue.Push(packet, _FILE_AND_LINE_); + packetReturnQueue.push_back(packet); packetReturnCondition.notify_all(); packetReturnMutex.Unlock(); } @@ -3111,35 +3094,28 @@ void RakPeer::PushBackPacket(Packet *packet, bool pushAtHead) // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::ChangeSystemAddress(RakNetGUID guid, const SystemAddress &systemAddress) { - BufferedCommandStruct *bcs; - - bcs = bufferedCommands.Allocate(_FILE_AND_LINE_); - bcs->data = 0; + auto bcs = std::make_unique(); bcs->systemIdentifier.systemAddress = systemAddress; bcs->systemIdentifier.rakNetGuid = guid; bcs->command = BufferedCommandStruct::BCS_CHANGE_SYSTEM_ADDRESS; - bufferedCommands.Push(bcs); + bufferedCommands.Push(std::move(bcs)); } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Packet *RakPeer::AllocatePacket(unsigned dataSize) { - return AllocPacket(dataSize, _FILE_AND_LINE_); + return AllocPacket(dataSize); } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- RakNetSocket2 *RakPeer::GetSocket(const SystemAddress target) { // Send a query to the thread to get the socket, and return when we got it - BufferedCommandStruct *bcs; - bcs = bufferedCommands.Allocate(_FILE_AND_LINE_); + auto bcs = std::make_unique(); bcs->command = BufferedCommandStruct::BCS_GET_SOCKET; bcs->systemIdentifier = target; - bcs->data = 0; - bufferedCommands.Push(bcs); + bufferedCommands.Push(std::move(bcs)); // Block up to one second to get the socket, although it should actually take virtually no time - SocketQueryOutput *sqo; MafiaNet::TimeMS stopWaiting = MafiaNet::GetTimeMS() + 1000; - DataStructures::List output; while (manualUpdate || MafiaNet::GetTimeMS() < stopWaiting) { if (manualUpdate) @@ -3149,36 +3125,27 @@ RakNetSocket2 *RakPeer::GetSocket(const SystemAddress target) else RakSleep(0); - sqo = socketQueryOutput.Pop(); - if (sqo) + if (auto sqo = socketQueryOutput.TryPop()) { - output = sqo->sockets; - sqo->sockets.Clear(false, _FILE_AND_LINE_); - socketQueryOutput.Deallocate(sqo, _FILE_AND_LINE_); - if (output.Size()) - return output[0]; + if ((*sqo)->sockets.empty() == false) + return (*sqo)->sockets[0]; break; } } return 0; } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::GetSockets(DataStructures::List &sockets) +void RakPeer::GetSockets(std::vector &sockets) { - sockets.Clear(false, _FILE_AND_LINE_); + sockets.clear(); // Send a query to the thread to get the socket, and return when we got it - BufferedCommandStruct *bcs; - - bcs = bufferedCommands.Allocate(_FILE_AND_LINE_); + auto bcs = std::make_unique(); bcs->command = BufferedCommandStruct::BCS_GET_SOCKET; bcs->systemIdentifier = UNASSIGNED_SYSTEM_ADDRESS; - bcs->data = 0; - bufferedCommands.Push(bcs); + bufferedCommands.Push(std::move(bcs)); // Block up to one second to get the socket, although it should actually take virtually no time - SocketQueryOutput *sqo; - // RakNetSocket2* output; for (;;) { if (manualUpdate) @@ -3188,20 +3155,17 @@ void RakPeer::GetSockets(DataStructures::List &sockets) else RakSleep(0); - sqo = socketQueryOutput.Pop(); - if (sqo) + if (auto sqo = socketQueryOutput.TryPop()) { - sockets = sqo->sockets; - sqo->sockets.Clear(false, _FILE_AND_LINE_); - socketQueryOutput.Deallocate(sqo, _FILE_AND_LINE_); + sockets = (*sqo)->sockets; return; } } return; } -void RakPeer::ReleaseSockets(DataStructures::List &sockets) +void RakPeer::ReleaseSockets(std::vector &sockets) { - sockets.Clear(false, _FILE_AND_LINE_); + sockets.clear(); } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -3302,7 +3266,7 @@ bool RakPeer::SendOutOfBand(const char *host, unsigned short remotePort, const c return false; // If this assert hits then Startup wasn't called or the call failed. - RakAssert(connectionSocketIndex < socketList.Size()); + RakAssert(connectionSocketIndex < socketList.size()); // This is a security measure. Don't send data longer than this value RakAssert(dataLength <= (MAX_OFFLINE_DATA_LENGTH + sizeof(unsigned char) + sizeof(MafiaNet::Time) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID))); @@ -3327,7 +3291,7 @@ bool RakPeer::SendOutOfBand(const char *host, unsigned short remotePort, const c systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); unsigned i; - for (i=0; i < pluginListNTS.Size(); i++) + for (i=0; i < pluginListNTS.size(); i++) pluginListNTS[i]->OnDirectSocketSend((const char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), systemAddress); SocketLayer::SendTo( socketList[realIndex], (const char*)bitStream.GetData(), (int) bitStream.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); @@ -3339,9 +3303,9 @@ bool RakPeer::SendOutOfBand(const char *host, unsigned short remotePort, const c if (!ResolveOneShotTarget(host, remotePort, socketList[realIndex], &bsp.systemAddress)) return false; unsigned i; - for (i = 0; i < pluginListNTS.Size(); i++) + for (i = 0; i < pluginListNTS.size(); i++) pluginListNTS[i]->OnDirectSocketSend((const char *) bsp.data, BYTES_TO_BITS(bsp.length), bsp.systemAddress); - socketList[realIndex]->Send(&bsp, _FILE_AND_LINE_); + socketList[realIndex]->Send(&bsp); return true; } @@ -3392,11 +3356,11 @@ RakNetStatistics *RakPeer::GetStatistics(const SystemAddress systemAddress, RakN return 0; } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::GetStatisticsList(DataStructures::List &addresses, DataStructures::List &guids, DataStructures::List &statistics) +void RakPeer::GetStatisticsList(std::vector &addresses, std::vector &guids, std::vector &statistics) { - addresses.Clear(false, _FILE_AND_LINE_); - guids.Clear(false, _FILE_AND_LINE_); - statistics.Clear(false, _FILE_AND_LINE_); + addresses.clear(); + guids.clear(); + statistics.clear(); if (remoteSystemList == 0 || endThreads == true) return; @@ -3407,11 +3371,11 @@ void RakPeer::GetStatisticsList(DataStructures::List &addresses, if ((activeSystemList[i])->isActive && (activeSystemList[i])->connectMode == RakPeer::RemoteSystemStruct::CONNECTED) { - addresses.Push((activeSystemList[i])->systemAddress, _FILE_AND_LINE_); - guids.Push((activeSystemList[i])->guid, _FILE_AND_LINE_); + addresses.push_back((activeSystemList[i])->systemAddress); + guids.push_back((activeSystemList[i])->guid); RakNetStatistics rns; (activeSystemList[i])->reliabilityLayer.GetStatistics(&rns); - statistics.Push(rns, _FILE_AND_LINE_); + statistics.push_back(rns); } } } @@ -3438,8 +3402,8 @@ bool RakPeer::WaitForPackets(MafiaNet::TimeMS timeoutMs) lock.lock(); packetReturnCondition.wait_for(lock, std::chrono::milliseconds(timeoutMs), [this] - { return packetReturnQueue.IsEmpty() == false || IsActive() == false; }); - const bool packetsWaiting = packetReturnQueue.IsEmpty() == false; + { return packetReturnQueue.empty() == false || IsActive() == false; }); + const bool packetsWaiting = packetReturnQueue.empty() == false; lock.unlock(); return packetsWaiting; } @@ -3448,7 +3412,7 @@ unsigned int RakPeer::GetReceiveBufferSize(void) { unsigned int size; packetReturnMutex.Lock(); - size = packetReturnQueue.Size(); + size = static_cast(packetReturnQueue.size()); // the interface returns unsigned int; the queue never nears 2^32 packets packetReturnMutex.Unlock(); return size; } @@ -3579,7 +3543,7 @@ ConnectionAttemptResult RakPeer::SendConnectionRequest(const char *host, unsigne // Return false if already pending, else push on queue requestedConnectionQueueMutex.Lock(); - for (unsigned int i = 0; i < requestedConnectionQueue.Size(); i++) + for (unsigned int i = 0; i < requestedConnectionQueue.size(); i++) { for (int c = 0; c < candidateCount; c++) { @@ -3623,7 +3587,7 @@ ConnectionAttemptResult RakPeer::SendConnectionRequest(const char *host, unsigne } else randombytes_buf(&requests[c]->handshakeNonce, sizeof(requests[c]->handshakeNonce)); // echoed by REPLY_2, so unpredictable here too - requestedConnectionQueue.Push(requests[c], _FILE_AND_LINE_); + requestedConnectionQueue.push_back(requests[c]); } requestedConnectionQueueMutex.Unlock(); @@ -3669,7 +3633,7 @@ ConnectionAttemptResult RakPeer::SendConnectionRequest(const char *host, unsigne // Return false if already pending, else push on queue unsigned int i = 0; requestedConnectionQueueMutex.Lock(); - for (; i < requestedConnectionQueue.Size(); i++) + for (; i < requestedConnectionQueue.size(); i++) { if (RequestMatches(requestedConnectionQueue[i], systemAddress)) { @@ -3692,7 +3656,7 @@ ConnectionAttemptResult RakPeer::SendConnectionRequest(const char *host, unsigne } else randombytes_buf(&rcs->handshakeNonce, sizeof(rcs->handshakeNonce)); // echoed by REPLY_2, so unpredictable here too - requestedConnectionQueue.Push(rcs, _FILE_AND_LINE_); + requestedConnectionQueue.push_back(rcs); requestedConnectionQueueMutex.Unlock(); return CONNECTION_ATTEMPT_STARTED; @@ -3920,7 +3884,7 @@ void RakPeer::ProduceWithheldConnectionPacket(RemoteSystemStruct *remoteSystem, Packet *packet; if (remoteSystem->withheldConnectionPacketData != 0 && remoteSystem->withheldConnectionPacketLength > 0) { - packet = AllocPacket(remoteSystem->withheldConnectionPacketLength, _FILE_AND_LINE_); + packet = AllocPacket(remoteSystem->withheldConnectionPacketLength); memcpy(packet->data, remoteSystem->withheldConnectionPacketData, remoteSystem->withheldConnectionPacketLength); packet->bitSize = remoteSystem->withheldConnectionPacketLength * 8; std::free(remoteSystem->withheldConnectionPacketData); @@ -3931,7 +3895,7 @@ void RakPeer::ProduceWithheldConnectionPacket(RemoteSystemStruct *remoteSystem, { // The original bytes could not be stashed (allocation failure); the message id alone still tells // the application a connection exists, which is what every caller keys off. - packet = AllocPacket(sizeof(MessageID), _FILE_AND_LINE_); + packet = AllocPacket(sizeof(MessageID)); packet->data[0] = messageId; packet->bitSize = sizeof(MessageID) * 8; } @@ -3946,38 +3910,35 @@ void RakPeer::ProduceWithheldConnectionPacket(RemoteSystemStruct *remoteSystem, // Then what the remote sent while we were still finishing the handshake, in arrival order: right // behind the connection packet, never ahead of it. Delivered the way any later message is, so the // network-thread plugins see each one now, when it reaches the application, not when it arrived. - while (!remoteSystem->heldApplicationMessages.IsEmpty()) + while (!remoteSystem->heldApplicationMessages.empty()) { - const RemoteSystemStruct::HeldApplicationMessage held = remoteSystem->heldApplicationMessages.Pop(); - DeliverApplicationMessage(remoteSystem, held.data, held.bitSize); + RemoteSystemStruct::HeldApplicationMessage held = std::move(remoteSystem->heldApplicationMessages.front()); + remoteSystem->heldApplicationMessages.pop_front(); + DeliverApplicationMessage(remoteSystem, held.data.release(), held.bitSize); } - // Pop() never shrinks the array; a large hold must not keep it for the life of the connection. - remoteSystem->heldApplicationMessages.Clear(_FILE_AND_LINE_); + // clear() hands the deque's blocks back; a large hold must not keep them for the life of the connection. + remoteSystem->heldApplicationMessages.clear(); remoteSystem->heldApplicationBytes = 0; } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- bool RakPeer::HoldApplicationMessage(RemoteSystemStruct *remoteSystem, unsigned char *data, BitSize_t bitSize) { + // Owns `data` from here on: freed when refused, by FreeHeldApplicationMessages(), or released to a Packet. + RemoteSystemStruct::HeldApplicationMessage held; + held.data.reset(data); + held.bitSize = bitSize; const unsigned int byteSize = (unsigned int) BITS_TO_BYTES(bitSize); - if (remoteSystem->heldApplicationMessages.Size() >= (unsigned int) MAXIMUM_HELD_SESSION_MESSAGES || + if (remoteSystem->heldApplicationMessages.size() >= (size_t) MAXIMUM_HELD_SESSION_MESSAGES || byteSize > (unsigned int) MAXIMUM_HELD_SESSION_BYTES - remoteSystem->heldApplicationBytes) - { - std::free(data); return false; - } - RemoteSystemStruct::HeldApplicationMessage held; - held.data = data; - held.bitSize = bitSize; - remoteSystem->heldApplicationMessages.Push(held, _FILE_AND_LINE_); + remoteSystem->heldApplicationMessages.push_back(std::move(held)); remoteSystem->heldApplicationBytes += byteSize; return true; } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::FreeHeldApplicationMessages(RemoteSystemStruct *remoteSystem) { - while (!remoteSystem->heldApplicationMessages.IsEmpty()) - std::free(remoteSystem->heldApplicationMessages.Pop().data); - remoteSystem->heldApplicationMessages.Clear(_FILE_AND_LINE_); + remoteSystem->heldApplicationMessages.clear(); remoteSystem->heldApplicationBytes = 0; } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -3987,7 +3948,7 @@ void RakPeer::FailSessionHandshakeAsConnectingSide(RemoteSystemStruct *remoteSys // Our Connect() is answered here, once: the slot leaves EXCHANGING_SESSION_DATA for a silent close, // which the failure path does not report again. - Packet *packet = AllocPacket(sizeof(MessageID), _FILE_AND_LINE_); + Packet *packet = AllocPacket(sizeof(MessageID)); packet->data[0] = (unsigned char) ID_CONNECTION_ATTEMPT_FAILED; packet->bitSize = sizeof(MessageID) * 8; packet->systemAddress = remoteSystem->systemAddress; @@ -4008,7 +3969,7 @@ void RakPeer::FailSessionHandshakeAsConnectingSide(RemoteSystemStruct *remoteSys // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::DeliverApplicationMessage(RemoteSystemStruct *remoteSystem, unsigned char *data, BitSize_t bitSize) { - Packet *packet = AllocPacket((unsigned int) BITS_TO_BYTES(bitSize), data, _FILE_AND_LINE_); + Packet *packet = AllocPacket((unsigned int) BITS_TO_BYTES(bitSize), data); packet->bitSize = bitSize; packet->systemAddress = remoteSystem->systemAddress; packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; @@ -4026,7 +3987,7 @@ void RakPeer::ProduceSessionAbandoned(RemoteSystemStruct *remoteSystem) // Once only: the flag is what made the request the application's to answer. remoteSystem->sessionConfigAwaitingLocalDecision = false; - Packet *packet = AllocPacket(sizeof(MessageID), _FILE_AND_LINE_); + Packet *packet = AllocPacket(sizeof(MessageID)); packet->data[0] = (unsigned char) ID_SESSION_CONFIG_ABANDONED; packet->bitSize = sizeof(MessageID) * 8; packet->systemAddress = remoteSystem->systemAddress; @@ -4319,9 +4280,9 @@ void RakPeer::ReferenceRemoteSystem(const SystemAddress &sa, unsigned int remote remoteSystemList[remoteSystemListIndex].systemAddress = sa; unsigned int hashIndex = RemoteSystemLookupHashIndex(sa); - // One entry per reference. (A second Allocate on a bucket collision used to overwrite the first, which - // was then never released: one pool entry lost per colliding connection for the life of the peer.) - RemoteSystemIndex *rsi = remoteSystemIndexPool.Allocate(_FILE_AND_LINE_); + // One entry per reference. (A second allocation on a bucket collision used to overwrite the first, which + // was then never freed: one entry lost per colliding connection for the life of the peer.) + RemoteSystemIndex *rsi = new RemoteSystemIndex; ++liveRemoteSystemIndexEntries; rsi->next = 0; rsi->index = remoteSystemListIndex; @@ -4371,7 +4332,7 @@ void RakPeer::DereferenceRemoteSystem(const SystemAddress &sa) { last->next = cur->next; } - remoteSystemIndexPool.Release(cur, _FILE_AND_LINE_); + delete cur; --liveRemoteSystemIndexEntries; break; } @@ -4403,10 +4364,23 @@ RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystem(const SystemAddress &sa) c // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::ClearRemoteSystemLookup(void) { - remoteSystemIndexPool.Clear(_FILE_AND_LINE_); + if (remoteSystemLookup != 0) + { + for (unsigned int bucket = 0; bucket < remoteSystemLookupSize; bucket++) + { + RemoteSystemIndex *cur = remoteSystemLookup[bucket]; + while (cur != 0) + { + RemoteSystemIndex *next = cur->next; + delete cur; + cur = next; + } + } + } liveRemoteSystemIndexEntries = 0; delete[] remoteSystemLookup; remoteSystemLookup = 0; + remoteSystemLookupSize = 0; } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::AddToActiveSystemList(unsigned int remoteSystemListIndex) @@ -4570,19 +4544,20 @@ MafiaNet::TimeMS RakPeer::GetSessionTimeoutFor(RemoteSystemStruct *remoteSystem) return sessionTimeout != 0 ? sessionTimeout : remoteSystem->reliabilityLayer.GetTimeoutTime(); } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line) +void RakPeer::DeallocRNS2RecvStruct(RNS2RecvStruct *s) { bufferedPacketsFreePoolMutex.Lock(); - bufferedPacketsFreePool.Push(s, file, line); + bufferedPacketsFreePool.push_back(s); bufferedPacketsFreePoolMutex.Unlock(); } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RNS2RecvStruct *RakPeer::AllocRNS2RecvStruct([[maybe_unused]] const char *file, [[maybe_unused]] unsigned int line) +RNS2RecvStruct *RakPeer::AllocRNS2RecvStruct() { bufferedPacketsFreePoolMutex.Lock(); - if (bufferedPacketsFreePool.Size() > 0) + if (bufferedPacketsFreePool.empty() == false) { - RNS2RecvStruct *s = bufferedPacketsFreePool.Pop(); + RNS2RecvStruct *s = bufferedPacketsFreePool.back(); + bufferedPacketsFreePool.pop_back(); bufferedPacketsFreePoolMutex.Unlock(); return s; } @@ -4596,13 +4571,15 @@ RNS2RecvStruct *RakPeer::AllocRNS2RecvStruct([[maybe_unused]] const char *file, void RakPeer::ClearBufferedPackets(void) { bufferedPacketsFreePoolMutex.Lock(); - while (bufferedPacketsFreePool.Size() > 0) - delete bufferedPacketsFreePool.Pop(); + for (RNS2RecvStruct *s : bufferedPacketsFreePool) + delete s; + bufferedPacketsFreePool.clear(); bufferedPacketsFreePoolMutex.Unlock(); bufferedPacketsQueueMutex.Lock(); - while (bufferedPacketsQueue.Size() > 0) - delete bufferedPacketsQueue.Pop(); + for (RNS2RecvStruct *s : bufferedPacketsQueue) + delete s; + bufferedPacketsQueue.clear(); bufferedPacketsQueueMutex.Unlock(); } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -4613,16 +4590,17 @@ void RakPeer::SetupBufferedPackets(void) void RakPeer::PushBufferedPacket(RNS2RecvStruct *p) { bufferedPacketsQueueMutex.Lock(); - bufferedPacketsQueue.Push(p, _FILE_AND_LINE_); + bufferedPacketsQueue.push_back(p); bufferedPacketsQueueMutex.Unlock(); } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- RNS2RecvStruct *RakPeer::PopBufferedPacket(void) { bufferedPacketsQueueMutex.Lock(); - if (bufferedPacketsQueue.Size() > 0) + if (bufferedPacketsQueue.empty() == false) { - RNS2RecvStruct *s = bufferedPacketsQueue.Pop(); + RNS2RecvStruct *s = bufferedPacketsQueue.front(); + bufferedPacketsQueue.pop_front(); bufferedPacketsQueueMutex.Unlock(); return s; } @@ -4727,31 +4705,24 @@ void RakPeer::CloseConnectionInternal2(const AddressOrGUID &systemIdentifier, bo } else { - BufferedCommandStruct *bcs; - bcs = bufferedCommands.Allocate(_FILE_AND_LINE_); + auto bcs = std::make_unique(); bcs->command = BufferedCommandStruct::BCS_CLOSE_CONNECTION; bcs->systemIdentifier = target; // #med - review whether this is safe here - can't the socket be closed/released before its used? bcs->socket = &socket; - bcs->data = 0; bcs->orderingChannel = orderingChannel; bcs->priority = disconnectionNotificationPriority; - bufferedCommands.Push(bcs); + bufferedCommands.Push(std::move(bcs)); } } } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::SendBuffered(const char *data, BitSize_t numberOfBitsToSend, MafiaNet::Priority priority, MafiaNet::Reliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt) { - BufferedCommandStruct *bcs; - - bcs = bufferedCommands.Allocate(_FILE_AND_LINE_); + auto bcs = std::make_unique(); bcs->data = (char *) std::malloc((size_t) BITS_TO_BYTES(numberOfBitsToSend)); // Making a copy doesn't lose efficiency because I tell the reliability layer to use this allocation for its own copy if (bcs->data == 0) - { - bufferedCommands.Deallocate(bcs, _FILE_AND_LINE_); return; - } RakAssert(!((unsigned int) reliability >= MafiaNet::NUMBER_OF_RELIABILITIES || (int) reliability < 0)); RakAssert(!((int) priority > (int) MafiaNet::NUMBER_OF_PRIORITIES || (int) priority < 0)); @@ -4767,7 +4738,7 @@ void RakPeer::SendBuffered(const char *data, BitSize_t numberOfBitsToSend, Mafia bcs->connectionMode = connectionMode; bcs->receipt = receipt; bcs->command = BufferedCommandStruct::BCS_SEND; - bufferedCommands.Push(bcs); + bufferedCommands.Push(std::move(bcs)); if (priority == MafiaNet::Priority::Immediate) { @@ -4778,7 +4749,6 @@ void RakPeer::SendBuffered(const char *data, BitSize_t numberOfBitsToSend, Mafia // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::SendBufferedList(const char **data, const int *lengths, const int numParameters, MafiaNet::Priority priority, MafiaNet::Reliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt) { - BufferedCommandStruct *bcs; unsigned int totalLength = 0; unsigned int lengthOffset; int i; @@ -4816,7 +4786,7 @@ void RakPeer::SendBufferedList(const char **data, const int *lengths, const int RakAssert(!((int) priority > (int) MafiaNet::NUMBER_OF_PRIORITIES || (int) priority < 0)); RakAssert(!(orderingChannel >= NUMBER_OF_ORDERED_STREAMS)); - bcs = bufferedCommands.Allocate(_FILE_AND_LINE_); + auto bcs = std::make_unique(); bcs->data = dataAggregate; bcs->numberOfBitsToSend = BYTES_TO_BITS(totalLength); bcs->priority = priority; @@ -4827,7 +4797,7 @@ void RakPeer::SendBufferedList(const char **data, const int *lengths, const int bcs->connectionMode = connectionMode; bcs->receipt = receipt; bcs->command = BufferedCommandStruct::BCS_SEND; - bufferedCommands.Push(bcs); + bufferedCommands.Push(std::move(bcs)); if (priority == MafiaNet::Priority::Immediate) { @@ -4960,33 +4930,23 @@ void RakPeer::OnConnectedPong(MafiaNet::Time sendPingTime, MafiaNet::Time sendPo // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::ClearBufferedCommands(void) { - BufferedCommandStruct *bcs; - - while ((bcs = bufferedCommands.Pop()) != 0) - { - if (bcs->data) - std::free(bcs->data); - - bufferedCommands.Deallocate(bcs, _FILE_AND_LINE_); - } - bufferedCommands.Clear(_FILE_AND_LINE_); + // Each dropped command frees its own data (~BufferedCommandStruct). + bufferedCommands.Clear(); } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::ClearSocketQueryOutput(void) { - socketQueryOutput.Clear(_FILE_AND_LINE_); + socketQueryOutput.Clear(); } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::ClearRequestedConnectionList(void) { - DataStructures::Queue freeQueue; + std::deque freeQueue; requestedConnectionQueueMutex.Lock(); - while (requestedConnectionQueue.Size()) - freeQueue.Push(requestedConnectionQueue.Pop(), _FILE_AND_LINE_); + freeQueue.swap(requestedConnectionQueue); requestedConnectionQueueMutex.Unlock(); - unsigned i; - for (i = 0; i < freeQueue.Size(); i++) - DeleteRequestedConnection(freeQueue[i]); + for (RequestedConnectionStruct *rcs : freeQueue) + DeleteRequestedConnection(rcs); } bool RakPeer::IsInsideNetworkThreadReceive(void) const { @@ -5010,7 +4970,7 @@ void RakPeer::DispatchNetworkThreadUpdate(void) } networkThreadDispatcher = std::this_thread::get_id(); - for (unsigned int i = 0; i < pluginListNetworkThread.Size(); i++) + for (unsigned int i = 0; i < pluginListNetworkThread.size(); i++) pluginListNetworkThread[i]->UpdateNetworkThread(); networkThreadDispatcher = std::thread::id(); pluginListNetworkThreadMutex.Unlock(); @@ -5027,7 +4987,7 @@ bool RakPeer::DispatchNetworkThreadReceive(Packet *packet) networkThreadDispatcher = std::this_thread::get_id(); PluginReceiveResult result = RR_CONTINUE_PROCESSING; - for (unsigned int i = 0; i < pluginListNetworkThread.Size() && result == RR_CONTINUE_PROCESSING; i++) + for (unsigned int i = 0; i < pluginListNetworkThread.size() && result == RR_CONTINUE_PROCESSING; i++) result = pluginListNetworkThread[i]->OnReceiveNetworkThread(packet); networkThreadDispatcher = std::thread::id(); pluginListNetworkThreadMutex.Unlock(); @@ -5040,7 +5000,7 @@ bool RakPeer::DispatchNetworkThreadReceive(Packet *packet) inline void RakPeer::AddPacketToProducer(MafiaNet::Packet *p) { packetReturnMutex.Lock(); - packetReturnQueue.Push(p, _FILE_AND_LINE_); + packetReturnQueue.push_back(p); packetReturnCondition.notify_all(); packetReturnMutex.Unlock(); } @@ -5116,7 +5076,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, systemAddress.ToString(false, str1, static_cast(64)); if (rakPeer->IsBanned(str1)) { - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length * 8, systemAddress); MafiaNet::BitStream bs; @@ -5129,13 +5089,13 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bsp.data = (char *) bs.GetData(); bsp.length = bs.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((char *) bs.GetData(), bs.GetNumberOfBitsUsed(), systemAddress); - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); /* unsigned i; - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + for (i=0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((char*) bs.GetData(), bs.GetNumberOfBitsUsed(), systemAddress); SocketLayer::SendTo( rakNetSocket, (char*) bs.GetData(), bs.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); */ @@ -5196,7 +5156,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, if (*isOfflineMessage) { - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length * 8, systemAddress); // These are all messages from unconnected systems. Messages here can be any size, but are never processed from connected systems. @@ -5224,18 +5184,18 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, outBitStream.Write((char *) rakPeer->offlinePingResponse.GetData(), rakPeer->offlinePingResponse.GetNumberOfBytesUsed()); rakPeer->rakPeerMutexes[RakPeer::offlinePingResponse_Mutex].Unlock(); - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char *) outBitStream.GetData(), outBitStream.GetNumberOfBytesUsed(), systemAddress); RNS2_SendParameters bsp; bsp.data = (char *) outBitStream.GetData(); bsp.length = outBitStream.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); // SocketLayer::SendTo( rakNetSocket, (const char*)outBitStream.GetData(), (unsigned int) outBitStream.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - packet = rakPeer->AllocPacket(sizeof(MessageID), _FILE_AND_LINE_); + packet = rakPeer->AllocPacket(sizeof(MessageID)); packet->data[0] = data[0]; packet->systemAddress = systemAddress; packet->guid = remoteGuid; @@ -5247,7 +5207,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, // UNCONNECTED MESSAGE Pong with no data. else if ((unsigned char) data[0] == ID_UNCONNECTED_PONG && (size_t) length >= sizeof(unsigned char) + sizeof(MafiaNet::Time) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID) && (size_t) length < sizeof(unsigned char) + sizeof(MafiaNet::Time) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID) + MAX_OFFLINE_DATA_LENGTH) { - packet = rakPeer->AllocPacket((unsigned int) (length - sizeof(OFFLINE_MESSAGE_DATA_ID) - RakNetGUID::size() - sizeof(MafiaNet::Time) + sizeof(MafiaNet::TimeMS)), _FILE_AND_LINE_); + packet = rakPeer->AllocPacket((unsigned int) (length - sizeof(OFFLINE_MESSAGE_DATA_ID) - RakNetGUID::size() - sizeof(MafiaNet::Time) + sizeof(MafiaNet::TimeMS))); MafiaNet::BitStream bsIn((unsigned char *) data, length, false); bsIn.IgnoreBytes(sizeof(unsigned char)); MafiaNet::Time ping; @@ -5274,7 +5234,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, { unsigned int dataLength = (unsigned int) (length - sizeof(OFFLINE_MESSAGE_DATA_ID) - RakNetGUID::size() - sizeof(MessageID)); RakAssert(dataLength < 1024); - packet = rakPeer->AllocPacket(dataLength + 1, _FILE_AND_LINE_); + packet = rakPeer->AllocPacket(dataLength + 1); RakAssert(packet->length < 1024); MafiaNet::BitStream bs2((unsigned char *) data, length, false); @@ -5301,7 +5261,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, } else if ((unsigned char) (data)[0] == (MessageID) ID_OPEN_CONNECTION_REPLY_1) { - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length * 8, systemAddress); // ID_OPEN_CONNECTION_REPLY_1 (S->C), byte-aligned: @@ -5327,7 +5287,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bsOut.Write(challenge); rakPeer->requestedConnectionQueueMutex.Lock(); - for (i = 0; i < rakPeer->requestedConnectionQueue.Size(); i++) + for (i = 0; i < rakPeer->requestedConnectionQueue.size(); i++) { RakPeer::RequestedConnectionStruct *rcs; rcs = rakPeer->requestedConnectionQueue[i]; @@ -5363,14 +5323,14 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, // Our guid bsOut.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char *) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); RNS2_SendParameters bsp; bsp.data = (char *) bsOut.GetData(); bsp.length = bsOut.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); return true; } @@ -5379,7 +5339,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, } else if ((unsigned char) (data)[0] == (MessageID) ID_OPEN_CONNECTION_REPLY_2) { - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length * 8, systemAddress); // ID_OPEN_CONNECTION_REPLY_2 (S->C), byte-aligned: @@ -5404,7 +5364,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, RakPeer::RequestedConnectionStruct *rcs; bool unlock = true; rakPeer->requestedConnectionQueueMutex.Lock(); - for (i = 0; i < rakPeer->requestedConnectionQueue.Size(); i++) + for (i = 0; i < rakPeer->requestedConnectionQueue.size(); i++) { rcs = rakPeer->requestedConnectionQueue[i]; @@ -5429,7 +5389,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, rakPeer->requestedConnectionQueueMutex.Unlock(); rakPeer->DeleteRequestedConnection(rcs); - packet = rakPeer->AllocPacket(sizeof(char), _FILE_AND_LINE_); + packet = rakPeer->AllocPacket(sizeof(char)); packet->data[0] = rakPeer->encryptionMode == EncryptionMode::Plaintext ? (MessageID) ID_REMOTE_SYSTEM_REQUIRES_ENCRYPTION : (MessageID) ID_OUR_SYSTEM_REQUIRES_SECURITY; packet->bitSize = (sizeof(char) * 8); packet->systemAddress = reportedAddress; @@ -5475,7 +5435,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, rakPeer->requestedConnectionQueueMutex.Unlock(); rakPeer->DeleteRequestedConnection(rcs); // wipes the ephemeral key pair - packet = rakPeer->AllocPacket(sizeof(char), _FILE_AND_LINE_); + packet = rakPeer->AllocPacket(sizeof(char)); packet->data[0] = ID_PUBLIC_KEY_MISMATCH; packet->bitSize = (sizeof(char) * 8); packet->systemAddress = reportedAddress; @@ -5517,7 +5477,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, rakPeer->requestedConnectionQueueMutex.Unlock(); rakPeer->DeleteRequestedConnection(rcs); - packet = rakPeer->AllocPacket(sizeof(char), _FILE_AND_LINE_); + packet = rakPeer->AllocPacket(sizeof(char)); packet->data[0] = ID_CONNECTION_ATTEMPT_FAILED; packet->bitSize = (sizeof(char) * 8); packet->systemAddress = reportedAddress; @@ -5583,7 +5543,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, else { // Failed, no connections available anymore - packet = rakPeer->AllocPacket(sizeof(char), _FILE_AND_LINE_); + packet = rakPeer->AllocPacket(sizeof(char)); packet->data[0] = ID_CONNECTION_ATTEMPT_FAILED; // Attempted a connection and couldn't packet->bitSize = (sizeof(char) * 8); packet->systemAddress = reportedAddress; @@ -5631,7 +5591,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bool connectionAttemptCancelled = false; SystemAddress reportedAddress = systemAddress; rakPeer->requestedConnectionQueueMutex.Lock(); - for (i = 0; i < rakPeer->requestedConnectionQueue.Size(); i++) + for (i = 0; i < rakPeer->requestedConnectionQueue.size(); i++) { rcs = rakPeer->requestedConnectionQueue[i]; if (rcs->actionToTake == RakPeer::RequestedConnectionStruct::CONNECT && rcs->systemAddress == systemAddress && !rcs->cancelled) @@ -5643,13 +5603,13 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, { reportedAddress = rcs->raceFirstAddress; rakPeer->CancelRaceSiblings_NoLock(rcs); - for (i = 0; i < rakPeer->requestedConnectionQueue.Size(); i++) + for (i = 0; i < rakPeer->requestedConnectionQueue.size(); i++) { if (rakPeer->requestedConnectionQueue[i] == rcs) break; } } - rakPeer->requestedConnectionQueue.RemoveAtIndex(i); + rakPeer->requestedConnectionQueue.erase(rakPeer->requestedConnectionQueue.begin() + i); rakPeer->DeleteRequestedConnection(rcs); break; } @@ -5660,7 +5620,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, if (connectionAttemptCancelled) { // Tell user of connection attempt failed - packet = rakPeer->AllocPacket(sizeof(char), _FILE_AND_LINE_); + packet = rakPeer->AllocPacket(sizeof(char)); packet->data[0] = data[0]; // Attempted a connection and couldn't packet->bitSize = (sizeof(char) * 8); packet->systemAddress = reportedAddress; @@ -5686,7 +5646,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bs.WriteAlignedBytes((const unsigned char *) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); bs.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((char *) bs.GetData(), bs.GetNumberOfBitsUsed(), systemAddress); // SocketLayer::SendTo( rakNetSocket, (char*)bs.GetData(), bs.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); @@ -5696,11 +5656,11 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bsp.length = bs.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); return true; } - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length * 8, systemAddress); // ID_OPEN_CONNECTION_REPLY_1 (S->C): see the receiving side above for the layout. The challenge is @@ -5717,7 +5677,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, else bsOut.WriteCasted(length + UDP_HEADER_SIZE); - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char *) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); // SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); @@ -5725,7 +5685,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bsp.data = (char *) bsOut.GetData(); bsp.length = bsOut.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); } else if ((unsigned char) (data)[0] == ID_OPEN_CONNECTION_REQUEST_2) { @@ -5758,13 +5718,13 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bsOut.Write(rakPeer->encryptionMode == EncryptionMode::Required ? (MessageID) ID_REMOTE_SYSTEM_REQUIRES_ENCRYPTION : (MessageID) ID_OUR_SYSTEM_REQUIRES_SECURITY); bsOut.WriteAlignedBytes((const unsigned char *) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); bsOut.Write(rakPeer->myGuid); - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char *) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); RNS2_SendParameters bsp; bsp.data = (char *) bsOut.GetData(); bsp.length = bsOut.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); return true; } @@ -5837,7 +5797,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, // The identity block the first REPLY_2 carried, and the one the session keys were derived from. rssFromSA->handshakeLocalIdentity.Serialize(&bsAnswer); - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char *) bsAnswer.GetData(), bsAnswer.GetNumberOfBitsUsed(), systemAddress); // SocketLayer::SendTo( rakNetSocket, (const char*) bsAnswer.GetData(), bsAnswer.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); @@ -5845,7 +5805,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bsp.data = (char *) bsAnswer.GetData(); bsp.length = bsAnswer.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); return true; } @@ -5854,14 +5814,14 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bsOut.Write((MessageID) ID_ALREADY_CONNECTED); bsOut.WriteAlignedBytes((const unsigned char *) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); bsOut.Write(rakPeer->myGuid); - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char *) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); // SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); RNS2_SendParameters bsp; bsp.data = (char *) bsOut.GetData(); bsp.length = bsOut.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); return true; } @@ -5874,7 +5834,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, if (rakPeer->encryptionMode == EncryptionMode::Required && guid.g < rakPeer->myGuid.g) { rakPeer->requestedConnectionQueueMutex.Lock(); - for (i = 0; i < rakPeer->requestedConnectionQueue.Size(); i++) + for (i = 0; i < rakPeer->requestedConnectionQueue.size(); i++) { RakPeer::RequestedConnectionStruct *rcs = rakPeer->requestedConnectionQueue[i]; if (rcs->systemAddress == systemAddress && !rcs->cancelled) @@ -5885,14 +5845,14 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bsOut.Write((MessageID) ID_NO_FREE_INCOMING_CONNECTIONS); bsOut.WriteAlignedBytes((const unsigned char *) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); bsOut.Write(rakPeer->myGuid); - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char *) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); //SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); RNS2_SendParameters bsp; bsp.data = (char *) bsOut.GetData(); bsp.length = bsOut.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); return true; } @@ -5919,7 +5879,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, if (rakPeer->encryptionMode == EncryptionMode::Required) { bool refuseForPin = false; - for (i = 0; i < rakPeer->requestedConnectionQueue.Size(); i++) + for (i = 0; i < rakPeer->requestedConnectionQueue.size(); i++) { RakPeer::RequestedConnectionStruct *rcs = rakPeer->requestedConnectionQueue[i]; if (rcs->systemAddress == systemAddress && !rcs->cancelled && rcs->actionToTake == RakPeer::RequestedConnectionStruct::CONNECT) @@ -5942,13 +5902,13 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bsOut.Write((MessageID) ID_ALREADY_CONNECTED); bsOut.WriteAlignedBytes((const unsigned char *) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); bsOut.Write(rakPeer->myGuid); - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char *) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); RNS2_SendParameters bsp; bsp.data = (char *) bsOut.GetData(); bsp.length = bsOut.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); return true; } } @@ -6003,7 +5963,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bsOut.Write((MessageID) ID_IP_RECENTLY_CONNECTED); bsOut.WriteAlignedBytes((const unsigned char *) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); bsOut.Write(rakPeer->myGuid); - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char *) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); //SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); @@ -6011,7 +5971,7 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, bsp.data = (char *) bsOut.GetData(); bsp.length = bsOut.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); return true; } @@ -6022,14 +5982,14 @@ bool ProcessOfflineNetworkPacket(SystemAddress systemAddress, const char *data, serverInfo.Serialize(&bsAnswer); identity.Serialize(&bsAnswer); - for (i = 0; i < rakPeer->pluginListNTS.Size(); i++) + for (i = 0; i < rakPeer->pluginListNTS.size(); i++) rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char *) bsAnswer.GetData(), bsAnswer.GetNumberOfBitsUsed(), systemAddress); // SocketLayer::SendTo( rakNetSocket, (const char*) bsAnswer.GetData(), bsAnswer.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); RNS2_SendParameters bsp; bsp.data = (char *) bsAnswer.GetData(); bsp.length = bsAnswer.GetNumberOfBytesUsed(); bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + rakNetSocket->Send(&bsp); } return true; } @@ -6100,20 +6060,20 @@ unsigned int RakPeer::GenerateSeedFromGuid(void) void RakPeer::DerefAllSockets(void) { unsigned int i; - for (i = 0; i < socketList.Size(); i++) + for (i = 0; i < socketList.size(); i++) { if (socketFactory && !socketList[i]->IsBerkleySocket()) socketFactory->Destroy(socketList[i]); else delete socketList[i]; } - socketList.Clear(false, _FILE_AND_LINE_); + socketList.clear(); } // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- unsigned int RakPeer::GetRakNetSocketFromUserConnectionSocketIndex(unsigned int userIndex) const { unsigned int i; - for (i = 0; i < socketList.Size(); i++) + for (i = 0; i < socketList.size(); i++) { if (socketList[i]->GetUserConnectionSocketIndex() == userIndex) return i; @@ -6183,9 +6143,7 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) //SystemAddress authoritativeClientSystemAddress; BitSize_t bitSize; unsigned int byteSize; - unsigned char *data; SystemAddress systemAddress; - BufferedCommandStruct *bcs; bool callerDataAllocationUsed; RakNetStatistics *rnss; MafiaNet::TimeUS timeNS = 0; @@ -6214,22 +6172,30 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) while ((recvFromStruct = PopBufferedPacket()) != 0) { /* - for (socketListIndex=0; socketListIndex < socketList.Size(); socketListIndex++) + for (socketListIndex=0; socketListIndex < socketList.size(); socketListIndex++) { if ((RakNetSocket*) socketList[socketListIndex]==recvFromStruct->s) break; } - if (socketListIndex!=socketList.Size()) + if (socketListIndex!=socketList.size()) */ ProcessNetworkPacket(recvFromStruct->systemAddress, recvFromStruct->data, recvFromStruct->bytesRead, this, recvFromStruct->socket, recvFromStruct->timeRead, updateBitStream); - DeallocRNS2RecvStruct(recvFromStruct, _FILE_AND_LINE_); + DeallocRNS2RecvStruct(recvFromStruct); } // Before the send queue is drained, so whatever the plugins send here leaves this cycle. DispatchNetworkThreadUpdate(); - while ((bcs = bufferedCommands.PopInaccurate()) != 0) + // Only the commands queued when this cycle reached here. A user thread that keeps pushing (e.g. CloseConnection + // in a polling loop until the peer is gone) must not hold the network thread in this loop: the rest of the + // cycle, which completes that very disconnect, would never run. Later commands are handled next cycle. + for (std::size_t pendingCommands = bufferedCommands.Size(); pendingCommands > 0; pendingCommands--) { + auto popped = bufferedCommands.TryPop(); + if (!popped) + break; + // Destroying the command at the end of the iteration frees its data, unless a branch handed it on. + BufferedCommandStruct *bcs = popped->get(); if (bcs->command == BufferedCommandStruct::BCS_SEND) { // GetTime is a very slow call so do it once and as late as possible @@ -6240,8 +6206,8 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) } callerDataAllocationUsed = SendImmediate((char *) bcs->data, bcs->numberOfBitsToSend, bcs->priority, bcs->reliability, bcs->orderingChannel, bcs->systemIdentifier, bcs->broadcast, true, timeNS, bcs->receipt); - if (callerDataAllocationUsed == false) - std::free(bcs->data); + if (callerDataAllocationUsed) + bcs->data = nullptr; // The reliability layer took the allocation over and frees it // Set the new connection state AFTER we call sendImmediate in case we are setting it to a disconnection state, which does not allow further sends if (bcs->connectionMode != RemoteSystemStruct::NO_ACTION) @@ -6284,8 +6250,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) else SendSessionConfigRejection(sessionSystem, bcs->data, sessionPayloadLength); } - if (bcs->data) - std::free(bcs->data); } else if (bcs->command == BufferedCommandStruct::BCS_SESSION_STATUS) { @@ -6305,8 +6269,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) SendImmediate((char *) statusStream.GetData(), statusStream.GetNumberOfBitsUsed(), MafiaNet::Priority::Immediate, MafiaNet::Reliability::ReliableOrdered, 0, sessionSystem->systemAddress, false, false, MafiaNet::GetTimeUS(), 0); sessionSystem->sessionConfigStartTime = MafiaNet::GetTimeMS(); } - if (bcs->data) - std::free(bcs->data); } else if (bcs->command == BufferedCommandStruct::BCS_CLOSE_CONNECTION) { @@ -6315,7 +6277,7 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) // RakAssert is a no-op in release (NDEBUG), so guard explicitly and fall back // to the primary socket to avoid dereferencing null in CloseConnectionInternal2. RakNetSocket2 *closeSocket = bcs->socket; - if (closeSocket == nullptr && socketList.Size() > 0) + if (closeSocket == nullptr && socketList.size() > 0) closeSocket = socketList[0]; if (closeSocket != nullptr) CloseConnectionInternal2(bcs->systemIdentifier, false, true, bcs->orderingChannel, bcs->priority, *closeSocket); @@ -6332,39 +6294,25 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) } else if (bcs->command == BufferedCommandStruct::BCS_GET_SOCKET) { - SocketQueryOutput *sqo; + auto sqo = std::make_unique(); if (bcs->systemIdentifier.IsUndefined()) - { - sqo = socketQueryOutput.Allocate(_FILE_AND_LINE_); sqo->sockets = socketList; - socketQueryOutput.Push(sqo); - } else { remoteSystem = GetRemoteSystem(bcs->systemIdentifier, true, true); - sqo = socketQueryOutput.Allocate(_FILE_AND_LINE_); - - sqo->sockets.Clear(false, _FILE_AND_LINE_); if (remoteSystem) - { - sqo->sockets.Push(remoteSystem->rakNetSocket, _FILE_AND_LINE_); - } - else - { - // Leave empty smart pointer - } - socketQueryOutput.Push(sqo); + sqo->sockets.push_back(remoteSystem->rakNetSocket); + // else leave the list empty: no such system } + socketQueryOutput.Push(std::move(sqo)); } - -#ifdef _DEBUG - bcs->data = 0; -#endif - - bufferedCommands.Deallocate(bcs, _FILE_AND_LINE_); } - if (requestedConnectionQueue.IsEmpty() == false) + // Connect() pushes from user threads, so even the emptiness check takes the lock. + requestedConnectionQueueMutex.Lock(); + const bool haveRequestedConnections = requestedConnectionQueue.empty() == false; + requestedConnectionQueueMutex.Unlock(); + if (haveRequestedConnections) { if (timeNS == 0) { @@ -6375,14 +6323,14 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) bool condition1, condition2; unsigned requestedConnectionQueueIndex = 0; requestedConnectionQueueMutex.Lock(); - while (requestedConnectionQueueIndex < requestedConnectionQueue.Size()) + while (requestedConnectionQueueIndex < requestedConnectionQueue.size()) { RequestedConnectionStruct *rcs; rcs = requestedConnectionQueue[requestedConnectionQueueIndex]; if (rcs->cancelled) { // CancelConnectionAttempt flagged it; this thread is the one that frees requests. - requestedConnectionQueue.RemoveAtIndex(requestedConnectionQueueIndex); + requestedConnectionQueue.erase(requestedConnectionQueue.begin() + requestedConnectionQueueIndex); DeleteRequestedConnection(rcs); continue; } @@ -6400,11 +6348,11 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) SystemAddress failedAddress = rcs->systemAddress; requestedConnectionQueueMutex.Lock(); - for (unsigned int k = 0; k < requestedConnectionQueue.Size(); k++) + for (unsigned int k = 0; k < requestedConnectionQueue.size(); k++) { if (requestedConnectionQueue[k] == rcs) { - requestedConnectionQueue.RemoveAtIndex(k); + requestedConnectionQueue.erase(requestedConnectionQueue.begin() + k); break; } } @@ -6424,7 +6372,7 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) if (reportFailure) { // Tell user of connection attempt failed - packet = AllocPacket(sizeof(char), _FILE_AND_LINE_); + packet = AllocPacket(sizeof(char)); packet->data[0] = ID_CONNECTION_ATTEMPT_FAILED; // Attempted a connection and couldn't packet->bitSize = (sizeof(char) * 8); packet->systemAddress = failedAddress; @@ -6456,7 +6404,7 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) //RAKNET_DEBUG_PRINTF("%i:IOCR, ", __LINE__); unsigned i; - for (i = 0; i < pluginListNTS.Size(); i++) + for (i = 0; i < pluginListNTS.size(); i++) pluginListNTS[i]->OnDirectSocketSend((const char *) bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), rcs->systemAddress); RakNetSocket2 *socketToUse; @@ -6476,7 +6424,7 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) bsp.data = (char *) bitStream.GetData(); bsp.length = bitStream.GetNumberOfBytesUsed(); bsp.systemAddress = rcs->systemAddress; - const RNS2SendResult sendResult = socketToUse->Send(&bsp, _FILE_AND_LINE_); + const RNS2SendResult sendResult = socketToUse->Send(&bsp); // Send() reports failure as sendto's own return value and drops the reason, so // the error has to be read back off the thread before anything else touches a // socket. The old test here compared that return value against 10040 directly, @@ -6621,7 +6569,7 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) // ID_CONNECTION_ATTEMPT_FAILED are locally synthesized and stay payload-less. const bool attachReason = !absorbedAttemptUnanswered && remoteSystem->connectMode == RemoteSystemStruct::DISCONNECT_ON_NO_ACK && remoteSystem->disconnectReasonData != 0; const unsigned int reasonLength = attachReason ? remoteSystem->disconnectReasonLength : 0; - packet = AllocPacket(sizeof(char) + reasonLength, _FILE_AND_LINE_); + packet = AllocPacket(sizeof(char) + reasonLength); if (absorbedAttemptUnanswered || remoteSystem->connectMode == RemoteSystemStruct::REQUESTED_CONNECTION) packet->data[0] = ID_CONNECTION_ATTEMPT_FAILED; // Attempted a connection and couldn't else if (remoteSystem->connectMode == RemoteSystemStruct::EXCHANGING_SESSION_DATA) @@ -6672,7 +6620,7 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) // Only the connecting side reports a failure; a server just drops the half-open slot. if (remoteSystem->sessionConfigIsConnectingSide) { - Packet *timeoutPacket = AllocPacket(sizeof(MessageID), _FILE_AND_LINE_); + Packet *timeoutPacket = AllocPacket(sizeof(MessageID)); timeoutPacket->data[0] = (unsigned char) ID_CONNECTION_ATTEMPT_FAILED; timeoutPacket->bitSize = sizeof(MessageID) * 8; timeoutPacket->systemAddress = remoteSystem->systemAddress; @@ -6707,18 +6655,23 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) // Does the reliability layer have any packets waiting for us? // To be thread safe, this has to be called in the same thread as HandleSocketReceiveFromConnectedPlayer - bitSize = remoteSystem->reliabilityLayer.Receive(&data); - - while (bitSize > 0) + for (;;) { + unsigned char *received = nullptr; + bitSize = remoteSystem->reliabilityLayer.Receive(&received); + if (bitSize == 0) + break; + // Owns the message for this iteration: it is freed when the iteration ends, whichever branch + // handled it, unless that branch hands it on to a Packet with message.release(). + ReceivedMessage message(received); + unsigned char *const data = received; + // These types are for internal use and should never arrive from a network packet. // A remote that sends one anyway gets it dropped: freed, and the rest of the // queue still processed (this used to leak it and stall the queue, and to // RakAssert in Debug). if (data[0] == ID_CONNECTION_ATTEMPT_FAILED) { - std::free(data); - bitSize = remoteSystem->reliabilityLayer.Receive(&data); continue; } @@ -6731,7 +6684,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) if ((unsigned char) (data)[0] == ID_CONNECTION_REQUEST) { ParseConnectionRequestPacket(remoteSystem, systemAddress, (const char *) data, byteSize); - std::free(data); // Cross-connection: if our own attempt to this peer is still pending, it lost the tie-break (see the // REQUEST_2 handler). The winner's ID_CONNECTION_REQUEST just opened under these keys, so this is the // connection: drop our attempt quietly, wiping its key pair. @@ -6750,9 +6702,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) char str1[64]; systemAddress.ToString(false, str1, static_cast(64)); AddToBanList(str1, remoteSystem->reliabilityLayer.GetTimeoutTime()); - - - std::free(data); } } else @@ -6781,7 +6730,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) // This can happen due to race conditions with the fully connected mesh OnConnectionRequest(remoteSystem, incomingTimestamp); } - std::free(data); } else if ((unsigned char) data[0] == ID_NEW_INCOMING_CONNECTION && byteSize > sizeof(unsigned char) + sizeof(unsigned int) + sizeof(unsigned short) + sizeof(MafiaNet::Time) * 2) { @@ -6826,8 +6774,8 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) firstExternalID.debugPort = ntohs(firstExternalID.address.addr4.sin_port); } - // Hold this until the session handshake completes; the bytes are copied because `data` - // is freed as soon as this branch returns. + // Hold this until the session handshake completes; the bytes are copied because `message` + // frees `data` at the end of this iteration. ClearSessionConfig(remoteSystem); remoteSystem->sessionConfigStartTime = timeMS != 0 ? (MafiaNet::TimeMS) timeMS : MafiaNet::GetTimeMS(); // compared with GetTimeMS(), 32-bit remoteSystem->withheldConnectionPacketData = (unsigned char *) std::malloc(byteSize); @@ -6836,14 +6784,12 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) memcpy(remoteSystem->withheldConnectionPacketData, data, byteSize); remoteSystem->withheldConnectionPacketLength = byteSize; } - // AllocPacket used to take ownership of `data` here; the copy above means this branch does. - std::free(data); } else { - // Already connected (or not expecting it): ignore it. The buffer is ours to free; a remote that - // repeats this message must not make us leak one per datagram. - std::free(data); + // Already connected (or not expecting it): ignore it. Both sides of a cross connection send + // this, so it arrives here on every simultaneous connect; a remote that repeats it must not make + // us leak one per datagram (the receive loop frees it). } } else if ((unsigned char) data[0] == ID_CONNECTED_PONG && byteSize == sizeof(unsigned char) + sizeof(MafiaNet::Time) * 2) @@ -6860,8 +6806,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) inBitStream.Read(sendPongTime); OnConnectedPong(sendPingTime, sendPongTime, remoteSystem); - - std::free(data); } else if ((unsigned char) data[0] == ID_CONNECTED_PING && byteSize == sizeof(unsigned char) + sizeof(MafiaNet::Time)) { @@ -6878,8 +6822,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) // Update again immediately after this tick so the ping goes out right away quitAndDataEvents.SetEvent(); - - std::free(data); } else if ((unsigned char) data[0] == ID_DISCONNECTION_NOTIFICATION && remoteSystem->connectMode == RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY) { @@ -6887,14 +6829,14 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) // how it ended (ID_CONNECTION_ATTEMPT_FAILED from FailSessionHandshakeAsConnectingSide, a // refused session, a wrong password) or was never told of it at all. Moving it to // DISCONNECT_ON_NO_ACK would report the close a second time, as ID_DISCONNECTION_NOTIFICATION - // for a connection the application never saw. The reliability layer still acks the message. - std::free(data); + // for a connection the application never saw. The reliability layer still acks the message, and + // `message` frees it. } else if ((unsigned char) data[0] == ID_DISCONNECTION_NOTIFICATION) { // Stash any reason payload (everything after the 1-byte ID) so it can ride along with the // user-facing notification packet synthesized once outstanding ACKs are flushed. This raw - // reliability-layer buffer is freed below, so the bytes have to be copied out now. + // reliability-layer buffer is freed with `message`, so the bytes have to be copied out now. ClearDisconnectReason(remoteSystem); if (byteSize > sizeof(unsigned char)) { @@ -6909,14 +6851,12 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) // We shouldn't close the connection immediately because we need to ack the ID_DISCONNECTION_NOTIFICATION remoteSystem->connectMode = RemoteSystemStruct::DISCONNECT_ON_NO_ACK; - std::free(data); // AddPacketToProducer(packet); } else if ((unsigned char) (data)[0] == ID_DETECT_LOST_CONNECTIONS && byteSize == sizeof(unsigned char)) { // Do nothing - std::free(data); } else if ((unsigned char) (data)[0] == ID_SESSION_CONFIG_REQUEST) { @@ -6928,14 +6868,13 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) { // Oversized payload is a protocol violation; drop without answering. remoteSystem->connectMode = RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; - std::free(data); } else if (sessionConfigInteractive) { // Let the application decide. The connection stays unreported until it calls // AcceptSession() or RejectSession(), or the handshake times out below. remoteSystem->sessionConfigAwaitingLocalDecision = true; - packet = AllocPacket(byteSize, data, _FILE_AND_LINE_); + packet = AllocPacket(byteSize, message.release()); packet->bitSize = bitSize; packet->systemAddress = systemAddress; packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; @@ -6948,14 +6887,12 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) rakPeerMutexes[sessionConfig_Mutex].Lock(); SendSessionConfigResponse(remoteSystem, (const char *) sessionConfig.GetData(), (unsigned int) sessionConfig.GetNumberOfBytesUsed()); rakPeerMutexes[sessionConfig_Mutex].Unlock(); - std::free(data); } } else { // A repeat request, or one on an already-established connection: ignore it rather than // letting a peer re-run the handshake and re-produce the connection packet. - std::free(data); } } else if ((unsigned char) (data)[0] == ID_SESSION_CONFIG) @@ -6981,7 +6918,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) ProduceWithheldConnectionPacket(remoteSystem, (MessageID) ID_CONNECTION_REQUEST_ACCEPTED); } } - std::free(data); } else if ((unsigned char) (data)[0] == ID_SESSION_CONFIG_STATUS) { @@ -6991,7 +6927,7 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) if (remoteSystem->connectMode == RemoteSystemStruct::EXCHANGING_SESSION_DATA && remoteSystem->sessionConfigIsConnectingSide) { remoteSystem->sessionConfigStartTime = timeMS != 0 ? (MafiaNet::TimeMS) timeMS : MafiaNet::GetTimeMS(); // compared with GetTimeMS(), 32-bit - packet = AllocPacket(byteSize, data, _FILE_AND_LINE_); + packet = AllocPacket(byteSize, message.release()); packet->bitSize = bitSize; packet->systemAddress = systemAddress; packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; @@ -6999,10 +6935,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) packet->guid.systemIndex = packet->systemAddress.systemIndex; AddPacketToProducer(packet); } - else - { - std::free(data); - } } else if ((unsigned char) (data)[0] == ID_SESSION_CONFIG_REJECTED) { @@ -7013,7 +6945,7 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) { // Rewrite in place so the reason string the server supplied stays at data+1, matching the // ID_DISCONNECTION_NOTIFICATION reason convention. No connection is ever reported. - packet = AllocPacket(byteSize, data, _FILE_AND_LINE_); + packet = AllocPacket(byteSize, message.release()); packet->data[0] = (unsigned char) ID_CONNECTION_ATTEMPT_FAILED; packet->bitSize = bitSize; packet->systemAddress = systemAddress; @@ -7024,16 +6956,12 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) remoteSystem->connectMode = RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; } - else - { - std::free(data); - } } else if ((unsigned char) (data)[0] == ID_INVALID_PASSWORD) { if (remoteSystem->connectMode == RemoteSystemStruct::REQUESTED_CONNECTION) { - packet = AllocPacket(byteSize, data, _FILE_AND_LINE_); + packet = AllocPacket(byteSize, message.release()); packet->bitSize = bitSize; packet->systemAddress = systemAddress; packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; @@ -7043,10 +6971,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) remoteSystem->connectMode = RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; } - else - { - std::free(data); - } } else if ((unsigned char) (data)[0] == ID_CONNECTION_REQUEST_ACCEPTED) { @@ -7118,7 +7042,7 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) } // Hold the connection-complete packet until the session handshake finishes. The bytes are - // copied because `data` is freed as soon as this branch returns. + // copied because `message` frees `data` at the end of this iteration. // // With allowConnectionResponseIPMigration a repeated ID_CONNECTION_REQUEST_ACCEPTED on a // connection still in the handshake starts it over, and ClearSessionConfig() then frees the @@ -7133,8 +7057,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) memcpy(remoteSystem->withheldConnectionPacketData, data, byteSize); remoteSystem->withheldConnectionPacketLength = byteSize; } - // AllocPacket used to take ownership of `data` here; the copy above means this branch does. - std::free(data); MafiaNet::BitStream outBitStream; outBitStream.Write((MessageID) ID_NEW_INCOMING_CONNECTION); @@ -7166,7 +7088,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) else { // Ignore, already connected - std::free(data); } } else @@ -7174,7 +7095,6 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) // Too short to be a connection accept: drop it. Not a RakAssert -- the // remote chooses this size, and a Debug build must not abort on it. The attempt // then times out normally. - std::free(data); } } else @@ -7194,7 +7114,7 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) // are dropped. if (!MayReachApplicationFromRemote(data, byteSize) || !remoteSystem->isActive) { - std::free(data); + // Dropped: `message` frees it. } else if (remoteSystem->connectMode == RemoteSystemStruct::EXCHANGING_SESSION_DATA && remoteSystem->sessionConfigIsConnectingSide && remoteSystem->weInitiatedTheConnection) @@ -7206,23 +7126,15 @@ bool RakPeer::RunUpdateCycle(BitStream &updateBitStream) // // Bounded: exceeding the bound is a protocol violation, and failing the attempt beats // silently losing the one message that did not fit -- the very bug this hold exists for. - if (!HoldApplicationMessage(remoteSystem, data, bitSize)) + if (!HoldApplicationMessage(remoteSystem, message.release(), bitSize)) FailSessionHandshakeAsConnectingSide(remoteSystem); } else if (remoteSystem->connectionReportedToApplication) { - DeliverApplicationMessage(remoteSystem, data, bitSize); - } - else - { - std::free(data); + DeliverApplicationMessage(remoteSystem, message.release(), bitSize); } } } - - // Does the reliability layer have any more packets waiting for us? - // To be thread safe, this has to be called in the same thread as HandleSocketReceiveFromConnectedPlayer - bitSize = remoteSystem->reliabilityLayer.Receive(&data); } } @@ -7314,7 +7226,7 @@ RAK_THREAD_DECLARATION(MafiaNet::UpdateNetworkLoop) { WSAEVENT eventArray[256]; unsigned int i, eventArrayIndex; - for (i=0,eventArrayIndex=0; i < rakPeer->socketList.Size(); i++) + for (i=0,eventArrayIndex=0; i < rakPeer->socketList.size(); i++) { if (rakPeer->socketList[i]->recvEvent!=INVALID_HANDLE_VALUE) { @@ -7353,9 +7265,9 @@ RAK_THREAD_DECLARATION(MafiaNet::UpdateNetworkLoop) return 0; } -void RakPeer::CallPluginCallbacks(DataStructures::List &pluginList, Packet *packet) +void RakPeer::CallPluginCallbacks(std::vector &pluginList, Packet *packet) { - for (unsigned int i = 0; i < pluginList.Size(); i++) + for (unsigned int i = 0; i < pluginList.size(); i++) { switch (packet->data[0]) { diff --git a/Source/src/reliability_layer.cpp b/Source/src/reliability_layer.cpp index aa1f87950..338aa23d3 100644 --- a/Source/src/reliability_layer.cpp +++ b/Source/src/reliability_layer.cpp @@ -87,7 +87,6 @@ static const CCTimeType HISTOGRAM_RESTART_CYCLE = 10000; // Every 10 seconds res static const CCTimeType MAX_TIME_BETWEEN_PACKETS = 350000; // 350 milliseconds //static const CCTimeType HISTOGRAM_RESTART_CYCLE=10000000; // Every 10 seconds reset the histogram #endif -static const int DEFAULT_HAS_RECEIVED_PACKET_QUEUE_SIZE = 512; static const CCTimeType STARTING_TIME_BETWEEN_PACKETS = MAX_TIME_BETWEEN_PACKETS; //static const long double TIME_BETWEEN_PACKETS_INCREASE_MULTIPLIER_DEFAULT=.02; //static const long double TIME_BETWEEN_PACKETS_DECREASE_MULTIPLIER_DEFAULT=1.0 / 9.0; @@ -110,16 +109,14 @@ BPSTracker::TimeAndValue2::TimeAndValue2(MafiaNet::TimeUS t, uint64_t v1) //BPSTracker::TimeAndValue2::TimeAndValue2(MafiaNet::TimeUS t, uint64_t v1, uint64_t v2) : time(t), value1(v1), value2(v2) {} BPSTracker::BPSTracker() { - Reset(_FILE_AND_LINE_); + Reset(); } BPSTracker::~BPSTracker() {} -//void BPSTracker::Reset(const char *file, unsigned int line) {total1=total2=lastSec1=lastSec2=0; dataQueue.Clear(file,line);} -void BPSTracker::Reset(const char *file, unsigned int line) +void BPSTracker::Reset() { total1 = lastSec1 = 0; - dataQueue.Clear(file, line); + dataQueue.clear(); } -//void BPSTracker::Push2(RakNetTimeUS time, uint64_t value1, uint64_t value2) {dataQueue.Push(TimeAndValue2(time,value1,value2),_FILE_AND_LINE_); total1+=value1; lastSec1+=value1; total2+=value2; lastSec2+=value2;} //uint64_t BPSTracker::GetBPS2(RakNetTimeUS time) {ClearExpired2(time); return lastSec2;} //void BPSTracker::GetBPS1And2(RakNetTimeUS time, uint64_t &out1, uint64_t &out2) {ClearExpired2(time); out1=lastSec1; out2=lastSec2;} uint64_t BPSTracker::GetTotal1(void) const @@ -128,30 +125,18 @@ uint64_t BPSTracker::GetTotal1(void) const } //uint64_t BPSTracker::GetTotal2(void) const {return total2;} -// void BPSTracker::ClearExpired2(MafiaNet::TimeUS time) { -// MafiaNet::TimeUS threshold=time; -// if (threshold < 1000000) -// return; -// threshold-=1000000; -// while (dataQueue.IsEmpty()==false && dataQueue.Peek().time < threshold) -// { -// lastSec1-=dataQueue.Peek().value1; -// lastSec2-=dataQueue.Peek().value2; -// dataQueue.Pop(); -// } -// } void BPSTracker::ClearExpired1(MafiaNet::TimeUS time) { - while (dataQueue.IsEmpty() == false && + while (!dataQueue.empty() && #if CC_TIME_TYPE_BYTES == 8 - dataQueue.Peek().time + 1000000 < time + dataQueue.front().time + 1000000 < time #else - dataQueue.Peek().time + 1000 < time + dataQueue.front().time + 1000 < time #endif ) { - lastSec1 -= dataQueue.Peek().value1; - dataQueue.Pop(); + lastSec1 -= dataQueue.front().value1; + dataQueue.pop_front(); } } @@ -342,59 +327,6 @@ bool SplitPacketSort::Add(InternalPacket *internalPacket) return m_fragments.emplace(index, internalPacket).second; } -int MafiaNet::SplitPacketChannelComp(SplitPacketIdType const &key, SplitPacketChannel *const &data) -{ -#if PREALLOCATE_LARGE_MESSAGES == 1 - if (key < data->returnedPacket->splitPacketId) - return -1; - if (key == data->returnedPacket->splitPacketId) - return 0; -#else - if (key < data->splitPacketList.GetPacketId()) - return -1; - if (key == data->splitPacketList.GetPacketId()) - return 0; -#endif - return 1; -} - -// DEFINE_MULTILIST_PTR_TO_MEMBER_COMPARISONS( InternalPacket, SplitPacketIndexType, splitPacketIndex ) -/* -bool operator<( const DataStructures::MLKeyRef &inputKey, const InternalPacket *cls ) -{ - return inputKey.Get() < cls->splitPacketIndex; -} -bool operator>( const DataStructures::MLKeyRef &inputKey, const InternalPacket *cls ) -{ - return inputKey.Get() > cls->splitPacketIndex; -} -bool operator==( const DataStructures::MLKeyRef &inputKey, const InternalPacket *cls ) -{ - return inputKey.Get() == cls->splitPacketIndex; -} -/// Semi-hack: This is necessary to call Sort() -bool operator<( const DataStructures::MLKeyRef &inputKey, const InternalPacket *cls ) -{ - return inputKey.Get()->splitPacketIndex < cls->splitPacketIndex; -} -bool operator>( const DataStructures::MLKeyRef &inputKey, const InternalPacket *cls ) -{ - return inputKey.Get()->splitPacketIndex > cls->splitPacketIndex; -} -bool operator==( const DataStructures::MLKeyRef &inputKey, const InternalPacket *cls ) -{ - return inputKey.Get()->splitPacketIndex == cls->splitPacketIndex; -} -*/ - -int SplitPacketIndexComp(SplitPacketIndexType const &key, InternalPacket *const &data) -{ - if (key < data->splitPacketIndex) - return -1; - if (key == data->splitPacketIndex) - return 0; - return 1; -} //------------------------------------------------------------------------------------------------------- // Constructor @@ -424,10 +356,6 @@ ReliabilityLayer::ReliabilityLayer() #endif InitializeVariables(); - //int i = sizeof(InternalPacket); - datagramHistoryMessagePool.SetPageSize(sizeof(MessageNumberNode) * 128); - internalPacketPool.SetPageSize(sizeof(InternalPacket) * INTERNAL_PACKET_PAGE_SIZE); - refCountedDataPool.SetPageSize(sizeof(InternalPacketRefCountedData) * 32); } //------------------------------------------------------------------------------------------------------- @@ -480,13 +408,13 @@ uint64_t ReliabilityLayer::GetDroppedKeylessSendsForTesting() const //------------------------------------------------------------------------------------------------------- unsigned int ReliabilityLayer::GetReceiveWindowSizeForTesting() const { - return hasReceivedPacketQueue.Size(); + return (unsigned int) hasReceivedPacketQueue.size(); } //------------------------------------------------------------------------------------------------------- unsigned int ReliabilityLayer::GetPartialSplitMessagesForTesting() const { - return splitPacketChannelList.Size(); + return (unsigned int) splitPacketChannelList.size(); } //------------------------------------------------------------------------------------------------------- @@ -619,7 +547,7 @@ void ReliabilityLayer::InitializeVariables() for (int i = 0; i < RNS_PER_SECOND_METRICS_COUNT; i++) { - bpsMetrics[i].Reset(_FILE_AND_LINE_); + bpsMetrics[i].Reset(); } } @@ -636,71 +564,47 @@ void ReliabilityLayer::FreeMemory(bool freeAllImmediately) void ReliabilityLayer::FreeThreadSafeMemory(void) { - unsigned i, j; + unsigned i; InternalPacket *internalPacket; ClearPacketsAndDatagrams(); - for (i = 0; i < splitPacketChannelList.Size(); i++) + for (const auto &channelEntry : splitPacketChannelList) { - for (const auto &fragment : splitPacketChannelList[i]->splitPacketList.Fragments()) + SplitPacketChannel *channel = channelEntry.second; + for (const auto &fragment : channel->splitPacketList.Fragments()) { internalPacket = fragment.second; FreeInternalPacketData(internalPacket, _FILE_AND_LINE_); ReleaseToInternalPacketPool(internalPacket); } #if PREALLOCATE_LARGE_MESSAGES == 1 - if (splitPacketChannelList[i]->returnedPacket) + if (channel->returnedPacket) { - FreeInternalPacketData(splitPacketChannelList[i]->returnedPacket, __FILE__, __LINE__); - ReleaseToInternalPacketPool(splitPacketChannelList[i]->returnedPacket); + FreeInternalPacketData(channel->returnedPacket, __FILE__, __LINE__); + ReleaseToInternalPacketPool(channel->returnedPacket); } #endif - delete splitPacketChannelList[i]; + delete channel; } - splitPacketChannelList.Clear(false, _FILE_AND_LINE_); + splitPacketChannelList.clear(); - while (outputQueue.Size() > 0) + for (InternalPacket *queued : outputQueue) { - internalPacket = outputQueue.Pop(); - FreeInternalPacketData(internalPacket, _FILE_AND_LINE_); - ReleaseToInternalPacketPool(internalPacket); + FreeInternalPacketData(queued, _FILE_AND_LINE_); + ReleaseToInternalPacketPool(queued); } + outputQueue.clear(); - outputQueue.ClearAndForceAllocation(32, _FILE_AND_LINE_); - - /* - for ( i = 0; i < orderingList.Size(); i++ ) - { - if ( orderingList[ i ] ) - { - DataStructures::LinkedList* theList = orderingList[ i ]; - - if ( theList ) - { - while ( theList->Size() ) - { - internalPacket = orderingList[ i ]->Pop(); - FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ ); - ReleaseToInternalPacketPool( internalPacket ); - } - - delete theList; - } - } - } - - orderingList.Clear(false, _FILE_AND_LINE_); - */ for (i = 0; i < NUMBER_OF_ORDERED_STREAMS; i++) { - for (j = 0; j < orderingHeaps[i].Size(); j++) + for (const HeapEntry &entry : orderingHeaps[i]) { - FreeInternalPacketData(orderingHeaps[i][j], _FILE_AND_LINE_); - ReleaseToInternalPacketPool(orderingHeaps[i][j]); + FreeInternalPacketData(entry.packet, _FILE_AND_LINE_); + ReleaseToInternalPacketPool(entry.packet); } - orderingHeaps[i].Clear(true, _FILE_AND_LINE_); + orderingHeaps[i].clear(); } //resendList.ForEachData(DeleteInternalPacket); @@ -733,56 +637,41 @@ void ReliabilityLayer::FreeThreadSafeMemory(void) // acknowlegements.Clear(_FILE_AND_LINE_); - for (j = 0; j < outgoingPacketBuffer.Size(); j++) + for (const HeapEntry &entry : outgoingPacketBuffer) { - if (outgoingPacketBuffer[j]->data) - FreeInternalPacketData(outgoingPacketBuffer[j], _FILE_AND_LINE_); - ReleaseToInternalPacketPool(outgoingPacketBuffer[j]); + if (entry.packet->data) + FreeInternalPacketData(entry.packet, _FILE_AND_LINE_); + ReleaseToInternalPacketPool(entry.packet); } - outgoingPacketBuffer.Clear(true, _FILE_AND_LINE_); + outgoingPacketBuffer.clear(); #ifdef _DEBUG - for (i = 0; i < delayList.Size(); i++) - delete delayList[i]; - delayList.Clear(__FILE__, __LINE__); + for (DataAndTime *delayed : delayList) + delete delayed; + delayList.clear(); #endif - unreliableWithAckReceiptHistory.Clear(false, _FILE_AND_LINE_); - - packetsToSendThisUpdate.Clear(false, _FILE_AND_LINE_); - packetsToSendThisUpdate.Preallocate(512, _FILE_AND_LINE_); - packetsToDeallocThisUpdate.Clear(false, _FILE_AND_LINE_); - packetsToDeallocThisUpdate.Preallocate(512, _FILE_AND_LINE_); - packetsToSendThisUpdateDatagramBoundaries.Clear(false, _FILE_AND_LINE_); - packetsToSendThisUpdateDatagramBoundaries.Preallocate(128, _FILE_AND_LINE_); - datagramSizesInBytes.Clear(false, _FILE_AND_LINE_); - datagramSizesInBytes.Preallocate(128, _FILE_AND_LINE_); + unreliableWithAckReceiptHistory.clear(); - internalPacketPool.Clear(_FILE_AND_LINE_); + packetsToSendThisUpdate.clear(); + packetsToSendThisUpdate.reserve(512); + packetsToDeallocThisUpdate.clear(); + packetsToDeallocThisUpdate.reserve(512); + packetsToSendThisUpdateDatagramBoundaries.clear(); + packetsToSendThisUpdateDatagramBoundaries.reserve(128); + datagramSizesInBytes.clear(); + datagramSizesInBytes.reserve(128); - refCountedDataPool.Clear(_FILE_AND_LINE_); - - /* - DataStructures::Page *cur = datagramMessageIDTree.GetListHead(); - while (cur) - { - int treeIndex; - for (treeIndex=0; treeIndex < cur->size; treeIndex++) - ReleaseToDatagramMessageIDPool(cur->data[treeIndex]); - cur=cur->resendNext; - } - datagramMessageIDTree.Clear(_FILE_AND_LINE_); - datagramMessageIDPool.Clear(_FILE_AND_LINE_); - */ + // Everything acquired was released above; drop the spares too. + internalPacketPool.Clear(); - while (datagramHistory.Size()) + while (!datagramHistory.empty()) { RemoveFromDatagramHistory(datagramHistoryPopCount); - datagramHistory.Pop(); + datagramHistory.pop_front(); datagramHistoryPopCount++; } - datagramHistoryMessagePool.Clear(_FILE_AND_LINE_); datagramHistoryPopCount = 0; acknowlegements.Clear(); @@ -800,7 +689,7 @@ void ReliabilityLayer::FreeThreadSafeMemory(void) //split packets //------------------------------------------------------------------------------------------------------- bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( - const char *buffer, unsigned int length, SystemAddress &systemAddress, DataStructures::List &messageHandlerList, int mtuSize, + const char *buffer, unsigned int length, SystemAddress &systemAddress, std::vector &messageHandlerList, int mtuSize, RakNetSocket2 *s, RakNetRandom *rnr, CCTimeType timeRead, BitStream &updateBitStream) { // unreferenced parameters @@ -848,7 +737,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( if (length <= 2 || buffer == nullptr) { // Length of 1 is a connection request resend that we just ignore - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("length <= 2 || buffer == nullptr", BYTES_TO_BITS(length), systemAddress, true); return true; } @@ -872,7 +761,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( dhf.Deserialize(&socketData); if (!dhf.isValid) { - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("!dhf.isValid", BYTES_TO_BITS(length), systemAddress, true); } @@ -913,7 +802,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( incomingAcks.Clear(); if (!incomingAcks.Deserialize(&socketData)) { - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("incomingAcks.Deserialize failed", BYTES_TO_BITS(length), systemAddress, true); } @@ -921,8 +810,8 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( return false; } - unsigned int k = 0; - while (k < unreliableWithAckReceiptHistory.Size()) + std::size_t k = 0; + while (k < unreliableWithAckReceiptHistory.size()) { if (incomingAcks.IsWithinRange(unreliableWithAckReceiptHistory[k].datagramNumber)) { @@ -931,10 +820,10 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( ackReceipt->dataBitLength = BYTES_TO_BITS(5); ackReceipt->data[0] = (MessageID) ID_SND_RECEIPT_ACKED; memcpy(ackReceipt->data + sizeof(MessageID), &unreliableWithAckReceiptHistory[k].sendReceiptSerial, sizeof(uint32_t)); - outputQueue.Push(ackReceipt, _FILE_AND_LINE_); + outputQueue.push_back(ackReceipt); - // Remove, swap with last - unreliableWithAckReceiptHistory.RemoveAtIndex(k); + // Remove, keeping the order of the rest + unreliableWithAckReceiptHistory.erase(unreliableWithAckReceiptHistory.begin() + k); } else { @@ -943,20 +832,20 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( } // early out, if we've got no outstanding datagramHistory entries - if (datagramHistory.IsEmpty()) + if (datagramHistory.empty()) { receivePacketCount++; return true; } - for (i = 0; i < incomingAcks.ranges.Size(); i++) + for (i = 0; i < incomingAcks.ranges.size(); i++) { // note: minIndex is ensured to be always <= maxIndex - otherwise Deserialize() would have failed RakAssert(incomingAcks.ranges[i].minIndex <= incomingAcks.ranges[i].maxIndex); if (incomingAcks.ranges[i].maxIndex == (uint24_t) (0xFFFFFFFF)) { - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("incomingAcks maxIndex is max value", BYTES_TO_BITS(length), systemAddress, true); } @@ -968,7 +857,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( for (datagramNumber = incomingAcks.ranges[i].minIndex; datagramNumber <= incomingAcks.ranges[i].maxIndex; datagramNumber++) { const DatagramSequenceNumberType offsetIntoList = datagramNumber - datagramHistoryPopCount; - if (offsetIntoList >= datagramHistory.Size()) + if (offsetIntoList >= datagramHistory.size()) { // reached the end of the datagramHistory list - hence, we are done receivePacketCount++; @@ -1020,17 +909,17 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( else if (dhf.isNAK) { // early out, if we've got no outstanding datagramHistory entries - if (datagramHistory.IsEmpty()) + if (datagramHistory.empty()) { receivePacketCount++; return true; } DatagramSequenceNumberType messageNumber; - DataStructures::RangeList incomingNAKs; + RangeList incomingNAKs; if (!incomingNAKs.Deserialize(&socketData)) { - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("incomingNAKs.Deserialize failed", BYTES_TO_BITS(length), systemAddress, true); } @@ -1038,14 +927,14 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( // it's an invalid incoming package --- let's abort processing (there's no point in continuing processing other ranges if the package is invalid) return false; } - for (i = 0; i < incomingNAKs.ranges.Size(); i++) + for (i = 0; i < incomingNAKs.ranges.size(); i++) { // note: minIndex is ensured to be always <= maxIndex - otherwise Deserialize() would have failed RakAssert(incomingNAKs.ranges[i].minIndex <= incomingNAKs.ranges[i].maxIndex); if (incomingNAKs.ranges[i].maxIndex == (uint24_t) (0xFFFFFFFF)) { - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("incomingNAKs maxIndex is max value", BYTES_TO_BITS(length), systemAddress, true); } @@ -1062,7 +951,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( // printf("%p NAK %i\n", this, dhf.datagramNumber.val); const DatagramSequenceNumberType offsetIntoList = messageNumber - datagramHistoryPopCount; - if (offsetIntoList >= datagramHistory.Size()) + if (offsetIntoList >= datagramHistory.size()) { // reached the end of the datagramHistory list - hence, we are done receivePacketCount++; @@ -1093,7 +982,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( uint32_t skippedMessageCount; if (!congestionManager.OnGotPacket(dhf.datagramNumber, dhf.isContinuousSend, timeRead, length, &skippedMessageCount)) { - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("congestionManager.OnGotPacket failed", BYTES_TO_BITS(length), systemAddress, true); } @@ -1125,7 +1014,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( InternalPacket *internalPacket = CreateInternalPacketFromBitStream(&socketData, timeRead); if (internalPacket == 0) { - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("CreateInternalPacketFromBitStream failed", BYTES_TO_BITS(length), systemAddress, true); } @@ -1135,7 +1024,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( while (internalPacket) { - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { #if CC_TIME_TYPE_BYTES == 4 messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, receivePacketCount, systemAddress, timeRead, false); @@ -1150,7 +1039,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( // We do the actual reset in this function so the data is not modified by multiple threads if (resetReceivedPackets) { - hasReceivedPacketQueue.ClearAndForceAllocation(DEFAULT_HAS_RECEIVED_PACKET_QUEUE_SIZE, _FILE_AND_LINE_); + hasReceivedPacketQueue.clear(); receivedPacketsBaseIndex = 0; resetReceivedPackets = false; } @@ -1163,7 +1052,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( { if (internalPacket->orderingChannel >= NUMBER_OF_ORDERED_STREAMS) { - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("internalPacket->orderingChannel >= NUMBER_OF_ORDERED_STREAMS", BYTES_TO_BITS(length), systemAddress, true); } @@ -1192,9 +1081,9 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( if (holeCount == (DatagramSequenceNumberType) 0) { // Got what we were expecting - if (hasReceivedPacketQueue.Size()) + if (!hasReceivedPacketQueue.empty()) { - hasReceivedPacketQueue.Pop(); + hasReceivedPacketQueue.pop_front(); } ++receivedPacketsBaseIndex; } @@ -1202,7 +1091,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( { bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead, BITS_TO_BYTES(internalPacket->dataBitLength)); - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("holeCount > typeRange/(DatagramSequenceNumberType) 2", BYTES_TO_BITS(length), systemAddress, false); } @@ -1213,27 +1102,27 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( goto CONTINUE_SOCKET_DATA_PARSE_LOOP; } - else if ((unsigned int) holeCount < hasReceivedPacketQueue.Size()) + else if ((unsigned int) holeCount < hasReceivedPacketQueue.size()) { // Got a higher count out of order packet that was missing in the sequence or we already got - if (hasReceivedPacketQueue[holeCount] != false) + if (hasReceivedPacketQueue[(uint32_t) holeCount] != false) { // non-zero means this is a hole #ifdef LOG_TRIVIAL_NOTIFICATIONS - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Higher count pushed to hasReceivedPacketQueue", BYTES_TO_BITS(length), systemAddress, false); } #endif // Fill in the hole - hasReceivedPacketQueue[holeCount] = false; // We got the packet at holeCount + hasReceivedPacketQueue[(uint32_t) holeCount] = false; // We got the packet at holeCount } else { bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead, BITS_TO_BYTES(internalPacket->dataBitLength)); #ifdef LOG_TRIVIAL_NOTIFICATIONS - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Duplicate packet ignored", BYTES_TO_BITS(length), systemAddress, false); } @@ -1252,7 +1141,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( { // A peer jumped its message numbering far ahead: drop the // packet. No RakAssert -- the remote chooses this value. - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("holeCount > 1000000", BYTES_TO_BITS(length), systemAddress, true); } @@ -1267,7 +1156,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( } #ifdef LOG_TRIVIAL_NOTIFICATIONS - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Adding to hasReceivedPacketQueue later ordered message", BYTES_TO_BITS(length), systemAddress, false); } @@ -1279,30 +1168,24 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( // Fixed by late assigning message IDs on the sender // Add 0 times to the queue until (reliableMessageNumber - baseIndex) < queue size. - while ((unsigned int) (holeCount) > hasReceivedPacketQueue.Size()) + while ((unsigned int) (holeCount) > hasReceivedPacketQueue.size()) { - hasReceivedPacketQueue.Push(true, _FILE_AND_LINE_); // time+(CCTimeType)60 * (CCTimeType)1000 * (CCTimeType)1000); // Didn't get this packet - set the time to give up waiting + hasReceivedPacketQueue.push_back(true); // Didn't get this packet: a hole } - hasReceivedPacketQueue.Push(false, _FILE_AND_LINE_); // Got the packet + hasReceivedPacketQueue.push_back(false); // Got the packet #ifdef _DEBUG // If this assert hits then DatagramSequenceNumberType has overflowed - RakAssert(hasReceivedPacketQueue.Size() < (unsigned int) ((DatagramSequenceNumberType) (uint32_t) (-1))); + RakAssert(hasReceivedPacketQueue.size() < (unsigned int) ((DatagramSequenceNumberType) (uint32_t) (-1))); #endif } - while (hasReceivedPacketQueue.Size() > 0 && !hasReceivedPacketQueue.Peek()) + while (!hasReceivedPacketQueue.empty() && !hasReceivedPacketQueue.front()) { - hasReceivedPacketQueue.Pop(); + hasReceivedPacketQueue.pop_front(); ++receivedPacketsBaseIndex; } } - // If the allocated buffer is > DEFAULT_HAS_RECEIVED_PACKET_QUEUE_SIZE and it is 3x greater than the number of elements actually being used - if (hasReceivedPacketQueue.AllocationSize() > (unsigned int) DEFAULT_HAS_RECEIVED_PACKET_QUEUE_SIZE && hasReceivedPacketQueue.AllocationSize() > hasReceivedPacketQueue.Size() * 3) - { - hasReceivedPacketQueue.Compress(_FILE_AND_LINE_); - } - /* if (internalPacket->reliability == MafiaNet::Reliability::ReliableSequenced || internalPacket->reliability == MafiaNet::Reliability::UnreliableSequenced) { @@ -1314,7 +1197,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( FreeInternalPacketData(internalPacket, _FILE_AND_LINE_); ReleaseToInternalPacketPool(internalPacket); - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) { + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("internalPacket->orderingChannel >= NUMBER_OF_ORDERED_STREAMS", BYTES_TO_BITS(length), systemAddress); } @@ -1343,7 +1226,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( waitingForSequencedPacketReadIndex[ internalPacket->orderingChannel ] = internalPacket->orderingIndex + (OrderingIndexType)1; // If there is a rebuilt packet, add it to the output queue - outputQueue.Push(internalPacket, _FILE_AND_LINE_); + outputQueue.push_back(internalPacket); internalPacket = 0; } @@ -1354,7 +1237,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( // Not a split packet. Add the packet to the output queue bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_PROCESSED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength)); - outputQueue.Push( internalPacket, _FILE_AND_LINE_ ); + outputQueue.push_back(internalPacket); internalPacket = 0; } } else { @@ -1391,79 +1274,6 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( } */ - /* - if ( internalPacket->reliability == MafiaNet::Reliability::ReliableOrdered ) - { -#ifdef _DEBUG - RakAssert( internalPacket->orderingChannel < NUMBER_OF_ORDERED_STREAMS ); -#endif - - if ( internalPacket->orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) - { - // Invalid packet - FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ ); - ReleaseToInternalPacketPool( internalPacket ); - - bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength)); - - goto CONTINUE_SOCKET_DATA_PARSE_LOOP; - } - - bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_PROCESSED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength)); - - if ( waitingForOrderedPacketReadIndex[ internalPacket->orderingChannel ] == internalPacket->orderingIndex ) - { - // Get the list to hold ordered packets for this stream - DataStructures::LinkedList *orderingListAtOrderingStream; - unsigned char orderingChannelCopy = internalPacket->orderingChannel; - - // Push the packet for the user to read - outputQueue.Push( internalPacket, _FILE_AND_LINE_ ); - internalPacket = 0; // Don't reference this any longer since other threads access it - - // Wait for the resendNext ordered packet in sequence - waitingForOrderedPacketReadIndex[ orderingChannelCopy ] ++; // This wraps - - orderingListAtOrderingStream = GetOrderingListAtOrderingStream( orderingChannelCopy ); - - if ( orderingListAtOrderingStream != 0) - { - while ( orderingListAtOrderingStream->Size() > 0 ) - { - // Cycle through the list until nothing is found - orderingListAtOrderingStream->Beginning(); - indexFound=false; - size=orderingListAtOrderingStream->Size(); - count=0; - - while (count++ < size) - { - if ( orderingListAtOrderingStream->Peek()->orderingIndex == waitingForOrderedPacketReadIndex[ orderingChannelCopy ] ) - { - outputQueue.Push( orderingListAtOrderingStream->Pop(), _FILE_AND_LINE_ ); - waitingForOrderedPacketReadIndex[ orderingChannelCopy ]++; - indexFound=true; - } - else - (*orderingListAtOrderingStream)++; - } - - if (indexFound==false) - break; - } - } - internalPacket = 0; - } - else - { - // This is a newer ordered packet than we are waiting for. Store it for future use - AddToOrderingList( internalPacket ); - } - - - goto CONTINUE_SOCKET_DATA_PARSE_LOOP; - } - */ // Is this a split packet? If so then reassemble if (internalPacket->splitPacketCount > 0) @@ -1482,7 +1292,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( if (internalPacket == 0) { #ifdef LOG_TRIVIAL_NOTIFICATIONS - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("BuildPacketFromSplitPacketList did not return anything.", BYTES_TO_BITS(length), systemAddress, false); #endif @@ -1501,7 +1311,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( { if (verdict == RESPLIT_MALFORMED) { - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Malformed re-split header", BYTES_TO_BITS(length), systemAddress, true); } bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead, BITS_TO_BYTES(copyBitLength)); @@ -1599,7 +1409,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( #endif #ifdef LOG_TRIVIAL_NOTIFICATIONS - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Sequenced rejected: lower than highest known value", BYTES_TO_BITS(length), systemAddress, false); #endif @@ -1614,16 +1424,16 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( { // Push to output buffer immediately bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_PROCESSED].Push1(timeRead, BITS_TO_BYTES(internalPacket->dataBitLength)); - outputQueue.Push(internalPacket, _FILE_AND_LINE_); + outputQueue.push_back(internalPacket); #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST if (packetId == ID_USER_PACKET_ENUM + 1 && fp) { fprintf(fp, "outputting immediate %i, %s. OI=%i. SI=%i.", receivedPacketNumber, type, internalPacket->orderingIndex.val, internalPacket->sequencingIndex); - if (orderingHeaps[internalPacket->orderingChannel].Size() == 0) + if (orderingHeaps[internalPacket->orderingChannel].empty()) fprintf(fp, "heap empty\n"); else - fprintf(fp, "heap head=%i\n", orderingHeaps[internalPacket->orderingChannel].Peek()->orderingIndex.val); + fprintf(fp, "heap head=%i\n", orderingHeaps[internalPacket->orderingChannel].front().packet->orderingIndex.val); if (receivedPacketNumber < packetNumber) { @@ -1644,24 +1454,24 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( // Return off heap until order lost const unsigned char channel = internalPacket->orderingChannel; - while (orderingHeaps[channel].Size() > 0) + while (!orderingHeaps[channel].empty()) { // A buffered packet older than the read index is a second copy of a message already // delivered. It sorts first, so leaving it would stop this loop for good and stall the // channel: drop it, as a late arrival is dropped. A re-split copy sent after an MTU // step-down used to be one (#93); ResolveReSplitCopy now discards those before they get // here, so this is a backstop. - if (IsOlderOrderedPacket(orderingHeaps[channel].Peek()->orderingIndex, orderedReadIndex[channel])) + if (IsOlderOrderedPacket(orderingHeaps[channel].front().packet->orderingIndex, orderedReadIndex[channel])) { - InternalPacket *stale = orderingHeaps[channel].Pop(0); + InternalPacket *stale = HeapPop(orderingHeaps[channel]); bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead, BITS_TO_BYTES(stale->dataBitLength)); FreeInternalPacketData(stale, _FILE_AND_LINE_); ReleaseToInternalPacketPool(stale); continue; } - if (orderingHeaps[channel].Peek()->orderingIndex != orderedReadIndex[channel]) + if (orderingHeaps[channel].front().packet->orderingIndex != orderedReadIndex[channel]) break; - internalPacket = orderingHeaps[channel].Pop(0); + internalPacket = HeapPop(orderingHeaps[channel]); #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST BitStream bitStream2(internalPacket->data, BITS_TO_BYTES(internalPacket->dataBitLength), false); @@ -1695,7 +1505,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( #endif bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_PROCESSED].Push1(timeRead, BITS_TO_BYTES(internalPacket->dataBitLength)); - outputQueue.Push(internalPacket, _FILE_AND_LINE_); + outputQueue.push_back(internalPacket); if (internalPacket->reliability == MafiaNet::Reliability::ReliableOrdered) { @@ -1718,7 +1528,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( // Sequenced has a lower heap weight, ordered has max sequenced weight // Keep orderedHoleCount count small - if (orderingHeaps[internalPacket->orderingChannel].Size() == 0) + if (orderingHeaps[internalPacket->orderingChannel].empty()) heapIndexOffsets[internalPacket->orderingChannel] = orderedReadIndex[internalPacket->orderingChannel]; reliabilityHeapWeightType orderedHoleCount = internalPacket->orderingIndex - heapIndexOffsets[internalPacket->orderingChannel]; @@ -1728,7 +1538,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( weight += internalPacket->sequencingIndex; else weight += (1048576 - 1); - orderingHeaps[internalPacket->orderingChannel].Push(weight, internalPacket, _FILE_AND_LINE_); + HeapPush(orderingHeaps[internalPacket->orderingChannel], orderingHeapNextSeq[internalPacket->orderingChannel], weight, internalPacket); #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST if (packetId == ID_USER_PACKET_ENUM + 1 && fp) @@ -1739,7 +1549,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( #endif #ifdef LOG_TRIVIAL_NOTIFICATIONS - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Larger number ordered packet leaving holes", BYTES_TO_BITS(length), systemAddress, false); #endif @@ -1753,7 +1563,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( ReleaseToInternalPacketPool(internalPacket); #ifdef LOG_TRIVIAL_NOTIFICATIONS - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Rejected older resend", BYTES_TO_BITS(length), systemAddress, false); #endif @@ -1765,7 +1575,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_PROCESSED].Push1(timeRead, BITS_TO_BYTES(internalPacket->dataBitLength)); // Nothing special about this packet. Add it to the output queue - outputQueue.Push(internalPacket, _FILE_AND_LINE_); + outputQueue.push_back(internalPacket); internalPacket = 0; } @@ -1791,12 +1601,10 @@ BitSize_t ReliabilityLayer::Receive(unsigned char **data) { InternalPacket *internalPacket; - if (outputQueue.Size() > 0) + if (!outputQueue.empty()) { - // #ifdef _DEBUG - // RakAssert(bitStream->GetNumberOfBitsUsed()==0); - // #endif - internalPacket = outputQueue.Pop(); + internalPacket = outputQueue.front(); + outputQueue.pop_front(); BitSize_t bitLength; *data = internalPacket->data; @@ -1954,8 +1762,9 @@ bool ReliabilityLayer::Send(char *data, BitSize_t numberOfBitsToSend, MafiaNet:: RakAssert(internalPacket->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); RakAssert(internalPacket->messageNumberAssigned == false); - outgoingPacketBuffer.Push(GetNextWeight((int) internalPacket->priority), internalPacket, _FILE_AND_LINE_); - RakAssert(outgoingPacketBuffer.Size() == 0 || outgoingPacketBuffer.Peek()->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); + const reliabilityHeapWeightType weight = GetNextWeight((int) internalPacket->priority); + HeapPush(outgoingPacketBuffer, outgoingPacketBufferNextSeq, weight, internalPacket); + RakAssert(outgoingPacketBuffer.empty() || outgoingPacketBuffer.front().packet->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); statistics.messageInSendBuffer[(int) internalPacket->priority]++; statistics.bytesInSendBuffer[(int) internalPacket->priority] += (double) BITS_TO_BYTES(internalPacket->dataBitLength); @@ -1967,7 +1776,7 @@ bool ReliabilityLayer::Send(char *data, BitSize_t numberOfBitsToSend, MafiaNet:: //------------------------------------------------------------------------------------------------------- void ReliabilityLayer::Update(RakNetSocket2 *s, SystemAddress &systemAddress, int MTUSize, CCTimeType time, unsigned bitsPerSecondLimit, - DataStructures::List &messageHandlerList, + std::vector &messageHandlerList, RakNetRandom *rnr, BitStream &updateBitStream) { @@ -1976,7 +1785,7 @@ void ReliabilityLayer::Update(RakNetSocket2 *s, SystemAddress &systemAddress, in void ReliabilityLayer::UpdateAndForceACKs(RakNetSocket2 *s, SystemAddress &systemAddress, int MTUSize, CCTimeType time, unsigned bitsPerSecondLimit, - DataStructures::List &messageHandlerList, + std::vector &messageHandlerList, RakNetRandom *rnr, BitStream &updateBitStream) { @@ -1985,7 +1794,7 @@ void ReliabilityLayer::UpdateAndForceACKs(RakNetSocket2 *s, SystemAddress &syste void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAddress, int MTUSize, CCTimeType time, unsigned bitsPerSecondLimit, - DataStructures::List &messageHandlerList, + std::vector &messageHandlerList, RakNetRandom *rnr, BitStream &updateBitStream, bool forceSendACKs) { @@ -2000,18 +1809,19 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd #endif #ifdef _DEBUG - while (delayList.Size()) + while (!delayList.empty()) { - if (delayList.Peek()->sendTime <= timeMs) + if (delayList.front()->sendTime <= timeMs) { - DataAndTime *dat = delayList.Pop(); + DataAndTime *dat = delayList.front(); + delayList.pop_front(); // SocketLayer::SendTo( dat->s, dat->data, dat->length, systemAddress, __FILE__, __LINE__ ); RNS2_SendParameters bsp; bsp.data = (char *) dat->data; bsp.length = dat->length; bsp.systemAddress = systemAddress; - dat->s->Send(&bsp, _FILE_AND_LINE_); + dat->s->Send(&bsp); delete dat; } @@ -2126,7 +1936,7 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd // sendPacketSet[1].IsEmpty()==false || // sendPacketSet[2].IsEmpty()==false || // sendPacketSet[3].IsEmpty()==false; - bandwidthExceededStatistic = outgoingPacketBuffer.Size() > 0; + bandwidthExceededStatistic = !outgoingPacketBuffer.empty(); const bool hasDataToSendOrResend = IsResendQueueEmpty() == false || bandwidthExceededStatistic; RakAssert(MafiaNet::NUMBER_OF_PRIORITIES == 4); @@ -2152,26 +1962,26 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd lastBpsClear = time; } - if (unreliableWithAckReceiptHistory.Size() > 0) + if (!unreliableWithAckReceiptHistory.empty()) { - i = 0; - while (i < unreliableWithAckReceiptHistory.Size()) + std::size_t receiptIndex = 0; + while (receiptIndex < unreliableWithAckReceiptHistory.size()) { - //if (unreliableWithAckReceiptHistory[i].nextActionTime < time) - if (time - unreliableWithAckReceiptHistory[i].nextActionTime < (((CCTimeType) -1) / 2)) + //if (unreliableWithAckReceiptHistory[receiptIndex].nextActionTime < time) + if (time - unreliableWithAckReceiptHistory[receiptIndex].nextActionTime < (((CCTimeType) -1) / 2)) { InternalPacket *ackReceipt = AllocateFromInternalPacketPool(); AllocInternalPacketData(ackReceipt, 5, false, _FILE_AND_LINE_); ackReceipt->dataBitLength = BYTES_TO_BITS(5); ackReceipt->data[0] = (MessageID) ID_SND_RECEIPT_LOSS; - memcpy(ackReceipt->data + sizeof(MessageID), &unreliableWithAckReceiptHistory[i].sendReceiptSerial, sizeof(uint32_t)); - outputQueue.Push(ackReceipt, _FILE_AND_LINE_); + memcpy(ackReceipt->data + sizeof(MessageID), &unreliableWithAckReceiptHistory[receiptIndex].sendReceiptSerial, sizeof(uint32_t)); + outputQueue.push_back(ackReceipt); - // Remove, swap with last - unreliableWithAckReceiptHistory.RemoveAtIndex(i); + // Remove, keeping the order of the rest + unreliableWithAckReceiptHistory.erase(unreliableWithAckReceiptHistory.begin() + receiptIndex); } else - i++; + receiptIndex++; } } @@ -2258,12 +2068,12 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd pushedAnything = true; - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { #if CC_TIME_TYPE_BYTES == 4 - messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, packetsToSendThisUpdateDatagramBoundaries.Size() + congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (MafiaNet::TimeMS) time, true); + messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, (uint32_t) packetsToSendThisUpdateDatagramBoundaries.size() + congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (MafiaNet::TimeMS) time, true); #else - messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, packetsToSendThisUpdateDatagramBoundaries.Size() + congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (MafiaNet::TimeMS)(time / (CCTimeType) 1000), true); + messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, (uint32_t) packetsToSendThisUpdateDatagramBoundaries.size() + congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (MafiaNet::TimeMS)(time / (CCTimeType) 1000), true); #endif } @@ -2303,7 +2113,7 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd ((int) BITS_TO_BYTES(allDatagramSizesSoFar) < transmissionBandwidth || // This condition means if we want to send a datagram pair, and only have one datagram buffered, exceed bandwidth to add another (countdownToNextPacketPair == 0 && - datagramsToSendThisUpdateIsPair.Size() == 1))) + datagramsToSendThisUpdateIsPair.size() == 1))) { // Fill with packets until MTU is reached // for ( i = 0; i < MafiaNet::NUMBER_OF_PRIORITIES; i++ ) @@ -2313,20 +2123,20 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd statistics.isLimitedByOutgoingBandwidthLimit = bitsPerSecondLimit != 0 && BITS_TO_BYTES(bitsPerSecondLimit) < bpsMetrics[USER_MESSAGE_BYTES_SENT].GetBPS1(time); - while (outgoingPacketBuffer.Size() && + while (!outgoingPacketBuffer.empty() && statistics.isLimitedByOutgoingBandwidthLimit == false) //while ( sendPacketSet[ i ].Size() ) { - internalPacket = outgoingPacketBuffer.Peek(); + internalPacket = outgoingPacketBuffer.front().packet; RakAssert(internalPacket->messageNumberAssigned == false); - RakAssert(outgoingPacketBuffer.Size() == 0 || outgoingPacketBuffer.Peek()->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); + RakAssert(outgoingPacketBuffer.empty() || outgoingPacketBuffer.front().packet->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); // internalPacket = sendPacketSet[ i ].Peek(); if (internalPacket->data == 0) { //sendPacketSet[ i ].Pop(); - outgoingPacketBuffer.Pop(0); - RakAssert(outgoingPacketBuffer.Size() == 0 || outgoingPacketBuffer.Peek()->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); + HeapPop(outgoingPacketBuffer); + RakAssert(outgoingPacketBuffer.empty() || outgoingPacketBuffer.front().packet->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); statistics.messageInSendBuffer[(int) internalPacket->priority]--; statistics.bytesInSendBuffer[(int) internalPacket->priority] -= (double) BITS_TO_BYTES(internalPacket->dataBitLength); ReleaseToInternalPacketPool(internalPacket); @@ -2355,8 +2165,8 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd isReliable = false; //sendPacketSet[ i ].Pop(); - outgoingPacketBuffer.Pop(0); - RakAssert(outgoingPacketBuffer.Size() == 0 || outgoingPacketBuffer.Peek()->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); + HeapPop(outgoingPacketBuffer); + RakAssert(outgoingPacketBuffer.empty() || outgoingPacketBuffer.front().packet->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); RakAssert(internalPacket->messageNumberAssigned == false); statistics.messageInSendBuffer[(int) internalPacket->priority]--; statistics.bytesInSendBuffer[(int) internalPacket->priority] -= (double) BITS_TO_BYTES(internalPacket->dataBitLength); @@ -2408,11 +2218,10 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd } else if (internalPacket->reliability == MafiaNet::Reliability::UnreliableWithAckReceipt) { - unreliableWithAckReceiptHistory.Push(UnreliableWithAckReceiptNode( - congestionManager.GetNextDatagramSequenceNumber() + packetsToSendThisUpdateDatagramBoundaries.Size(), - internalPacket->sendReceiptSerial, - congestionManager.GetRTOForRetransmission(internalPacket->timesSent + 1) + time), - _FILE_AND_LINE_); + unreliableWithAckReceiptHistory.push_back(UnreliableWithAckReceiptNode( + congestionManager.GetNextDatagramSequenceNumber() + (uint32_t) packetsToSendThisUpdateDatagramBoundaries.size(), + internalPacket->sendReceiptSerial, + congestionManager.GetRTOForRetransmission(internalPacket->timesSent + 1) + time)); } // If isReliable is false, the packet and its contents will be added to a list to be freed in ClearPacketsAndDatagrams @@ -2426,12 +2235,12 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd PushPacket(time, internalPacket, isReliable); internalPacket->timesSent++; - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { #if CC_TIME_TYPE_BYTES == 4 - messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, packetsToSendThisUpdateDatagramBoundaries.Size() + congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (MafiaNet::TimeMS) time, true); + messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, (uint32_t) packetsToSendThisUpdateDatagramBoundaries.size() + congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (MafiaNet::TimeMS) time, true); #else - messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, packetsToSendThisUpdateDatagramBoundaries.Size() + congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (MafiaNet::TimeMS)(time / (CCTimeType) 1000), true); + messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, (uint32_t) packetsToSendThisUpdateDatagramBoundaries.size() + congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (MafiaNet::TimeMS)(time / (CCTimeType) 1000), true); #endif } pushedAnything = true; @@ -2459,7 +2268,7 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd // sendmmsg. Loop-local: flushed at the single loop exit below. RNS2SendBatch sendBatch(s, systemAddress); #endif - for (unsigned int datagramIndex = 0; datagramIndex < packetsToSendThisUpdateDatagramBoundaries.Size(); datagramIndex++) + for (unsigned int datagramIndex = 0; datagramIndex < packetsToSendThisUpdateDatagramBoundaries.size(); datagramIndex++) { if (datagramIndex > 0) dhf.isContinuousSend = true; @@ -2529,8 +2338,8 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd // The node just pushed is this datagram's: record its size, estimated the way the black-hole // check estimates a packet's (DatagramHeaderFormat::GetDataHeaderByteLength() for the header), // so that an ack for it compares like with like against failed packets. - datagramHistory[datagramHistory.Size() - 1].wireBytes = (int) BITS_TO_BYTES(updateBitStream.GetNumberOfBitsUsed() - datagramHeaderBits) + - (int) DatagramHeaderFormat::GetDataHeaderByteLength() + UDP_HEADER_SIZE + cipherOverheadBytes; + datagramHistory.back().wireBytes = (int) BITS_TO_BYTES(updateBitStream.GetNumberOfBitsUsed() - datagramHeaderBits) + + (int) DatagramHeaderFormat::GetDataHeaderByteLength() + UDP_HEADER_SIZE + cipherOverheadBytes; // Store what message ids were sent with this datagram // datagramMessageIDTree.Insert(dhf.datagramNumber,idList); @@ -2543,7 +2352,7 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd SendBitStream(s, systemAddress, &updateBitStream, rnr, time); #endif - bandwidthExceededStatistic = outgoingPacketBuffer.Size() > 0; + bandwidthExceededStatistic = !outgoingPacketBuffer.empty(); // bandwidthExceededStatistic=sendPacketSet[0].IsEmpty()==false || // sendPacketSet[1].IsEmpty()==false || // sendPacketSet[2].IsEmpty()==false || @@ -2564,7 +2373,7 @@ void ReliabilityLayer::UpdateInternal(RakNetSocket2 *s, SystemAddress &systemAdd ClearPacketsAndDatagrams(); // Any data waiting to send after attempting to send, then bandwidth is exceeded - bandwidthExceededStatistic = outgoingPacketBuffer.Size() > 0; + bandwidthExceededStatistic = !outgoingPacketBuffer.empty(); // bandwidthExceededStatistic=sendPacketSet[0].IsEmpty()==false || // sendPacketSet[1].IsEmpty()==false || // sendPacketSet[2].IsEmpty()==false || @@ -2638,7 +2447,7 @@ void ReliabilityLayer::SendBitStream(RakNetSocket2 *s, SystemAddress &systemAddr dat->length = length; dat->sendTime = 0; dat->extraSocketOptions = extraSocketOptions; - delayList.PushAtHead(dat, 0, _FILE_AND_LINE_); + delayList.push_front(dat); #else MafiaNet::TimeMS delay = minExtraPing; if (extraPingVariance > 0) @@ -2650,17 +2459,17 @@ void ReliabilityLayer::SendBitStream(RakNetSocket2 *s, SystemAddress &systemAddr dat->s = s; dat->length = length; dat->sendTime = MafiaNet::GetTimeMS() + delay; - for (unsigned int i = 0; i < delayList.Size(); i++) + for (std::size_t i = 0; i < delayList.size(); i++) { if (dat->sendTime < delayList[i]->sendTime) { - delayList.PushAtHead(dat, i, __FILE__, __LINE__); + delayList.insert(delayList.begin() + i, dat); dat = 0; break; } } if (dat != 0) - delayList.Push(dat, __FILE__, __LINE__); + delayList.push_back(dat); return; } #endif @@ -2695,7 +2504,7 @@ void ReliabilityLayer::SendBitStream(RakNetSocket2 *s, SystemAddress &systemAddr bsp.data = (char *) bitStream->GetData(); bsp.length = length; bsp.systemAddress = systemAddress; - s->Send(&bsp, _FILE_AND_LINE_); + s->Send(&bsp); } #endif } @@ -2705,7 +2514,7 @@ void ReliabilityLayer::SendBitStream(RakNetSocket2 *s, SystemAddress &systemAddr //------------------------------------------------------------------------------------------------------- bool ReliabilityLayer::IsOutgoingDataWaiting(void) { - if (outgoingPacketBuffer.Size() > 0) + if (!outgoingPacketBuffer.empty()) return true; // unsigned i; @@ -2802,7 +2611,7 @@ void ReliabilityLayer::UpdateWindowFromAck(CCTimeType time) //------------------------------------------------------------------------------------------------------- // Does what the function name says //------------------------------------------------------------------------------------------------------- -unsigned ReliabilityLayer::RemovePacketFromResendListAndDeleteOlderReliableSequenced(const MessageNumberType messageNumber, CCTimeType time, DataStructures::List &messageHandlerList, const SystemAddress &systemAddress) +unsigned ReliabilityLayer::RemovePacketFromResendListAndDeleteOlderReliableSequenced(const MessageNumberType messageNumber, CCTimeType time, std::vector &messageHandlerList, const SystemAddress &systemAddress) { (void) time; (void) messageNumber; @@ -2813,7 +2622,7 @@ unsigned ReliabilityLayer::RemovePacketFromResendListAndDeleteOlderReliableSeque // OrderingIndexType orderingIndex; // The ID used as identification for ordering channels // unsigned j; - for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++) + for (unsigned int messageHandlerIndex = 0; messageHandlerIndex < messageHandlerList.size(); messageHandlerIndex++) { #if CC_TIME_TYPE_BYTES == 4 messageHandlerList[messageHandlerIndex]->OnAck(messageNumber, systemAddress, time); @@ -2859,7 +2668,7 @@ unsigned ReliabilityLayer::RemovePacketFromResendListAndDeleteOlderReliableSeque ackReceipt->dataBitLength = BYTES_TO_BITS(5); ackReceipt->data[0] = (MessageID) ID_SND_RECEIPT_ACKED; memcpy(ackReceipt->data + sizeof(MessageID), &internalPacket->sendReceiptSerial, sizeof(internalPacket->sendReceiptSerial)); - outputQueue.Push(ackReceipt, _FILE_AND_LINE_); + outputQueue.push_back(ackReceipt); } bool isReliable; @@ -3193,69 +3002,6 @@ bool ReliabilityLayer::CheckSHA1(char code[SHA1_LENGTH], unsigned char *const bu return true; } -/* -//------------------------------------------------------------------------------------------------------- -// Search the specified list for sequenced packets on the specified ordering -// stream, optionally skipping those with splitPacketId, and delete them -//------------------------------------------------------------------------------------------------------- -void ReliabilityLayer::DeleteSequencedPacketsInList( unsigned char orderingChannel, DataStructures::List&theList, int splitPacketId ) -{ - unsigned i = 0; - - while ( i < theList.Size() ) - { - if ( ( - theList[ i ]->reliability == MafiaNet::Reliability::ReliableSequenced || - theList[ i ]->reliability == MafiaNet::Reliability::UnreliableSequenced -// || -// theList[ i ]->reliability == RELIABLE_SEQUENCED_WITH_ACK_RECEIPT || -// theList[ i ]->reliability == UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT - ) && - theList[ i ]->orderingChannel == orderingChannel && ( splitPacketId == -1 || theList[ i ]->splitPacketId != (unsigned int) splitPacketId ) ) - { - InternalPacket * internalPacket = theList[ i ]; - theList.RemoveAtIndex( i ); - FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ ); - ReleaseToInternalPacketPool( internalPacket ); - } - - else - i++; - } -} - -//------------------------------------------------------------------------------------------------------- -// Search the specified list for sequenced packets with a value less than orderingIndex and delete them -// Note - I added functionality so you can use the Queue as a list (in this case for searching) but it is less efficient to do so than a regular list -//------------------------------------------------------------------------------------------------------- -void ReliabilityLayer::DeleteSequencedPacketsInList( unsigned char orderingChannel, DataStructures::Queue&theList ) -{ - InternalPacket * internalPacket; - int listSize = theList.Size(); - int i = 0; - - while ( i < listSize ) - { - if ( ( - theList[ i ]->reliability == MafiaNet::Reliability::ReliableSequenced || - theList[ i ]->reliability == MafiaNet::Reliability::UnreliableSequenced -// || -// theList[ i ]->reliability == RELIABLE_SEQUENCED_WITH_ACK_RECEIPT || -// theList[ i ]->reliability == UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT - ) && theList[ i ]->orderingChannel == orderingChannel ) - { - internalPacket = theList[ i ]; - theList.RemoveAtIndex( i ); - FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ ); - ReleaseToInternalPacketPool( internalPacket ); - listSize--; - } - - else - i++; - } -} -*/ //------------------------------------------------------------------------------------------------------- // Returns true if newPacketOrderingIndex is older than the waitingForPacketOrderingIndex @@ -3352,9 +3098,9 @@ void ReliabilityLayer::SplitPacket(InternalPacket *internalPacket) // InternalPacket *workingPacket; - // Tell the heap we are going to push a list of elements where each element in the list follows the heap order - RakAssert(outgoingPacketBuffer.Size() == 0 || outgoingPacketBuffer.Peek()->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); - outgoingPacketBuffer.StartSeries(); + // The fragments get non-decreasing weights, so each push_heap stops after + // one comparison with its parent (what the old PushSeries shortcut did). + RakAssert(outgoingPacketBuffer.empty() || outgoingPacketBuffer.front().packet->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); // Copy all the new packets into the split packet list for (i = 0; i < (int) internalPacket->splitPacketCount; i++) @@ -3365,8 +3111,9 @@ void ReliabilityLayer::SplitPacket(InternalPacket *internalPacket) // sendPacketSet[ internalPacket->priority ].Push( internalPacketArray[ i ], _FILE_AND_LINE_ ); RakAssert(internalPacketArray[i]->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); RakAssert(internalPacketArray[i]->messageNumberAssigned == false); - outgoingPacketBuffer.PushSeries(GetNextWeight((int) internalPacketArray[i]->priority), internalPacketArray[i], _FILE_AND_LINE_); - RakAssert(outgoingPacketBuffer.Size() == 0 || outgoingPacketBuffer.Peek()->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); + const reliabilityHeapWeightType weight = GetNextWeight((int) internalPacketArray[i]->priority); + HeapPush(outgoingPacketBuffer, outgoingPacketBufferNextSeq, weight, internalPacketArray[i]); + RakAssert(outgoingPacketBuffer.empty() || outgoingPacketBuffer.front().packet->dataBitLength < BYTES_TO_BITS(MAXIMUM_MTU_SIZE)); statistics.messageInSendBuffer[(int) internalPacketArray[i]->priority]++; statistics.bytesInSendBuffer[(int) (int) internalPacketArray[i]->priority] += (double) BITS_TO_BYTES(internalPacketArray[i]->dataBitLength); // workingPacket=sendPacketSet[internalPacket->priority].WriteLock(); @@ -3417,7 +3164,7 @@ void ReliabilityLayer::ReSplitOversizedMessages(void) std::vector retiredNumbers; }; std::vector reSplits; - DataStructures::List resendVictims; + std::vector resendVictims; // Rebuild a full-length copy of the message pkt belongs to, before any of // its packets are freed. Returns 0 (leaving the queues untouched) when the @@ -3525,16 +3272,16 @@ void ReliabilityLayer::ReSplitOversizedMessages(void) if (rebuilt) { reSplits.push_back(ReSplit{rebuilt, false, 0, false, {p->reliableMessageNumber}}); - resendVictims.Push(p, _FILE_AND_LINE_); + resendVictims.push_back(p); } } } p = p->resendNext; } while (p != resendLinkedListHead); } - for (i = 0; i < outgoingPacketBuffer.Size(); i++) + for (const HeapEntry &entry : outgoingPacketBuffer) { - p = outgoingPacketBuffer[i]; + p = entry.packet; if (p->data == 0) continue; if (GetMessageHeaderLengthBits(p) + p->dataBitLength <= maxDatagramBits) @@ -3569,14 +3316,14 @@ void ReliabilityLayer::ReSplitOversizedMessages(void) if (r) { r->retiredNumbers.push_back(p->reliableMessageNumber); - resendVictims.Push(p, _FILE_AND_LINE_); + resendVictims.push_back(p); } p = p->resendNext; } while (p != resendLinkedListHead); } - for (i = 0; i < outgoingPacketBuffer.Size(); i++) + for (const HeapEntry &entry : outgoingPacketBuffer) { - p = outgoingPacketBuffer[i]; + p = entry.packet; ReSplit *r = (p->data != 0 && p->splitPacketCount > 0) ? findStaleSplit(p->splitPacketId) : nullptr; if (r) { @@ -3587,7 +3334,7 @@ void ReliabilityLayer::ReSplitOversizedMessages(void) // Unlink the resend-list victims the way an ack would, minus the receipt. // Their numbers are never sent again: the re-split header retires them. - for (i = 0; i < resendVictims.Size(); i++) + for (i = 0; i < resendVictims.size(); i++) { p = resendVictims[i]; if (resendBuffer[p->reliableMessageNumber & (uint32_t) RESEND_BUFFER_ARRAY_MASK] == p) @@ -3800,7 +3547,7 @@ bool ReliabilityLayer::IsInReceiveWindow(MessageNumberType number) const const DatagramSequenceNumberType typeRange = (DatagramSequenceNumberType) (uint32_t) -1; if (holeCount > typeRange / (DatagramSequenceNumberType) 2) return true; // Older than the window: already received. - return (unsigned int) holeCount < hasReceivedPacketQueue.Size() || holeCount <= (DatagramSequenceNumberType) maxReceiveWindow; + return (unsigned int) holeCount < hasReceivedPacketQueue.size() || holeCount <= (DatagramSequenceNumberType) maxReceiveWindow; } //------------------------------------------------------------------------------------------------------- @@ -3811,7 +3558,7 @@ bool ReliabilityLayer::MarkReliableMessageNumberReceived(MessageNumberType numbe const DatagramSequenceNumberType typeRange = (DatagramSequenceNumberType) (uint32_t) -1; if (holeCount > typeRange / (DatagramSequenceNumberType) 2) return false; // Older than the window: already received. - if ((unsigned int) holeCount < hasReceivedPacketQueue.Size()) + if ((unsigned int) holeCount < hasReceivedPacketQueue.size()) { if (hasReceivedPacketQueue[holeCount] == false) return false; // Already received. @@ -3819,13 +3566,13 @@ bool ReliabilityLayer::MarkReliableMessageNumberReceived(MessageNumberType numbe } else { - while ((unsigned int) holeCount > hasReceivedPacketQueue.Size()) - hasReceivedPacketQueue.Push(true, _FILE_AND_LINE_); - hasReceivedPacketQueue.Push(false, _FILE_AND_LINE_); + while ((unsigned int) holeCount > hasReceivedPacketQueue.size()) + hasReceivedPacketQueue.push_back(true); + hasReceivedPacketQueue.push_back(false); } - while (hasReceivedPacketQueue.Size() > 0 && !hasReceivedPacketQueue.Peek()) + while (!hasReceivedPacketQueue.empty() && !hasReceivedPacketQueue.front()) { - hasReceivedPacketQueue.Pop(); + hasReceivedPacketQueue.pop_front(); ++receivedPacketsBaseIndex; } return true; @@ -3834,11 +3581,10 @@ bool ReliabilityLayer::MarkReliableMessageNumberReceived(MessageNumberType numbe //------------------------------------------------------------------------------------------------------- void ReliabilityLayer::DiscardSplitPacketChannel(SplitPacketIdType id) { - bool objectExists; - const unsigned int index = splitPacketChannelList.GetIndexFromKey(id, &objectExists); - if (!objectExists) + const auto channelIt = splitPacketChannelList.find(id); + if (channelIt == splitPacketChannelList.end()) return; - SplitPacketChannel *channel = splitPacketChannelList[index]; + SplitPacketChannel *channel = channelIt->second; for (const auto &fragment : channel->splitPacketList.Fragments()) { FreeInternalPacketData(fragment.second, _FILE_AND_LINE_); @@ -3852,7 +3598,7 @@ void ReliabilityLayer::DiscardSplitPacketChannel(SplitPacketIdType id) } #endif delete channel; - splitPacketChannelList.RemoveAtIndex(index); + splitPacketChannelList.erase(channelIt); } //------------------------------------------------------------------------------------------------------- @@ -3860,15 +3606,13 @@ void ReliabilityLayer::DiscardSplitPacketChannel(SplitPacketIdType id) //------------------------------------------------------------------------------------------------------- void ReliabilityLayer::InsertIntoSplitPacketList(InternalPacket *internalPacket, CCTimeType time) { - bool objectExists; - unsigned index; - // Find in splitPacketChannelList if a SplitPacketChannel with this splitPacketId was already allocated. If not, allocate and insert the channel into the list. - index = splitPacketChannelList.GetIndexFromKey(internalPacket->splitPacketId, &objectExists); - if (objectExists == false) + // Find the SplitPacketChannel for this splitPacketId. If this is the first fragment seen, allocate one and insert it. + auto channelIt = splitPacketChannelList.find(internalPacket->splitPacketId); + if (channelIt == splitPacketChannelList.end()) { SplitPacketChannel *newChannel = new SplitPacketChannel; + channelIt = splitPacketChannelList.emplace(internalPacket->splitPacketId, newChannel).first; #if PREALLOCATE_LARGE_MESSAGES == 1 - index = splitPacketChannelList.Insert(internalPacket->splitPacketId, newChannel, true, __FILE__, __LINE__); newChannel->returnedPacket = CreateInternalPacketCopy(internalPacket, 0, 0, time); newChannel->gotFirstPacket = false; newChannel->splitPacketsArrived = 0; @@ -3876,80 +3620,79 @@ void ReliabilityLayer::InsertIntoSplitPacketList(InternalPacket *internalPacket, RakAssert(newChannel->returnedPacket->data); #else newChannel->firstPacket = 0; - index = splitPacketChannelList.Insert(internalPacket->splitPacketId, newChannel, true, __FILE__, __LINE__); newChannel->splitPacketList.Init(internalPacket); - #endif } + SplitPacketChannel *channel = channelIt->second; #if PREALLOCATE_LARGE_MESSAGES == 1 - splitPacketChannelList[index]->lastUpdateTime = time; - splitPacketChannelList[index]->splitPacketsArrived++; - splitPacketChannelList[index]->returnedPacket->dataBitLength += internalPacket->dataBitLength; + channel->lastUpdateTime = time; + channel->splitPacketsArrived++; + channel->returnedPacket->dataBitLength += internalPacket->dataBitLength; bool dealloc; if (internalPacket->splitPacketIndex == 0) { - splitPacketChannelList[index]->gotFirstPacket = true; - splitPacketChannelList[index]->stride = BITS_TO_BYTES(internalPacket->dataBitLength); + channel->gotFirstPacket = true; + channel->stride = BITS_TO_BYTES(internalPacket->dataBitLength); - for (unsigned int j = 0; j < splitPacketChannelList[index]->splitPacketList.Size(); j++) + for (unsigned int j = 0; j < channel->splitPacketList.Size(); j++) { - memcpy(splitPacketChannelList[index]->returnedPacket->data + internalPacket->splitPacketIndex * splitPacketChannelList[index]->stride, internalPacket->data, (size_t) BITS_TO_BYTES(internalPacket->dataBitLength)); - FreeInternalPacketData(splitPacketChannelList[index]->splitPacketList[j], __FILE__, __LINE__); - ReleaseToInternalPacketPool(splitPacketChannelList[index]->splitPacketList[j]); + memcpy(channel->returnedPacket->data + internalPacket->splitPacketIndex * channel->stride, internalPacket->data, (size_t) BITS_TO_BYTES(internalPacket->dataBitLength)); + FreeInternalPacketData(channel->splitPacketList[j], __FILE__, __LINE__); + ReleaseToInternalPacketPool(channel->splitPacketList[j]); } - memcpy(splitPacketChannelList[index]->returnedPacket->data, internalPacket->data, (size_t) BITS_TO_BYTES(internalPacket->dataBitLength)); - splitPacketChannelList[index]->splitPacketList.Clear(true, __FILE__, __LINE__); + memcpy(channel->returnedPacket->data, internalPacket->data, (size_t) BITS_TO_BYTES(internalPacket->dataBitLength)); + channel->splitPacketList.Clear(true, __FILE__, __LINE__); dealloc = true; } else { - if (splitPacketChannelList[index]->gotFirstPacket == true) + if (channel->gotFirstPacket == true) { - memcpy(splitPacketChannelList[index]->returnedPacket->data + internalPacket->splitPacketIndex * splitPacketChannelList[index]->stride, internalPacket->data, (size_t) BITS_TO_BYTES(internalPacket->dataBitLength)); + memcpy(channel->returnedPacket->data + internalPacket->splitPacketIndex * channel->stride, internalPacket->data, (size_t) BITS_TO_BYTES(internalPacket->dataBitLength)); dealloc = true; } else { - splitPacketChannelList[index]->splitPacketList.Push(internalPacket, __FILE__, __LINE__); + channel->splitPacketList.Push(internalPacket, __FILE__, __LINE__); dealloc = false; } } - if (splitPacketChannelList[index]->gotFirstPacket == true && + if (channel->gotFirstPacket == true && splitMessageProgressInterval && - // splitPacketChannelList[index]->firstPacket && - // splitPacketChannelList[index]->splitPacketList.Size()!=splitPacketChannelList[index]->firstPacket->splitPacketCount && - // (splitPacketChannelList[index]->splitPacketList.Size()%splitMessageProgressInterval)==0 - splitPacketChannelList[index]->gotFirstPacket && - splitPacketChannelList[index]->splitPacketsArrived != splitPacketChannelList[index]->returnedPacket->splitPacketCount && - (splitPacketChannelList[index]->splitPacketsArrived % splitMessageProgressInterval) == 0) + // channel->firstPacket && + // channel->splitPacketList.Size()!=channel->firstPacket->splitPacketCount && + // (channel->splitPacketList.Size()%splitMessageProgressInterval)==0 + channel->gotFirstPacket && + channel->splitPacketsArrived != channel->returnedPacket->splitPacketCount && + (channel->splitPacketsArrived % splitMessageProgressInterval) == 0) { // Return ID_DOWNLOAD_PROGRESS // Write splitPacketIndex (SplitPacketIndexType) // Write splitPacketCount (SplitPacketIndexType) // Write byteLength (4) - // Write data, splitPacketChannelList[index]->splitPacketList[0]->data + // Write data, the first fragment's data InternalPacket *progressIndicator = AllocateFromInternalPacketPool(); - // unsigned int len = sizeof(MessageID) + sizeof(unsigned int)*2 + sizeof(unsigned int) + (unsigned int) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength); - unsigned int l = (unsigned int) splitPacketChannelList[index]->stride; + // unsigned int len = sizeof(MessageID) + sizeof(unsigned int)*2 + sizeof(unsigned int) + (unsigned int) BITS_TO_BYTES(channel->firstPacket->dataBitLength); + unsigned int l = (unsigned int) channel->stride; const unsigned int len = sizeof(MessageID) + sizeof(unsigned int) * 2 + sizeof(unsigned int) + l; AllocInternalPacketData(progressIndicator, len, false, __FILE__, __LINE__); progressIndicator->dataBitLength = BYTES_TO_BITS(len); progressIndicator->data[0] = (MessageID) ID_DOWNLOAD_PROGRESS; unsigned int temp; - // temp=splitPacketChannelList[index]->splitPacketList.Size(); - temp = splitPacketChannelList[index]->splitPacketsArrived; + // temp=channel->splitPacketList.Size(); + temp = channel->splitPacketsArrived; memcpy(progressIndicator->data + sizeof(MessageID), &temp, sizeof(unsigned int)); temp = (unsigned int) internalPacket->splitPacketCount; memcpy(progressIndicator->data + sizeof(MessageID) + sizeof(unsigned int) * 1, &temp, sizeof(unsigned int)); - // temp=(unsigned int) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength); + // temp=(unsigned int) BITS_TO_BYTES(channel->firstPacket->dataBitLength); temp = (unsigned int) BITS_TO_BYTES(l); memcpy(progressIndicator->data + sizeof(MessageID) + sizeof(unsigned int) * 2, &temp, sizeof(unsigned int)); - //memcpy(progressIndicator->data+sizeof(MessageID)+sizeof(unsigned int)*3, splitPacketChannelList[index]->firstPacket->data, (size_t) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength)); - memcpy(progressIndicator->data + sizeof(MessageID) + sizeof(unsigned int) * 3, splitPacketChannelList[index]->returnedPacket->data, (size_t) BITS_TO_BYTES(l)); + //memcpy(progressIndicator->data+sizeof(MessageID)+sizeof(unsigned int)*3, channel->firstPacket->data, (size_t) BITS_TO_BYTES(channel->firstPacket->dataBitLength)); + memcpy(progressIndicator->data + sizeof(MessageID) + sizeof(unsigned int) * 3, channel->returnedPacket->data, (size_t) BITS_TO_BYTES(l)); } if (dealloc) @@ -3959,52 +3702,52 @@ void ReliabilityLayer::InsertIntoSplitPacketList(InternalPacket *internalPacket, } #else // Insert the packet into the SplitPacketChannel - if (!splitPacketChannelList[index]->splitPacketList.Add(internalPacket)) + if (!channel->splitPacketList.Add(internalPacket)) { FreeInternalPacketData(internalPacket, _FILE_AND_LINE_); ReleaseToInternalPacketPool(internalPacket); // A channel whose first fragment was rejected holds nothing; drop it so // a malformed fragment cannot pin a channel slot. - if (splitPacketChannelList[index]->splitPacketList.GetNumAddedPackets() == 0) + if (channel->splitPacketList.GetNumAddedPackets() == 0) { - delete splitPacketChannelList[index]; - splitPacketChannelList.RemoveAtIndex(index); + delete channel; + splitPacketChannelList.erase(channelIt); } return; } - splitPacketChannelList[index]->lastUpdateTime = time; + channel->lastUpdateTime = time; // If the index is 0, then this is the first packet. Record this so it can be returned to the user with download progress if (internalPacket->splitPacketIndex == 0) - splitPacketChannelList[index]->firstPacket = internalPacket; + channel->firstPacket = internalPacket; // Return download progress if we have the first packet, the list is not complete, and there are enough packets to justify it if (splitMessageProgressInterval && - splitPacketChannelList[index]->firstPacket && - splitPacketChannelList[index]->splitPacketList.GetNumAddedPackets() != splitPacketChannelList[index]->firstPacket->splitPacketCount && - (splitPacketChannelList[index]->splitPacketList.GetNumAddedPackets() % splitMessageProgressInterval) == 0) + channel->firstPacket && + channel->splitPacketList.GetNumAddedPackets() != channel->firstPacket->splitPacketCount && + (channel->splitPacketList.GetNumAddedPackets() % splitMessageProgressInterval) == 0) { // Return ID_DOWNLOAD_PROGRESS // Write splitPacketIndex (SplitPacketIndexType) // Write splitPacketCount (SplitPacketIndexType) // Write byteLength (4) - // Write data, splitPacketChannelList[index]->splitPacketList[0]->data + // Write data, the first fragment's data InternalPacket *progressIndicator = AllocateFromInternalPacketPool(); - unsigned int length = sizeof(MessageID) + sizeof(unsigned int) * 2 + sizeof(unsigned int) + (unsigned int) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength); + unsigned int length = sizeof(MessageID) + sizeof(unsigned int) * 2 + sizeof(unsigned int) + (unsigned int) BITS_TO_BYTES(channel->firstPacket->dataBitLength); AllocInternalPacketData(progressIndicator, length, false, __FILE__, __LINE__); progressIndicator->dataBitLength = BYTES_TO_BITS(length); progressIndicator->data[0] = (MessageID) ID_DOWNLOAD_PROGRESS; unsigned int temp; - temp = splitPacketChannelList[index]->splitPacketList.GetNumAddedPackets(); + temp = channel->splitPacketList.GetNumAddedPackets(); memcpy(progressIndicator->data + sizeof(MessageID), &temp, sizeof(unsigned int)); temp = (unsigned int) internalPacket->splitPacketCount; memcpy(progressIndicator->data + sizeof(MessageID) + sizeof(unsigned int) * 1, &temp, sizeof(unsigned int)); - temp = (unsigned int) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength); + temp = (unsigned int) BITS_TO_BYTES(channel->firstPacket->dataBitLength); memcpy(progressIndicator->data + sizeof(MessageID) + sizeof(unsigned int) * 2, &temp, sizeof(unsigned int)); - memcpy(progressIndicator->data + sizeof(MessageID) + sizeof(unsigned int) * 3, splitPacketChannelList[index]->firstPacket->data, (size_t) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength)); - outputQueue.Push(progressIndicator, __FILE__, __LINE__); + memcpy(progressIndicator->data + sizeof(MessageID) + sizeof(unsigned int) * 3, channel->firstPacket->data, (size_t) BITS_TO_BYTES(channel->firstPacket->dataBitLength)); + outputQueue.push_back(progressIndicator); } #endif @@ -4066,17 +3809,15 @@ InternalPacket *ReliabilityLayer::BuildPacketFromSplitPacketList(SplitPacketIdTy RakNetSocket2 *s, SystemAddress &systemAddress, RakNetRandom *rnr, BitStream &updateBitStream) { - unsigned int i; - bool objectExists; SplitPacketChannel *splitPacketChannel; InternalPacket *internalPacket; // Find in splitPacketChannelList the SplitPacketChannel with this splitPacketId. // It is gone if the fragment just inserted was rejected and was its only one. - i = splitPacketChannelList.GetIndexFromKey(inSplitPacketId, &objectExists); - if (!objectExists) + const auto channelIt = splitPacketChannelList.find(inSplitPacketId); + if (channelIt == splitPacketChannelList.end()) return 0; - splitPacketChannel = splitPacketChannelList[i]; + splitPacketChannel = channelIt->second; #if PREALLOCATE_LARGE_MESSAGES == 1 if (splitPacketChannel->splitPacketsArrived == splitPacketChannel->returnedPacket->splitPacketCount) @@ -4086,8 +3827,8 @@ InternalPacket *ReliabilityLayer::BuildPacketFromSplitPacketList(SplitPacketIdTy { // Ack immediately, because for large files this can take a long time SendACKs(s, systemAddress, time, rnr, updateBitStream); - internalPacket = BuildPacketFromSplitPacketList(splitPacketChannel, time); - splitPacketChannelList.RemoveAtIndex(i); + internalPacket = BuildPacketFromSplitPacketList(splitPacketChannel, time); // deletes the channel + splitPacketChannelList.erase(channelIt); return internalPacket; } else @@ -4095,35 +3836,6 @@ InternalPacket *ReliabilityLayer::BuildPacketFromSplitPacketList(SplitPacketIdTy return 0; } } -/* -//------------------------------------------------------------------------------------------------------- -// Delete any unreliable split packets that have long since expired -void ReliabilityLayer::DeleteOldUnreliableSplitPackets( CCTimeType time ) -{ -unsigned i,j; -i=0; -while (i < splitPacketChannelList.Size()) -{ -#if CC_TIME_TYPE_BYTES==4 -if (time > splitPacketChannelList[i]->lastUpdateTime + timeoutTime && -#else -if (time > splitPacketChannelList[i]->lastUpdateTime + (CCTimeType)timeoutTime*(CCTimeType)1000 && -#endif -(splitPacketChannelList[i]->splitPacketList[0]->reliability==MafiaNet::Reliability::Unreliable || splitPacketChannelList[i]->splitPacketList[0]->reliability==MafiaNet::Reliability::UnreliableSequenced)) -{ -for (j=0; j < splitPacketChannelList[i]->splitPacketList.Size(); j++) -{ -delete[] splitPacketChannelList[i]->splitPacketList[j]->data; -ReleaseToInternalPacketPool(splitPacketChannelList[i]->splitPacketList[j]); -} -delete splitPacketChannelList[i]; -splitPacketChannelList.RemoveAtIndex(i); -} -else -i++; -} -} -*/ //------------------------------------------------------------------------------------------------------- // Creates a copy of the specified internal packet with data copied from the original starting at dataByteOffset for dataByteLength bytes. @@ -4167,25 +3879,6 @@ InternalPacket *ReliabilityLayer::CreateInternalPacketCopy(InternalPacket *origi return copy; } -//------------------------------------------------------------------------------------------------------- -// Get the specified ordering list -//------------------------------------------------------------------------------------------------------- -/* -DataStructures::LinkedList *ReliabilityLayer::GetOrderingListAtOrderingStream( unsigned char orderingChannel ) -{ - if ( orderingChannel >= orderingList.Size() ) - return 0; - - return orderingList[ orderingChannel ]; -} - -//------------------------------------------------------------------------------------------------------- -// Add the internal packet to the ordering list in order based on order index -//------------------------------------------------------------------------------------------------------- -void ReliabilityLayer::AddToOrderingList( InternalPacket * internalPacket ) -{ - } -*/ //------------------------------------------------------------------------------------------------------- // Inserts a packet into the resend list in order @@ -4298,11 +3991,11 @@ CCTimeType ReliabilityLayer::GetAckPing(void) const //------------------------------------------------------------------------------------------------------- void ReliabilityLayer::ResetPacketsAndDatagrams(void) { - packetsToSendThisUpdate.Clear(true, _FILE_AND_LINE_); - packetsToDeallocThisUpdate.Clear(true, _FILE_AND_LINE_); - packetsToSendThisUpdateDatagramBoundaries.Clear(true, _FILE_AND_LINE_); - datagramsToSendThisUpdateIsPair.Clear(true, _FILE_AND_LINE_); - datagramSizesInBytes.Clear(true, _FILE_AND_LINE_); + packetsToSendThisUpdate.clear(); + packetsToDeallocThisUpdate.clear(); + packetsToSendThisUpdateDatagramBoundaries.clear(); + datagramsToSendThisUpdateIsPair.clear(); + datagramSizesInBytes.clear(); datagramSizeSoFar = 0; } //------------------------------------------------------------------------------------------------------- @@ -4312,9 +4005,9 @@ void ReliabilityLayer::PushPacket(CCTimeType time, InternalPacket *internalPacke datagramSizeSoFar += bitsForThisPacket; RakAssert(BITS_TO_BYTES(datagramSizeSoFar) < MAXIMUM_MTU_SIZE - UDP_HEADER_SIZE); allDatagramSizesSoFar += bitsForThisPacket; - packetsToSendThisUpdate.Push(internalPacket, _FILE_AND_LINE_); - packetsToDeallocThisUpdate.Push(isReliable == false, _FILE_AND_LINE_); internalPacket->lastTransmissionTime = time; + packetsToSendThisUpdate.push_back(internalPacket); + packetsToDeallocThisUpdate.push_back(isReliable == false); RakAssert(internalPacket->headerLength == GetMessageHeaderLengthBits(internalPacket)); // This code tells me how much time elapses between when you send, and when the message actually goes out @@ -4334,10 +4027,10 @@ void ReliabilityLayer::PushDatagram(void) { if (datagramSizeSoFar > 0) { - packetsToSendThisUpdateDatagramBoundaries.Push(packetsToSendThisUpdate.Size(), _FILE_AND_LINE_); - datagramsToSendThisUpdateIsPair.Push(false, _FILE_AND_LINE_); + packetsToSendThisUpdateDatagramBoundaries.push_back((unsigned int) packetsToSendThisUpdate.size()); + datagramsToSendThisUpdateIsPair.push_back(false); RakAssert(BITS_TO_BYTES(datagramSizeSoFar) < MAXIMUM_MTU_SIZE - UDP_HEADER_SIZE); - datagramSizesInBytes.Push(BITS_TO_BYTES(datagramSizeSoFar), _FILE_AND_LINE_); + datagramSizesInBytes.push_back(BITS_TO_BYTES(datagramSizeSoFar)); datagramSizeSoFar = 0; // Disable packet pairs @@ -4355,10 +4048,10 @@ void ReliabilityLayer::PushDatagram(void) //------------------------------------------------------------------------------------------------------- bool ReliabilityLayer::TagMostRecentPushAsSecondOfPacketPair(void) { - if (datagramsToSendThisUpdateIsPair.Size() >= 2) + if (datagramsToSendThisUpdateIsPair.size() >= 2) { - datagramsToSendThisUpdateIsPair[datagramsToSendThisUpdateIsPair.Size() - 2] = true; - datagramsToSendThisUpdateIsPair[datagramsToSendThisUpdateIsPair.Size() - 1] = true; + datagramsToSendThisUpdateIsPair[datagramsToSendThisUpdateIsPair.size() - 2] = true; + datagramsToSendThisUpdateIsPair[datagramsToSendThisUpdateIsPair.size() - 1] = true; return true; } return false; @@ -4367,7 +4060,7 @@ bool ReliabilityLayer::TagMostRecentPushAsSecondOfPacketPair(void) void ReliabilityLayer::ClearPacketsAndDatagrams(void) { unsigned int i; - for (i = 0; i < packetsToDeallocThisUpdate.Size(); i++) + for (i = 0; i < packetsToDeallocThisUpdate.size(); i++) { // packetsToDeallocThisUpdate holds a boolean indicating if packetsToSendThisUpdate at this index should be freed if (packetsToDeallocThisUpdate[i]) @@ -4378,7 +4071,7 @@ void ReliabilityLayer::ClearPacketsAndDatagrams(void) ReleaseToInternalPacketPool(packetsToSendThisUpdate[i]); } } - packetsToDeallocThisUpdate.Clear(true, _FILE_AND_LINE_); + packetsToDeallocThisUpdate.clear(); } //------------------------------------------------------------------------------------------------------- void ReliabilityLayer::MoveToListHead(InternalPacket *internalPacket) @@ -4501,27 +4194,10 @@ void ReliabilityLayer::SendACKs(RakNetSocket2 *s, SystemAddress &systemAddress, // congestionManager.OnSendBytes(time,UDP_HEADER_SIZE+updateBitStream.GetNumberOfBytesUsed()); } } -/* -//------------------------------------------------------------------------------------------------------- -ReliabilityLayer::DatagramMessageIDList* ReliabilityLayer::AllocateFromDatagramMessageIDPool(void) -{ -DatagramMessageIDList*s; -s=datagramMessageIDPool.Allocate( _FILE_AND_LINE_ ); -// Call new operator, memoryPool doesn't do this -s = new ((void*)s) DatagramMessageIDList; -return s; -} -//------------------------------------------------------------------------------------------------------- -void ReliabilityLayer::ReleaseToDatagramMessageIDPool(DatagramMessageIDList* d) -{ -d->~DatagramMessageIDList(); -datagramMessageIDPool.Release(d); -} -*/ //------------------------------------------------------------------------------------------------------- InternalPacket *ReliabilityLayer::AllocateFromInternalPacketPool(void) { - InternalPacket *ip = internalPacketPool.Allocate(_FILE_AND_LINE_); + InternalPacket *ip = internalPacketPool.Acquire(); ip->reliableMessageNumber = (MessageNumberType) (uint32_t) -1; ip->messageNumberAssigned = false; ip->nextActionTime = 0; @@ -4539,7 +4215,7 @@ InternalPacket *ReliabilityLayer::AllocateFromInternalPacketPool(void) //------------------------------------------------------------------------------------------------------- void ReliabilityLayer::ReleaseToInternalPacketPool(InternalPacket *ip) { - internalPacketPool.Release(ip, _FILE_AND_LINE_); + internalPacketPool.Release(ip); } //------------------------------------------------------------------------------------------------------- void ReliabilityLayer::RemoveFromUnreliableLinkedList(InternalPacket *internalPacket) @@ -4615,14 +4291,14 @@ bool ReliabilityLayer::ResendBufferOverflow(void) const //------------------------------------------------------------------------------------------------------- ReliabilityLayer::MessageNumberNode *ReliabilityLayer::GetMessageNumberNodeByDatagramIndex(DatagramSequenceNumberType index, CCTimeType *timeSent, int *wireBytes) { - if (datagramHistory.IsEmpty()) + if (datagramHistory.empty()) return nullptr; if (congestionManager.LessThan(index, datagramHistoryPopCount)) return nullptr; DatagramSequenceNumberType offsetIntoList = index - datagramHistoryPopCount; - if (offsetIntoList >= datagramHistory.Size()) + if (offsetIntoList >= datagramHistory.size()) return nullptr; *timeSent = datagramHistory[offsetIntoList].timeSent; @@ -4639,7 +4315,7 @@ void ReliabilityLayer::RemoveFromDatagramHistory(DatagramSequenceNumberType inde while (mnm) { next = mnm->next; - datagramHistoryMessagePool.Release(mnm, _FILE_AND_LINE_); + delete mnm; mnm = next; } datagramHistory[offsetIntoList].head = 0; @@ -4648,14 +4324,14 @@ void ReliabilityLayer::RemoveFromDatagramHistory(DatagramSequenceNumberType inde void ReliabilityLayer::AddFirstToDatagramHistory(DatagramSequenceNumberType datagramNumber, CCTimeType timeSent) { (void) datagramNumber; - if (datagramHistory.Size() > DATAGRAM_MESSAGE_ID_ARRAY_LENGTH) + if (datagramHistory.size() > DATAGRAM_MESSAGE_ID_ARRAY_LENGTH) { RemoveFromDatagramHistory(datagramHistoryPopCount); - datagramHistory.Pop(); + datagramHistory.pop_front(); datagramHistoryPopCount++; } - datagramHistory.Push(DatagramHistoryNode(0, timeSent), _FILE_AND_LINE_); + datagramHistory.push_back(DatagramHistoryNode(0, timeSent)); // printf("%p Pushed empty DatagramHistoryNode to datagram history at index %i\n", this, datagramHistory.Size()-1); } //------------------------------------------------------------------------------------------------------- @@ -4663,24 +4339,24 @@ ReliabilityLayer::MessageNumberNode *ReliabilityLayer::AddFirstToDatagramHistory { (void) datagramNumber; // RakAssert(datagramHistoryPopCount+(unsigned int) datagramHistory.Size()==datagramNumber); - if (datagramHistory.Size() > DATAGRAM_MESSAGE_ID_ARRAY_LENGTH) + if (datagramHistory.size() > DATAGRAM_MESSAGE_ID_ARRAY_LENGTH) { RemoveFromDatagramHistory(datagramHistoryPopCount); - datagramHistory.Pop(); + datagramHistory.pop_front(); datagramHistoryPopCount++; } - MessageNumberNode *mnm = datagramHistoryMessagePool.Allocate(_FILE_AND_LINE_); + MessageNumberNode *mnm = new MessageNumberNode; mnm->next = 0; mnm->messageNumber = messageNumber; - datagramHistory.Push(DatagramHistoryNode(mnm, timeSent), _FILE_AND_LINE_); + datagramHistory.push_back(DatagramHistoryNode(mnm, timeSent)); // printf("%p Pushed message %i to DatagramHistoryNode to datagram history at index %i\n", this, messageNumber.val, datagramHistory.Size()-1); return mnm; } //------------------------------------------------------------------------------------------------------- ReliabilityLayer::MessageNumberNode *ReliabilityLayer::AddSubsequentToDatagramHistory(MessageNumberNode *messageNumberNode, DatagramSequenceNumberType messageNumber) { - messageNumberNode->next = datagramHistoryMessagePool.Allocate(_FILE_AND_LINE_); + messageNumberNode->next = new MessageNumberNode; messageNumberNode->next->messageNumber = messageNumber; messageNumberNode->next->next = 0; return messageNumberNode->next; @@ -4692,8 +4368,7 @@ void ReliabilityLayer::AllocInternalPacketData(InternalPacket *internalPacket, I internalPacket->data = ourOffset; if (*refCounter == 0) { - *refCounter = refCountedDataPool.Allocate(_FILE_AND_LINE_); - // *refCounter = new InternalPacketRefCountedData; + *refCounter = new InternalPacketRefCountedData; (*refCounter)->refCount = 1; (*refCounter)->sharedDataBlock = externallyAllocatedPtr; } @@ -4722,7 +4397,7 @@ void ReliabilityLayer::AllocInternalPacketData(InternalPacket *internalPacket, u } } //------------------------------------------------------------------------------------------------------- -void ReliabilityLayer::FreeInternalPacketData(InternalPacket *internalPacket, const char *file, unsigned int line) +void ReliabilityLayer::FreeInternalPacketData(InternalPacket *internalPacket, [[maybe_unused]] const char *file, [[maybe_unused]] unsigned int line) { if (internalPacket == 0) return; @@ -4737,8 +4412,7 @@ void ReliabilityLayer::FreeInternalPacketData(InternalPacket *internalPacket, co { std::free(internalPacket->refCountedData->sharedDataBlock); internalPacket->refCountedData->sharedDataBlock = 0; - // delete internalPacket->refCountedData; - refCountedDataPool.Release(internalPacket->refCountedData, file, line); + delete internalPacket->refCountedData; internalPacket->refCountedData = 0; } } @@ -4777,10 +4451,10 @@ void ReliabilityLayer::InitHeapWeights(void) reliabilityHeapWeightType ReliabilityLayer::GetNextWeight(int priorityLevel) { uint64_t next = outgoingPacketBufferNextWeights[priorityLevel]; - if (outgoingPacketBuffer.Size() > 0) + if (!outgoingPacketBuffer.empty()) { - int peekPL = (int) outgoingPacketBuffer.Peek()->priority; - reliabilityHeapWeightType weight = outgoingPacketBuffer.PeekWeight(); + int peekPL = (int) outgoingPacketBuffer.front().packet->priority; + reliabilityHeapWeightType weight = outgoingPacketBuffer.front().weight; reliabilityHeapWeightType min = weight - (1 << peekPL) * peekPL + peekPL; if (next < min) next = min + (1 << priorityLevel) * priorityLevel + priorityLevel; diff --git a/Source/src/router2.cpp b/Source/src/router2.cpp index eab3ffd4f..07e433e79 100644 --- a/Source/src/router2.cpp +++ b/Source/src/router2.cpp @@ -148,10 +148,10 @@ bool Router2::ConnectInternal(RakNetGUID endpointGuid, bool returnConnectionLost // StoreRequest(endpointGuid, Largest(ping*2), systemsSentTo). Set state REQUEST_STATE_QUERY_FORWARDING Router2::ConnnectRequest *cr = new Router2::ConnnectRequest; - DataStructures::List addresses; - DataStructures::List guids; + std::vector addresses; + std::vector guids; rakPeerInterface->GetSystemList(addresses, guids); - if (guids.Size() == 0) + if (guids.size() == 0) { char buff[512]; if (debugInterface) @@ -163,7 +163,7 @@ bool Router2::ConnectInternal(RakNetGUID endpointGuid, bool returnConnectionLost cr->pingTimeout = MafiaNet::GetTimeMS() + largestPing * 2 + 1000; cr->endpointGuid = endpointGuid; cr->returnConnectionLostOnFailure = returnConnectionLostOnFailure; - for (unsigned int i = 0; i < guids.Size(); i++) + for (unsigned int i = 0; i < guids.size(); i++) { ConnectionRequestSystem crs; if (guids[i] != endpointGuid) diff --git a/Source/src/send_to_thread.cpp b/Source/src/send_to_thread.cpp index fe4e060e7..759d0cc79 100644 --- a/Source/src/send_to_thread.cpp +++ b/Source/src/send_to_thread.cpp @@ -28,7 +28,7 @@ using namespace MafiaNet; int SendToThread::refCount = 0; -DataStructures::ThreadsafeAllocatingQueue SendToThread::objectQueue; +LockedQueue> SendToThread::objectQueue; ThreadPool SendToThread::threadPool; SendToThread::SendToThreadBlock *SendToWorkerThread(SendToThread::SendToThreadBlock *input, bool *returnOutput, void *perThreadData) @@ -38,7 +38,7 @@ SendToThread::SendToThreadBlock *SendToWorkerThread(SendToThread::SendToThreadBl // MafiaNet::TimeUS *mostRecentTime=(MafiaNet::TimeUS *)input->data; // *mostRecentTime=MafiaNet::GetTimeUS(); SocketLayer::SendTo(input->s, input->data, input->dataWriteOffset, input->systemAddress, _FILE_AND_LINE_); - SendToThread::objectQueue.Push(input); + SendToThread::objectQueue.Push(std::unique_ptr(input)); return 0; } SendToThread::SendToThread() @@ -63,25 +63,20 @@ void SendToThread::Deref(void) threadPool.StopThreads(); RakAssert(threadPool.NumThreadsWorking() == 0); - unsigned i; - SendToThreadBlock *info; - for (i = 0; i < threadPool.InputSize(); i++) - { - info = threadPool.GetInputAtIndex(i); - objectQueue.Push(info); - } + // Blocks still waiting to be sent are dropped; free them together with the reusable ones. + for (unsigned i = 0; i < threadPool.InputSize(); i++) + objectQueue.Push(std::unique_ptr(threadPool.GetInputAtIndex(i))); threadPool.ClearInput(); - objectQueue.Clear(_FILE_AND_LINE_); + objectQueue.Clear(); } } } SendToThread::SendToThreadBlock *SendToThread::AllocateBlock(void) { - SendToThread::SendToThreadBlock *b; - b = objectQueue.Pop(); - if (b == 0) - b = objectQueue.Allocate(_FILE_AND_LINE_); - return b; + // The caller hands the block to ProcessBlock; the worker thread returns it to objectQueue once sent. + if (auto recycled = objectQueue.TryPop()) + return recycled->release(); + return new SendToThreadBlock; } void SendToThread::ProcessBlock(SendToThread::SendToThreadBlock *threadedSend) { diff --git a/Source/src/socket2.cpp b/Source/src/socket2.cpp index abd87f9a5..1074083a4 100644 --- a/Source/src/socket2.cpp +++ b/Source/src/socket2.cpp @@ -128,7 +128,7 @@ void RakNetSocket2::SetRecvEventHandler(RNS2EventHandler *_eventHandler) { eventHandler = _eventHandler; } -RNS2SendResult RakNetSocket2::SendBatch(RNS2_SendParameters *sends, unsigned count, const char *file, unsigned int line) +RNS2SendResult RakNetSocket2::SendBatch(RNS2_SendParameters *sends, unsigned count) { // Portable default: send each datagram individually. Platforms with a real // batched syscall (Linux/sendmmsg) override this. Driving it through @@ -144,7 +144,7 @@ RNS2SendResult RakNetSocket2::SendBatch(RNS2_SendParameters *sends, unsigned cou [&](unsigned offset, unsigned remaining) -> int { (void) remaining; - RNS2SendResult r = Send(&sends[offset], file, line); + RNS2SendResult r = Send(&sends[offset]); return r >= 0 ? 1 : (int) r; }); } @@ -236,7 +236,7 @@ bool IRNS2_Berkley::IsPortInUse(unsigned short port, const char *hostAddress, un bbp.eventHandler = nullptr; SystemAddress boundAddress; RNS2_Berkley *rns2 = (RNS2_Berkley *) RakNetSocket2Allocator::AllocRNS2(); - RNS2BindResult bindResult = rns2->Bind(&bbp, _FILE_AND_LINE_); + RNS2BindResult bindResult = rns2->Bind(&bbp); RakNetSocket2Allocator::DeallocRNS2(rns2); // BR_FAMILY_NOT_SUPPORTED means "cannot tell", not "in use": see the declaration. return bindResult == BR_FAILED_TO_BIND_SOCKET; @@ -250,9 +250,9 @@ void SocketReadCallback(CFSocketRef /*s*/, CFSocketCallBackType /*type*/, CFData } #endif -RNS2BindResult RNS2_Berkley::BindShared(RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line) +RNS2BindResult RNS2_Berkley::BindShared(RNS2_BerkleyBindParameters *bindParameters) { - RNS2BindResult br = BindSharedIPV4And6(bindParameters, file, line); + RNS2BindResult br = BindSharedIPV4And6(bindParameters); if (br != BR_SUCCESS) return br; @@ -263,7 +263,7 @@ RNS2BindResult RNS2_Berkley::BindShared(RNS2_BerkleyBindParameters *bindParamete bsp.length = 4; bsp.systemAddress = SelfSendAddress(); bsp.ttl = 0; - RNS2SendResult sr = Send(&bsp, _FILE_AND_LINE_); + RNS2SendResult sr = Send(&bsp); if (sr < 0) return BR_FAILED_SEND_TEST; @@ -301,7 +301,7 @@ unsigned RNS2_Berkley::RecvFromLoopInt(void) while (endThreads == false) { RNS2RecvStruct *recvFromStruct; - recvFromStruct = binding.eventHandler->AllocRNS2RecvStruct(_FILE_AND_LINE_); + recvFromStruct = binding.eventHandler->AllocRNS2RecvStruct(); if (recvFromStruct != nullptr) { recvFromStruct->socket = this; @@ -315,7 +315,7 @@ unsigned RNS2_Berkley::RecvFromLoopInt(void) else { RakSleep(0); - binding.eventHandler->DeallocRNS2RecvStruct(recvFromStruct, _FILE_AND_LINE_); + binding.eventHandler->DeallocRNS2RecvStruct(recvFromStruct); } } } @@ -362,13 +362,13 @@ void RNS2_Berkley::BlockOnStopRecvPollingThread(void) bsp.length = 4; bsp.systemAddress = SelfSendAddress(); bsp.ttl = 0; - Send(&bsp, _FILE_AND_LINE_); + Send(&bsp); MafiaNet::TimeMS timeout = MafiaNet::GetTimeMS() + 1000; while (isRecvFromLoopThreadActive.GetValue() > 0 && MafiaNet::GetTimeMS() < timeout) { // Get recvfrom to unblock - Send(&bsp, _FILE_AND_LINE_); + Send(&bsp); RakSleep(30); } } @@ -388,18 +388,18 @@ RNS2_Windows::RNS2_Windows() slo = 0; } RNS2_Windows::~RNS2_Windows() {} -RNS2BindResult RNS2_Windows::Bind(RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line) +RNS2BindResult RNS2_Windows::Bind(RNS2_BerkleyBindParameters *bindParameters) { - RNS2BindResult bindResult = BindShared(bindParameters, file, line); + RNS2BindResult bindResult = BindShared(bindParameters); if (bindResult == BR_FAILED_TO_BIND_SOCKET) { // Sometimes windows will fail if the socket is recreated too quickly RakSleep(100); - bindResult = BindShared(bindParameters, file, line); + bindResult = BindShared(bindParameters); } return bindResult; } -RNS2SendResult RNS2_Windows::Send(RNS2_SendParameters *sendParameters, const char *file, unsigned int line) +RNS2SendResult RNS2_Windows::Send(RNS2_SendParameters *sendParameters) { if (slo) { @@ -408,7 +408,7 @@ RNS2SendResult RNS2_Windows::Send(RNS2_SendParameters *sendParameters, const cha if (len >= 0) return len; } - return Send_Windows_Linux(rns2Socket, SocketFamily(), dualStack, sendParameters, file, line); + return Send_Windows_Linux(rns2Socket, SocketFamily(), dualStack, sendParameters); } void RNS2_Windows::GetMyIP(SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS]) { @@ -423,18 +423,18 @@ SocketLayerOverride *RNS2_Windows::GetSocketLayerOverride(void) return slo; } #else -RNS2BindResult RNS2_Linux::Bind(RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line) +RNS2BindResult RNS2_Linux::Bind(RNS2_BerkleyBindParameters *bindParameters) { - return BindShared(bindParameters, file, line); + return BindShared(bindParameters); } -RNS2SendResult RNS2_Linux::Send(RNS2_SendParameters *sendParameters, const char *file, unsigned int line) +RNS2SendResult RNS2_Linux::Send(RNS2_SendParameters *sendParameters) { - return Send_Windows_Linux(rns2Socket, SocketFamily(), dualStack, sendParameters, file, line); + return Send_Windows_Linux(rns2Socket, SocketFamily(), dualStack, sendParameters); } // See the declaration in socket2.h for why __linux__ is required here and not // just the build flag. #if defined(__linux__) -RNS2SendResult RNS2_Linux::SendBatch(RNS2_SendParameters *sends, unsigned count, const char *file, unsigned int line) +RNS2SendResult RNS2_Linux::SendBatch(RNS2_SendParameters *sends, unsigned count) { // sendmmsg has no per-message TTL, whereas the scalar Send() honours // RNS2_SendParameters::ttl by bracketing the sendto with setsockopt(IP_TTL). @@ -445,16 +445,13 @@ RNS2SendResult RNS2_Linux::SendBatch(RNS2_SendParameters *sends, unsigned count, for (unsigned i = 0; i < count; ++i) { if (sends[i].ttl > 0) - return RakNetSocket2::SendBatch(sends, count, file, line); + return RakNetSocket2::SendBatch(sends, count); } // This kernel/sandbox does not implement sendmmsg (see MmsgUnavailable). // Every call would fail identically, so go straight to the portable loop. if (MmsgUnavailable()) - return RakNetSocket2::SendBatch(sends, count, file, line); - - (void) file; - (void) line; + return RakNetSocket2::SendBatch(sends, count); // DriveBatchedSend handles the partial-send resume; each transmit() call // coalesces up to MMSG_BATCH_MAX datagrams into one sendmmsg. sendmmsg @@ -505,7 +502,7 @@ RNS2SendResult RNS2_Linux::SendBatch(RNS2_SendParameters *sends, unsigned count, // sendmmsg turned out to be missing. It fails on the first call, so // `sent` is 0 and no datagram was duplicated by resending the batch. if (sent == 0 && MmsgUnavailable()) - return RakNetSocket2::SendBatch(sends, count, file, line); + return RakNetSocket2::SendBatch(sends, count); return sent; } #endif diff --git a/Source/src/socket2_berkley.cpp b/Source/src/socket2_berkley.cpp index e998fb8af..60fd9e5f3 100644 --- a/Source/src/socket2_berkley.cpp +++ b/Source/src/socket2_berkley.cpp @@ -242,11 +242,8 @@ static bool IsWildcardBindHost(const char *host) return true; } -RNS2BindResult RNS2_Berkley::BindSharedIPV4And6(RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line) +RNS2BindResult RNS2_Berkley::BindSharedIPV4And6(RNS2_BerkleyBindParameters *bindParameters) { - (void) file; - (void) line; - dualStack = false; if (bindParameters->addressFamily != AF_UNSPEC) return BindAddressFamily(bindParameters, bindParameters->addressFamily, false, nullptr); @@ -519,7 +516,7 @@ void RNS2_Berkley::RecvFromBatchedLoop(void) // the event handler -- no extra copy. for (; allocated < MMSG_BATCH_MAX; ++allocated) { - RNS2RecvStruct *s = binding.eventHandler->AllocRNS2RecvStruct(_FILE_AND_LINE_); + RNS2RecvStruct *s = binding.eventHandler->AllocRNS2RecvStruct(); if (s == nullptr) break; s->socket = this; @@ -601,7 +598,7 @@ void RNS2_Berkley::RecvFromBatchedLoop(void) // Release whatever the last pass was still holding. for (unsigned i = 0; i < allocated; ++i) - binding.eventHandler->DeallocRNS2RecvStruct(slots[i], _FILE_AND_LINE_); + binding.eventHandler->DeallocRNS2RecvStruct(slots[i]); } #endif // __linux__ diff --git a/Source/src/socket2_windows_linux.cpp b/Source/src/socket2_windows_linux.cpp index 76e26f5d6..45a10195b 100644 --- a/Source/src/socket2_windows_linux.cpp +++ b/Source/src/socket2_windows_linux.cpp @@ -184,7 +184,7 @@ static int SystemDefaultIPv4Ttl() return ttl; } -RNS2SendResult RNS2_Windows_Linux::Send_Windows_Linux(RNS2Socket rns2Socket, int socketFamily, bool dualStack, RNS2_SendParameters *sendParameters, const char *file, unsigned int line) +RNS2SendResult RNS2_Windows_Linux::Send_Windows_Linux(RNS2Socket rns2Socket, int socketFamily, bool dualStack, RNS2_SendParameters *sendParameters) { // IPv4 targets become ::ffff:a.b.c.d on a dual-stack socket; a target the // socket cannot reach at all is refused here rather than by sendto. @@ -196,10 +196,6 @@ RNS2SendResult RNS2_Windows_Linux::Send_Windows_Linux(RNS2Socket rns2Socket, int int len = 0; do { - (void) file; - (void) line; - - // A temporary TTL (NAT punchthrough): set it with the first option the // socket accepts for this target (see HopLimitOptionsFor), and put the // old value back after the send. diff --git a/Source/src/statistics.cpp b/Source/src/statistics.cpp index b13f38a05..e53913e18 100644 --- a/Source/src/statistics.cpp +++ b/Source/src/statistics.cpp @@ -19,6 +19,7 @@ #include "mafianet/statistics.h" #include // sprintf +#include #include "mafianet/get_time.h" #include "mafianet/string.h" #include "mafianet/linux_adapter.h" diff --git a/Source/src/statistics_history.cpp b/Source/src/statistics_history.cpp index d0a194b83..de197b7bd 100644 --- a/Source/src/statistics_history.cpp +++ b/Source/src/statistics_history.cpp @@ -781,12 +781,12 @@ bool StatisticsHistoryPlugin::UpdateAt(Time curTime) hasSampled = true; lastSampleTime = curTime; - DataStructures::List addresses; - DataStructures::List guids; - DataStructures::List stats; + std::vector addresses; + std::vector guids; + std::vector stats; rakPeerInterface->GetStatisticsList(addresses, guids, stats); - for (unsigned int idx = 0; idx < guids.Size(); idx++) + for (unsigned int idx = 0; idx < guids.size(); idx++) { unsigned int objectIndex = statistics.GetObjectIndex(guids[idx].g); if (objectIndex != (unsigned int) -1) @@ -835,10 +835,10 @@ bool StatisticsHistoryPlugin::UpdateAt(Time curTime) /* RakNetStatistics rns; - DataStructures::List addresses; - DataStructures::List guids; + std::vector addresses; + std::vector guids; rakPeerInterface->GetSystemList(addresses, guids); - for (unsigned int idx = 0; idx < guids.Size(); idx++) + for (unsigned int idx = 0; idx < guids.size(); idx++) { rakPeerInterface->GetStatistics(remoteSystems[idx], &rns); statistics.AddValue(); diff --git a/Source/src/string.cpp b/Source/src/string.cpp index 097c8edfb..79640bba9 100644 --- a/Source/src/string.cpp +++ b/Source/src/string.cpp @@ -30,11 +30,10 @@ using namespace MafiaNet; -//DataStructures::MemoryPool RakString::pool; RakString::SharedString RakString::emptyString = {0, 0, 0, (char *) "", (char *) "", {}}; //RakString::SharedString *RakString::sharedStringFreeList=0; //unsigned int RakString::sharedStringFreeListAllocationCount=0; -DataStructures::List RakString::freeList; +std::vector RakString::freeList; class RakStringCleanup { @@ -325,23 +324,21 @@ const MafiaNet::RakString operator+(const MafiaNet::RakString &lhs, const MafiaN RakString::SharedString *sharedString; RakString::LockMutex(); - // sharedString = RakString::pool.Allocate( _FILE_AND_LINE_ ); - if (RakString::freeList.Size() == 0) + if (RakString::freeList.empty()) { //RakString::sharedStringFreeList=(RakString::SharedString*) std::realloc(RakString::sharedStringFreeList, (RakString::sharedStringFreeListAllocationCount+1024)*sizeof(RakString::SharedString)); unsigned i; for (i = 0; i < 128; i++) { - // RakString::freeList.Insert(RakString::sharedStringFreeList+i+RakString::sharedStringFreeListAllocationCount); RakString::SharedString *ss; ss = (RakString::SharedString *) std::malloc(sizeof(RakString::SharedString)); ss->refCountMutex = new SimpleMutex; - RakString::freeList.Insert(ss, _FILE_AND_LINE_); + RakString::freeList.push_back(ss); } //RakString::sharedStringFreeListAllocationCount+=1024; } - sharedString = RakString::freeList[RakString::freeList.Size() - 1]; - RakString::freeList.RemoveAtIndex(RakString::freeList.Size() - 1); + sharedString = RakString::freeList.back(); + RakString::freeList.pop_back(); RakString::UnlockMutex(); const int smallStringSize = 128 - sizeof(unsigned int) - sizeof(size_t) - sizeof(char *) * 2; @@ -1228,12 +1225,12 @@ void RakString::FreeMemory(void) } void RakString::FreeMemoryNoMutex(void) { - for (unsigned int i = 0; i < freeList.Size(); i++) + for (SharedString *ss : freeList) { - delete freeList[i]->refCountMutex; - std::free(freeList[i]); + delete ss->refCountMutex; + std::free(ss); } - freeList.Clear(false, _FILE_AND_LINE_); + freeList.clear(); } void RakString::Serialize(BitStream *bs) const { @@ -1342,25 +1339,21 @@ void RakString::Clear(void) void RakString::Allocate(size_t len) { RakString::LockMutex(); - // sharedString = RakString::pool.Allocate( _FILE_AND_LINE_ ); - if (RakString::freeList.Size() == 0) + if (RakString::freeList.empty()) { //RakString::sharedStringFreeList=(RakString::SharedString*) std::realloc(RakString::sharedStringFreeList, (RakString::sharedStringFreeListAllocationCount+1024)*sizeof(RakString::SharedString)); unsigned i; for (i = 0; i < 128; i++) { - // RakString::freeList.Insert(RakString::sharedStringFreeList+i+RakString::sharedStringFreeListAllocationCount); - // RakString::freeList.Insert((RakString::SharedString*)std::malloc(sizeof(RakString::SharedString)), _FILE_AND_LINE_); - RakString::SharedString *ss; ss = (RakString::SharedString *) std::malloc(sizeof(RakString::SharedString)); ss->refCountMutex = new SimpleMutex; - RakString::freeList.Insert(ss, _FILE_AND_LINE_); + RakString::freeList.push_back(ss); } //RakString::sharedStringFreeListAllocationCount+=1024; } - sharedString = RakString::freeList[RakString::freeList.Size() - 1]; - RakString::freeList.RemoveAtIndex(RakString::freeList.Size() - 1); + sharedString = RakString::freeList.back(); + RakString::freeList.pop_back(); RakString::UnlockMutex(); const size_t smallStringSize = 128 - sizeof(unsigned int) - sizeof(size_t) - sizeof(char *) * 2; @@ -1539,14 +1532,8 @@ void RakString::Free(void) const size_t smallStringSize = 128 - sizeof(unsigned int) - sizeof(size_t) - sizeof(char *) * 2; if (sharedString->bytesUsed > smallStringSize) std::free(sharedString->bigString); - /* - poolMutex->Lock(); - pool.Release(sharedString); - poolMutex->Unlock(); - */ - RakString::LockMutex(); - RakString::freeList.Insert(sharedString, _FILE_AND_LINE_); + RakString::freeList.push_back(sharedString); RakString::UnlockMutex(); sharedString = &emptyString; diff --git a/Source/src/tcp_interface.cpp b/Source/src/tcp_interface.cpp index 7f702ff6c..a70ad34c9 100644 --- a/Source/src/tcp_interface.cpp +++ b/Source/src/tcp_interface.cpp @@ -401,19 +401,10 @@ void TCPInterface::Stop(void) delete[] remoteClients; remoteClients = 0; - // #low review whether we'd rather use PopInaccurate() here (i.e. check whether related threads accessing the queue terminated already) - // consider even adding a dtor to Packet which would then clear its data (at this point drop this explicit packet deallocation here) - MafiaNet::Packet *packet = incomingMessages.Pop(); - while (packet != nullptr) - { - DeallocatePacket(packet); - packet = incomingMessages.Pop(); - } - incomingMessages.Clear(_FILE_AND_LINE_); - newIncomingConnections.Clear(_FILE_AND_LINE_); - newRemoteClients.Clear(_FILE_AND_LINE_); - lostConnections.Clear(_FILE_AND_LINE_); - requestedCloseConnections.Clear(_FILE_AND_LINE_); + // What the TCP thread queued and the user never took; each packet is freed with its data (IncomingPacketDeleter). + incomingMessages.Clear(); + newIncomingConnections.Clear(); + lostConnections.Clear(); failedConnectionAttempts.Clear(_FILE_AND_LINE_); completedConnectionAttempts.Clear(_FILE_AND_LINE_); failedConnectionAttempts.Clear(_FILE_AND_LINE_); @@ -639,9 +630,8 @@ Packet *TCPInterface::ReceiveInt(void) return 0; if (headPush.IsEmpty() == false) return headPush.Pop(); - Packet *p = incomingMessages.PopInaccurate(); - if (p) - return p; + if (auto p = incomingMessages.TryPop()) + return p->release(); // The user frees it with DeallocatePacket if (tailPush.IsEmpty() == false) return tailPush.Pop(); return 0; @@ -717,17 +707,9 @@ void TCPInterface::DeallocatePacket(Packet *packet) { if (packet == 0) return; - if (packet->deleteData) - { - std::free(packet->data); - incomingMessages.Deallocate(packet, _FILE_AND_LINE_); - } - else - { - // Came from userspace AllocatePacket - std::free(packet->data); - delete packet; - } + // A packet from the TCP thread (deleteData == true) and one from AllocatePacket are both a `new` Packet + // whose data is std::malloc'd. + IncomingPacketDeleter()(packet); } Packet *TCPInterface::AllocatePacket(unsigned dataSize) { @@ -795,18 +777,13 @@ SystemAddress TCPInterface::HasFailedConnectionAttempt(void) } SystemAddress TCPInterface::HasNewIncomingConnection(void) { - SystemAddress *out, out2; - out = newIncomingConnections.PopInaccurate(); - if (out) + if (auto out = newIncomingConnections.TryPop()) { - out2 = *out; - newIncomingConnections.Deallocate(out, _FILE_AND_LINE_); - unsigned int i; for (i = 0; i < messageHandlerList.Size(); i++) - messageHandlerList[i]->OnNewConnection(out2, UNASSIGNED_RAKNET_GUID, true); + messageHandlerList[i]->OnNewConnection(*out, UNASSIGNED_RAKNET_GUID, true); - return out2; // out was just returned to the pool + return *out; } else { @@ -815,18 +792,13 @@ SystemAddress TCPInterface::HasNewIncomingConnection(void) } SystemAddress TCPInterface::HasLostConnection(void) { - SystemAddress *out, out2; - out = lostConnections.PopInaccurate(); - if (out) + if (auto out = lostConnections.TryPop()) { - out2 = *out; - lostConnections.Deallocate(out, _FILE_AND_LINE_); - unsigned int i; for (i = 0; i < messageHandlerList.Size(); i++) - messageHandlerList[i]->OnClosedConnection(out2, UNASSIGNED_RAKNET_GUID, LCR_DISCONNECTION_NOTIFICATION); + messageHandlerList[i]->OnClosedConnection(*out, UNASSIGNED_RAKNET_GUID, LCR_DISCONNECTION_NOTIFICATION); - return out2; // out was just returned to the pool + return *out; } else { @@ -1034,7 +1006,6 @@ RAK_THREAD_DECLARATION(MafiaNet::UpdateTCPInterfaceLoop) const unsigned int BUFF_SIZE = 1048576; //char data[ BUFF_SIZE ]; char *data = (char *) std::malloc(BUFF_SIZE); - Packet *incomingMessage; fd_set readFD, exceptionFD, writeFD; sts->threadRunning.Increment(); @@ -1063,9 +1034,7 @@ RAK_THREAD_DECLARATION(MafiaNet::UpdateTCPInterfaceLoop) rc.isActiveMutex.Unlock(); if (current) { - SystemAddress *lostConnectionSystemAddress = sts->lostConnections.Allocate(_FILE_AND_LINE_); - *lostConnectionSystemAddress = lostAddress; - sts->lostConnections.Push(lostConnectionSystemAddress); + sts->lostConnections.Push(lostAddress); } }; @@ -1164,9 +1133,7 @@ RAK_THREAD_DECLARATION(MafiaNet::UpdateTCPInterfaceLoop) sts->remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); - SystemAddress *newConnectionSystemAddress = sts->newIncomingConnections.Allocate(_FILE_AND_LINE_); - *newConnectionSystemAddress = sts->remoteClients[newRemoteClientIndex].systemAddress; - sts->newIncomingConnections.Push(newConnectionSystemAddress); + sts->newIncomingConnections.Push(sts->remoteClients[newRemoteClientIndex].systemAddress); break; } @@ -1249,7 +1216,7 @@ RAK_THREAD_DECLARATION(MafiaNet::UpdateTCPInterfaceLoop) if (len > 0) { - incomingMessage = sts->incomingMessages.Allocate(_FILE_AND_LINE_); + std::unique_ptr incomingMessage(new Packet()); incomingMessage->data = (unsigned char *) std::malloc(len + 1); memcpy(incomingMessage->data, data, len); incomingMessage->data[len] = 0; // Null terminate this so we can print it out as regular strings. This is different from RakNet which does not do this. @@ -1266,9 +1233,9 @@ RAK_THREAD_DECLARATION(MafiaNet::UpdateTCPInterfaceLoop) } */ incomingMessage->length = len; - incomingMessage->deleteData = true; // actually means came from SPSC, rather than AllocatePacket + incomingMessage->deleteData = true; // Marks a packet from the network (PacketizedTCP relies on it) incomingMessage->systemAddress = slotAddress; - sts->incomingMessages.Push(incomingMessage); + sts->incomingMessages.Push(std::move(incomingMessage)); } else { diff --git a/Source/src/threadsafe_packet_logger.cpp b/Source/src/threadsafe_packet_logger.cpp index 7df9d372b..1d489adb5 100644 --- a/Source/src/threadsafe_packet_logger.cpp +++ b/Source/src/threadsafe_packet_logger.cpp @@ -14,13 +14,9 @@ */ #include "mafianet/native_feature_includes.h" -#include #if _RAKNET_SUPPORT_PacketLogger == 1 #include "mafianet/threadsafe_packet_logger.h" -#include -#include "mafianet/linux_adapter.h" -#include "mafianet/osx_adapter.h" using namespace MafiaNet; @@ -29,27 +25,15 @@ ThreadsafePacketLogger::ThreadsafePacketLogger() } ThreadsafePacketLogger::~ThreadsafePacketLogger() { - char **msg; - while ((msg = logMessages.ReadLock()) != 0) - { - std::free((*msg)); - } } void ThreadsafePacketLogger::Update(void) { - char **msg; - while ((msg = logMessages.ReadLock()) != 0) - { - WriteLog(*msg); - std::free((*msg)); - } + while (auto msg = logMessages.TryPop()) + WriteLog(msg->c_str()); } void ThreadsafePacketLogger::AddToLog(const char *str) { - char **msg = logMessages.WriteLock(); - *msg = (char *) std::malloc(strlen(str) + 1); - strcpy_s(*msg, strlen(str) + 1, str); - logMessages.WriteUnlock(); + logMessages.Push(std::string(str)); } #endif // _RAKNET_SUPPORT_* diff --git a/Source/src/udp_forwarder.cpp b/Source/src/udp_forwarder.cpp index bfb5f571b..09e0afb6b 100644 --- a/Source/src/udp_forwarder.cpp +++ b/Source/src/udp_forwarder.cpp @@ -27,6 +27,7 @@ #include "mafianet/socket_defines.h" #include "mafianet/sockaddr_convert.h" #include "errno.h" +#include #ifdef _WIN32 #include @@ -74,8 +75,6 @@ UDPForwarder::UDPForwarder() maxForwardEntries = DEFAULT_MAX_FORWARD_ENTRIES; nextInputId = 0; - startForwardingInput.SetPageSize(sizeof(StartForwardingInputStruct) * 16); - stopForwardingCommands.SetPageSize(sizeof(StopForwardingStruct) * 16); } UDPForwarder::~UDPForwarder() { @@ -145,17 +144,16 @@ UDPForwarderResult UDPForwarder::StartForwarding(SystemAddress source, SystemAdd unsigned int inputId = nextInputId++; - StartForwardingInputStruct *sfis; - sfis = startForwardingInput.Allocate(_FILE_AND_LINE_); - sfis->source = source; - sfis->destination = destination; - sfis->timeoutOnNoDataMS = timeoutOnNoDataMS; + StartForwardingInputStruct sfis; + sfis.source = source; + sfis.destination = destination; + sfis.timeoutOnNoDataMS = timeoutOnNoDataMS; RakAssert(timeoutOnNoDataMS != 0); if (forceHostAddress && forceHostAddress[0]) - sfis->forceHostAddress = forceHostAddress; - sfis->socketFamily = socketFamily; - sfis->inputId = inputId; - startForwardingInput.Push(sfis); + sfis.forceHostAddress = forceHostAddress; + sfis.socketFamily = socketFamily; + sfis.inputId = inputId; + startForwardingInput.Push(std::move(sfis)); for (;;) { @@ -183,10 +181,9 @@ UDPForwarderResult UDPForwarder::StartForwarding(SystemAddress source, SystemAdd } void UDPForwarder::StopForwarding(SystemAddress source, SystemAddress destination) { - StopForwardingStruct *sfs; - sfs = stopForwardingCommands.Allocate(_FILE_AND_LINE_); - sfs->destination = destination; - sfs->source = source; + StopForwardingStruct sfs; + sfs.destination = destination; + sfs.source = source; stopForwardingCommands.Push(sfs); } void UDPForwarder::RecvFrom(MafiaNet::TimeMS curTime, ForwardEntry *forwardEntry) @@ -349,18 +346,14 @@ void UDPForwarder::UpdateUDPForwarder(void) { MafiaNet::TimeMS curTime = MafiaNet::GetTimeMS(); - StartForwardingInputStruct *sfis; StartForwardingOutputStruct sfos; sfos.forwardingSocket = INVALID_SOCKET; sfos.forwardingPort = 0; sfos.inputId = 0; sfos.result = UDPFORWARDER_RESULT_COUNT; - for (;;) + while (auto sfis = startForwardingInput.TryPop()) { - sfis = startForwardingInput.Pop(); - if (sfis == 0) - break; if (GetUsedForwardEntries() > maxForwardEntries) { @@ -457,17 +450,10 @@ void UDPForwarder::UpdateUDPForwarder(void) startForwardingOutputMutex.Lock(); startForwardingOutput.Push(sfos, _FILE_AND_LINE_); startForwardingOutputMutex.Unlock(); - - startForwardingInput.Deallocate(sfis, _FILE_AND_LINE_); } - StopForwardingStruct *sfs; - - for (;;) + while (auto sfs = stopForwardingCommands.TryPop()) { - sfs = stopForwardingCommands.Pop(); - if (sfs == 0) - break; ForwardEntry *fe; for (unsigned int i = 0; i < forwardListNotUpdated.Size(); i++) @@ -484,8 +470,6 @@ void UDPForwarder::UpdateUDPForwarder(void) break; } } - - stopForwardingCommands.Deallocate(sfs, _FILE_AND_LINE_); } unsigned int i; diff --git a/Tests/Fuzz/reliability_layer_fuzz.cpp b/Tests/Fuzz/reliability_layer_fuzz.cpp index 9b57a6736..5eaebf215 100644 --- a/Tests/Fuzz/reliability_layer_fuzz.cpp +++ b/Tests/Fuzz/reliability_layer_fuzz.cpp @@ -16,9 +16,9 @@ #include #include #include +#include #include "mafianet/bit_stream.h" -#include "mafianet/ds_list.h" #include "mafianet/get_time.h" #include "mafianet/mtu_size.h" #include "mafianet/plugin_interface2.h" @@ -33,7 +33,7 @@ namespace // Acks and resends the layer emits are discarded. struct NullSocket : public RakNetSocket2 { - RNS2SendResult Send(RNS2_SendParameters *sendParameters, const char *, unsigned int) override + RNS2SendResult Send(RNS2_SendParameters *sendParameters) override { return sendParameters->length; } @@ -45,7 +45,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) static NullSocket socket; static SystemAddress remote("127.0.0.1", 40001); static RakNetRandom rnr; - DataStructures::List handlers; + std::vector handlers; BitStream updateBitStream(MAXIMUM_MTU_SIZE); ReliabilityLayer layer; diff --git a/Tests/Integration/ConnectWithSocketTests.cpp b/Tests/Integration/ConnectWithSocketTests.cpp index 694a685ae..50c96b7a8 100644 --- a/Tests/Integration/ConnectWithSocketTests.cpp +++ b/Tests/Integration/ConnectWithSocketTests.cpp @@ -68,7 +68,7 @@ TEST_F(ConnectWithSocket, ConnectWithSocketFromGetSocketsAndGetSocket) { RakPeerInterface *server, *client; - DataStructures::List sockets; + std::vector sockets; TestHelpers::StandardClientPrep(client, destroyList); TestHelpers::StandardServerPrep(server, destroyList); const unsigned short serverPort = TestHelpers::BoundPort(server); diff --git a/Tests/Integration/DualStackDefaultLiveTests.cpp b/Tests/Integration/DualStackDefaultLiveTests.cpp index 02ef9c86b..b74387d2e 100644 --- a/Tests/Integration/DualStackDefaultLiveTests.cpp +++ b/Tests/Integration/DualStackDefaultLiveTests.cpp @@ -47,9 +47,9 @@ namespace { bool IsDualStackPeer(RakPeerInterface *peer) { - DataStructures::List sockets; + std::vector sockets; peer->GetSockets(sockets); - return sockets.Size() == 1 && sockets[0]->IsDualStack(); + return sockets.size() == 1 && sockets[0]->IsDualStack(); } #ifndef _WIN32 diff --git a/Tests/Integration/LockedQueueThreadTests.cpp b/Tests/Integration/LockedQueueThreadTests.cpp new file mode 100644 index 000000000..ba3cd4812 --- /dev/null +++ b/Tests/Integration/LockedQueueThreadTests.cpp @@ -0,0 +1,85 @@ +/* + * 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. + */ + +#include + +#include +#include +#include + +#include "mafianet/get_time.h" +#include "mafianet/locked_queue.h" +#include "mafianet/sleep.h" + +using namespace MafiaNet; + +namespace +{ +constexpr int kProducers = 4; +constexpr int kPushesPerProducer = 10000; + +class LockedQueueThreads : public ::testing::Test +{ +protected: + void TearDown() override + { + // Join even when an assertion bailed out early, so no thread outlives the fixture. + for (std::thread &producer : producers) + if (producer.joinable()) + producer.join(); + } + +public: + // Producer p pushes p*kPushesPerProducer+1 .. (p+1)*kPushesPerProducer, in order. + void Produce(int p) + { + for (int i = 1; i <= kPushesPerProducer; i++) + queue.Push(p * kPushesPerProducer + i); + } + +protected: + LockedQueue queue; + std::vector producers; +}; +} + +TEST_F(LockedQueueThreads, FourProducersOneConsumerLoseNothing) +{ + // Every value in 1..N is pushed exactly once across all producers. + for (int p = 0; p < kProducers; p++) + producers.emplace_back(&LockedQueueThreads::Produce, this, p); + + const std::int64_t expectedCount = (std::int64_t) kProducers * kPushesPerProducer; + const std::int64_t expectedSum = expectedCount * (expectedCount + 1) / 2; + std::vector lastSeenPerProducer(kProducers, 0); + bool perProducerOrderKept = true; + std::int64_t count = 0; + std::int64_t sum = 0; + + const MafiaNet::TimeMS start = MafiaNet::GetTimeMS(); + while (MafiaNet::GetTimeMS() - start < 10000 && count < expectedCount) + { + bool gotAny = false; + while (auto value = queue.TryPop()) + { + gotAny = true; + count++; + sum += *value; + const int producer = (*value - 1) / kPushesPerProducer; + if (*value <= lastSeenPerProducer[producer]) + perProducerOrderKept = false; + lastSeenPerProducer[producer] = *value; + } + if (!gotAny) + RakSleep(0); + } + + EXPECT_EQ(count, expectedCount); + EXPECT_EQ(sum, expectedSum); + EXPECT_TRUE(perProducerOrderKept); + EXPECT_TRUE(queue.IsEmpty()); +} diff --git a/Tests/Integration/ManyClientsOneServerBlockingTests.cpp b/Tests/Integration/ManyClientsOneServerBlockingTests.cpp index ef75376b2..004118ff3 100644 --- a/Tests/Integration/ManyClientsOneServerBlockingTests.cpp +++ b/Tests/Integration/ManyClientsOneServerBlockingTests.cpp @@ -150,8 +150,8 @@ TEST_F(ManyClientsOneServerBlocking, ClientsSurviveRepeatedDisconnectReconnectCy TimeMS entryTime = GetTimeMS(); //Loop entry time - DataStructures::List systemList; - DataStructures::List guidList; + std::vector systemList; + std::vector guidList; printf("Entering disconnect loop \n"); @@ -163,9 +163,9 @@ TEST_F(ManyClientsOneServerBlocking, ClientsSurviveRepeatedDisconnectReconnectCy { clientList[i]->GetSystemList(systemList, guidList); //Get connectionlist - int len = systemList.Size(); + const std::size_t len = systemList.size(); - for (int j = 0; j < len; j++) //Disconnect them all + for (std::size_t j = 0; j < len; j++) //Disconnect them all { clientList[i]->CloseConnection(systemList[j], true, 0, MafiaNet::Priority::Low); @@ -233,8 +233,8 @@ TEST_F(ManyClientsOneServerBlocking, ClientsSurviveRepeatedDisconnectReconnectCy { clientList[i]->GetSystemList(systemList, guidList); - int connNum = guidList.Size(); //Get the number of connections for the current peer + const std::size_t connNum = guidList.size(); //Get the number of connections for the current peer //Did we connect all? - ASSERT_EQ(connNum, 1) << "Peers did not connect normally. Not all clients reconnected normally. Failed on client number " << i; + ASSERT_EQ(connNum, 1u) << "Peers did not connect normally. Not all clients reconnected normally. Failed on client number " << i; } } diff --git a/Tests/Integration/ManyClientsOneServerDeallocateBlockingTests.cpp b/Tests/Integration/ManyClientsOneServerDeallocateBlockingTests.cpp index bce4a4b8a..12bbce4eb 100644 --- a/Tests/Integration/ManyClientsOneServerDeallocateBlockingTests.cpp +++ b/Tests/Integration/ManyClientsOneServerDeallocateBlockingTests.cpp @@ -211,8 +211,8 @@ TEST_F(ManyClientsOneServerDeallocateBlocking, ClientsReconnectAfterDeallocateAn TimeMS entryTime = GetTimeMS(); //Loop entry time - DataStructures::List systemList; - DataStructures::List guidList; + std::vector systemList; + std::vector guidList; printf("Entering disconnect loop \n"); @@ -227,7 +227,7 @@ TEST_F(ManyClientsOneServerDeallocateBlocking, ClientsReconnectAfterDeallocateAn continue; clientList[i]->GetSystemList(systemList, guidList); //Get connectionlist - int len = systemList.Size(); + const std::size_t len = systemList.size(); if (len >= 1) { @@ -347,7 +347,7 @@ TEST_F(ManyClientsOneServerDeallocateBlocking, ClientsReconnectAfterDeallocateAn activeCount++; clientList[i]->GetSystemList(systemList, guidList); - int connNum = guidList.Size(); + const std::size_t connNum = guidList.size(); if (connNum == 1) connectedCount++; } diff --git a/Tests/Integration/ManyClientsOneServerNonBlockingTests.cpp b/Tests/Integration/ManyClientsOneServerNonBlockingTests.cpp index 0880ef8de..28f0e1b4a 100644 --- a/Tests/Integration/ManyClientsOneServerNonBlockingTests.cpp +++ b/Tests/Integration/ManyClientsOneServerNonBlockingTests.cpp @@ -106,8 +106,8 @@ TEST_F(ManyClientsOneServerNonBlocking, ClientsSurviveRapidDisconnectReconnectCy TimeMS entryTime = GetTimeMS(); //Loop entry time - DataStructures::List systemList; - DataStructures::List guidList; + std::vector systemList; + std::vector guidList; printf("Entering disconnect loop \n"); @@ -119,9 +119,9 @@ TEST_F(ManyClientsOneServerNonBlocking, ClientsSurviveRapidDisconnectReconnectCy { clientList[i]->GetSystemList(systemList, guidList); //Get connectionlist - int len = systemList.Size(); + const std::size_t len = systemList.size(); - for (int j = 0; j < len; j++) //Disconnect them all + for (std::size_t j = 0; j < len; j++) //Disconnect them all { clientList[i]->CloseConnection(systemList[j], true, 0, MafiaNet::Priority::Low); @@ -229,8 +229,8 @@ TEST_F(ManyClientsOneServerNonBlocking, ClientsSurviveRapidDisconnectReconnectCy { clientList[i]->GetSystemList(systemList, guidList); - int connNum = guidList.Size(); //Get the number of connections for the current peer + const std::size_t connNum = guidList.size(); //Get the number of connections for the current peer //Did we connect to all? - ASSERT_EQ(connNum, 1) << "Peers did not connect normally. Not all clients reconnected normally. Failed on clients number " << i; + ASSERT_EQ(connNum, 1u) << "Peers did not connect normally. Not all clients reconnected normally. Failed on clients number " << i; } } diff --git a/Tests/Integration/SocketFamilyLiveTests.cpp b/Tests/Integration/SocketFamilyLiveTests.cpp index a5b5697a3..88b580568 100644 --- a/Tests/Integration/SocketFamilyLiveTests.cpp +++ b/Tests/Integration/SocketFamilyLiveTests.cpp @@ -44,10 +44,10 @@ StartupResult StartOnce(SocketDescriptor descriptor, bool *dualStack = nullptr, const StartupResult result = guard.peer->Startup(1, &descriptor, 1); if (result == RAKNET_STARTED) { - DataStructures::List sockets; + std::vector sockets; guard.peer->GetSockets(sockets); if (dualStack != nullptr) - *dualStack = sockets.Size() == 1 && sockets[0]->IsDualStack(); + *dualStack = sockets.size() == 1 && sockets[0]->IsDualStack(); if (bound != nullptr) *bound = guard.peer->GetMyBoundAddress(); } @@ -65,9 +65,9 @@ RakNetSocket2 *StartWildcard(RakPeerInterface *peer, unsigned short family) sd.socketFamily = family; if (peer->Startup(1, &sd, 1) != RAKNET_STARTED) return nullptr; - DataStructures::List sockets; + std::vector sockets; peer->GetSockets(sockets); - return sockets.Size() == 1 ? sockets[0] : nullptr; + return sockets.size() == 1 ? sockets[0] : nullptr; } } // namespace diff --git a/Tests/Integration/SystemAddressAndGuidTests.cpp b/Tests/Integration/SystemAddressAndGuidTests.cpp index feddfa28c..fab636fac 100644 --- a/Tests/Integration/SystemAddressAndGuidTests.cpp +++ b/Tests/Integration/SystemAddressAndGuidTests.cpp @@ -92,16 +92,16 @@ TEST_F(SystemAddressAndGuid, AddressAndGuidLookups) ASSERT_TRUE(TestHelpers::WaitAndConnectTwoPeersLocally(client, server, 5000)) << "Could not connect the client"; - DataStructures::List systemList; - DataStructures::List guidList; + std::vector systemList; + std::vector guidList; printf("Test GetSystemList and NumberOfConnections\n"); client->GetSystemList(systemList, guidList); //Get connectionlist - int len = systemList.Size(); - int len2 = guidList.Size(); + const std::size_t len = systemList.size(); + const std::size_t len2 = guidList.size(); - int conNum = client->NumberOfConnections(); + const std::size_t conNum = client->NumberOfConnections(); printf("Test if systemList size matches guidList size \n"); @@ -112,9 +112,9 @@ TEST_F(SystemAddressAndGuid, AddressAndGuidLookups) { if (conNum == 1 || len == 1) { - ASSERT_EQ(conNum, 1) << "NumberOfConnections problem (system list size is " << len << " and NumberOfConnections return is " << conNum << ")"; + ASSERT_EQ(conNum, 1u) << "NumberOfConnections problem (system list size is " << len << " and NumberOfConnections return is " << conNum << ")"; - ASSERT_EQ(len, 1) << "SystemList problem with GetSystemList (system list size is " << len << " and NumberOfConnections return is " << conNum << ")"; + ASSERT_EQ(len, 1u) << "SystemList problem with GetSystemList (system list size is " << len << " and NumberOfConnections return is " << conNum << ")"; } else { @@ -123,7 +123,7 @@ TEST_F(SystemAddressAndGuid, AddressAndGuidLookups) } else { - ASSERT_EQ(conNum, 1) << "Both SystemList and Number of connections have problems and report same results (system list size is " << len << " and NumberOfConnections return is " << conNum << ")"; + ASSERT_EQ(conNum, 1u) << "Both SystemList and Number of connections have problems and report same results (system list size is " << len << " and NumberOfConnections return is " << conNum << ")"; } printf("Test GetSystemListValues of the system and guid list\n"); diff --git a/Tests/Soak/SimulatedSoakTests.cpp b/Tests/Soak/SimulatedSoakTests.cpp index df214a07c..b3e815bb3 100644 --- a/Tests/Soak/SimulatedSoakTests.cpp +++ b/Tests/Soak/SimulatedSoakTests.cpp @@ -142,15 +142,6 @@ struct Totals uint64_t mtuStepDownsOnCappedLinks = 0; }; -// Pages in a peer's packet pool. Never instantiated: the derived type only names the protected member, read -// through a pointer-to-member on the real RakPeer. -class PacketPoolInspector : public RakPeer -{ -public: - static const DataStructures::MemoryPool &Pool(const RakPeer *peer) { return peer->*&PacketPoolInspector::packetAllocationPool; } - static int64_t Pages(const RakPeer *peer) { return (int64_t) Pool(peer).GetAvailablePagesSize() + Pool(peer).GetUnavailablePagesSize(); } -}; - class SimulatedSoak { public: @@ -837,18 +828,8 @@ void SimulatedSoak::Run() EXPECT_TRUE(released) << "slot leak: " << why; RunFor(1000, false); afterWave.push_back(Soak::Allocations()); - int64_t packetPoolPages = 0; - for (size_t i = 0; i < nodes.size(); ++i) - { - // Every inbox is drained each tick, so no packet is out: no page is full, and an idle pool keeps - // at most DS_MEMORY_POOL_MAX_FREE_PAGES empty ones. - const DataStructures::MemoryPool &pool = PacketPoolInspector::Pool(nodes[i].peer); - EXPECT_EQ(0, pool.GetUnavailablePagesSize()) << Name((int) i) << ": packet-pool pages still full with nothing queued"; - EXPECT_LE(pool.GetAvailablePagesSize(), DS_MEMORY_POOL_MAX_FREE_PAGES) << Name((int) i) << ": packet pool grew past its free-page bound"; - packetPoolPages += PacketPoolInspector::Pages(nodes[i].peer); - } - std::printf("[soak] wave %d done at %" PRIu64 " s virtual: new blocks %" PRId64 " (%" PRId64 " bytes, %" PRId64 " packet-pool pages)\n", - wave, (NowMs() - startMs) / 1000, afterWave.back().newBlocks, afterWave.back().newBytes, packetPoolPages); + std::printf("[soak] wave %d done at %" PRIu64 " s virtual: new blocks %" PRId64 " (%" PRId64 " bytes)\n", + wave, (NowMs() - startMs) / 1000, afterWave.back().newBlocks, afterWave.back().newBytes); } const double wallSeconds = std::chrono::duration(std::chrono::steady_clock::now() - wallStart).count(); diff --git a/Tests/Support/ReliabilityLayerPair.h b/Tests/Support/ReliabilityLayerPair.h index 6377a8303..9e5343a83 100644 --- a/Tests/Support/ReliabilityLayerPair.h +++ b/Tests/Support/ReliabilityLayerPair.h @@ -5,12 +5,26 @@ * license.txt file in the root directory of this source tree. * * Two real ReliabilityLayer instances joined by a fake socket, with fully - * simulated time -- no loopback traffic, no wall clock. The channel between - * them applies a per-test drop rule (to model a tunnel or plain loss), logs - * every a->b datagram, and lets a test inject extra datagrams towards b. + * simulated time -- no RakPeer, no loopback traffic, no threads, no wall + * clock. Each layer writes its datagrams into a CapturingSocket, and the test + * decides which of them reach the other layer, in which order, and how often. * - * Shared by ReliabilityLayerBlackHoleTests.cpp and - * ReliabilityLayerCryptoTests.cpp. + * Two flavours share the socket: + * - MafiaNetTest::ReliabilityLayerPair, a plain struct a test (or a helper + * struct) holds or derives from and drives step by step. Used by the + * characterization and split-packet tests. + * - RelPairTest::ReliabilityLayerPair, a gtest fixture whose channel applies + * a per-test drop rule (to model a tunnel or plain loss), logs every a->b + * datagram, and lets a test inject extra datagrams towards b. Shared by + * ReliabilityLayerBlackHoleTests.cpp, ReliabilityLayerCryptoTests.cpp and + * ReliabilityLayerReSplitHeaderTests.cpp. + * + * `now` starts at the real clock: ReliabilityLayer stamps + * timeLastDatagramArrived with the real GetTimeMS() on every receive, and + * AckTimeout compares that against the time passed in. A fixed fake epoch + * fails on any machine whose process uptime exceeds it (this killed every + * test on Windows CI). All progress is still driven by fixed fake increments + * from this anchor. */ #ifndef MAFIANET_TESTS_RELIABILITY_LAYER_PAIR_H @@ -24,7 +38,6 @@ #include #include "mafianet/bit_stream.h" -#include "mafianet/ds_list.h" #include "mafianet/get_time.h" #include "mafianet/mtu_size.h" #include "mafianet/packet_priority.h" @@ -34,34 +47,119 @@ #include "mafianet/session_crypto.h" #include "mafianet/socket2.h" -namespace RelPairTest +namespace MafiaNetTest { -const int TEST_MTU = MAXIMUM_MTU_SIZE; -const MafiaNet::TimeMS TEST_TIMEOUT_MS = 1000000; - -// Captures every datagram the reliability layer hands to the OS, so the test -// fixture can shuttle it to the other endpoint (or drop it, like a tunnel). +// Captures every datagram a ReliabilityLayer hands to the OS, so the test can +// shuttle it to the other endpoint (or drop, reorder or duplicate it). struct CapturingSocket : public MafiaNet::RakNetSocket2 { std::vector> sent; - virtual MafiaNet::RNS2SendResult Send(MafiaNet::RNS2_SendParameters *sendParameters, const char *file, unsigned int line) + MafiaNet::RNS2SendResult Send(MafiaNet::RNS2_SendParameters *sendParameters) override { - (void) file; - (void) line; sent.push_back(std::vector(sendParameters->data, sendParameters->data + sendParameters->length)); return sendParameters->length; } }; +// Both layers run in mode Plaintext: the keyless cleartext path. A layer in +// mode Required sends and accepts nothing until it has session keys. +struct ReliabilityLayerPair +{ + // Large enough that no layer ever times out within a test. The default + // (10 s, 30 s in Debug) is measured from the real clock at Reset() while + // `now` runs ahead in 10 ms steps: a sender whose unacked messages outlive + // it would go dead and stop sending. + static constexpr MafiaNet::TimeMS kNeverTimeOutMs = 1000000; + + MafiaNet::ReliabilityLayer a, b; + CapturingSocket aSock, bSock; + MafiaNet::SystemAddress aAddr, bAddr; + std::vector handlers; + MafiaNet::RakNetRandom rnr; + MafiaNet::BitStream updateBitStream{MAXIMUM_MTU_SIZE}; + CCTimeType now = MafiaNet::GetTimeUS(); + + ReliabilityLayerPair() + { + EXPECT_TRUE(aAddr.FromStringExplicitPort("127.0.0.1", 40001)); + EXPECT_TRUE(bAddr.FromStringExplicitPort("127.0.0.1", 40002)); + ResetA(); + ResetB(); + } + + void ResetA() + { + a.SetEncryptionMode(MafiaNet::EncryptionMode::Plaintext); + a.Reset(true, MAXIMUM_MTU_SIZE); + a.SetTimeoutTime(kNeverTimeOutMs); + ReanchorClock(); + } + void ResetB() + { + b.SetEncryptionMode(MafiaNet::EncryptionMode::Plaintext); + b.Reset(true, MAXIMUM_MTU_SIZE); + b.SetTimeoutTime(kNeverTimeOutMs); + ReanchorClock(); + } + + // Reset() stamps the layer's lastUpdateTime with the real clock, and Update() + // does nothing for a time at or before it: keep `now` from falling behind. + void ReanchorClock() + { + const CCTimeType real = MafiaNet::GetTimeUS(); + if (real > now) + now = real; + } + + void UpdateA() { a.Update(&aSock, bAddr, MAXIMUM_MTU_SIZE, now, 0, handlers, &rnr, updateBitStream); } + void UpdateB() { b.Update(&bSock, aAddr, MAXIMUM_MTU_SIZE, now, 0, handlers, &rnr, updateBitStream); } + + // Hands one datagram a sent to b, and one b sent to a. + void DeliverToB(std::vector &datagram) + { + b.HandleSocketReceiveFromConnectedPlayer(datagram.data(), (unsigned int) datagram.size(), aAddr, handlers, + MAXIMUM_MTU_SIZE, &bSock, &rnr, now, updateBitStream); + } + void DeliverToA(std::vector &datagram) + { + a.HandleSocketReceiveFromConnectedPlayer(datagram.data(), (unsigned int) datagram.size(), bAddr, handlers, + MAXIMUM_MTU_SIZE, &aSock, &rnr, now, updateBitStream); + } + + // Every complete message `layer` has ready, in delivery order. + static std::vector> Drain(MafiaNet::ReliabilityLayer &layer) + { + std::vector> messages; + unsigned char *data; + MafiaNet::BitSize_t bits; + while ((bits = layer.Receive(&data)) > 0) + { + messages.emplace_back(data, data + BITS_TO_BYTES(bits)); + std::free(data); + } + return messages; + } +}; + +} // namespace MafiaNetTest + +namespace RelPairTest +{ + +const int TEST_MTU = MAXIMUM_MTU_SIZE; +const MafiaNet::TimeMS TEST_TIMEOUT_MS = 1000000; + +using MafiaNetTest::CapturingSocket; + class ReliabilityLayerPair : public ::testing::Test { protected: MafiaNet::ReliabilityLayer a, b; CapturingSocket aSock, bSock; MafiaNet::SystemAddress aAddr, bAddr; - DataStructures::List handlers; + std::vector handlers; MafiaNet::RakNetRandom rnr; MafiaNet::BitStream updateBitStream; CCTimeType now; diff --git a/Tests/Support/SimulatedNetwork.cpp b/Tests/Support/SimulatedNetwork.cpp index 440c742be..90db1330b 100644 --- a/Tests/Support/SimulatedNetwork.cpp +++ b/Tests/Support/SimulatedNetwork.cpp @@ -47,7 +47,7 @@ class SimulatedNetwork::SimSocket : public RakNetSocket2 dualStack = ipv4Alias != UNASSIGNED_SYSTEM_ADDRESS; } - RNS2SendResult Send(RNS2_SendParameters *sendParameters, const char *, unsigned int) override + RNS2SendResult Send(RNS2_SendParameters *sendParameters) override { const SystemAddress &from = dualStack && sendParameters->systemAddress.GetIPVersion() == 4 ? ipv4Alias : boundAddress; @@ -348,7 +348,7 @@ void SimulatedNetwork::DeliverDue() continue; if (datagram.bytes.empty() || datagram.bytes.size() > MAXIMUM_MTU_SIZE) continue; - RNS2RecvStruct *recv = handler->AllocRNS2RecvStruct(_FILE_AND_LINE_); + RNS2RecvStruct *recv = handler->AllocRNS2RecvStruct(); std::memcpy(recv->data, datagram.bytes.data(), datagram.bytes.size()); recv->bytesRead = (int) datagram.bytes.size(); recv->systemAddress = datagram.from; diff --git a/Tests/Support/TestHelpers.cpp b/Tests/Support/TestHelpers.cpp index e893d302c..d85797366 100644 --- a/Tests/Support/TestHelpers.cpp +++ b/Tests/Support/TestHelpers.cpp @@ -46,9 +46,9 @@ unsigned short TestHelpers::StartDualStack(RakPeerInterface *peer, unsigned int *startup = result; if (result != RAKNET_STARTED) return 0; - DataStructures::List sockets; + std::vector sockets; peer->GetSockets(sockets); - if (sockets.Size() != 1 || !sockets[0]->IsDualStack()) + if (sockets.size() != 1 || !sockets[0]->IsDualStack()) return 0; peer->SetMaximumIncomingConnections(maxIncoming); return peer->GetMyBoundAddress().GetPort(); diff --git a/Tests/Support/TestHelpers.h b/Tests/Support/TestHelpers.h index 2f5a69af3..5a286000e 100644 --- a/Tests/Support/TestHelpers.h +++ b/Tests/Support/TestHelpers.h @@ -13,6 +13,7 @@ #include // getenv, used by MAFIANET_SKIP_WITHOUT_IPV6 #include "mafianet/string.h" +#include "mafianet/ds_list.h" #include "mafianet/peer_interface.h" #include "mafianet/message_identifiers.h" diff --git a/Tests/Unit/EightPeerTests.cpp b/Tests/Unit/EightPeerTests.cpp index c98eb0ca9..223f9de05 100644 --- a/Tests/Unit/EightPeerTests.cpp +++ b/Tests/Unit/EightPeerTests.cpp @@ -91,10 +91,10 @@ TEST_F(EightPeerSim, FullMeshConnectsWithoutFailures) for (int i = 0; i < kPeerNum; ++i) { EXPECT_EQ(kPeerNum - 1, ConnectionsSeen(i)) << "peer " << i; - DataStructures::List systemList; - DataStructures::List guidList; + std::vector systemList; + std::vector guidList; peers[i]->GetSystemList(systemList, guidList); - EXPECT_EQ((unsigned) (kPeerNum - 1), guidList.Size()) << "peer " << i; + EXPECT_EQ((unsigned) (kPeerNum - 1), guidList.size()) << "peer " << i; } ExpectNoFailures(); } diff --git a/Tests/Unit/LockedQueueTests.cpp b/Tests/Unit/LockedQueueTests.cpp new file mode 100644 index 000000000..fe08da328 --- /dev/null +++ b/Tests/Unit/LockedQueueTests.cpp @@ -0,0 +1,90 @@ +/* + * 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. + */ + +#include + +#include +#include + +#include "mafianet/locked_queue.h" + +using namespace MafiaNet; + +namespace +{ +// Counts its destructions so a test can see which items the queue freed. +struct Tracked +{ + explicit Tracked(int *counter) + : destroyed(counter) {} + ~Tracked() { ++*destroyed; } + Tracked(const Tracked &) = delete; + Tracked &operator=(const Tracked &) = delete; + int *destroyed; +}; +} + +TEST(LockedQueue, PopsInFifoOrder) +{ + LockedQueue queue; + queue.Push(1); + queue.Push(2); + queue.Push(3); + EXPECT_EQ(queue.Size(), 3u); + EXPECT_EQ(queue.TryPop(), std::optional(1)); + EXPECT_EQ(queue.TryPop(), std::optional(2)); + EXPECT_EQ(queue.TryPop(), std::optional(3)); + EXPECT_EQ(queue.TryPop(), std::nullopt); + EXPECT_TRUE(queue.IsEmpty()); +} + +TEST(LockedQueue, TryPopOnEmptyReturnsNullopt) +{ + LockedQueue queue; + EXPECT_TRUE(queue.IsEmpty()); + EXPECT_EQ(queue.Size(), 0u); + EXPECT_FALSE(queue.TryPop().has_value()); +} + +TEST(LockedQueue, HoldsMoveOnlyItems) +{ + LockedQueue> queue; + queue.Push(std::make_unique(7)); + queue.Push(std::make_unique(8)); + auto first = queue.TryPop(); + ASSERT_TRUE(first.has_value()); + ASSERT_NE(*first, nullptr); + EXPECT_EQ(**first, 7); + auto second = queue.TryPop(); + ASSERT_TRUE(second.has_value()); + EXPECT_EQ(**second, 8); + EXPECT_FALSE(queue.TryPop().has_value()); +} + +TEST(LockedQueue, DestroyingANonEmptyQueueFreesItems) +{ + int destroyed = 0; + { + LockedQueue> queue; + for (int i = 0; i < 5; i++) + queue.Push(std::make_unique(&destroyed)); + EXPECT_EQ(destroyed, 0); + } + EXPECT_EQ(destroyed, 5); +} + +TEST(LockedQueue, ClearFreesItemsAndEmptiesTheQueue) +{ + int destroyed = 0; + LockedQueue> queue; + for (int i = 0; i < 3; i++) + queue.Push(std::make_unique(&destroyed)); + queue.Clear(); + EXPECT_EQ(destroyed, 3); + EXPECT_TRUE(queue.IsEmpty()); + EXPECT_FALSE(queue.TryPop().has_value()); +} diff --git a/Tests/Unit/MaximumConnectTests.cpp b/Tests/Unit/MaximumConnectTests.cpp index 5caf3058f..697efde2b 100644 --- a/Tests/Unit/MaximumConnectTests.cpp +++ b/Tests/Unit/MaximumConnectTests.cpp @@ -25,10 +25,10 @@ const unsigned short kMaxConnections = 4; int ConnectionCount(RakPeer *peer) { - DataStructures::List systemList; - DataStructures::List guidList; + std::vector systemList; + std::vector guidList; peer->GetSystemList(systemList, guidList); - return (int) guidList.Size(); + return (int) guidList.size(); } } diff --git a/Tests/Unit/MmsgBatchTests.cpp b/Tests/Unit/MmsgBatchTests.cpp index 9bea47098..08f687e49 100644 --- a/Tests/Unit/MmsgBatchTests.cpp +++ b/Tests/Unit/MmsgBatchTests.cpp @@ -65,11 +65,11 @@ struct RecordingHandler : RNS2EventHandler received.push_back({s->bytesRead, s->systemAddress.GetPort(), s->socket, s->timeRead}); } - void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *, unsigned int) override + void DeallocRNS2RecvStruct(RNS2RecvStruct *s) override { deallocated.push_back(s); } - RNS2RecvStruct *AllocRNS2RecvStruct(const char *, unsigned int) override + RNS2RecvStruct *AllocRNS2RecvStruct() override { return nullptr; // unused by DispatchRecvBatch } @@ -114,7 +114,7 @@ struct RecordingSocket : RakNetSocket2 int failFrom = -1; // fail every Send() from this index on int failOnly = -1; // fail only the Send() at this index - RNS2SendResult Send(RNS2_SendParameters *p, const char *, unsigned int) override + RNS2SendResult Send(RNS2_SendParameters *p) override { const unsigned index = sendCalls++; if (failFrom >= 0 && index >= (unsigned) failFrom) @@ -674,7 +674,7 @@ TEST(SocketSendBatch, ReturnsDatagramCountNotByteTotal) sends[i].systemAddress = MakeDest(1000); } - EXPECT_EQ(socket.SendBatch(sends, 3, _FILE_AND_LINE_), 3); + EXPECT_EQ(socket.SendBatch(sends, 3), 3); EXPECT_EQ(socket.sent.size(), 3u); } @@ -691,12 +691,12 @@ TEST(SocketSendBatch, PropagatesErrorOnlyWhenNothingWasSent) RecordingSocket allFail; allFail.failFrom = 0; - EXPECT_EQ(allFail.SendBatch(sends, 4, _FILE_AND_LINE_), -1); + EXPECT_EQ(allFail.SendBatch(sends, 4), -1); // Two out, then a failure: report the progress made, like sendmmsg does. RecordingSocket partial; partial.failFrom = 2; - EXPECT_EQ(partial.SendBatch(sends, 4, _FILE_AND_LINE_), 2); + EXPECT_EQ(partial.SendBatch(sends, 4), 2); EXPECT_EQ(partial.sent.size(), 2u); } @@ -715,7 +715,7 @@ TEST(SocketSendBatch, DropsOnlyTheDatagramThatFailed) sends[i].systemAddress = MakeDest((unsigned short) (1000 + i)); } - EXPECT_EQ(socket.SendBatch(sends, 4, _FILE_AND_LINE_), 3); + EXPECT_EQ(socket.SendBatch(sends, 4), 3); ASSERT_EQ(socket.sent.size(), 3u); EXPECT_EQ(socket.sent[0].port, 1000); EXPECT_EQ(socket.sent[1].port, 1002) << "the datagram after the bad one must still go out"; @@ -738,7 +738,7 @@ TEST(SocketSendBatch, ForwardsPerDatagramTtl) sends[i].ttl = (int) (i + 1); } - EXPECT_EQ(socket.SendBatch(sends, 2, _FILE_AND_LINE_), 2); + EXPECT_EQ(socket.SendBatch(sends, 2), 2); ASSERT_EQ(socket.sent.size(), 2u); EXPECT_EQ(socket.sent[0].ttl, 1); EXPECT_EQ(socket.sent[1].ttl, 2); diff --git a/Tests/Unit/ObjectPoolTests.cpp b/Tests/Unit/ObjectPoolTests.cpp new file mode 100644 index 000000000..179b4ba1e --- /dev/null +++ b/Tests/Unit/ObjectPoolTests.cpp @@ -0,0 +1,98 @@ +/* + * 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. + */ + +#include + +#include "mafianet/object_pool.h" + +#include + +namespace +{ +// Counts live instances so a test can see what the pool frees. +struct Tracked +{ + static int live; + int value = 0; + Tracked() { ++live; } + ~Tracked() { --live; } +}; +int Tracked::live = 0; +} + +TEST(ObjectPool, AcquireReusesReleasedObjects) +{ + MafiaNet::ObjectPool pool; + Tracked *first = pool.Acquire(); + first->value = 42; + pool.Release(first); + EXPECT_EQ(pool.FreeCount(), 1u); + + Tracked *again = pool.Acquire(); + EXPECT_EQ(again, first); + EXPECT_EQ(again->value, 42); // not reconstructed: callers set what they read + EXPECT_EQ(pool.FreeCount(), 0u); + pool.Release(again); +} + +TEST(ObjectPool, AcquireAfterExhaustionAllocates) +{ + MafiaNet::ObjectPool pool; + Tracked *a = pool.Acquire(); + pool.Release(a); + Tracked *reused = pool.Acquire(); + Tracked *fresh = pool.Acquire(); // free list is empty again + EXPECT_EQ(reused, a); + EXPECT_NE(fresh, reused); + EXPECT_EQ(Tracked::live, 2); + pool.Release(reused); + pool.Release(fresh); + EXPECT_EQ(pool.FreeCount(), 2u); +} + +TEST(ObjectPool, DestroyingThePoolFreesAll) +{ + const int before = Tracked::live; + { + MafiaNet::ObjectPool pool; + std::set distinct; + Tracked *objects[8]; + for (Tracked *&object : objects) + { + object = pool.Acquire(); + distinct.insert(object); + } + EXPECT_EQ(distinct.size(), 8u); + for (Tracked *object : objects) + pool.Release(object); + EXPECT_EQ(Tracked::live, before + 8); + } + EXPECT_EQ(Tracked::live, before); +} + +TEST(ObjectPool, ClearFreesReleasedButNotAcquiredObjects) +{ + const int before = Tracked::live; + MafiaNet::ObjectPool pool; + Tracked *kept = pool.Acquire(); + Tracked *spareA = pool.Acquire(); + Tracked *spareB = pool.Acquire(); + pool.Release(spareA); + pool.Release(spareB); + EXPECT_EQ(Tracked::live, before + 3); + pool.Clear(); + EXPECT_EQ(pool.FreeCount(), 0u); + EXPECT_EQ(Tracked::live, before + 1); // the acquired one is still the caller's + pool.Release(kept); +} + +TEST(ObjectPool, ReleaseOfNullIsIgnored) +{ + MafiaNet::ObjectPool pool; + pool.Release(nullptr); + EXPECT_EQ(pool.FreeCount(), 0u); +} diff --git a/Tests/Unit/PeerConnectDisconnectTests.cpp b/Tests/Unit/PeerConnectDisconnectTests.cpp index d13fff16e..4ef3dafe8 100644 --- a/Tests/Unit/PeerConnectDisconnectTests.cpp +++ b/Tests/Unit/PeerConnectDisconnectTests.cpp @@ -45,20 +45,20 @@ class PeerConnectDisconnectSim : public ::testing::Test int ConnectionCount(int i) { - DataStructures::List systemList; - DataStructures::List guidList; + std::vector systemList; + std::vector guidList; peers[i]->GetSystemList(systemList, guidList); - return (int) guidList.Size(); + return (int) guidList.size(); } void CloseAllConnections() { for (int i = 0; i < kPeerNum; ++i) { - DataStructures::List systemList; - DataStructures::List guidList; + std::vector systemList; + std::vector guidList; peers[i]->GetSystemList(systemList, guidList); - for (unsigned j = 0; j < systemList.Size(); ++j) + for (unsigned j = 0; j < systemList.size(); ++j) peers[i]->CloseConnection(systemList[j], true, 0, Priority::Low); } } diff --git a/Tests/Unit/PeerConnectDisconnectWithCancelPendingTests.cpp b/Tests/Unit/PeerConnectDisconnectWithCancelPendingTests.cpp index 60e5acd00..fb93af7dd 100644 --- a/Tests/Unit/PeerConnectDisconnectWithCancelPendingTests.cpp +++ b/Tests/Unit/PeerConnectDisconnectWithCancelPendingTests.cpp @@ -39,20 +39,20 @@ class PeerConnectDisconnectWithCancelPendingSim : public ::testing::Test int ConnectionCount(int i) { - DataStructures::List systemList; - DataStructures::List guidList; + std::vector systemList; + std::vector guidList; peers[i]->GetSystemList(systemList, guidList); - return (int) guidList.Size(); + return (int) guidList.size(); } void CloseAllConnections() { for (int i = 0; i < kPeerNum; ++i) { - DataStructures::List systemList; - DataStructures::List guidList; + std::vector systemList; + std::vector guidList; peers[i]->GetSystemList(systemList, guidList); - for (unsigned j = 0; j < systemList.Size(); ++j) + for (unsigned j = 0; j < systemList.size(); ++j) peers[i]->CloseConnection(systemList[j], true, 0, Priority::Low); } } diff --git a/Tests/Unit/PeerSystemListTests.cpp b/Tests/Unit/PeerSystemListTests.cpp new file mode 100644 index 000000000..11575af9c --- /dev/null +++ b/Tests/Unit/PeerSystemListTests.cpp @@ -0,0 +1,136 @@ +/* + * 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. + * + * RakPeer's list-returning public API (GetSystemList, GetStatisticsList, + * GetSockets / ReleaseSockets) on std::vector out-params, run on the + * simulated network. + */ + +#include + +#include +#include +#include + +#include "SimulatedNetwork.h" + +#include "mafianet/bit_stream.h" +#include "mafianet/message_identifiers.h" +#include "mafianet/statistics.h" + +using namespace MafiaNet; + +namespace +{ +const MessageID ID_TEST_PAYLOAD = ID_USER_PACKET_ENUM + 11; + +void SendPayload(RakPeer *from, const RakNetGUID &to, unsigned int bytes) +{ + BitStream bs; + bs.Write(ID_TEST_PAYLOAD); + for (unsigned int i = 1; i < bytes; i++) + bs.Write(static_cast(i)); + ASSERT_NE(0u, from->Send(&bs, Priority::High, Reliability::ReliableOrdered, 0, to, false)); +} +} // namespace + +TEST(PeerSystemList, GetSystemListReturnsEveryConnectedPeer) +{ + SimulatedNetwork net; + RakPeer *server = net.AddPeer(4, 4); + RakPeer *clients[3] = {net.AddPeer(1), net.AddPeer(1), net.AddPeer(1)}; + for (RakPeer *client : clients) + ASSERT_TRUE(net.Connect(client, server)); + + std::vector addresses; + std::vector guids; + // Stale contents must be replaced, not appended to. + addresses.push_back(UNASSIGNED_SYSTEM_ADDRESS); + guids.push_back(UNASSIGNED_RAKNET_GUID); + server->GetSystemList(addresses, guids); + + ASSERT_EQ(addresses.size(), 3u); + ASSERT_EQ(guids.size(), 3u); + for (std::size_t i = 0; i < guids.size(); i++) + EXPECT_EQ(server->GetGuidFromSystemAddress(addresses[i]), guids[i]) << "index " << i; + for (RakPeer *client : clients) + { + const RakNetGUID guid = client->GetMyGUID(); + EXPECT_NE(std::find(guids.begin(), guids.end(), guid), guids.end()); + } + + // A client sees exactly its server. + clients[0]->GetSystemList(addresses, guids); + ASSERT_EQ(guids.size(), 1u); + EXPECT_EQ(guids[0], server->GetMyGUID()); + EXPECT_EQ(addresses[0], net.AddressOf(server)); +} + +TEST(PeerSystemList, GetStatisticsListIsIndexAlignedWithGuids) +{ + SimulatedNetwork net; + RakPeer *server = net.AddPeer(4, 4); + RakPeer *clients[3] = {net.AddPeer(1), net.AddPeer(1), net.AddPeer(1)}; + for (RakPeer *client : clients) + ASSERT_TRUE(net.Connect(client, server)); + + // A distinct amount of user data to each client, so each connection's + // statistics identify the client they belong to. + std::map payloadBytes; + for (unsigned int k = 0; k < 3; k++) + { + payloadBytes[clients[k]->GetMyGUID()] = 1000 * (k + 1); + SendPayload(server, clients[k]->GetMyGUID(), 1000 * (k + 1)); + } + for (RakPeer *client : clients) + ASSERT_TRUE(net.RunUntilReceived(client, ID_TEST_PAYLOAD, 2000)); + + std::vector addresses; + std::vector guids; + std::vector statistics; + server->GetStatisticsList(addresses, guids, statistics); + + ASSERT_EQ(addresses.size(), 3u); + ASSERT_EQ(guids.size(), 3u); + ASSERT_EQ(statistics.size(), 3u); + for (std::size_t i = 0; i < guids.size(); i++) + { + ASSERT_EQ(payloadBytes.count(guids[i]), 1u) << "index " << i; + const uint64_t pushed = statistics[i].runningTotal[USER_MESSAGE_BYTES_PUSHED]; + // The payload plus a few internal messages (connection handshake, pings). + EXPECT_GE(pushed, payloadBytes[guids[i]]) << "index " << i; + EXPECT_LT(pushed, payloadBytes[guids[i]] + 500u) << "index " << i; + + RakNetStatistics single; + ASSERT_NE(server->GetStatistics(addresses[i], &single), nullptr); + EXPECT_EQ(single.runningTotal[USER_MESSAGE_BYTES_PUSHED], pushed) << "index " << i; + } +} + +TEST(PeerSystemList, GetSocketsReturnsBoundSockets) +{ + SimulatedNetwork net; + RakPeer *peer = static_cast(RakPeerInterface::GetInstance()); + peer->SetSocketFactory(&net); + peer->SetManualUpdate(true); + SocketDescriptor descriptors[2] = {SocketDescriptor(61000, "10.0.0.200"), SocketDescriptor(61001, "10.0.0.200")}; + ASSERT_EQ(peer->Startup(2, descriptors, 2), RAKNET_STARTED); + + std::vector sockets; + sockets.push_back(nullptr); // stale contents must be replaced + peer->GetSockets(sockets); + ASSERT_EQ(sockets.size(), 2u); + ASSERT_NE(sockets[0], nullptr); + ASSERT_NE(sockets[1], nullptr); + EXPECT_EQ(sockets[0]->GetBoundAddress().GetPort(), 61000); + EXPECT_EQ(sockets[1]->GetBoundAddress().GetPort(), 61001); + + peer->ReleaseSockets(sockets); + EXPECT_TRUE(sockets.empty()); + + peer->Shutdown(0); + RakPeerInterface::DestroyInstance(peer); +} diff --git a/Tests/Unit/RangeListTests.cpp b/Tests/Unit/RangeListTests.cpp new file mode 100644 index 000000000..ef89d681b --- /dev/null +++ b/Tests/Unit/RangeListTests.cpp @@ -0,0 +1,104 @@ +/* + * 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. + */ + +#include + +#include "mafianet/bit_stream.h" +#include "mafianet/range_list.h" + +using namespace MafiaNet; + +namespace +{ +typedef RangeList Ranges; + +void ExpectRange(const Ranges &list, size_t i, unsigned min, unsigned max) +{ + ASSERT_LT(i, list.ranges.size()); + EXPECT_EQ(list.ranges[i].minIndex, min); + EXPECT_EQ(list.ranges[i].maxIndex, max); +} +} + +TEST(RangeList, InsertMergesAdjacentIndices) +{ + Ranges list; + list.Insert(5); + list.Insert(7); + list.Insert(6); + ASSERT_EQ(list.ranges.size(), 1u); + ExpectRange(list, 0, 5, 7); + EXPECT_EQ(list.RangeSum(), 3u); +} + +TEST(RangeList, InsertKeepsRangesSorted) +{ + Ranges list; + list.Insert(9); + list.Insert(1); + list.Insert(5); + ASSERT_EQ(list.ranges.size(), 3u); + ExpectRange(list, 0, 1, 1); + ExpectRange(list, 1, 5, 5); + ExpectRange(list, 2, 9, 9); +} + +TEST(RangeList, InsertIgnoresDuplicates) +{ + Ranges list; + list.Insert(3); + list.Insert(4); + list.Insert(3); + list.Insert(4); + ASSERT_EQ(list.ranges.size(), 1u); + ExpectRange(list, 0, 3, 4); + EXPECT_TRUE(list.IsWithinRange(4)); + EXPECT_FALSE(list.IsWithinRange(5)); +} + +TEST(RangeList, ClearEmptiesList) +{ + Ranges list; + list.Insert(1); + list.Clear(); + EXPECT_EQ(list.Size(), 0u); +} + +TEST(RangeList, SerializeWithClearRemovesOnlyWrittenRanges) +{ + Ranges list; + list.Insert(1); + list.Insert(10); + list.Insert(20); + list.Insert(30); + + // Each single-value range costs 8 + 32 bits; the check also reserves + // 16 + 65 bits, so 16 + 2 * 40 + 64 admits exactly two ranges. + BitStream out; + const BitSize_t written = list.Serialize(&out, 16 + 2 * 40 + 64, true); + EXPECT_GT(written, 0u); + ASSERT_EQ(list.ranges.size(), 2u); + ExpectRange(list, 0, 20, 20); + ExpectRange(list, 1, 30, 30); + + Ranges decoded; + out.ResetReadPointer(); + ASSERT_TRUE(decoded.Deserialize(&out)); + ASSERT_EQ(decoded.ranges.size(), 2u); + ExpectRange(decoded, 0, 1, 1); + ExpectRange(decoded, 1, 10, 10); +} + +TEST(RangeList, DeserializeRejectsHugeCountWithoutPayload) +{ + BitStream in; + const unsigned short count = 0xFFFF; + in.Write(count); + in.ResetReadPointer(); + Ranges decoded; + EXPECT_FALSE(decoded.Deserialize(&in)); +} diff --git a/Tests/Unit/RangeListWireTests.cpp b/Tests/Unit/RangeListWireTests.cpp index a4ea25234..52536449f 100644 --- a/Tests/Unit/RangeListWireTests.cpp +++ b/Tests/Unit/RangeListWireTests.cpp @@ -13,7 +13,7 @@ #include "mafianet/bit_stream.h" #include "mafianet/cc_sliding_window.h" -#include "mafianet/ds_range_list.h" +#include "mafianet/range_list.h" #include #include @@ -22,7 +22,7 @@ using namespace MafiaNet; namespace { -typedef DataStructures::RangeList Ranges; +typedef MafiaNet::RangeList Ranges; DatagramSequenceNumberType Seq(uint32_t v) { diff --git a/Tests/Unit/ReliabilityCharacterizationTests.cpp b/Tests/Unit/ReliabilityCharacterizationTests.cpp index 21e0158f5..2f40c073f 100644 --- a/Tests/Unit/ReliabilityCharacterizationTests.cpp +++ b/Tests/Unit/ReliabilityCharacterizationTests.cpp @@ -8,19 +8,30 @@ * channel ordering, split-message reassembly, sequencing and equal-priority * send order. These pin current behaviour so a rewrite of the layer's * containers can prove nothing observable changed. Seeds are fixed, so every - * run is identical. + * run is identical. The one deliberate change is pinned at the end: entries + * of equal heap weight now pop in push order. */ #include +#include #include +#include #include +#include #include +#include "ReliabilityLayerPair.h" #include "SimulatedNetwork.h" #include "mafianet/bit_stream.h" +#include "mafianet/get_time.h" #include "mafianet/message_identifiers.h" +#include "mafianet/mtu_size.h" +#include "mafianet/plugin_interface2.h" +#include "mafianet/rand.h" +#include "mafianet/reliability_layer.h" +#include "mafianet/socket2.h" using namespace MafiaNet; @@ -223,3 +234,231 @@ TEST(ReliabilityCharacterization, EqualPriorityMessagesLeaveInSendOrder) ASSERT_TRUE(received[i].intact); } } + +namespace +{ +// Two bare ReliabilityLayers joined by hand (a sends, b receives): the test +// decides which of the sender's datagrams reach the receiver. Resetting the +// sender restarts its ordering and sequencing indices, which is the only way to +// make the receiver buffer several messages with the same ordering-heap weight. +struct LayerPair : MafiaNetTest::ReliabilityLayerPair +{ + void ResetSender() { ResetA(); } + + // Queues `count` copies of one tagged message and flushes them, apart from + // anything queued earlier: they leave in the next datagram(s) the sender + // writes. One update is enough at these sizes, so no retransmission timer + // gets a chance to fire and bundle an earlier "lost" message along. + std::vector> SendAlone(uint32_t counter, Reliability reliability, uint32_t count = 1) + { + BitStream bs; + bs.Write(ID_CHARACTERIZATION); + bs.Write(counter); + for (uint32_t i = 0; i < count; i++) + EXPECT_TRUE(a.Send((char *) bs.GetData(), bs.GetNumberOfBitsUsed(), Priority::High, reliability, 0, true, MAXIMUM_MTU_SIZE, now, 0)); + for (int i = 0; i < 100 && aSock.sent.empty(); i++) + { + now += 10000; + UpdateA(); + } + EXPECT_FALSE(aSock.sent.empty()); + std::vector> out; + out.swap(aSock.sent); + return out; + } + + void Deliver(std::vector> &datagrams) + { + for (std::vector &datagram : datagrams) + DeliverToB(datagram); + bSock.sent.clear(); // acks; the sender never needs them here + } + + std::vector ReceivedCounters() + { + std::vector out; + unsigned char *data; + BitSize_t bits; + while ((bits = b.Receive(&data)) > 0) + { + if (BITS_TO_BYTES(bits) == 5 && data[0] == ID_CHARACTERIZATION) + { + BitStream bs(data, 5, false); + bs.IgnoreBytes(1); + uint32_t counter = 0; + bs.Read(counter); + out.push_back(counter); + } + std::free(data); + } + return out; + } +}; + +const uint32_t kHoleCounter = 0xFFFFu; +} // namespace + +TEST(ReliabilityCharacterization, EqualWeightOrderingHeapEntriesPopInPushOrder) +{ + // Each round, a freshly reset sender sends ordered message 0 (lost) and then + // an unreliable sequenced message, which carries ordering index 1 and + // sequencing index 0 every time. The receiver, still waiting on index 0, + // buffers all of them with one identical heap weight. When the ordered + // message finally arrives, every buffered entry is due (sequenced messages + // do not advance the read index): they must come out in arrival order. + LayerPair p; + const uint32_t kEntries = 64; + for (uint32_t i = 0; i < kEntries; i++) + { + p.ResetSender(); + p.SendAlone(kHoleCounter, Reliability::ReliableOrdered); // lost + std::vector> sequenced = p.SendAlone(i, Reliability::UnreliableSequenced); + p.Deliver(sequenced); + } + ASSERT_TRUE(p.ReceivedCounters().empty()) << "sequenced messages must wait behind the hole"; + + p.ResetSender(); + std::vector> hole = p.SendAlone(kHoleCounter, Reliability::ReliableOrdered); + p.Deliver(hole); + + const std::vector received = p.ReceivedCounters(); + ASSERT_EQ(kEntries + 1, received.size()); + EXPECT_EQ(kHoleCounter, received[0]); + for (uint32_t i = 0; i < kEntries; i++) + EXPECT_EQ(i, received[i + 1]) << "position " << i + 1; +} + +TEST(ReliabilityCharacterization, EqualWeightOrderedDuplicatesDeliverTheFirstBufferedCopy) +{ + // Ordered messages that share an ordering index share a heap weight. Only + // one of them can be delivered: the first one buffered. The rest are stale + // once the read index moves past them and must be dropped, not delivered + // and not left blocking the channel. + LayerPair p; + const uint32_t kCopies = 64; + for (uint32_t i = 0; i < kCopies; i++) + { + p.ResetSender(); + p.SendAlone(kHoleCounter, Reliability::ReliableOrdered); // lost; reliable message number 0 + // Burn reliable message numbers so each copy carries a fresh one, as a + // re-split copy would; the receiver would drop a repeated number. + if (i > 0) + p.SendAlone(kHoleCounter, Reliability::Reliable, i); // lost + std::vector> copy = p.SendAlone(1000 + i, Reliability::ReliableOrdered); + p.Deliver(copy); + } + ASSERT_TRUE(p.ReceivedCounters().empty()); + + p.ResetSender(); + std::vector> hole = p.SendAlone(kHoleCounter, Reliability::ReliableOrdered); + p.Deliver(hole); + const std::vector received = p.ReceivedCounters(); + ASSERT_EQ(2u, received.size()); + EXPECT_EQ(kHoleCounter, received[0]); + EXPECT_EQ(1000u, received[1]); + + // The channel is not blocked: the next ordered index is delivered at once. + p.ResetSender(); + p.SendAlone(kHoleCounter, Reliability::ReliableOrdered); // index 0 again: already passed + p.SendAlone(kHoleCounter, Reliability::Reliable, kCopies); // fresh reliable numbers, lost + p.SendAlone(kHoleCounter, Reliability::ReliableOrdered); // index 1: already passed + std::vector> next = p.SendAlone(2000, Reliability::ReliableOrdered); // index 2 + p.Deliver(next); + EXPECT_EQ(std::vector{2000u}, p.ReceivedCounters()); +} + +// The heap helpers on their own: ReliabilityLayer::HeapPush/HeapPop keep a +// std::vector as a min-heap on (weight, push order). +namespace +{ +using HeapEntry = ReliabilityLayer::HeapEntry; + +struct HeapUnderTest +{ + std::vector packets; // identities only; index = push order + std::vector heap; + std::uint64_t nextSeq = 0; + + explicit HeapUnderTest(size_t capacity) + : packets(capacity) + { + } + void Push(size_t id, reliabilityHeapWeightType weight) { ReliabilityLayer::HeapPush(heap, nextSeq, weight, &packets[id]); } + size_t Pop() { return (size_t) (ReliabilityLayer::HeapPop(heap) - packets.data()); } +}; +} // namespace + +TEST(ReliabilityHeap, PopsDistinctWeightsInAscendingOrder) +{ + const reliabilityHeapWeightType weights[] = {50, 10, 40, 20, 30, 0, 60}; + HeapUnderTest h(7); + for (size_t i = 0; i < 7; i++) + h.Push(i, weights[i]); + const size_t expected[] = {5, 1, 3, 4, 2, 0, 6}; + for (size_t i = 0; i < 7; i++) + { + EXPECT_EQ(weights[expected[i]], h.heap.front().weight); + EXPECT_EQ(expected[i], h.Pop()); + } + EXPECT_TRUE(h.heap.empty()); +} + +TEST(ReliabilityHeap, EqualWeightsPopInPushOrder) +{ + HeapUnderTest h(100); + for (size_t i = 0; i < 100; i++) + h.Push(i, 7); + for (size_t i = 0; i < 100; i++) + ASSERT_EQ(i, h.Pop()); +} + +TEST(ReliabilityHeap, SeqCountsEveryPushAndSurvivesPops) +{ + HeapUnderTest h(3); + h.Push(0, 1); + h.Push(1, 1); + EXPECT_EQ(0u, h.Pop()); + h.Push(2, 1); + EXPECT_EQ(3u, h.nextSeq); + EXPECT_EQ(1u, h.Pop()); + EXPECT_EQ(2u, h.Pop()); +} + +TEST(ReliabilityHeap, MatchesAStableSortUnderInterleavedPushAndPop) +{ + // Fixed-seed differential test against a reference model: a list kept + // sorted by weight with ties in insertion order. Few distinct weights, so + // ties are the common case. + const size_t kOps = 20000; + HeapUnderTest h(kOps); + std::vector> model; // (weight, id), stable-sorted + uint32_t lcg = 0xC0FFEEu; + size_t pushed = 0; + for (size_t op = 0; op < kOps; op++) + { + lcg = lcg * 1664525u + 1013904223u; + const bool push = model.empty() || (lcg >> 16) % 3 != 0; + if (push && pushed < kOps) + { + const reliabilityHeapWeightType weight = (lcg >> 8) % 16; + h.Push(pushed, weight); + auto at = std::upper_bound(model.begin(), model.end(), weight, + [](reliabilityHeapWeightType w, const std::pair &e) + { return w < e.first; }); + model.insert(at, {weight, pushed}); + pushed++; + } + else + { + ASSERT_EQ(model.front().first, h.heap.front().weight) << "op " << op; + ASSERT_EQ(model.front().second, h.Pop()) << "op " << op; + model.erase(model.begin()); + } + ASSERT_EQ(model.size(), h.heap.size()); + } + while (!model.empty()) + { + ASSERT_EQ(model.front().second, h.Pop()); + model.erase(model.begin()); + } +} diff --git a/Tests/Unit/ReliabilityLayerReSplitHeaderTests.cpp b/Tests/Unit/ReliabilityLayerReSplitHeaderTests.cpp index 888cb9a2a..e61b88e03 100644 --- a/Tests/Unit/ReliabilityLayerReSplitHeaderTests.cpp +++ b/Tests/Unit/ReliabilityLayerReSplitHeaderTests.cpp @@ -22,7 +22,6 @@ #include #include "mafianet/bit_stream.h" -#include "mafianet/ds_list.h" #include "mafianet/get_time.h" #include "mafianet/mtu_size.h" #include "mafianet/packet_priority.h" @@ -37,7 +36,7 @@ namespace { struct NullSocket : public RakNetSocket2 { - RNS2SendResult Send(RNS2_SendParameters *sendParameters, const char *, unsigned int) override + RNS2SendResult Send(RNS2_SendParameters *sendParameters) override { return sendParameters->length; } @@ -59,7 +58,7 @@ class ReSplitHeaderFixture : public ::testing::Test ReliabilityLayer layer; NullSocket socket; SystemAddress remote{"127.0.0.1", 40001}; - DataStructures::List handlers; + std::vector handlers; RakNetRandom rnr; BitStream updateBitStream{MAXIMUM_MTU_SIZE}; CCTimeType now = 0; diff --git a/Tests/Unit/ReliabilityLayerSplitPacketTests.cpp b/Tests/Unit/ReliabilityLayerSplitPacketTests.cpp index a4c6544a7..f5b3c2f05 100644 --- a/Tests/Unit/ReliabilityLayerSplitPacketTests.cpp +++ b/Tests/Unit/ReliabilityLayerSplitPacketTests.cpp @@ -17,11 +17,12 @@ #include #include +#include #include +#include #include #include "mafianet/bit_stream.h" -#include "mafianet/ds_list.h" #include "mafianet/get_time.h" #include "mafianet/mtu_size.h" #include "mafianet/packet_priority.h" @@ -30,13 +31,15 @@ #include "mafianet/reliability_layer.h" #include "mafianet/socket2.h" +#include "ReliabilityLayerPair.h" + using namespace MafiaNet; namespace { struct NullSocket : public RakNetSocket2 { - RNS2SendResult Send(RNS2_SendParameters *sendParameters, const char *, unsigned int) override + RNS2SendResult Send(RNS2_SendParameters *sendParameters) override { return sendParameters->length; } @@ -57,7 +60,7 @@ class SplitPacketFixture : public ::testing::Test ReliabilityLayer layer; NullSocket socket; SystemAddress remote{"127.0.0.1", 40001}; - DataStructures::List handlers; + std::vector handlers; RakNetRandom rnr; BitStream updateBitStream{MAXIMUM_MTU_SIZE}; CCTimeType now = 0; @@ -254,69 +257,131 @@ TEST_F(SplitPacketFixture, DuplicateFragmentIsIgnored) EXPECT_EQ(Concat(first, second), messages[0]); } -namespace -{ -struct CapturingSocket : public RakNetSocket2 -{ - std::vector> datagrams; - - RNS2SendResult Send(RNS2_SendParameters *sendParameters, const char *, unsigned int) override - { - datagrams.emplace_back(sendParameters->data, sendParameters->data + sendParameters->length); - return sendParameters->length; - } -}; -} // namespace - // The sender splits into one fragment per datagram-sized slice and holds one pointer per fragment while it // does. The layer has no fixed cap on the fragment count, so this uses a message of tens of thousands of // fragments (about 24,000), far more than a small stack buffer would have been sized for. TEST(ReliabilityLayerSplitPacket, ReassemblesMessageOfTensOfThousandsOfFragments) { - ReliabilityLayer sender, receiver; - CapturingSocket senderSocket, receiverSocket; - SystemAddress senderAddress{"127.0.0.1", 40001}, receiverAddress{"127.0.0.1", 40002}; - DataStructures::List handlers; - RakNetRandom rnr; - BitStream updateBitStream{MAXIMUM_MTU_SIZE}; - CCTimeType now = GetTimeUS(); // see SplitPacketFixture::SetUp - // Mode Plaintext, as in SplitPacketFixture: no keys are exchanged here. - sender.SetEncryptionMode(EncryptionMode::Plaintext); - receiver.SetEncryptionMode(EncryptionMode::Plaintext); - sender.Reset(true, MAXIMUM_MTU_SIZE); - receiver.Reset(true, MAXIMUM_MTU_SIZE); - sender.SetTimeoutTime(1000000); - receiver.SetTimeoutTime(1000000); + MafiaNetTest::ReliabilityLayerPair p; // a sends, b receives const size_t kMessageBytes = 32 * 1024 * 1024; std::vector message(kMessageBytes); message[0] = 200; // clear of internal message ids for (size_t i = 1; i < message.size(); ++i) message[i] = (unsigned char) (i * 31 + 7); - ASSERT_TRUE(sender.Send((char *) message.data(), BYTES_TO_BITS((BitSize_t) message.size()), MafiaNet::Priority::Medium, - MafiaNet::Reliability::ReliableOrdered, 0, true, MAXIMUM_MTU_SIZE, now, 0)); + ASSERT_TRUE(p.a.Send((char *) message.data(), BYTES_TO_BITS((BitSize_t) message.size()), MafiaNet::Priority::Medium, + MafiaNet::Reliability::ReliableOrdered, 0, true, MAXIMUM_MTU_SIZE, p.now, 0)); std::vector received; for (int tick = 0; tick < 200000 && received.empty(); ++tick) { - now += 10000; - sender.Update(&senderSocket, receiverAddress, MAXIMUM_MTU_SIZE, now, 0, handlers, &rnr, updateBitStream); - receiver.Update(&receiverSocket, senderAddress, MAXIMUM_MTU_SIZE, now, 0, handlers, &rnr, updateBitStream); - for (std::vector &datagram : senderSocket.datagrams) - receiver.HandleSocketReceiveFromConnectedPlayer(datagram.data(), (unsigned int) datagram.size(), senderAddress, handlers, MAXIMUM_MTU_SIZE, &receiverSocket, &rnr, now, updateBitStream); - senderSocket.datagrams.clear(); - for (std::vector &datagram : receiverSocket.datagrams) - sender.HandleSocketReceiveFromConnectedPlayer(datagram.data(), (unsigned int) datagram.size(), receiverAddress, handlers, MAXIMUM_MTU_SIZE, &senderSocket, &rnr, now, updateBitStream); - receiverSocket.datagrams.clear(); + p.now += 10000; + p.UpdateA(); + p.UpdateB(); + for (std::vector &datagram : p.aSock.sent) + p.DeliverToB(datagram); + p.aSock.sent.clear(); + for (std::vector &datagram : p.bSock.sent) + p.DeliverToA(datagram); + p.bSock.sent.clear(); - unsigned char *data; - BitSize_t bits; - while ((bits = receiver.Receive(&data)) > 0) - { - received.assign(data, data + BITS_TO_BYTES(bits)); - std::free(data); - } + for (std::vector &m : MafiaNetTest::ReliabilityLayerPair::Drain(p.b)) + received = std::move(m); } ASSERT_EQ(message.size(), received.size()); EXPECT_TRUE(message == received); } + +// Three split messages are open in the receiver at once: their datagrams arrive interleaved, in +// reverse, and each one twice. Each message must still come out exactly once and intact. The second +// copy of a datagram is dropped by the reliable-message-number check (hasReceivedPacketQueue) before +// it reaches the split-channel map; SplitPacketSort::Add's own duplicate rejection is covered by +// SplitPacketFixture.DuplicateFragmentIsIgnored. +TEST(ReliabilityLayerSplitPacket, ReassemblesInterleavedSplitsWithReorderedAndDuplicatedDatagrams) +{ + MafiaNetTest::ReliabilityLayerPair p; // a sends, b receives + + const size_t kSizes[3] = {5000, 7000, 9000}; + std::vector> messages; + for (size_t m = 0; m < 3; ++m) + { + std::vector message(kSizes[m]); + message[0] = 200; // clear of internal message ids + for (size_t i = 1; i < message.size(); ++i) + message[i] = (unsigned char) (m * 101 + i * 13 + (i >> 8)); + messages.push_back(message); + } + + // Capture each message's datagrams on their own. The sender's window is one datagram until it + // is acked, so b plays the acking receiver here; it is reset before the real delivery below. + std::vector>> datagramsOf(3); + for (size_t m = 0; m < 3; ++m) + { + ASSERT_TRUE(p.a.Send((char *) messages[m].data(), BYTES_TO_BITS((BitSize_t) messages[m].size()), MafiaNet::Priority::Medium, + MafiaNet::Reliability::Reliable, 0, true, MAXIMUM_MTU_SIZE, p.now, 0)); + size_t got = 0; + for (int tick = 0; tick < 1000 && got == 0; ++tick) + { + p.now += 10000; + p.UpdateA(); + p.UpdateB(); + for (std::vector &datagram : p.aSock.sent) + { + datagramsOf[m].push_back(datagram); + p.DeliverToB(datagram); + } + p.aSock.sent.clear(); + for (std::vector &datagram : p.bSock.sent) + p.DeliverToA(datagram); + p.bSock.sent.clear(); + got = MafiaNetTest::ReliabilityLayerPair::Drain(p.b).size(); + } + ASSERT_EQ(1u, got) << "message " << m; + ASSERT_GE(datagramsOf[m].size(), 3u) << "message " << m << " should span several fragments"; + } + + // Interleave the three messages' datagrams round-robin, then reverse. + std::vector> order; + for (size_t i = 0;; ++i) + { + bool any = false; + for (size_t m = 0; m < 3; ++m) + { + if (i < datagramsOf[m].size()) + { + order.push_back(datagramsOf[m][i]); + any = true; + } + } + if (!any) + break; + } + std::reverse(order.begin(), order.end()); + + // A fresh receiver: every fragment is new to it. + p.ResetB(); + std::vector> received; + for (std::vector &datagram : order) + { + for (int copy = 0; copy < 2; ++copy) + { + std::vector bytes = datagram; + p.DeliverToB(bytes); + } + for (std::vector &m : MafiaNetTest::ReliabilityLayerPair::Drain(p.b)) + received.push_back(std::move(m)); + } + for (int tick = 0; tick < 10; ++tick) + { + p.now += 10000; + p.UpdateB(); + for (std::vector &m : MafiaNetTest::ReliabilityLayerPair::Drain(p.b)) + received.push_back(std::move(m)); + } + + ASSERT_EQ(3u, received.size()); + std::sort(received.begin(), received.end(), [](const std::vector &l, const std::vector &r) + { return l.size() < r.size(); }); + for (size_t m = 0; m < 3; ++m) + EXPECT_TRUE(messages[m] == received[m]) << "message " << m; +} diff --git a/Tests/Unit/SessionConfigSimulatedTests.cpp b/Tests/Unit/SessionConfigSimulatedTests.cpp index d2cd5f0e7..276d7be84 100644 --- a/Tests/Unit/SessionConfigSimulatedTests.cpp +++ b/Tests/Unit/SessionConfigSimulatedTests.cpp @@ -108,7 +108,7 @@ class SlotInspector : public RakPeer out.reported = slot.connectionReportedToApplication; out.payloadLength = slot.remoteSessionConfigLength; out.exchanging = slot.connectMode == RemoteSystemStruct::EXCHANGING_SESSION_DATA; - out.heldMessages = slot.heldApplicationMessages.Size(); + out.heldMessages = (unsigned int) slot.heldApplicationMessages.size(); out.heldBytes = slot.heldApplicationBytes; out.connectingSide = slot.sessionConfigIsConnectingSide; out.handlingRequest = slot.connectMode == RemoteSystemStruct::HANDLING_CONNECTION_REQUEST; diff --git a/Tests/Unit/SocketFamilyTests.cpp b/Tests/Unit/SocketFamilyTests.cpp index 1a730f2f4..a741a8a64 100644 --- a/Tests/Unit/SocketFamilyTests.cpp +++ b/Tests/Unit/SocketFamilyTests.cpp @@ -75,7 +75,7 @@ TEST(SocketFamilyBind, ResolverFamilyErrorReportsFamilyNotSupported) bbp.pollingThreadPriority = 0; bbp.eventHandler = nullptr; - const RNS2BindResult result = static_cast(socket)->Bind(&bbp, _FILE_AND_LINE_); + const RNS2BindResult result = static_cast(socket)->Bind(&bbp); RakNetSocket2Allocator::DeallocRNS2(socket); EXPECT_EQ(result, BR_FAMILY_NOT_SUPPORTED); } diff --git a/Tests/Unit/ThreadsafePacketLoggerTests.cpp b/Tests/Unit/ThreadsafePacketLoggerTests.cpp new file mode 100644 index 000000000..15f749bea --- /dev/null +++ b/Tests/Unit/ThreadsafePacketLoggerTests.cpp @@ -0,0 +1,70 @@ +/* + * 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. + */ + +#include + +#include "mafianet/native_feature_includes.h" + +#if _RAKNET_SUPPORT_PacketLogger == 1 + +#include +#include + +#include "mafianet/threadsafe_packet_logger.h" + +using namespace MafiaNet; + +namespace +{ +// Records what reaches WriteLog instead of printing it. +class RecordingLogger : public ThreadsafePacketLogger +{ +public: + void Log(const char *str) { AddToLog(str); } + void WriteLog(const char *str) override { written.push_back(str); } + + std::vector written; +}; +} + +TEST(ThreadsafePacketLogger, LinesAreWrittenOnlyByUpdateInOrder) +{ + RecordingLogger logger; + logger.Log("first"); + logger.Log("second"); + EXPECT_TRUE(logger.written.empty()); + + logger.Update(); + ASSERT_EQ(logger.written.size(), 2u); + EXPECT_EQ(logger.written[0], "first"); + EXPECT_EQ(logger.written[1], "second"); + + // Nothing is written twice. + logger.Update(); + EXPECT_EQ(logger.written.size(), 2u); +} + +TEST(ThreadsafePacketLogger, ManyLinesBetweenUpdatesAreAllKept) +{ + // More than the old ring buffer's initial capacity, to cover growth. + RecordingLogger logger; + for (int i = 0; i < 100; i++) + logger.Log(std::to_string(i).c_str()); + logger.Update(); + ASSERT_EQ(logger.written.size(), 100u); + for (int i = 0; i < 100; i++) + EXPECT_EQ(logger.written[i], std::to_string(i)); +} + +TEST(ThreadsafePacketLogger, DestroyingWithUnwrittenLinesFreesThem) +{ + // Run under ASan/LSan: the queued strings must not leak. + RecordingLogger logger; + logger.Log("never written"); +} + +#endif diff --git a/ci/tsan.supp b/ci/tsan.supp index 7dade345f..9d1fe202c 100644 --- a/ci/tsan.supp +++ b/ci/tsan.supp @@ -24,8 +24,6 @@ race:MafiaNet::RakPeer::GetSystemAddressFromIndex race:MafiaNet::RakPeer::GetAveragePing race:MafiaNet::RakPeer::GetLastPing race:MafiaNet::ReliabilityLayer::GetStatistics -# Send()'s session-handshake gate reads connectMode the same way -race:MafiaNet::RakPeer::IsExchangingSessionData # --- Config get/set on the user thread, read lock-free by the network thread --- race:MafiaNet::RakPeer::SetMaximumIncomingConnections @@ -42,20 +40,3 @@ race:MafiaNet::RakPeer::RemoveFromActiveSystemList race:MafiaNet::RakPeer::AssignSystemAddressToRemoteSystemList race:MafiaNet::RakPeer::ParseConnectionRequestPacket race:MafiaNet::RakPeer::SetOccasionalPing - -# --- Thread-handoff command queues: producers on the user thread, consumer on -# the network thread. The queue access pattern predates this work; baselined -# pending a review of the intended locking discipline --- -race:DataStructures::Queue::Push -race:DataStructures::Queue::Push - -# --- TCPInterface's ThreadsafeAllocatingQueue::PopInaccurate peeks -# queue.IsEmpty() without the lock (hence "Inaccurate") before locking to -# pop; the TCP thread pushes under the lock. A stale peek only delays the pop -# to the next poll: the same lock-free getter model as above. Scoped to the -# two instantiations only TCPInterface uses -- -# (HasNewIncomingConnection, HasLostConnection, the SSL start queue) and -# (Receive) -- so RakPeer's bufferedCommands and the other queues -# stay checked --- -race:DataStructures::ThreadsafeAllocatingQueue::PopInaccurate -race:DataStructures::ThreadsafeAllocatingQueue::PopInaccurate