Skip to content

Commit

Permalink
fix(Server): Fix packet parsing and update header initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterLaplace committed Jan 13, 2024
1 parent fbd12c1 commit dcca1ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Flakkari/Protocol/Header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
};
Expand Down
42 changes: 14 additions & 28 deletions Flakkari/Server/UDPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(&header));

std::cout << (*packet->first.get()); // Address
std::cout << " : ";
std::cout << packet->second << std::endl; // Buffer
Protocol::API::Packet<Protocol::API::CommandId> 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<const char*>(&sendHeader), reinterpret_cast<const char*>(&sendHeader) + sizeof(sendHeader), buffer.begin());
Protocol::API::Packet<Protocol::API::CommandId> 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()
Expand Down

0 comments on commit dcca1ed

Please sign in to comment.