Skip to content

Commit f0ea05c

Browse files
author
Alexandre jublot
committed
docs: implemented entire library headers docs (except exceptions)
1 parent ba23dc5 commit f0ea05c

File tree

10 files changed

+246
-5
lines changed

10 files changed

+246
-5
lines changed

include/Polymorph/Network/SerializerTrait.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ namespace polymorph::network
2828
struct SerializerTrait<T, true>
2929
{
3030

31+
/**
32+
* @brief Serialize an standard type to byte array
33+
* @param data standard type
34+
* @return Vector of byte containing the byte representation of the object
35+
*/
3136
static std::vector<std::byte> serialize(const T &data)
3237
{
3338
std::vector<std::byte> buffer(sizeof(T));
@@ -36,6 +41,11 @@ namespace polymorph::network
3641
return buffer;
3742
}
3843

44+
/**
45+
* @brief Deserialize a standard type to byte array
46+
* @param data Vector of byte containing a representation of
47+
* @return Vector of byte containing the byte representation of the object
48+
*/
3949
static T deserialize(const std::vector<std::byte> &buffer)
4050
{
4151
T dto;

include/Polymorph/Network/dto/ACKDto.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@ namespace polymorph::network
1515
#ifdef _WIN32
1616
#pragma pack(push, 1)
1717
#endif
18+
/**
19+
* @struct Data transfer object for the ACK packet
20+
*/
1821
struct ACKDto
1922
{
23+
/**
24+
* @property The packet operation code
25+
*/
2026
static constexpr OpId opId = 1;
27+
28+
/**
29+
* @property The packet identifier
30+
*/
2131
PacketId id;
2232
}
2333
#ifdef _WIN32

include/Polymorph/Network/dto/ConnectionDto.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,24 @@ namespace polymorph::network
1515
#ifdef _WIN32
1616
#pragma pack(push, 1)
1717
#endif
18+
/**
19+
* @struct Data transfer object for the incoming connection packet
20+
*/
1821
struct ConnectionDto
1922
{
23+
/**
24+
* @property The packet operation code
25+
*/
2026
static constexpr OpId opId = 0;
27+
28+
/**
29+
* @property The session identifier, used to identify the remote connection
30+
*/
2131
SessionId sessionId;
32+
33+
/**
34+
* @property The authentication token, used as proof of identity
35+
*/
2236
AuthorizationKey authKey;
2337
}
2438
#ifdef _WIN32

include/Polymorph/Network/dto/ConnectionResponseDto.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,22 @@ namespace polymorph::network
1515
#ifdef _WIN32
1616
#pragma pack(push, 1)
1717
#endif
18+
/**
19+
* @struct Data transfer object for responses to the incoming connection packet
20+
*/
1821
struct ConnectionResponseDto
1922
{
23+
/**
24+
* @property The packet operation code
25+
*/
2026
static constexpr OpId opId = 0;
27+
28+
/**
29+
* @property The authentication status
30+
*
31+
* @return true if the connection was accepted and authenticated
32+
* @return false if the connection was refused or the authentication failed
33+
*/
2134
bool authorized;
2235
}
2336
#ifdef _WIN32

include/Polymorph/Network/tcp/APacketHandler.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
namespace polymorph::network::tcp
2020
{
2121

22+
/**
23+
* @class Base class for packet handlers
24+
* @note TCP exclusive
25+
*
26+
* @inherit IPacketHandler The interface for packet handlers
27+
*/
2228
class APacketHandler : public IPacketHandler {
2329

2430
////////////////////// CONSTRUCTORS/DESTRUCTORS /////////////////////////
@@ -38,11 +44,23 @@ namespace polymorph::network::tcp
3844

3945

4046
private:
47+
/**
48+
* @property Map of the packet handlers, indexed by their operation code
49+
*
50+
* @return std::map<OpId, std::function<void(const Packet&)>> the map of the packet handlers
51+
* @brief The packet handlers are functions that will be called when a packet with the corresponding operation code is received
52+
*/
4153
std::map<polymorph::network::OpId,
4254
std::vector<std::function<bool(const PacketHeader &header, const std::vector<std::byte> &bytes)>>> _receiveCallbacks;
55+
/**
56+
* @property Thread that will handle the network operations (asio context)
57+
*/
4358
std::thread _thread;
4459

4560
protected:
61+
/**
62+
* @property The asio context for the PacketHandler
63+
*/
4664
asio::io_context _context;
4765

4866

@@ -54,6 +72,12 @@ namespace polymorph::network::tcp
5472
public:
5573
bool packetReceived(const PacketHeader &, const std::vector<std::byte> &bytes) final;
5674

75+
/**
76+
* @brief Register the receive handler for the given operation code
77+
*
78+
* @param opId The operation code that will be associated with the receive handler function
79+
* @param handler The receive handler function
80+
*/
5781
template<typename T>
5882
void registerReceiveHandler(polymorph::network::OpId opId, std::function<bool(const PacketHeader &, const T &)> handler)
5983
{
@@ -62,9 +86,18 @@ namespace polymorph::network::tcp
6286
return handler(header, packet.payload);
6387
});
6488
}
89+
90+
/**
91+
* @brief Unregister all the receive handlers for the given operation code
92+
*
93+
* @param opId The operation code that will be unregistered from the receive handlers
94+
*/
6595
void unregisterReceiveHandlers(polymorph::network::OpId opId);
6696

6797
protected:
98+
/**
99+
* @brief Run the asio context in a thread
100+
*/
68101
virtual void _run() final;
69102

70103

include/Polymorph/Network/tcp/Client.hpp

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@
1616
namespace polymorph::network::tcp
1717
{
1818

19+
/**
20+
* @class TCP Client class
21+
* @note TCP exclusive
22+
*/
1923
class Client : public APacketHandler
2024
{
2125

2226
////////////////////// CONSTRUCTORS/DESTRUCTORS /////////////////////////
2327

2428
public:
29+
/**
30+
* @brief Constructor of the TCP Client class
31+
*
32+
* @param host The server host ip address
33+
* @param port The server port
34+
*/
2535
Client(std::string host, std::uint16_t port);
2636

2737
~Client() override;
@@ -36,29 +46,77 @@ namespace polymorph::network::tcp
3646

3747

3848
private:
49+
/**
50+
* @property Client socket
51+
*/
3952
asio::ip::tcp::socket _socket;
40-
asio::ip::tcp::endpoint _serverEndpoint;
4153

54+
/**
55+
* @property Remote server endpoint
56+
*/
57+
asio::ip::tcp::endpoint _serverEndpoint;
58+
59+
/**
60+
* @property The current packet being received
61+
* @brief Is used in the header of every packet to communicate the id of the packet
62+
*/
4263
PacketId _currentPacketId = 0;
64+
65+
/**
66+
* @property The current session id
67+
* @brief Is used in the header of every packet sent to communicate the session id
68+
*/
4369
SessionId _currentSession = 0;
70+
71+
/**
72+
* @property Atomic boolean to know if the client is connected
73+
*/
4474
std::atomic<bool> _isConnected = false;
75+
76+
/**
77+
* @property Atomic boolean to know if the client is connecting
78+
*/
4579
std::atomic<bool> _isConnecting = true;
4680

81+
/**
82+
* @property Queue of packets to send
83+
*/
4784
std::queue<std::pair<std::vector<std::byte>, std::function<void (const PacketHeader &, const std::vector<std::byte> &)>>> _sendQueue;
85+
86+
/**
87+
* @property Atomic boolean to know if the client is already sending a packet
88+
*/
4889
std::atomic<bool> _writeInProgress;
90+
91+
/**
92+
* @property Mutex to lock the send queue and ensure thread safety
93+
*/
4994
std::mutex _sendQueueMutex;
5095

96+
/**
97+
* @property Buffer to receive packets
98+
*/
5199
std::vector<std::byte> _receiveBuffer;
100+
101+
/**
102+
* @property Internal buffer to receive packets
103+
*/
52104
std::array<std::byte, 1024> _internalBuffer;
53105

54106

55107

56108
//////////////////////--------------------------/////////////////////////
57109

58-
59-
60110
/////////////////////////////// METHODS /////////////////////////////////
61111
public:
112+
/**
113+
* @brief Send a payload to the server
114+
*
115+
* @tparam T The type of the payload to pack
116+
* @param opId The operation id of the Packet
117+
* @param payload The actual Packet data
118+
* @param callback Callback function used when the data has been (or not) delivered
119+
*/
62120
template<typename T>
63121
void send(OpId opId, T &payload, std::function<void(const PacketHeader &, const T &)> callback = nullptr)
64122
{
@@ -86,16 +144,33 @@ namespace polymorph::network::tcp
86144
_doSend();
87145
}
88146
}
147+
148+
/**
149+
* @brief Connect to server and execute a callback when done
150+
* @param callback Callback executed when the connection is done (or not)
151+
* @note bool of std::function correspond to the auth status (true = success, false otherwise)
152+
*/
89153
void connect(std::function<void(bool, SessionId)> callback);
90154

155+
/**
156+
* @brief Connect from a SessionId
157+
* @note This method is used to communicate with the server with a authenticated session
158+
*/
91159
void connectWithSession(SessionId session, AuthorizationKey authKey, std::function<void(bool, SessionId)> callback);
92160

93161

94162
private:
163+
/**
164+
* @brief Method that use the queue to send packets
165+
*/
95166
void _doSend();
96167

168+
/**
169+
* @brief Method to handle the reception of a packet, deserialize it and call the callback
170+
*/
97171
void _doReceive();
98172

173+
// TODO: remove ?
99174
bool _broadcastReceivedPacket(const PacketHeader &header, const std::vector<std::byte> &bytes);
100175

101176

include/Polymorph/Network/tcp/ConnectionPool.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
namespace polymorph::network::tcp
1717
{
1818

19+
/**
20+
* @class Base class for connection pools
21+
* @note TCP exclusive
22+
*
23+
* @inherit IConnectionPool The interface for connection pools
24+
*/
1925
class ConnectionPool : public IConnectionPool
2026
{
2127

@@ -36,7 +42,17 @@ namespace polymorph::network::tcp
3642

3743

3844
private:
45+
/**
46+
* @property The set of the connections
47+
* @brief The connections are stored in a set to avoid duplicates and fast access
48+
*
49+
* @return std::set<Connection> the set of the connections
50+
*/
3951
std::set<std::shared_ptr<IClientConnection>> _connections;
52+
53+
/**
54+
* @property The mutex for the connections
55+
*/
4056
std::mutex _connectionsMutex;
4157

4258

@@ -50,8 +66,18 @@ namespace polymorph::network::tcp
5066

5167
void leave(std::shared_ptr<IClientConnection> connection) override;
5268

69+
/**
70+
* @brief Get the connections
71+
*
72+
* @return std::vector<Connection> The connections stored in the pool
73+
*/
5374
std::vector<std::shared_ptr<IClientConnection>> getConnections();
5475

76+
/**
77+
* @brief Get the connections
78+
*
79+
* @return std::vector<Connection> The connections stored in the pool
80+
*/
5581
std::shared_ptr<IClientConnection> getConnectionBySessionId(SessionId id);
5682

5783

include/Polymorph/Network/tcp/IConnectionPool.hpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,32 @@
1010
#include <memory>
1111
#include "Polymorph/Network/udp/IClientConnection.hpp"
1212

13-
namespace polymorph::network::tcp {
13+
namespace polymorph::network::tcp
14+
{
15+
1416
class ClientConnection;
1517

18+
/**
19+
* @class Interface for connection pools
20+
* @note TCP exclusive
21+
*/
1622
class IConnectionPool {
1723

1824
public:
1925
virtual ~IConnectionPool() = default;
2026

27+
/**
28+
* @brief Add a connection to the pool
29+
*
30+
* @param connection The connection to add
31+
*/
2132
virtual void join(std::shared_ptr<IClientConnection> connection) = 0;
2233

34+
/**
35+
* @brief Remove a connection from the pool
36+
*
37+
* @param connection The connection to remove
38+
*/
2339
virtual void leave(std::shared_ptr<IClientConnection> connection) = 0;
2440

2541
};

0 commit comments

Comments
 (0)