first commit
This commit is contained in:
76
include/sp/protocol/packet/PacketData.h
Normal file
76
include/sp/protocol/packet/PacketData.h
Normal file
@@ -0,0 +1,76 @@
|
||||
// #pragma once
|
||||
|
||||
// #include <td/Types.h>
|
||||
// #include <vector>
|
||||
// #include <td/protocol/command/Commands.h>
|
||||
|
||||
// // Make it dynamic ?
|
||||
// #define LOCKSTEP_BUFFER_SIZE 10
|
||||
|
||||
// namespace sp {
|
||||
// namespace protocol {
|
||||
|
||||
// struct PlayerInfo {
|
||||
// PlayerID m_PlayerId;
|
||||
// std::string m_PlayerName;
|
||||
// };
|
||||
|
||||
// struct MapData {
|
||||
|
||||
// };
|
||||
|
||||
// namespace pdata {
|
||||
|
||||
// /** Client attempts to login (very basic) */
|
||||
// struct PlayerLogin {
|
||||
// std::string m_PlayerName;
|
||||
// };
|
||||
|
||||
// /** Server indicates success */
|
||||
// struct LoggingSuccess {
|
||||
// PlayerID m_PlayerId;
|
||||
// };
|
||||
|
||||
// /** Player joins the lobby */
|
||||
// struct PlayerJoin {
|
||||
// PlayerInfo m_Player;
|
||||
// };
|
||||
|
||||
// /** Player leaves the lobby */
|
||||
// struct PlayerLeave {
|
||||
// PlayerID m_PlayerId;
|
||||
// };
|
||||
|
||||
// /** Keep alive */
|
||||
// struct KeepAlive {
|
||||
// std::uint64_t m_KeepAliveId;
|
||||
// };
|
||||
|
||||
// /** Can be used by both client and server */
|
||||
// struct Disconnect {
|
||||
// std::string m_Reason;
|
||||
// };
|
||||
|
||||
// /** Chat message */
|
||||
// struct ChatMessage {
|
||||
// std::string m_Text;
|
||||
// };
|
||||
|
||||
// // TODO: handle players joining in the first second
|
||||
|
||||
// struct BeginGame {
|
||||
// MapData m_Map;
|
||||
// std::vector<PlayerInfo> m_BlueTeam;
|
||||
// std::vector<PlayerInfo> m_RedTeam;
|
||||
// // optional, used for players joining mid game
|
||||
// std::vector<LockStep> m_FirstLocksteps;
|
||||
// };
|
||||
|
||||
// struct LockSteps {
|
||||
// std::uint16_t m_FirstFrameNumber;
|
||||
// std::array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
|
||||
// };
|
||||
|
||||
// } // namespace pdata
|
||||
// } // namespace protocol
|
||||
// } // namespace sp
|
||||
48
include/sp/protocol/packet/PacketDeclare.h
Normal file
48
include/sp/protocol/packet/PacketDeclare.h
Normal file
@@ -0,0 +1,48 @@
|
||||
// #pragma once
|
||||
|
||||
// /**
|
||||
// * \file PacketDeclare.h
|
||||
// * \brief Holds the definitions of the packets (but not their content)
|
||||
// */
|
||||
|
||||
// namespace sp {
|
||||
// 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 sp
|
||||
17
include/sp/protocol/packet/PacketDispatcher.h
Normal file
17
include/sp/protocol/packet/PacketDispatcher.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file PacketDispatcher.h
|
||||
* \brief File containing the sp::protocol::PacketDispatcher class
|
||||
*/
|
||||
|
||||
#include <sp/protocol/Dispatcher.h>
|
||||
#include <sp/protocol/packet/Packets.h>
|
||||
|
||||
namespace sp {
|
||||
namespace protocol {
|
||||
|
||||
using PacketDispatcher = Dispatcher<PacketType, PacketVisitor, Packet>;
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace sp
|
||||
19
include/sp/protocol/packet/PacketFactory.h
Normal file
19
include/sp/protocol/packet/PacketFactory.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <sp/protocol/packet/Packets.h>
|
||||
|
||||
namespace sp {
|
||||
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 sp
|
||||
21
include/sp/protocol/packet/PacketSerializer.h
Normal file
21
include/sp/protocol/packet/PacketSerializer.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <sp/common/DataBuffer.h>
|
||||
|
||||
namespace sp {
|
||||
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 sp
|
||||
39
include/sp/protocol/packet/PacketVisitor.h
Normal file
39
include/sp/protocol/packet/PacketVisitor.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file PacketVisitor.h
|
||||
* \brief File containing the sp::protocol::PacketVisitor class
|
||||
*/
|
||||
|
||||
#include <sp/protocol/packet/Packets.h>
|
||||
|
||||
namespace sp {
|
||||
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 sp
|
||||
115
include/sp/protocol/packet/Packets.h
Normal file
115
include/sp/protocol/packet/Packets.h
Normal file
@@ -0,0 +1,115 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file Packets.h
|
||||
* \brief File containing the definitions of the packets
|
||||
*/
|
||||
|
||||
#include <sp/common/NonCopyable.h>
|
||||
#include <sp/protocol/packet/PacketData.h>
|
||||
#include <sp/protocol/packet/PacketDeclare.h>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace sp {
|
||||
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
|
||||
};
|
||||
|
||||
|
||||
#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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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 sp::protocol::data namespace)
|
||||
*/
|
||||
template <PacketType PT, typename Data>
|
||||
class ConcretePacket : public Packet {
|
||||
public:
|
||||
/** The type of the struct holding the data */
|
||||
using PacketDataType = Data;
|
||||
|
||||
/** 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 SP_INSTANCIATE_PACKETS
|
||||
// before including this file
|
||||
// if you want to instantiate templates
|
||||
#ifdef SP_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
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace sp
|
||||
Reference in New Issue
Block a user