Skip to content

Commit

Permalink
Server: Add limit to concurrent chunk create packets to avoid overloa…
Browse files Browse the repository at this point in the history
…d network
  • Loading branch information
SirLynix committed Feb 22, 2024
1 parent 02135e6 commit d87a1d3
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 10 deletions.
3 changes: 2 additions & 1 deletion include/CommonLib/NetworkReactor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace tsom

void QueryInfo(std::size_t peerId, PeerInfoCallback callback);

void SendData(std::size_t peerId, Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::NetPacket&& packet);
void SendData(std::size_t peerId, Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::NetPacket&& packet, std::function<void()> acknowledgeCallback = {});

NetworkReactor& operator=(const NetworkReactor&) = delete;
NetworkReactor& operator=(NetworkReactor&&) = delete;
Expand Down Expand Up @@ -124,6 +124,7 @@ namespace tsom
Nz::ENetPacketFlags flags;
Nz::UInt8 channelId;
Nz::NetPacket packet;
std::function<void()> acknowledgeCallback;
};

struct QueryPeerInfo
Expand Down
2 changes: 1 addition & 1 deletion include/CommonLib/NetworkSession.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace tsom

void HandlePacket(Nz::NetPacket&& netPacket);

template<typename T> void SendPacket(const T& packet);
template<typename T> void SendPacket(const T& packet, std::function<void()> acknowledgeCallback = {});

SessionHandler& SetHandler(std::unique_ptr<SessionHandler>&& sessionHandler);
template<typename T, typename... Args> T& SetupHandler(Args&&... args);
Expand Down
4 changes: 2 additions & 2 deletions include/CommonLib/NetworkSession.inl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace tsom
}

template<typename T>
void NetworkSession::SendPacket(const T& packet)
void NetworkSession::SendPacket(const T& packet, std::function<void()> acknowledgeCallback)
{
static_assert(PacketCount < 0xFF);

Expand All @@ -37,7 +37,7 @@ namespace tsom

netPacket.FlushBits();

m_reactor.SendData(m_peerId, sendAttributes.channelId, sendAttributes.flags, std::move(netPacket));
m_reactor.SendData(m_peerId, sendAttributes.channelId, sendAttributes.flags, std::move(netPacket), std::move(acknowledgeCallback));
}

template<typename T, typename ...Args>
Expand Down
3 changes: 1 addition & 2 deletions include/CommonLib/Protocol/Packets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,13 @@ namespace tsom

struct ChunkUpdate
{
Helper::ChunkId chunkId;

struct BlockUpdate
{
Helper::VoxelLocation voxelLoc;
Nz::UInt8 newContent;
};

Helper::ChunkId chunkId;
std::vector<BlockUpdate> updates;
};

Expand Down
3 changes: 3 additions & 0 deletions include/ServerLib/SessionVisibilityHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <entt/entt.hpp>
#include <tsl/hopscotch_map.h>
#include <tsl/hopscotch_set.h>
#include <memory>

namespace tsom
{
Expand Down Expand Up @@ -59,6 +60,7 @@ namespace tsom
void DispatchChunks();
void DispatchEntities(Nz::UInt16 tickIndex);

static constexpr std::size_t MaxConcurrentChunkUpdate = 3;
static constexpr std::size_t FreeChunkIdGrowRate = 128;
static constexpr std::size_t FreeEntityIdGrowRate = 512;

Expand All @@ -80,6 +82,7 @@ namespace tsom
tsl::hopscotch_set<entt::handle, HandlerHasher> m_deletedEntities;
tsl::hopscotch_set<entt::handle, HandlerHasher> m_movingEntities;
tsl::hopscotch_map<Chunk*, std::size_t> m_chunkIndices;
std::shared_ptr<std::size_t> m_activeChunkUpdates;
std::vector<VisibleChunk> m_visibleChunks;
Nz::Bitset<Nz::UInt64> m_freeChunkIds;
Nz::Bitset<Nz::UInt64> m_freeEntityIds;
Expand Down
1 change: 1 addition & 0 deletions include/ServerLib/SessionVisibilityHandler.inl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace tsom
m_controlledCharacter(nullptr),
m_networkSession(networkSession)
{
m_activeChunkUpdates = std::make_shared<std::size_t>(0);
}

inline void SessionVisibilityHandler::UpdateControlledEntity(entt::handle entity, CharacterController* controller)
Expand Down
11 changes: 9 additions & 2 deletions src/CommonLib/NetworkReactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ namespace tsom
m_outgoingQueue.enqueue(std::move(outgoingRequest));
}

void NetworkReactor::SendData(std::size_t peerId, Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::NetPacket&& packet)
void NetworkReactor::SendData(std::size_t peerId, Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::NetPacket&& packet, std::function<void()> acknowledgeCallback)
{
assert(peerId >= m_idOffset);

OutgoingEvent::PacketEvent packetEvent;
packetEvent.acknowledgeCallback = std::move(acknowledgeCallback);
packetEvent.channelId = channelId;
packetEvent.packet = std::move(packet);
packetEvent.flags = flags;
Expand Down Expand Up @@ -325,7 +326,13 @@ namespace tsom
else if constexpr (std::is_same_v<T, OutgoingEvent::PacketEvent>)
{
if (Nz::ENetPeer* peer = m_clients[outEvent.peerId])
peer->Send(arg.channelId, arg.flags, std::move(arg.packet));
{
Nz::ENetPacketRef packet = m_host.AllocatePacket(arg.flags, std::move(arg.packet));
if (arg.acknowledgeCallback)
packet->OnAcknowledged.Connect(std::move(arg.acknowledgeCallback));

peer->Send(arg.channelId, std::move(packet));
}
}
else if constexpr (std::is_same_v<T, OutgoingEvent::QueryPeerInfo>)
{
Expand Down
13 changes: 11 additions & 2 deletions src/ServerLib/SessionVisibilityHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ namespace tsom

for (std::size_t chunkIndex = m_newlyVisibleChunk.FindFirst(); chunkIndex != m_newlyVisibleChunk.npos; chunkIndex = m_newlyVisibleChunk.FindNext(chunkIndex))
{
if (*m_activeChunkUpdates >= MaxConcurrentChunkUpdate)
break;

VisibleChunk& visibleChunk = m_visibleChunks[chunkIndex];

// Connect update signal on dispatch to prevent updates made during the same tick to be sent as update
Expand Down Expand Up @@ -151,9 +154,15 @@ namespace tsom
for (unsigned int i = 0; i < blockCount; ++i)
chunkCreatePacket.content[i] = Nz::SafeCast<Nz::UInt8>(chunkContent[i]);

m_networkSession->SendPacket(chunkCreatePacket);
(*m_activeChunkUpdates)++;
m_networkSession->SendPacket(chunkCreatePacket, [chunkLocation, chunkUpdateCount = m_activeChunkUpdates]
{
assert(*chunkUpdateCount > 0);
(*chunkUpdateCount)--;
});

m_newlyVisibleChunk.UnboundedReset(chunkIndex);
}
m_newlyVisibleChunk.Clear();

for (std::size_t chunkIndex = m_updatedChunk.FindFirst(); chunkIndex != m_updatedChunk.npos; chunkIndex = m_updatedChunk.FindNext(chunkIndex))
{
Expand Down

0 comments on commit d87a1d3

Please sign in to comment.