FastEngine 0.9.5
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_netCommand.hpp
1/*
2 * Copyright 2026 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_NETCOMMAND_HPP_INCLUDED
18#define _FGE_C_NETCOMMAND_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/network/C_protocol.hpp"
22#include <condition_variable>
23#include <deque>
24#include <future>
25#include <memory>
26#include <thread>
27
28#define FGE_NET_CMD_UPDATE_TICK_MS \
29 std::chrono::milliseconds \
30 { \
31 30 \
32 }
33
34#define FGE_NET_MTU_TIMEOUT_MS \
35 std::chrono::milliseconds \
36 { \
37 400 \
38 }
39#define FGE_NET_MTU_TRY_COUNT 12
40#define FGE_NET_MTU_MIN_INTERVAL 16
41
42#define FGE_NET_CONNECT_TIMEOUT_MS \
43 std::chrono::milliseconds \
44 { \
45 1000 \
46 }
47
48#define FGE_NET_DISCONNECT_TIMEOUT_MS \
49 std::chrono::milliseconds \
50 { \
51 1000 \
52 }
53
54#define FGE_NET_COMMAND_TIMEOUT_MS \
55 std::chrono::milliseconds \
56 { \
57 500 \
58 }
59
60#define FGE_NET_MAX_VERSIONING_STRING_SIZE 32
61
62namespace fge::net
63{
64
65enum class NetCommandTypes
66{
67 DISCOVER_MTU,
68 CONNECT,
69 DISCONNECT,
70 CONNECT_HANDLER
71};
72enum class NetCommandResults
73{
74 SUCCESS,
75 WORKING,
76 FAILURE
77};
78
79class NetCommand;
80using CommandQueue = std::deque<std::unique_ptr<NetCommand>>;
81
82class NetCommand
83{
84public:
85 inline explicit NetCommand(CommandQueue* commandQueue) :
86 _g_commandQueue(commandQueue)
87 {}
88 virtual ~NetCommand() = default;
89
90 [[nodiscard]] virtual NetCommandTypes getType() const = 0;
91 [[nodiscard]] NetCommandResults update(TransmitPacketPtr& buffPacket,
92 IpAddress::Types addressType,
93 Client& client,
94 std::chrono::milliseconds deltaTime);
95 virtual void onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) = 0;
96
97 [[nodiscard]] virtual std::chrono::milliseconds getTimeoutTarget() const;
98
99protected:
100 virtual void internalUpdate(TransmitPacketPtr& buffPacket,
101 IpAddress::Types addressType,
102 Client& client,
103 std::chrono::milliseconds deltaTime) = 0;
104 [[nodiscard]] virtual NetCommandResults timeout(Client& client);
105 void resetTimeout();
106
107 void markAsFailed();
108 void markAsSucceeded();
109
110 CommandQueue* _g_commandQueue{nullptr};
111
112private:
113 std::chrono::milliseconds g_timeout{0};
114 NetCommandResults g_currentResultState{NetCommandResults::WORKING};
115};
116
117class FGE_API NetMTUCommand final : public NetCommand
118{
119public:
120 using NetCommand::NetCommand;
121 ~NetMTUCommand() override = default;
122
123 [[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::DISCOVER_MTU; }
124
125 void internalUpdate(TransmitPacketPtr& buffPacket,
126 IpAddress::Types addressType,
127 Client& client,
128 std::chrono::milliseconds deltaTime) override;
129 void onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;
130
131 [[nodiscard]] inline std::future<uint16_t> get_future() { return this->g_promise.get_future(); }
132
133 [[nodiscard]] inline std::chrono::milliseconds getTimeoutTarget() const override { return FGE_NET_MTU_TIMEOUT_MS; }
134
135private:
136 [[nodiscard]] NetCommandResults timeout(Client& client) override;
137
138 std::promise<uint16_t> g_promise;
139 uint16_t g_currentMTU{0};
140 uint16_t g_targetMTU{0};
141 uint16_t g_maximumMTU{0};
142 uint16_t g_intervalMTU{0};
143 std::size_t g_tryCount{0};
144 enum class States
145 {
146 ASKING,
147 WAITING_RESPONSE,
148
149 DISCOVER,
150 WAITING
151 } g_state{States::ASKING};
152};
153
154class FGE_API NetConnectCommand final : public NetCommand
155{
156public:
157 using NetCommand::NetCommand;
158 ~NetConnectCommand() override = default;
159
160 void setVersioningString(std::string_view versioningString);
161 [[nodiscard]] std::string const& getVersioningString() const;
162
163 [[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::CONNECT; }
164
165 void internalUpdate(TransmitPacketPtr& buffPacket,
166 IpAddress::Types addressType,
167 Client& client,
168 std::chrono::milliseconds deltaTime) override;
169 void onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;
170
171 [[nodiscard]] inline std::future<bool> get_future() { return this->g_promise.get_future(); }
172
173 [[nodiscard]] inline std::chrono::milliseconds getTimeoutTarget() const override
174 {
175 return FGE_NET_CONNECT_TIMEOUT_MS;
176 }
177
178private:
179 [[nodiscard]] NetCommandResults timeout(Client& client) override;
180
181 std::promise<bool> g_promise;
182 enum class States
183 {
184 TRANSMIT_FGE_HANDSHAKE,
185 WAITING_FGE_HANDSHAKE,
186
187 DEALING_WITH_MTU,
188 WAITING_SERVER_FINAL_MTU,
189
190 CRYPT_HANDSHAKE,
191 CRYPT_WAITING,
192
193 CONNECTED
194 } g_state{States::TRANSMIT_FGE_HANDSHAKE};
195 bool g_mtuTested{false};
196 std::future<uint16_t> g_mtuFuture;
197 std::string g_versioningString;
198};
199
200class FGE_API NetConnectHandlerCommand final : public NetCommand
201{
202public:
203 using NetCommand::NetCommand;
204 ~NetConnectHandlerCommand() override = default;
205
206 [[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::CONNECT_HANDLER; }
207
208 void internalUpdate(TransmitPacketPtr& buffPacket,
209 IpAddress::Types addressType,
210 Client& client,
211 std::chrono::milliseconds deltaTime) override;
212 void onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;
213
214 [[nodiscard]] inline std::future<bool> get_future() { return this->g_promise.get_future(); }
215
216 [[nodiscard]] inline std::chrono::milliseconds getTimeoutTarget() const override
217 {
218 return FGE_NET_CONNECT_TIMEOUT_MS;
219 }
220
221private:
222 [[nodiscard]] NetCommandResults timeout(Client& client) override;
223
224 std::promise<bool> g_promise;
225 enum class States
226 {
227 LOOKUP_MTU,
228
229 DEALING_WITH_MTU,
230 WAITING_CLIENT_FINAL_MTU,
231
232 CRYPT_HANDSHAKE,
233 CRYPT_WAITING,
234
235 CONNECTED
236 } g_state{States::LOOKUP_MTU};
237
238 std::future<uint16_t> g_mtuFuture;
239 NetMTUCommand g_mtuCommand{this->_g_commandQueue};
240};
241
242class FGE_API NetDisconnectCommand final : public NetCommand
243{
244public:
245 using NetCommand::NetCommand;
246 ~NetDisconnectCommand() override = default;
247
248 [[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::DISCONNECT; }
249
250 void internalUpdate(TransmitPacketPtr& buffPacket,
251 IpAddress::Types addressType,
252 Client& client,
253 std::chrono::milliseconds deltaTime) override;
254 void onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;
255
256 [[nodiscard]] inline std::future<void> get_future() { return this->g_promise.get_future(); }
257
258 [[nodiscard]] inline std::chrono::milliseconds getTimeoutTarget() const override
259 {
260 return FGE_NET_DISCONNECT_TIMEOUT_MS;
261 }
262
263private:
264 [[nodiscard]] NetCommandResults timeout(Client& client) override;
265
266 std::promise<void> g_promise;
267 bool g_transmitted{false};
268};
269
270} // namespace fge::net
271
272#endif // _FGE_C_NETCOMMAND_HPP_INCLUDED
Class that represent the identity of a client.
Definition C_client.hpp:239
Definition C_netCommand.hpp:83
Definition C_netCommand.hpp:155
Definition C_netCommand.hpp:201
Definition C_netCommand.hpp:243
Definition C_netCommand.hpp:118