splib transition
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user