Skip to content

Commit

Permalink
Merge pull request #820 from openmultiplayer/amir/remove-preconnect-p…
Browse files Browse the repository at this point in the history
…ackets

Remove preconnect packet handler
  • Loading branch information
AmyrAhmady authored Dec 31, 2023
2 parents ebb13f6 + 6b5752c commit 384b8e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 98 deletions.
116 changes: 23 additions & 93 deletions Server/Components/LegacyNetwork/legacy_network_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,6 @@ IPlayer* RakNetLegacyNetwork::OnPeerConnect(RakNet::RPCParameters* rpcParams, bo

playerFromRakIndex[rpcParams->senderIndex] = newConnectionResult.second;

// Handle pre-connect received custom packets here, now that player is initialized
if (hasUnprocessedPreConnectPackets[rpcParams->senderIndex])
{
handlePreConnectPacketData(rpcParams->senderIndex);
hasUnprocessedPreConnectPackets[rpcParams->senderIndex] = false;
}

return newConnectionResult.second;
}

Expand Down Expand Up @@ -523,12 +516,6 @@ void RakNetLegacyNetwork::OnRakNetDisconnect(RakNet::PlayerIndex rid, PeerDiscon
{
IPlayer* player = playerFromRakIndex[rid];

for (RakNet::Packet* customPkt : preConnectPackets[rid])
{
rakNetServer.DeallocatePacket(customPkt);
}
preConnectPackets[rid].clear();

if (!player)
{
return;
Expand Down Expand Up @@ -874,108 +861,51 @@ void RakNetLegacyNetwork::start()
}
}

void RakNetLegacyNetwork::handlePreConnectPacketData(int playerIndex)
{
IPlayer* player = playerFromRakIndex[playerIndex];

for (RakNet::Packet* customPkt : preConnectPackets[playerIndex])
{
NetworkBitStream bs(customPkt->data, customPkt->length, false);
uint8_t type;

if (bs.readUINT8(type))
{
const bool res = inEventDispatcher.stopAtFalse([&player, type, &bs](NetworkInEventHandler* handler)
{
bs.SetReadOffset(8); // Ignore packet ID
return handler->onReceivePacket(*player, type, bs);
});

if (res)
{
packetInEventDispatcher.stopAtFalse(type, [&player, &bs](SingleNetworkInEventHandler* handler)
{
bs.SetReadOffset(8); // Ignore packet ID
return handler->onReceive(*player, bs);
});
}
}

rakNetServer.DeallocatePacket(customPkt);
}

preConnectPackets[playerIndex].clear();
}

void RakNetLegacyNetwork::onTick(Microseconds elapsed, TimePoint now)
{
for (RakNet::Packet* pkt = rakNetServer.Receive(); pkt; pkt = rakNetServer.Receive())
{

bool mustDeallocatePacket = true;

if (pkt->playerIndex >= playerFromRakIndex.size())
{
rakNetServer.DeallocatePacket(pkt);
continue;
}

NetworkBitStream bs(pkt->data, pkt->length, false);
uint8_t type;

if (bs.readUINT8(type))
IPlayer* player = playerFromRakIndex[pkt->playerIndex];
if (player)
{
if (type == RakNet::ID_DISCONNECTION_NOTIFICATION)
{
OnRakNetDisconnect(pkt->playerIndex, PeerDisconnectReason_Quit);
}
else if (type == RakNet::ID_CONNECTION_LOST)
NetworkBitStream bs(pkt->data, pkt->length, false);
uint8_t type;
if (bs.readUINT8(type))
{
OnRakNetDisconnect(pkt->playerIndex, PeerDisconnectReason_Timeout);
}
else
{
IPlayer* player = playerFromRakIndex[pkt->playerIndex];
if (!player)
{
// Here we collect custom packets sent before player is fully initialized
preConnectPackets[pkt->playerIndex].push_back(pkt);
hasUnprocessedPreConnectPackets[pkt->playerIndex] = true;
mustDeallocatePacket = false;
}
else if (player)
{
// It usually shouldn't even go through this part,
// Because RakNetLegacyNetwork::OnPeerConnect will handle it before this stage
if (hasUnprocessedPreConnectPackets[pkt->playerIndex])
// Call event handlers for packet receive
const bool res = inEventDispatcher.stopAtFalse([&player, type, &bs](NetworkInEventHandler* handler)
{
handlePreConnectPacketData(pkt->playerIndex);
hasUnprocessedPreConnectPackets[pkt->playerIndex] = false;
}
bs.SetReadOffset(8); // Ignore packet ID
return handler->onReceivePacket(*player, type, bs);
});

// Call event handlers for packet receive
const bool res = inEventDispatcher.stopAtFalse([&player, type, &bs](NetworkInEventHandler* handler)
if (res)
{
packetInEventDispatcher.stopAtFalse(type, [&player, &bs](SingleNetworkInEventHandler* handler)
{
bs.SetReadOffset(8); // Ignore packet ID
return handler->onReceivePacket(*player, type, bs);
return handler->onReceive(*player, bs);
});
}

if (res)
{
packetInEventDispatcher.stopAtFalse(type, [&player, &bs](SingleNetworkInEventHandler* handler)
{
bs.SetReadOffset(8); // Ignore packet ID
return handler->onReceive(*player, bs);
});
}
if (type == RakNet::ID_DISCONNECTION_NOTIFICATION)
{
OnRakNetDisconnect(pkt->playerIndex, PeerDisconnectReason_Quit);
}
else if (type == RakNet::ID_CONNECTION_LOST)
{
OnRakNetDisconnect(pkt->playerIndex, PeerDisconnectReason_Timeout);
}
}
}

if (mustDeallocatePacket)
{
rakNetServer.DeallocatePacket(pkt);
}
rakNetServer.DeallocatePacket(pkt);
}

if (now - lastCookieSeed > cookieSeedTime)
Expand Down
6 changes: 1 addition & 5 deletions Server/Components/LegacyNetwork/legacy_network_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using namespace Impl;
#define MAGNITUDE_EPSILON 0.00001f

static const StaticArray<StringView, 2> ProtectedRules = {
"version", "allow_037"
"version", "allowed_clients"
};

class Core;
Expand All @@ -38,8 +38,6 @@ class RakNetLegacyNetwork final : public Network, public CoreEventHandler, publi
Query query;
RakNet::RakServerInterface& rakNetServer;
std::array<IPlayer*, PLAYER_POOL_SIZE> playerFromRakIndex;
std::array<bool, PLAYER_POOL_SIZE> hasUnprocessedPreConnectPackets;
FlatHashMap<int, DynamicArray<RakNet::Packet*>> preConnectPackets;
Milliseconds cookieSeedTime;
TimePoint lastCookieSeed;

Expand Down Expand Up @@ -355,8 +353,6 @@ class RakNetLegacyNetwork final : public Network, public CoreEventHandler, publi
return false;
}

void handlePreConnectPacketData(int playerIndex);

/// Synchronize players after banning an IP, kicking any that match the banned IP
void synchronizeBans();

Expand Down

0 comments on commit 384b8e3

Please sign in to comment.