Skip to content

Commit 525f6f9

Browse files
committed
feat: rename packet queue methods for clarity and add separate send queue handling in Client class
1 parent 2e3b8b1 commit 525f6f9

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

Flakkari/Server/Client/Client.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ bool Client::incrementWarningCount()
4444
return _warningCount >= _maxWarningCount;
4545
}
4646

47-
void Client::addPacketToQueue(const Protocol::Packet<Protocol::CommandId> &packet)
47+
void Client::addPacketToReceiveQueue(const Protocol::Packet<Protocol::CommandId> &packet)
4848
{
4949
_apiVersion = packet.header._apiVersion;
5050
_receiveQueue.push_back(packet);
5151
}
5252

53+
void Client::addPacketToSendQueue(const Protocol::Packet<Protocol::CommandId> &packet)
54+
{
55+
_apiVersion = packet.header._apiVersion;
56+
_sendQueue.push_back(packet);
57+
}
58+
5359
} /* namespace Flakkari */

Flakkari/Server/Client/Client.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,21 @@ class Client {
8181
*/
8282
bool incrementWarningCount();
8383

84+
/**
85+
* @brief Add a packet to the client's receive queue and set the api version
86+
* used by the client
87+
*
88+
* @param packet The packet to add
89+
*/
90+
void addPacketToReceiveQueue(const Protocol::Packet<Protocol::CommandId> &packet);
91+
8492
/**
8593
* @brief Add a packet to the client's send queue and set the api version
8694
* used by the client
8795
*
8896
* @param packet The packet to add
8997
*/
90-
void addPacketToQueue(const Protocol::Packet<Protocol::CommandId> &packet);
98+
void addPacketToSendQueue(const Protocol::Packet<Protocol::CommandId> &packet);
9199

92100
/**
93101
* @brief Get the client's address
@@ -126,6 +134,8 @@ class Client {
126134
return _receiveQueue;
127135
}
128136

137+
[[nodiscard]] Network::PacketQueue<Protocol::Packet<Protocol::CommandId>> &getSendQueue() { return _sendQueue; }
138+
129139
private:
130140
std::chrono::steady_clock::time_point _lastActivity;
131141
std::shared_ptr<Network::Address> _address;

Flakkari/Server/Client/ClientManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ ClientManager::receivePacketFromClient(const std::shared_ptr<Network::Address> &
130130
if (packet.deserialize(buffer))
131131
{
132132
FLAKKARI_LOG_LOG("Client " + clientName + " sent a valid packet: " + packet.to_string());
133-
tmp_client->addPacketToQueue(packet);
133+
tmp_client->addPacketToReceiveQueue(packet);
134134
return std::nullopt;
135135
}
136136

Flakkari/Server/Game/Game.cpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void Game::loadSystems(Engine::ECS::Registry &registry, const std::string &scene
4444
if (sysName == "position")
4545
registry.add_system([this](Engine::ECS::Registry &r) { Engine::ECS::Systems::_2D::position(r, _deltaTime); });
4646

47-
else if (name == "apply_movable")
47+
else if (sysName == "apply_movable")
4848
registry.add_system(
4949
[this](Engine::ECS::Registry &r) { Engine::ECS::Systems::_3D::apply_movable(r, _deltaTime); });
5050

@@ -158,8 +158,7 @@ void Game::sendOnSameScene(const std::string &sceneName, Protocol::Packet<Protoc
158158

159159
packet.header._apiVersion = player->getApiVersion();
160160

161-
ClientManager::GetInstance().sendPacketToClient(player->getAddress(), packet.serialize());
162-
ClientManager::UnlockInstance();
161+
player->addPacketToSendQueue(packet);
163162
}
164163
}
165164

@@ -179,8 +178,7 @@ void Game::sendOnSameSceneExcept(const std::string &sceneName, Protocol::Packet<
179178

180179
packet.header._apiVersion = player->getApiVersion();
181180

182-
ClientManager::GetInstance().sendPacketToClient(player->getAddress(), packet.serialize());
183-
ClientManager::UnlockInstance();
181+
player->addPacketToSendQueue(packet);
184182
}
185183
}
186184

@@ -343,21 +341,44 @@ void Game::updateIncomingPackets(unsigned char maxMessagePerFrame)
343341
FLAKKARI_LOG_INFO("packet received: " + packet.to_string());
344342
messageCount--;
345343

346-
if (packet.header._commandId == Protocol::CommandId::REQ_USER_UPDATE)
347-
handleEvent(player, packet);
344+
if (packet.header._commandId == Protocol::CommandId::REQ_USER_UPDATES)
345+
handleEvents(player, packet);
348346

349-
if (packet.header._commandId == Protocol::CommandId::REQ_HEARTBEAT)
347+
else if (packet.header._commandId == Protocol::CommandId::REQ_HEARTBEAT)
350348
{
351349
Protocol::Packet<Protocol::CommandId> repPacket;
352-
repPacket.header._commandId = Protocol::CommandId::REP_HEARTBEAT;
353350
repPacket.header._apiVersion = packet.header._apiVersion;
354-
ClientManager::GetInstance().sendPacketToClient(player->getAddress(), repPacket.serialize());
355-
ClientManager::UnlockInstance();
351+
repPacket.header._commandId = Protocol::CommandId::REP_HEARTBEAT;
352+
353+
player->addPacketToSendQueue(repPacket);
356354
}
357355
}
358356
}
359357
}
360358

359+
void Game::updateOutcomingPackets(unsigned char maxMessagePerFrame)
360+
{
361+
for (auto &player : _players)
362+
{
363+
if (!player->isConnected())
364+
continue;
365+
auto &packets = player->getSendQueue();
366+
auto messageCount = maxMessagePerFrame;
367+
368+
Network::Buffer buffer;
369+
370+
while (!packets.empty() && messageCount > 0)
371+
{
372+
auto packet = packets.pop_front();
373+
messageCount--;
374+
375+
buffer += packet.serialize();
376+
ClientManager::GetInstance().sendPacketToClient(player->getAddress(), buffer);
377+
ClientManager::UnlockInstance();
378+
}
379+
}
380+
}
381+
361382
void Game::update()
362383
{
363384
auto now = std::chrono::steady_clock::now();

0 commit comments

Comments
 (0)