From 3eb3eadd4bc4976f1e34a1fad3b694f2e2ebb33f Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 20 Jul 2024 00:45:22 +0200 Subject: [PATCH] more doc --- include/blitz/network/EnetConnexion.h | 9 +++--- include/blitz/protocol/PacketDeclare.h | 41 ++++++++++++++++---------- include/blitz/protocol/PacketVisitor.h | 18 +++++++++-- include/blitz/protocol/Packets.h | 30 ++++++++++++++++--- src/blitz/network/EnetConnexion.cpp | 2 +- 5 files changed, 73 insertions(+), 27 deletions(-) diff --git a/include/blitz/network/EnetConnexion.h b/include/blitz/network/EnetConnexion.h index 9ad802d..e5d8704 100644 --- a/include/blitz/network/EnetConnexion.h +++ b/include/blitz/network/EnetConnexion.h @@ -19,8 +19,9 @@ class EnetServer; #define DeclarePacket(PacketName, ...) \ + /** Sends a PacketName over the network */ \ void Send##PacketName(const blitz::protocol::data::PacketName& a_##PacketName) const; \ - \ + /** Use On##PacketName.Connect() to process a PacketName incoming from network */ \ NazaraSignal(On##PacketName, const blitz::protocol::data::PacketName&); @@ -33,10 +34,10 @@ class EnetConnexion { bool IsConnected() const; - DeclareAllPacket() + DeclareAllPacket(); - private : - Nz::ENetPeer* m_Peer; + private: + Nz::ENetPeer* m_Peer; void Recieve(Nz::ByteArray&); void SetPeer(Nz::ENetPeer* a_Peer); diff --git a/include/blitz/protocol/PacketDeclare.h b/include/blitz/protocol/PacketDeclare.h index c48ee8d..bd5a9b8 100644 --- a/include/blitz/protocol/PacketDeclare.h +++ b/include/blitz/protocol/PacketDeclare.h @@ -1,8 +1,17 @@ #pragma once +/** + * \file PacketDeclare.h + * \brief Holds the definitions of the packets (but not their content) + */ + namespace blitz { namespace protocol { +/** + * \enum PacketSender + * \brief Indicate who should send a packet + */ enum class PacketSender { /** Sent by clients and server */ Both, @@ -18,22 +27,22 @@ enum class PacketSender { * \brief Avoids repetitive operations on packets */ #define DeclareAllPacket() \ - DeclarePacket(ChatMessage, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Both) \ - DeclarePacket(Disconnect, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Both) \ - DeclarePacket(KeepAlive, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Both) \ - DeclarePacket(LoggingSuccess, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Server) \ - DeclarePacket(PlayerDeath, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Server) \ - DeclarePacket(PlayerJoin, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Server) \ - DeclarePacket(PlayerLeave, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Server) \ - DeclarePacket(PlayerList, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Server) \ - DeclarePacket(PlayerLogin, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Client) \ - DeclarePacket(PlayerPositionAndRotation, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Both) \ - DeclarePacket(PlayerShoot, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Both) \ - DeclarePacket(PlayerStats, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Server) \ - DeclarePacket(ServerConfig, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Server) \ - DeclarePacket(ServerTps, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Server) \ - DeclarePacket(UpdateGameState, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Server) \ - DeclarePacket(UpdateHealth, Nz::ENetPacketFlag::Reliable, blitz::protocol::PacketSender::Client) + 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, Reliable, 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 diff --git a/include/blitz/protocol/PacketVisitor.h b/include/blitz/protocol/PacketVisitor.h index cb37bf1..036e589 100644 --- a/include/blitz/protocol/PacketVisitor.h +++ b/include/blitz/protocol/PacketVisitor.h @@ -1,20 +1,34 @@ #pragma once -#include +/** + * \file PacketVisitor.h + * \brief File containing the blitz::protocol::PacketVisitor class + */ + #include +#include namespace blitz { namespace protocol { -#define DeclarePacket(PacketName, ...) \ +#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 { 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() diff --git a/include/blitz/protocol/Packets.h b/include/blitz/protocol/Packets.h index a9c8d99..96d76d3 100644 --- a/include/blitz/protocol/Packets.h +++ b/include/blitz/protocol/Packets.h @@ -1,5 +1,10 @@ #pragma once +/** + * \file Packets.h + * \brief File containing the definitions of the packets + */ + #include #include #include @@ -9,14 +14,20 @@ namespace protocol { class PacketVisitor; +/** A Packet id is 8 bits wide */ using PacketID = std::uint8_t; -#define DeclarePacket(PacketName, ...) PacketName, +#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 }; @@ -26,9 +37,16 @@ enum class PacketType : PacketID { class Packet { public: + /** + * \return The real type of the packet + */ virtual PacketType GetType() const = 0; + private: + /** Use a PacketVisitor to make double-dispatch possible */ virtual void Accept(PacketVisitor& a_Visitor) const = 0; + + friend class PacketVisitor; }; @@ -38,16 +56,21 @@ class Packet { 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 blitz::protocol::data namespace) */ template 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 { @@ -56,8 +79,6 @@ class ConcretePacket : public Packet { private: void Accept(PacketVisitor& a_Visitor) const override; - - friend class PacketVisitor; }; @@ -72,7 +93,8 @@ class ConcretePacket : public Packet { using PacketName = ConcretePacket; \ template class ConcretePacket; #else -#define DeclarePacket(PacketName, ...) using PacketName = ConcretePacket; +#define DeclarePacket(PacketName, ...) /** Defines the PacketName packet */ \ + using PacketName = ConcretePacket; #endif DeclareAllPacket() diff --git a/src/blitz/network/EnetConnexion.cpp b/src/blitz/network/EnetConnexion.cpp index 496d6fa..edcc326 100644 --- a/src/blitz/network/EnetConnexion.cpp +++ b/src/blitz/network/EnetConnexion.cpp @@ -55,7 +55,7 @@ void EnetConnexion::Recieve(Nz::ByteArray& a_Data) { #define DeclarePacket(Name, NFlag, ...) \ void EnetConnexion::Send##Name(const blitz::protocol::data::Name& a_##Name) const { \ - m_Peer->Send(0, NFlag, protocol::PacketSerializer::Serialize(protocol::packets::Name(a_##Name))); \ + m_Peer->Send(0, Nz::ENetPacketFlag::NFlag, protocol::PacketSerializer::Serialize(protocol::packets::Name(a_##Name))); \ } DeclareAllPacket()