gros push encore
This commit is contained in:
76
include/td/protocol/packet/PacketData.h
Normal file
76
include/td/protocol/packet/PacketData.h
Normal file
@@ -0,0 +1,76 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/Types.h>
|
||||
#include <vector>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
struct PlayerInfo {
|
||||
PlayerID m_PlayerId;
|
||||
std::string m_PlayerName;
|
||||
};
|
||||
|
||||
namespace pdata {
|
||||
|
||||
struct PlayerLogin {
|
||||
std::string m_PlayerName;
|
||||
};
|
||||
|
||||
struct UpdateHealth {
|
||||
float m_NewHealth;
|
||||
};
|
||||
|
||||
struct LoggingSuccess {
|
||||
PlayerID m_PlayerId;
|
||||
};
|
||||
|
||||
struct PlayerDeath {};
|
||||
|
||||
struct PlayerJoin {
|
||||
PlayerInfo m_Player;
|
||||
};
|
||||
|
||||
struct PlayerLeave {
|
||||
PlayerID m_PlayerId;
|
||||
};
|
||||
|
||||
struct PlayerStats {};
|
||||
|
||||
struct PlayerList {
|
||||
std::vector<PlayerInfo> m_Players;
|
||||
};
|
||||
|
||||
struct ServerConfig {};
|
||||
|
||||
struct ServerTps {};
|
||||
|
||||
struct UpdateGameState {};
|
||||
|
||||
struct KeepAlive {
|
||||
std::uint64_t m_KeepAliveId;
|
||||
};
|
||||
|
||||
struct Disconnect {};
|
||||
|
||||
struct ChatMessage {
|
||||
std::string m_Text;
|
||||
};
|
||||
|
||||
struct PlayerPositionAndRotation {
|
||||
PlayerID m_Player;
|
||||
// godot::Vector3 m_Position;
|
||||
// godot::Vector3 m_Rotation;
|
||||
// godot::Vector3 m_Velocity;
|
||||
};
|
||||
|
||||
struct PlayerShoot {
|
||||
PlayerID m_Sender;
|
||||
// godot::Vector3 m_Position;
|
||||
// godot::Vector3 m_Rotation;
|
||||
// godot::Vector3 m_Velocity;
|
||||
};
|
||||
|
||||
} // namespace pdata
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
55
include/td/protocol/packet/PacketDeclare.h
Normal file
55
include/td/protocol/packet/PacketDeclare.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#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(Disconnect, Reliable, Both) \
|
||||
DeclarePacket(KeepAlive, Reliable, Both) \
|
||||
DeclarePacket(LoggingSuccess, Reliable, Server) \
|
||||
DeclarePacket(PlayerDeath, Reliable, Server) \
|
||||
DeclarePacket(PlayerJoin, Reliable, Server) \
|
||||
DeclarePacket(PlayerLeave, Reliable, Server) \
|
||||
DeclarePacket(PlayerList, Reliable, Server) \
|
||||
DeclarePacket(PlayerLogin, Reliable, Client) \
|
||||
DeclarePacket(PlayerPositionAndRotation, Unreliable, Both) \
|
||||
DeclarePacket(PlayerShoot, Reliable, Both) \
|
||||
DeclarePacket(PlayerStats, Reliable, Server) \
|
||||
DeclarePacket(ServerConfig, Reliable, Server) \
|
||||
DeclarePacket(ServerTps, Reliable, Server) \
|
||||
DeclarePacket(UpdateGameState, Reliable, Server) \
|
||||
DeclarePacket(UpdateHealth, Reliable, Client)
|
||||
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
18
include/td/protocol/packet/PacketDispatcher.h
Normal file
18
include/td/protocol/packet/PacketDispatcher.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file PacketDispatcher.h
|
||||
* \brief File containing the td::protocol::PacketDispatcher class
|
||||
*/
|
||||
|
||||
#include <td/protocol/Dispatcher.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
class PacketHandler;
|
||||
|
||||
using PacketDispatcher = Dispatcher<PacketType, PacketHandler, Packet>;
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
19
include/td/protocol/packet/PacketFactory.h
Normal file
19
include/td/protocol/packet/PacketFactory.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#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
|
||||
33
include/td/protocol/packet/PacketHandler.h
Normal file
33
include/td/protocol/packet/PacketHandler.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file PacketHandler.h
|
||||
* \brief File containing the td::protocol::PacketHandler class
|
||||
*/
|
||||
|
||||
#include <td/protocol/packet/PacketVisitor.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
#define DeclarePacket(PacketName, ...) \
|
||||
virtual void Visit(const packets::PacketName&); \
|
||||
virtual void HandlePacket(const packets::PacketName&) {}
|
||||
|
||||
/**
|
||||
* \class PacketHandler
|
||||
* \brief Class used to handle packets
|
||||
*/
|
||||
class PacketHandler : public PacketVisitor {
|
||||
public:
|
||||
PacketHandler() {}
|
||||
~PacketHandler() {}
|
||||
|
||||
DeclareAllPacket()
|
||||
};
|
||||
|
||||
#undef DeclarePacket
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
59
include/td/protocol/packet/PacketSender.h
Normal file
59
include/td/protocol/packet/PacketSender.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/protocol/packet/PacketVisitor.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
class NetworkInterface;
|
||||
|
||||
namespace protocol {
|
||||
|
||||
#define DeclarePacket(PacketName, Reliability, ...) void Visit(const protocol::packets::PacketName& a_Packet) override;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
///////////////////////
|
||||
/* PacketBroadcaster */
|
||||
///////////////////////
|
||||
class PacketBroadcaster : public protocol::PacketVisitor {
|
||||
private:
|
||||
NetworkInterface& m_NetworkInterface;
|
||||
|
||||
public:
|
||||
PacketBroadcaster(NetworkInterface& a_NetworkInterface) : m_NetworkInterface(a_NetworkInterface) {}
|
||||
|
||||
void BroadcastPacket(const protocol::Packet& a_Packet) {
|
||||
Check(a_Packet);
|
||||
}
|
||||
|
||||
DeclareAllPacket()
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//////////////////
|
||||
/* PacketSender */
|
||||
//////////////////
|
||||
class PacketSender : public protocol::PacketVisitor {
|
||||
private:
|
||||
NetworkInterface& m_NetworkInterface;
|
||||
PeerID m_PeerId;
|
||||
|
||||
public:
|
||||
PacketSender(PeerID a_PeerId, NetworkInterface& a_NetworkInterface) : m_PeerId(a_PeerId), m_NetworkInterface(a_NetworkInterface) {}
|
||||
|
||||
void SendPacket(const protocol::Packet& a_Packet) {
|
||||
Check(a_Packet);
|
||||
}
|
||||
|
||||
DeclareAllPacket()
|
||||
};
|
||||
|
||||
#undef DeclarePacket
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
21
include/td/protocol/packet/PacketSerializer.h
Normal file
21
include/td/protocol/packet/PacketSerializer.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#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
|
||||
39
include/td/protocol/packet/PacketVisitor.h
Normal file
39
include/td/protocol/packet/PacketVisitor.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#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
|
||||
113
include/td/protocol/packet/Packets.h
Normal file
113
include/td/protocol/packet/Packets.h
Normal file
@@ -0,0 +1,113 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file Packets.h
|
||||
* \brief File containing the definitions of the packets
|
||||
*/
|
||||
|
||||
#include <td/common/NonCopyable.h>
|
||||
#include <td/protocol/packet/PacketData.h>
|
||||
#include <td/protocol/packet/PacketDeclare.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
|
||||
};
|
||||
|
||||
|
||||
#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 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;
|
||||
|
||||
/** 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
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user