-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
15 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,28 @@ | ||
#include "protocol.h" | ||
|
||
std::string ToHexString(const unsigned char* data, size_t length) { | ||
std::ostringstream oss; | ||
oss << std::hex << std::setfill('0'); | ||
for (size_t i = 0; i < length; ++i) { | ||
oss << std::setw(2) << static_cast<int>(data[i]); | ||
} | ||
return oss.str(); | ||
} | ||
|
||
void ProtocolManager::Init(RakPeerInterface* peer) { | ||
Packet* packet; | ||
while ((packet = peer->Receive())) { | ||
int packetType = packet->data[0]; | ||
|
||
Logger::Info("Received packet with ID: " + std::to_string(packetType)); | ||
|
||
switch (packetType) { | ||
case 0: | ||
ProtocolManager::PingPacketCallback(peer, packet); | ||
case 1: | ||
ProtocolManager::PingPacketCallback(peer, packet); | ||
} | ||
ProtocolManager::PingPacketCallback(peer, packet); | ||
|
||
peer->DeallocatePacket(packet); | ||
} | ||
}; | ||
|
||
void ProtocolManager::PingPacketCallback(RakPeerInterface* peer, Packet* packet) { | ||
PingPacket* pingPacket = reinterpret_cast<PingPacket*>(packet); | ||
|
||
PingPacket pongPacket; | ||
pongPacket.packetType = 1; | ||
pongPacket.clientTimestamp = pingPacket->clientTimestamp; | ||
|
||
peer->Send(reinterpret_cast<const char*>(&pongPacket), sizeof(PingPacket), HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->systemAddress, false); | ||
Logger::Info("send a pong"); | ||
}; | ||
std::string hexData = ToHexString(packet->data, packet->length); | ||
Logger::Info("packet data: " + hexData); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters