splib transition

This commit is contained in:
2025-07-10 16:34:28 +02:00
parent c0473e5a65
commit cc57e03bc4
29 changed files with 146 additions and 1127 deletions

View File

@@ -3,40 +3,40 @@
#include <array>
#include <string>
#include <td/Types.h>
#include <sp/protocol/BitField.h>
namespace td {
namespace protocol {
namespace cdata {
struct PlaceTower {
TowerType m_Type : 4;
PlayerID m_Placer : 4;
sp::BitField<TowerType, 4> m_Type;
sp::BitField<PlayerID, 4> m_Placer;
TowerCoords m_Position;
};
struct UpgradeTower {
TowerID m_Tower : 12;
std::uint8_t m_Upgrade : 4;
sp::BitField<TowerID, 12> m_Tower;
sp::BitField<std::uint8_t, 4> m_Upgrade;
};
struct SpawnTroop {
EntityType m_Type : 5;
std::uint8_t m_Level : 3;
sp::BitField<EntityType, 5> m_Type;
sp::BitField<std::uint8_t, 3> m_Level;
EntityCoords m_Position;
PlayerID m_Sender;
};
struct UseItem {
ShopItem m_Item : 4;
PlayerID m_User : 4;
sp::BitField<ShopItem, 4> m_Item;
sp::BitField<PlayerID, 4> m_User;
EntityCoords m_Position;
};
struct TeamChange {
PlayerID m_Player : 7;
Team m_NewTeam : 1;
sp::BitField<PlayerID, 7> m_Player;
sp::BitField<Team, 1> m_NewTeam;
};
struct PlayerJoin {

View File

@@ -1,22 +0,0 @@
#pragma once
namespace td {
namespace protocol {
/**
* \def DeclareAllPacket
* \brief Avoids repetitive operations on commands
*/
#define DeclareAllCommand() \
DeclareCommand(End) \
DeclareCommand(PlaceTower) \
DeclareCommand(PlayerJoin) \
DeclareCommand(SpawnTroop) \
DeclareCommand(TeamChange) \
DeclareCommand(UpgradeTower) \
DeclareCommand(UseItem)
} // namespace protocol
} // namespace td

View File

@@ -1,17 +0,0 @@
#pragma once
/**
* \file CommandDispatcher.h
* \brief File containing the td::protocol::CommandDispatcher class
*/
#include <td/protocol/Dispatcher.h>
#include <td/protocol/command/Commands.h>
namespace td {
namespace protocol {
using CommandDispatcher = Dispatcher<CommandType, CommandVisitor, Command>;
} // namespace protocol
} // namespace td

View File

@@ -1,19 +0,0 @@
#pragma once
#include <memory>
#include <td/protocol/command/Commands.h>
namespace td {
namespace protocol {
namespace CommandFactory {
template <typename CommandDerived, typename = typename std::enable_if<std::is_base_of<Command, CommandDerived>::value>::type>
std::shared_ptr<CommandDerived> CreateCommand() {
return std::make_shared<CommandDerived>();
}
const std::shared_ptr<Command>& CreateReadOnlyCommand(CommandType a_Type);
} // namespace CommandFactory
} // namespace protocol
} // namespace td

View File

@@ -1,21 +0,0 @@
#pragma once
#include <memory>
#include <td/common/DataBuffer.h>
namespace td {
namespace protocol {
class Command;
using CommandPtr = std::shared_ptr<Command>;
namespace CommandSerializer {
DataBuffer Serialize(const Command& a_Command);
std::shared_ptr<Command> Deserialize(DataBuffer& a_Data);
} // namespace CommandSerializer
} // namespace protocol
} // namespace td

View File

@@ -1,39 +0,0 @@
#pragma once
/**
* \file CommandVisitor.h
* \brief File containing the td::protocol::CommandVisitor class
*/
#include <td/protocol/command/Commands.h>
namespace td {
namespace protocol {
#define DeclareCommand(CommandName, ...) \
/** This function is called when the packet processed by CommandVisitor::Check is a CommandName */ \
virtual void Visit(const commands::CommandName&) {}
/**
* \class CommandVisitor
* \brief This class uses double-dispatch in order to find the real type of a packet
*/
class CommandVisitor : private NonCopyable {
protected:
CommandVisitor() {}
virtual ~CommandVisitor() {}
public:
/**
* \brief Calls the right CommandVisitor::Visit method corresponding to the real type of the packet
* \param packet the Command to visit
*/
void Check(const Command& packet);
DeclareAllCommand()
};
#undef DeclareCommand
} // namespace protocol
} // namespace td

View File

@@ -5,107 +5,59 @@
* \brief File containing the definitions of the lockstep commands
*/
#include <memory>
#include <sp/protocol/ConcreteMessage.h>
#include <sp/protocol/MessageDispatcher.h>
#include <sp/protocol/MessageFactory.h>
#include <sp/protocol/MessageHandler.h>
#include <td/Types.h>
#include <td/common/NonCopyable.h>
#include <td/protocol/command/CommandData.h>
#include <td/protocol/command/CommandDeclare.h>
#include <memory>
namespace td {
namespace protocol {
class CommandVisitor;
/** A Command id is 8 bits wide */
using CommandID = std::uint8_t;
#define DeclareCommand(CommandName, ...) /** CommandName */ CommandName,
/**
* \enum CommandType
* \brief Map a Command to an id
*/
enum class CommandType : CommandID {
DeclareAllCommand()
/** The number of Commands */
COMMAND_COUNT
};
#undef DeclareCommand
class Command : private NonCopyable {
public:
/**
* \return The real type of the Command
*/
virtual CommandType GetType() const = 0;
private:
/** Use a CommandVisitor to make double-dispatch possible */
virtual void Accept(CommandVisitor& a_Visitor) const = 0;
friend class CommandVisitor;
enum class CommandID : std::uint8_t {
End = 0,
PlaceTower,
PlayerJoin,
SpawnTroop,
TeamChange,
UpgradeTower,
UseItem,
};
class CommandHandler;
using CommandBase = sp::MessageBase<CommandID, CommandHandler>;
template <typename TData, CommandID ID>
using CommandMessage = sp::ConcreteMessage<TData, CommandID, ID, CommandHandler>;
namespace commands {
/**
* \class ConcreteCommand
* \brief A Command associated with an id and holding data
* \tparam PT The Command type
* \tparam Data The structure holding the data of the Command (in td::protocol::data namespace)
*/
template <CommandType CT, typename Data>
class ConcreteCommand : public Command {
public:
/** The type of the struct holding the data */
using CommandDataType = Data;
/** The structure holding the actual data */
CommandDataType m_Data;
/** Construct the Command with data of type CommandDataType */
ConcreteCommand(const CommandDataType& a_Data = {});
constexpr CommandType GetType() const override {
return CT;
};
private:
void Accept(CommandVisitor& a_Visitor) const override;
};
// define TD_INSTANCIATE_COMMANDS
// before including this file
// if you want to instantiate templates
#ifdef TD_INSTANCIATE_COMMANDS
#define DeclareCommand(CommandName, ...) \
using CommandName = ConcreteCommand<CommandType::CommandName, cdata::CommandName>; \
template class ConcreteCommand<CommandType::CommandName, cdata::CommandName>;
#else
#define DeclareCommand(CommandName, ...) /** Defines the CommandName Command */ \
using CommandName = ConcreteCommand<CommandType::CommandName, cdata::CommandName>;
#endif
DeclareAllCommand()
#undef DeclareCommand
using EndCommand = CommandMessage<cdata::End, CommandID::End>;
using PlaceTowerCommand = CommandMessage<cdata::PlaceTower, CommandID::PlaceTower>;
using PlayerJoinCommand = CommandMessage<cdata::PlayerJoin, CommandID::PlayerJoin>;
using SpawnTroopCommand = CommandMessage<cdata::SpawnTroop, CommandID::SpawnTroop>;
using TeamChangeCommand = CommandMessage<cdata::TeamChange, CommandID::TeamChange>;
using UpgradeTowerCommand = CommandMessage<cdata::UpgradeTower, CommandID::UpgradeTower>;
using UseItemCommand = CommandMessage<cdata::UseItem, CommandID::UseItem>;
} // namespace commands
using LockStep = std::vector<std::shared_ptr<protocol::Command>>;
using AllCommands = std::tuple<commands::EndCommand, commands::PlaceTowerCommand, commands::PlayerJoinCommand,
commands::SpawnTroopCommand, commands::TeamChangeCommand, commands::UpgradeTowerCommand, commands::UseItemCommand>;
class CommandHandler : public sp::MessageHandler<AllCommands> {};
using CommandDispatcher = sp::MessageDispatcher<CommandBase>;
using CommandFactory = sp::MessageFactory<CommandBase, AllCommands>;
using LockStep = std::vector<std::shared_ptr<CommandBase>>;
} // namespace protocol
} // namespace td

View File

@@ -3,6 +3,7 @@
#include <td/Types.h>
#include <vector>
#include <td/protocol/command/Commands.h>
#include <td/common/Array.h>
// Make it dynamic ?
#define LOCKSTEP_BUFFER_SIZE 10
@@ -68,7 +69,7 @@ struct BeginGame {
struct LockSteps {
std::uint16_t m_FirstFrameNumber;
std::array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
Array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
};
} // namespace pdata

View File

@@ -1,48 +0,0 @@
#pragma once
/**
* \file PacketDeclare.h
* \brief Holds the definitions of the packets (but not their content)
*/
namespace td {
namespace protocol {
/**
* \enum PacketSender
* \brief Indicate who should send a packet
*/
enum class PacketSenderType {
/** Sent by clients and server */
Both = 1,
/** Sent by clients to the server */
Client,
/** Sent by server to the clients */
Server,
};
enum class PacketSendType {
Reliable = 1,
Unreliable,
UnreliableOrdered,
};
/**
* \def DeclareAllPacket
* \brief Avoids repetitive operations on packets
*/
#define DeclareAllPacket() \
DeclarePacket(ChatMessage, Reliable, Both) \
DeclarePacket(BeginGame, Reliable, Server) \
DeclarePacket(Disconnect, Reliable, Both) \
DeclarePacket(KeepAlive, Reliable, Both) \
DeclarePacket(LockSteps, Unreliable, Both) \
DeclarePacket(LoggingSuccess, Reliable, Server) \
DeclarePacket(PlayerJoin, Reliable, Server) \
DeclarePacket(PlayerLeave, Reliable, Server) \
DeclarePacket(PlayerLogin, Reliable, Client) \
} // namespace protocol
} // namespace td

View File

@@ -1,17 +0,0 @@
#pragma once
/**
* \file PacketDispatcher.h
* \brief File containing the td::protocol::PacketDispatcher class
*/
#include <td/protocol/Dispatcher.h>
#include <td/protocol/packet/Packets.h>
namespace td {
namespace protocol {
using PacketDispatcher = Dispatcher<PacketType, PacketVisitor, Packet>;
} // namespace protocol
} // namespace td

View File

@@ -1,19 +0,0 @@
#pragma once
#include <memory>
#include <td/protocol/packet/Packets.h>
namespace td {
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 td

View File

@@ -1,21 +0,0 @@
#pragma once
#include <memory>
#include <td/common/DataBuffer.h>
namespace td {
namespace protocol {
class Packet;
using PacketPtr = std::unique_ptr<Packet>;
namespace PacketSerializer {
DataBuffer Serialize(const Packet& a_Packet);
std::unique_ptr<Packet> Deserialize(DataBuffer& a_Data);
} // namespace PacketSerializer
} // namespace protocol
} // namespace td

View File

@@ -1,39 +0,0 @@
#pragma once
/**
* \file PacketVisitor.h
* \brief File containing the td::protocol::PacketVisitor class
*/
#include <td/protocol/packet/Packets.h>
namespace td {
namespace protocol {
#define DeclarePacket(PacketName, ...) \
/** This function is called when the packet processed by PacketVisitor::Check is a PacketName */ \
virtual void Visit(const packets::PacketName&) {}
/**
* \class PacketVisitor
* \brief This class uses double-dispatch in order to find the real type of a packet
*/
class PacketVisitor : private NonCopyable {
protected:
PacketVisitor() {}
virtual ~PacketVisitor() {}
public:
/**
* \brief Calls the right PacketVisitor::Visit method corresponding to the real type of the packet
* \param packet the Packet to visit
*/
void Check(const Packet& packet);
DeclareAllPacket()
};
#undef DeclarePacket
} // namespace protocol
} // namespace td

View File

@@ -7,107 +7,61 @@
#include <td/common/NonCopyable.h>
#include <td/protocol/packet/PacketData.h>
#include <td/protocol/packet/PacketDeclare.h>
#include <sp/protocol/ConcreteMessage.h>
#include <sp/protocol/MessageBase.h>
#include <sp/protocol/MessageDispatcher.h>
#include <sp/protocol/MessageFactory.h>
#include <sp/protocol/MessageHandler.h>
namespace td {
namespace protocol {
class PacketVisitor;
/** A Packet id is 8 bits wide */
using PacketID = std::uint8_t;
using PeerID = std::uint16_t;
#define DeclarePacket(PacketName, ...) /** PacketName */ PacketName,
/**
* \enum PacketType
* \brief Map a Packet to an id
*/
enum class PacketType : PacketID {
DeclareAllPacket()
/** The number of packets */
PACKET_COUNT
enum class PacketID : std::uint8_t {
ChatMessage = 0,
BeginGame,
Disconnect,
KeepAlive,
LockSteps,
LoggingSuccess,
PlayerJoin,
PlayerLeave,
PlayerLogin
};
class PacketHandler;
#undef DeclarePacket
class Packet : private NonCopyable {
public:
/**
* \return The real type of the packet
*/
virtual PacketType GetType() const = 0;
/**
* \brief The network peer who sent the packet
*/
PeerID m_Sender;
private:
/** Use a PacketVisitor to make double-dispatch possible */
virtual void Accept(PacketVisitor& a_Visitor) const = 0;
friend class PacketVisitor;
};
using PacketBase = sp::MessageBase<PacketID, PacketHandler>;
template <typename TData, PacketID ID>
using PacketMessage = sp::ConcreteMessage<TData, PacketID, ID, PacketHandler>;
namespace packets {
/**
* \class ConcretePacket
* \brief A Packet associated with an id and holding data
* \tparam PT The packet type
* \tparam Data The structure holding the data of the packet (in td::protocol::data namespace)
*/
template <PacketType PT, typename Data>
class ConcretePacket : public Packet {
public:
/** The type of the struct holding the data */
using PacketDataType = Data;
using BeginGamePacket = PacketMessage<pdata::BeginGame, PacketID::BeginGame>;
using ChatMessagePacket = PacketMessage<pdata::ChatMessage, PacketID::ChatMessage>;
using DisconnectPacket = PacketMessage<pdata::Disconnect, PacketID::Disconnect>;
using KeepAlivePacket = PacketMessage<pdata::KeepAlive, PacketID::KeepAlive>;
using LockStepsPacket = PacketMessage<pdata::LockSteps, PacketID::LockSteps>;
using LoggingSuccessPacket = PacketMessage<pdata::LoggingSuccess, PacketID::LoggingSuccess>;
using PlayerJoinPacket = PacketMessage<pdata::PlayerJoin, PacketID::PlayerJoin>;
using PlayerLeavePacket = PacketMessage<pdata::PlayerLeave, PacketID::PlayerLeave>;
using PlayerLoginPacket = PacketMessage<pdata::PlayerLogin, PacketID::PlayerLogin>;
/** The structure holding the actual data */
PacketDataType m_Data;
/** Construct the packet with data of type PacketDataType */
ConcretePacket(const PacketDataType& a_Data = {});
constexpr PacketType GetType() const override {
return PT;
};
private:
void Accept(PacketVisitor& a_Visitor) const override;
};
// define TD_INSTANCIATE_PACKETS
// before including this file
// if you want to instantiate templates
#ifdef TD_INSTANCIATE_PACKETS
#define DeclarePacket(PacketName, ...) \
using PacketName = ConcretePacket<PacketType::PacketName, pdata::PacketName>; \
template class ConcretePacket<PacketType::PacketName, pdata::PacketName>;
#else
#define DeclarePacket(PacketName, ...) /** Defines the PacketName packet */ \
using PacketName = ConcretePacket<PacketType::PacketName, pdata::PacketName>;
#endif
DeclareAllPacket()
#undef DeclarePacket
} // namespace packets
using AllPackets = std::tuple<packets::BeginGamePacket, packets::ChatMessagePacket, packets::DisconnectPacket,
packets::KeepAlivePacket, packets::LockStepsPacket, packets::LoggingSuccessPacket, packets::PlayerJoinPacket,
packets::PlayerLeavePacket, packets::PlayerLoginPacket>;
class PacketHandler : public sp::MessageHandler<AllPackets> {};
using PacketDispatcher = sp::MessageDispatcher<PacketBase>;
using PacketFactory = sp::MessageFactory<PacketBase, AllPackets>;
} // namespace protocol
} // namespace td