gros push encore
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <td/Types.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
#define LOCKSTEP_BUFFER_SIZE 10
|
||||
|
||||
struct LockStepCommand {};
|
||||
|
||||
struct LockStep {
|
||||
std::uint8_t m_CommandNumber;
|
||||
@@ -13,11 +17,11 @@ struct LockStep {
|
||||
|
||||
struct LockSteps {
|
||||
std::uint16_t m_FirstFrameNumber;
|
||||
std::array<LockStep, 10> m_LockSteps;
|
||||
std::array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
|
||||
};
|
||||
|
||||
|
||||
namespace data {
|
||||
namespace cdata {
|
||||
|
||||
|
||||
struct PlaceTower {
|
||||
@@ -49,9 +53,12 @@ struct TeamChange {
|
||||
Team m_NewTeam : 1;
|
||||
};
|
||||
|
||||
struct PlayerJoin {
|
||||
PlayerID m_ID;
|
||||
std::string m_Name;
|
||||
};
|
||||
|
||||
|
||||
} // namespace data
|
||||
} // namespace cdata
|
||||
|
||||
|
||||
} // namespace protocol
|
||||
|
||||
21
include/td/protocol/command/CommandDeclare.h
Normal file
21
include/td/protocol/command/CommandDeclare.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
/**
|
||||
* \def DeclareAllPacket
|
||||
* \brief Avoids repetitive operations on packets
|
||||
*/
|
||||
#define DeclareAllCommand() \
|
||||
DeclareCommand(PlaceTower) \
|
||||
DeclareCommand(UpgradeTower) \
|
||||
DeclareCommand(SpawnTroop) \
|
||||
DeclareCommand(UseItem) \
|
||||
DeclareCommand(TeamChange) \
|
||||
DeclareCommand(PlayerJoin)
|
||||
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
18
include/td/protocol/command/CommandDispatcher.h
Normal file
18
include/td/protocol/command/CommandDispatcher.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file CommandDispatcher.h
|
||||
* \brief File containing the td::protocol::CommandDispatcher class
|
||||
*/
|
||||
|
||||
#include <td/protocol/Dispatcher.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
class CommandHandler;
|
||||
|
||||
using CommandDispatcher = Dispatcher<CommandType, CommandHandler, Command>;
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
19
include/td/protocol/command/CommandFactory.h
Normal file
19
include/td/protocol/command/CommandFactory.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#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::unique_ptr<CommandDerived> CreateCommand() {
|
||||
return std::make_unique<CommandDerived>();
|
||||
}
|
||||
|
||||
const std::unique_ptr<Command>& CreateReadOnlyCommand(CommandType a_Type);
|
||||
|
||||
} // namespace CommandFactory
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
33
include/td/protocol/command/CommandHandler.h
Normal file
33
include/td/protocol/command/CommandHandler.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file CommandHandler.h
|
||||
* \brief File containing the td::protocol::CommandHandler class
|
||||
*/
|
||||
|
||||
#include <td/protocol/command/CommandVisitor.h>
|
||||
#include <td/protocol/command/Commands.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
#define DeclareCommand(CommandName, ...) \
|
||||
virtual void Visit(const commands::CommandName&); \
|
||||
virtual void HandleCommand(const commands::CommandName&) {}
|
||||
|
||||
/**
|
||||
* \class CommandHandler
|
||||
* \brief Class used to handle packets
|
||||
*/
|
||||
class CommandHandler : public CommandVisitor {
|
||||
public:
|
||||
CommandHandler() {}
|
||||
~CommandHandler() {}
|
||||
|
||||
DeclareAllCommand()
|
||||
};
|
||||
|
||||
#undef DeclareCommand
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
21
include/td/protocol/command/CommandSerializer.h
Normal file
21
include/td/protocol/command/CommandSerializer.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <td/common/DataBuffer.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
class Command;
|
||||
|
||||
using CommandPtr = std::unique_ptr<Command>;
|
||||
|
||||
namespace CommandSerializer {
|
||||
|
||||
DataBuffer Serialize(const Command& a_Command);
|
||||
|
||||
std::unique_ptr<Command> Deserialize(DataBuffer& a_Data);
|
||||
|
||||
} // namespace CommandSerializer
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
39
include/td/protocol/command/CommandVisitor.h
Normal file
39
include/td/protocol/command/CommandVisitor.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#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
|
||||
108
include/td/protocol/command/Commands.h
Normal file
108
include/td/protocol/command/Commands.h
Normal file
@@ -0,0 +1,108 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file Commands.h
|
||||
* \brief File containing the definitions of the lockstep commands
|
||||
*/
|
||||
|
||||
#include <td/Types.h>
|
||||
#include <td/common/NonCopyable.h>
|
||||
#include <td/protocol/command/CommandData.h>
|
||||
#include <td/protocol/command/CommandDeclare.h>
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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
|
||||
|
||||
} // namespace commands
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user