improve packet interface

This commit is contained in:
2024-07-18 20:45:55 +02:00
parent 9fadb86031
commit 8bc2f26578
11 changed files with 400 additions and 79 deletions

View File

@@ -0,0 +1,19 @@
#pragma once
#define DeclareAllPacket() \
DeclarePacket(PlayerLogin, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(UpdateHealth, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(LoggingSuccess, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(PlayerDeath, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(PlayerJoin, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(PlayerLeave, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(PlayerStats, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(PlayerList, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(ServerConfig, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(ServerTps, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(UpdateGameState, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(KeepAlive, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(Disconnect, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(ChatMessage, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(PlayerPositionAndRotation, Nz::ENetPacketFlag::Reliable); \
DeclarePacket(PlayerShoot, Nz::ENetPacketFlag::Reliable);

View File

@@ -1,10 +1,14 @@
#pragma once
#include <blitz/protocol/Packets.h>
#include <blitz/protocol/PacketDeclare.h>
namespace blitz {
namespace protocol {
#define DeclarePacket(PacketName, ...) \
virtual void Visit(const packets::PacketName&) {}
class PacketVisitor {
protected:
PacketVisitor() {}
@@ -13,23 +17,10 @@ class PacketVisitor {
public:
void Check(const Packet& packet);
virtual void Visit(const packets::PlayerLogin&) {}
virtual void Visit(const packets::UpdateHealth&) {}
virtual void Visit(const packets::LoggingSuccess&) {}
virtual void Visit(const packets::PlayerDeath&) {}
virtual void Visit(const packets::PlayerJoin&) {}
virtual void Visit(const packets::PlayerLeave&) {}
virtual void Visit(const packets::PlayerList&) {}
virtual void Visit(const packets::PlayerStats&) {}
virtual void Visit(const packets::ServerConfig&) {}
virtual void Visit(const packets::ServerTps&) {}
virtual void Visit(const packets::UpdateGameState&) {}
virtual void Visit(const packets::KeepAlive&) {}
virtual void Visit(const packets::Disconnect&) {}
virtual void Visit(const packets::ChatMessage&) {}
virtual void Visit(const packets::PlayerPositionAndRotation&) {}
virtual void Visit(const packets::PlayerShoot&) {}
DeclareAllPacket()
};
#undef DeclarePacket
} // namespace protocol
} // namespace blitz

View File

@@ -1,7 +1,8 @@
#pragma once
#include <string>
#include <blitz/protocol/PacketData.h>
#include <blitz/protocol/PacketDeclare.h>
#include <string>
namespace blitz {
namespace protocol {
@@ -64,7 +65,7 @@ template <PacketType PT, typename Data>
class ConcretePacket : public Packet {
public:
using PacketDataType = Data;
PacketDataType m_Data;
ConcretePacket(const PacketDataType& a_Data = {});
@@ -87,33 +88,16 @@ class ConcretePacket : public Packet {
// before including this file
// if you want to instantiate templates
#ifdef BLITZ_INSTANCIATE_PACKETS
#define DeclarePacket(Type) \
using Type = ConcretePacket<PacketType::Type, data::Type>; \
template class ConcretePacket<PacketType::Type, data::Type>
#define DeclarePacket(PacketName, ...) \
using PacketName = ConcretePacket<PacketType::PacketName, data::PacketName>; \
template class ConcretePacket<PacketType::PacketName, data::PacketName>
#else
#define DeclarePacket(Type) using Type = ConcretePacket<PacketType::Type, data::Type>
#define DeclarePacket(PacketName, ...) using PacketName = ConcretePacket<PacketType::PacketName, data::PacketName>
#endif
DeclareAllPacket()
DeclarePacket(PlayerLogin);
DeclarePacket(UpdateHealth);
DeclarePacket(LoggingSuccess);
DeclarePacket(PlayerDeath);
DeclarePacket(PlayerJoin);
DeclarePacket(PlayerLeave);
DeclarePacket(PlayerStats);
DeclarePacket(PlayerList);
DeclarePacket(ServerConfig);
DeclarePacket(ServerTps);
DeclarePacket(UpdateGameState);
DeclarePacket(KeepAlive);
DeclarePacket(Disconnect);
DeclarePacket(ChatMessage);
DeclarePacket(PlayerPositionAndRotation);
DeclarePacket(PlayerShoot);
#undef DeclarePacket
} // namespace packets