diff --git a/Flakkari/Protocol/Header.hpp b/Flakkari/Protocol/Header.hpp index ea03c983..b77331f3 100644 --- a/Flakkari/Protocol/Header.hpp +++ b/Flakkari/Protocol/Header.hpp @@ -65,7 +65,7 @@ inline namespace V_0 { Priority _priority : 4 = Priority::LOW; ApiVersion _apiVersion : 4 = ApiVersion::V_0; Id _commandId; - ushort _contentLength; + ushort _contentLength = 0; uint _sequenceNumber = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); }; diff --git a/Flakkari/Server/UDPServer.cpp b/Flakkari/Server/UDPServer.cpp index 9d932cd6..6d653d94 100644 --- a/Flakkari/Server/UDPServer.cpp +++ b/Flakkari/Server/UDPServer.cpp @@ -59,41 +59,27 @@ void UDPServer::handlePacket() ClientManager::checkInactiveClients(); // parse packet - Protocol::API::Header header; - std::copy(packet->second.begin(), packet->second.begin() + sizeof(header), reinterpret_cast(&header)); - - std::cout << (*packet->first.get()); // Address - std::cout << " : "; - std::cout << packet->second << std::endl; // Buffer + Protocol::API::Packet parsedPacket; + if (parsedPacket.deserialize(packet->second) == false) + FLAKKARI_LOG_WARNING("Invalid packet received"); std::cout << "RECV Header: " << std::endl; - std::cout << " Priority: " << (int)header._priority << std::endl; - std::cout << " ApiVersion: " << (int)header._apiVersion << std::endl; - std::cout << " CommandId: " << (int)header._commandId << std::endl; - std::cout << " ContentLength: " << (int)header._contentLength << std::endl; + std::cout << " Priority: " << (int)parsedPacket.header._priority << std::endl; + std::cout << " ApiVersion: " << (int)parsedPacket.header._apiVersion << std::endl; + std::cout << " CommandId: " << (int)parsedPacket.header._commandId << std::endl; + std::cout << " ContentLength: " << (int)parsedPacket.header._contentLength << std::endl; // send to all clients - Protocol::API::Header sendHeader( - Protocol::API::Priority::LOW, - Protocol::API::ApiVersion::V_1, - Protocol::API::FlakkariEventId::REP_ENTITY_SPAWN, - 0 - ); - - Protocol::API::PlayerPacket playerPacket; - - sendHeader._contentLength = sizeof(playerPacket); - - Network::Buffer buffer(sizeof(sendHeader) + sizeof(playerPacket)); - std::copy(reinterpret_cast(&sendHeader), reinterpret_cast(&sendHeader) + sizeof(sendHeader), buffer.begin()); + Protocol::API::Packet sendHeader; + sendHeader.header._commandId = Protocol::API::CommandId::REP_CONNECT; std::cout << "SEND Header: " << std::endl; - std::cout << " Priority: " << (int)sendHeader._priority << std::endl; - std::cout << " ApiVersion: " << (int)sendHeader._apiVersion << std::endl; - std::cout << " CommandId: " << (int)sendHeader._commandId << std::endl; - std::cout << " ContentLength: " << (int)sendHeader._contentLength << std::endl; + std::cout << " Priority: " << (int)sendHeader.header._priority << std::endl; + std::cout << " ApiVersion: " << (int)sendHeader.header._apiVersion << std::endl; + std::cout << " CommandId: " << (int)sendHeader.header._commandId << std::endl; + std::cout << " ContentLength: " << (int)sendHeader.header._contentLength << std::endl; - _socket->sendTo(packet->first, buffer); + _socket->sendTo(packet->first, sendHeader.serialize()); } void UDPServer::run()