diff --git a/Flakkari/Protocol/Header.hpp b/Flakkari/Protocol/Header.hpp index a5eeae71..526bffae 100644 --- a/Flakkari/Protocol/Header.hpp +++ b/Flakkari/Protocol/Header.hpp @@ -37,7 +37,8 @@ using ulong = unsigned long; // 64 bits (max: 18446744073709551615) * */ enum class ApiVersion : byte { - V_0, + V_0 = 0, + V_1 = 1, MAX_VERSION }; diff --git a/Flakkari/Server/Client/Client.cpp b/Flakkari/Server/Client/Client.cpp index a521623f..79c6c21b 100644 --- a/Flakkari/Server/Client/Client.cpp +++ b/Flakkari/Server/Client/Client.cpp @@ -11,9 +11,11 @@ namespace Flakkari { -Client::Client(const std::shared_ptr &address, const std::string &name) +Client::Client(const std::shared_ptr &address, const std::string &name, + Protocol::ApiVersion apiVersion) : _address(address), _gameName(name), _name(address->toString().value_or("")) { + _apiVersion = apiVersion; _lastActivity = std::chrono::steady_clock::now(); } @@ -42,6 +44,10 @@ bool Client::incrementWarningCount() return _warningCount >= _maxWarningCount; } -void Client::addPacketToQueue(const Protocol::Packet &packet) { _receiveQueue.push_back(packet); } +void Client::addPacketToQueue(const Protocol::Packet &packet) +{ + _apiVersion = packet.header._apiVersion; + _receiveQueue.push_back(packet); +} } /* namespace Flakkari */ diff --git a/Flakkari/Server/Client/Client.hpp b/Flakkari/Server/Client/Client.hpp index 8cc62dfb..797a0d1b 100644 --- a/Flakkari/Server/Client/Client.hpp +++ b/Flakkari/Server/Client/Client.hpp @@ -46,8 +46,9 @@ class Client { * * @param address The client's address * @param name The Game's name + * @param apiVersion The API version used by the client */ - Client(const std::shared_ptr &address, const std::string &name); + Client(const std::shared_ptr &address, const std::string &name, Protocol::ApiVersion apiVersion); ~Client(); /** @@ -118,6 +119,8 @@ class Client { [[nodiscard]] unsigned short getMaxPacketHistory() const { return _maxPacketHistory; } + [[nodiscard]] Protocol::ApiVersion getApiVersion() const { return _apiVersion; } + [[nodiscard]] Network::PacketQueue> &getReceiveQueue() { return _receiveQueue; @@ -131,6 +134,7 @@ class Client { std::string _gameName; bool _isConnected = true; std::string _name; + Protocol::ApiVersion _apiVersion; unsigned short _warningCount = 0; unsigned short _maxWarningCount = 5; unsigned short _maxPacketHistory = 10; diff --git a/Flakkari/Server/Client/ClientManager.cpp b/Flakkari/Server/Client/ClientManager.cpp index b178acef..7613b4e9 100644 --- a/Flakkari/Server/Client/ClientManager.cpp +++ b/Flakkari/Server/Client/ClientManager.cpp @@ -47,7 +47,8 @@ ClientManager::addClient(const std::shared_ptr &client, Networ } std::string gameName = std::string((const char *) packet.payload.getData(), packet.payload.getSize()); - _clients[clientString] = std::make_shared(client, gameName); + auto apiVersion = packet.header._apiVersion; + _clients[clientString] = std::make_shared(client, gameName, apiVersion); return std::make_pair(gameName, _clients[clientString]); } @@ -82,7 +83,7 @@ void ClientManager::checkInactiveClients() } } -void ClientManager::sendPacketToClient(std::shared_ptr client, const Network::Buffer &packet) +void ClientManager::sendPacketToClient(const std::shared_ptr &client, const Network::Buffer &packet) { std::thread([this, client, packet] { _socket->sendTo(client, packet); }).detach(); } diff --git a/Flakkari/Server/Game/Game.cpp b/Flakkari/Server/Game/Game.cpp index 730e7a1b..5a78efa2 100644 --- a/Flakkari/Server/Game/Game.cpp +++ b/Flakkari/Server/Game/Game.cpp @@ -30,6 +30,7 @@ void Game::sendAllEntities(const std::string &sceneName, std::shared_ptr Protocol::Packet packet; packet.header._commandId = Protocol::CommandId::REQ_ENTITY_SPAWN; + packet.header._apiVersion = player->getApiVersion(); packet << i; packet.injectString(sceneName); Protocol::PacketFactory::addComponentsToPacketByEntity(packet, registry, i); @@ -218,7 +219,7 @@ void Game::loadScene(const std::string &sceneName) } } -void Game::sendOnSameScene(const std::string &sceneName, const Network::Buffer &message) +void Game::sendOnSameScene(const std::string &sceneName, Protocol::Packet &packet) { for (auto &player : _players) { @@ -229,12 +230,14 @@ void Game::sendOnSameScene(const std::string &sceneName, const Network::Buffer & if (player->getSceneName() != sceneName) continue; - ClientManager::GetInstance().sendPacketToClient(player->getAddress(), message); + packet.header._apiVersion = player->getApiVersion(); + + ClientManager::GetInstance().sendPacketToClient(player->getAddress(), packet.serialize()); ClientManager::UnlockInstance(); } } -void Game::sendOnSameSceneExcept(const std::string &sceneName, const Network::Buffer &message, +void Game::sendOnSameSceneExcept(const std::string &sceneName, Protocol::Packet &packet, std::shared_ptr except) { for (auto &player : _players) @@ -248,7 +251,9 @@ void Game::sendOnSameSceneExcept(const std::string &sceneName, const Network::Bu if (player == except) continue; - ClientManager::GetInstance().sendPacketToClient(player->getAddress(), message); + packet.header._apiVersion = player->getApiVersion(); + + ClientManager::GetInstance().sendPacketToClient(player->getAddress(), packet.serialize()); ClientManager::UnlockInstance(); } } @@ -262,7 +267,7 @@ void Game::checkDisconnect() Protocol::Packet packet; packet.header._commandId = Protocol::CommandId::REQ_DISCONNECT; packet << player->getEntity(); - sendOnSameScene(player->getSceneName(), packet.serialize()); + sendOnSameScene(player->getSceneName(), packet); _scenes[player->getSceneName()].kill_entity(player->getEntity()); removePlayer(player); } @@ -291,7 +296,7 @@ void Game::sendUpdatePosition(std::shared_ptr player, Engine::ECS::Compo std::to_string(pos.position.vec.y) + ")" + ", Vel: (" + std::to_string(vel.velocity.vec.x) + ", " + std::to_string(vel.velocity.vec.y) + ")" + ", Acc: (" + std::to_string(vel.acceleration.vec.x) + ", " + std::to_string(vel.acceleration.vec.y) + ")" + ">"); - sendOnSameScene(player->getSceneName(), packet.serialize()); + sendOnSameScene(player->getSceneName(), packet); } void Game::handleEvent(std::shared_ptr player, Protocol::Packet packet) @@ -383,7 +388,7 @@ void Game::handleEvent(std::shared_ptr player, Protocol::PacketgetSceneName()); shootPacket << player->getEntity(); // create a bullet with player as parent - sendOnSameScene(player->getSceneName(), shootPacket.serialize()); + sendOnSameScene(player->getSceneName(), shootPacket); } return; } @@ -474,6 +479,7 @@ bool Game::addPlayer(std::shared_ptr player) Protocol::Packet packet; packet.header._commandId = Protocol::CommandId::REP_CONNECT; + packet.header._apiVersion = player->getApiVersion(); packet << newEntity; packet.injectString(sceneGame); packet.injectString(player->getName().value_or("")); @@ -483,11 +489,12 @@ bool Game::addPlayer(std::shared_ptr player) Protocol::Packet packet2; packet2.header._commandId = Protocol::CommandId::REQ_ENTITY_SPAWN; + packet2.header._apiVersion = packet.header._apiVersion; packet2 << newEntity; packet2.injectString(sceneGame); packet2.injectString(p_Template); - sendOnSameSceneExcept(sceneGame, packet2.serialize(), player); + sendOnSameSceneExcept(sceneGame, packet2, player); return true; } @@ -506,7 +513,7 @@ bool Game::removePlayer(std::shared_ptr player) packet.header._commandId = Protocol::CommandId::REQ_ENTITY_DESTROY; packet << entity; - sendOnSameScene(sceneGame, packet.serialize()); + sendOnSameScene(sceneGame, packet); registry.kill_entity(entity); _players.erase(it); diff --git a/Flakkari/Server/Game/Game.hpp b/Flakkari/Server/Game/Game.hpp index 9cd31fe0..a17679df 100644 --- a/Flakkari/Server/Game/Game.hpp +++ b/Flakkari/Server/Game/Game.hpp @@ -93,9 +93,9 @@ class Game { public: // Actions void sendAllEntities(const std::string &sceneName, std::shared_ptr player); - void sendOnSameScene(const std::string &sceneName, const Network::Buffer &message); + void sendOnSameScene(const std::string &sceneName, Protocol::Packet &packet); - void sendOnSameSceneExcept(const std::string &sceneName, const Network::Buffer &message, + void sendOnSameSceneExcept(const std::string &sceneName, Protocol::Packet &packet, std::shared_ptr except); /**