begin packet serialization

This commit is contained in:
2024-07-18 15:28:01 +02:00
parent 2e63a474b8
commit 6e9c747b2d
17 changed files with 842 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
#pragma once
#include <string>
namespace blitz {
namespace protocol {
namespace data {
struct PlayerLogin {
std::string m_PlayerName;
};
struct UpdateHealth {
float m_NewHealth;
};
struct LoggingSuccess {};
struct PlayerDeath {};
struct PlayerJoin {};
struct PlayerLeave {};
struct PlayerStats {};
struct PlayerList {};
struct ServerConfig {};
struct ServerTps {};
struct UpdateGameState {};
struct KeepAlive {};
struct Disconnect {};
struct ChatMessage {
std::string m_Text;
};
struct PlayerPositionAndRotation {};
struct PlayerShoot {};
} // namespace data
} // namespace protocol
} // namespace blitz

View File

@@ -0,0 +1,19 @@
#pragma once
#include <blitz/protocol/Packets.h>
#include <memory>
namespace blitz {
namespace protocol {
namespace PacketFactory {
template<typename PacketDerived, typename = typename std::enable_if<std::is_base_of<Packet, PacketDerived>::value>::type>
std::unique_ptr<PacketDerived> CreatePacket() {
return std::make_unique<PacketDerived>();
}
const std::unique_ptr<Packet>& CreateReadOnlyPacket(PacketType a_Type);
} // namespace PacketFactory
} // namespace protocol
} // namespace blitz

View File

@@ -0,0 +1,19 @@
#pragma once
#include <blitz/protocol/Packets.h>
#include <Nazara/Core/ByteArray.hpp>
namespace blitz {
namespace protocol {
using PacketPtr = std::unique_ptr<Packet>;
namespace PacketSerializer {
Nz::ByteArray Serialize(const Packet& a_Packet);
std::unique_ptr<Packet> Deserialize(Nz::ByteArray& a_Data);
}
} // namespace protocol
} // namespace blitz

View File

@@ -0,0 +1,35 @@
#pragma once
#include <blitz/protocol/Packets.h>
namespace blitz {
namespace protocol {
class PacketVisitor {
protected:
PacketVisitor() {}
virtual ~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&) {}
};
} // namespace protocol
} // namespace blitz

View File

@@ -0,0 +1,121 @@
#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