Skip to content

Commit

Permalink
Added support for sending a ping by setting the flag (isPing) in the …
Browse files Browse the repository at this point in the history
…MessageHeader
  • Loading branch information
NixAJ committed Aug 4, 2024
1 parent 4aac0cb commit 7445998
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Source/Gameplay/Gameplay/Network/GameMessageRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ namespace Network
return false;

GameOpcode opcode = static_cast<GameOpcode>(header.opcode);
if (header.flags.isPing)
opcode = GameOpcode::Shared_Ping;

if (opcode == GameOpcode::Invalid || opcode >= GameOpcode::Count)
return false;

const GameMessageHandler& opcodeHandler = _handlers[static_cast<u16>(header.opcode)];
const GameMessageHandler& opcodeHandler = _handlers[static_cast<u16>(opcode)];
bool isSmallerThanMinSize = header.size < opcodeHandler.minSize;
bool isLargerThanMaxSize = header.size > opcodeHandler.maxSize && opcodeHandler.maxSize != -1;

Expand Down
4 changes: 2 additions & 2 deletions Source/Gameplay/Gameplay/Network/Opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace Network
Client_Connect,
Server_Connected,

Client_Ping,
Server_Pong,
Shared_Ping,
Server_UpdateStats,

Client_SendCheatCommand,
Server_SendCheatCommandResult,
Expand Down
7 changes: 7 additions & 0 deletions Source/Network/Network/Define.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ namespace Network

struct MessageHeader
{
public:
struct Flags
{
u8 isPing : 1;
};

public:
OpcodeType opcode = 0;
u16 size = 0;
Flags flags = { 0 };
};

struct Message
Expand Down
11 changes: 11 additions & 0 deletions Source/Network/Network/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ namespace Network

buffer->SkipRead(sizeof(MessageHeader));

if (header->flags.isPing)
{
MessageHeader pongHeader;
pongHeader.flags.isPing = 1;

std::shared_ptr<Bytebuffer> pongBuffer = Bytebuffer::Borrow<PacketHeaderSize>();
pongBuffer->Put(pongHeader);

SendPacket(socketID, pongBuffer);
}

std::shared_ptr<Bytebuffer> messageBuffer = Bytebuffer::Borrow<DEFAULT_BUFFER_SIZE>();
{
// Payload
Expand Down

0 comments on commit 7445998

Please sign in to comment.