122 lines
2.2 KiB
C++
122 lines
2.2 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <blitz/protocol/PacketData.h>
|
|
|
|
namespace blitz {
|
|
namespace protocol {
|
|
|
|
class PacketVisitor;
|
|
|
|
using PacketID = std::uint8_t;
|
|
|
|
enum class PacketType : PacketID {
|
|
// client --> server
|
|
|
|
PlayerLogin = 0,
|
|
UpdateHealth,
|
|
|
|
// client <-- server
|
|
|
|
LoggingSuccess,
|
|
PlayerDeath,
|
|
PlayerJoin,
|
|
PlayerLeave,
|
|
PlayerList,
|
|
PlayerStats,
|
|
ServerConfig,
|
|
ServerTps,
|
|
UpdateGameState,
|
|
|
|
// client <--> server
|
|
|
|
KeepAlive,
|
|
Disconnect,
|
|
ChatMessage,
|
|
PlayerPositionAndRotation,
|
|
PlayerShoot,
|
|
|
|
PACKET_COUNT
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Packet {
|
|
public:
|
|
virtual PacketType GetType() const = 0;
|
|
|
|
virtual void Accept(PacketVisitor& a_Visitor) const = 0;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
namespace packets {
|
|
|
|
/**
|
|
* \tparam PT The packet type
|
|
* \tparam Data The structure holding the data of the packet
|
|
*/
|
|
template <PacketType PT, typename Data>
|
|
class ConcretePacket : public Packet {
|
|
public:
|
|
using PacketDataType = Data;
|
|
|
|
PacketDataType m_Data;
|
|
|
|
ConcretePacket(const PacketDataType& a_Data = {});
|
|
|
|
constexpr PacketType GetType() const override {
|
|
return PT;
|
|
};
|
|
|
|
private:
|
|
void Accept(PacketVisitor& a_Visitor) const override;
|
|
|
|
friend class PacketVisitor;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// define BLITZ_INSTANCIATE_PACKETS
|
|
// 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>
|
|
#else
|
|
#define DeclarePacket(Type) using Type = ConcretePacket<PacketType::Type, data::Type>
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
} // namespace packets
|
|
|
|
} // namespace protocol
|
|
} // namespace blitz
|