1616namespace 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
0 commit comments