FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_netCommand.hpp
1/*
2 * Copyright 2025 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};
71enum class NetCommandResults
72{
73 SUCCESS,
74 WORKING,
75 FAILURE
76};
77
78class NetCommand;
79using CommandQueue = std::deque<std::unique_ptr<NetCommand>>;
80
81class NetCommand
82{
83public:
84 inline explicit NetCommand(CommandQueue* commandQueue) :
85 _g_commandQueue(commandQueue)
86 {}
87 virtual ~NetCommand() = default;
88
89 [[nodiscard]] virtual NetCommandTypes getType() const = 0;
90 [[nodiscard]] NetCommandResults update(TransmitPacketPtr& buffPacket,
91 IpAddress::Types addressType,
92 Client& client,
93 std::chrono::milliseconds deltaTime);
94 [[nodiscard]] virtual NetCommandResults
95 onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) = 0;
96
97 [[nodiscard]] virtual std::chrono::milliseconds getTimeoutTarget() const;
98
99protected:
100 [[nodiscard]] virtual NetCommandResults
101 internalUpdate(TransmitPacketPtr& buffPacket, IpAddress::Types addressType, Client& client) = 0;
102 [[nodiscard]] virtual NetCommandResults timeout(Client& client);
103 void resetTimeout();
104
105 CommandQueue* _g_commandQueue{nullptr};
106
107private:
108 std::chrono::milliseconds g_timeout{0};
109};
110
111class FGE_API NetMTUCommand : public NetCommand
112{
113public:
114 using NetCommand::NetCommand;
115 ~NetMTUCommand() final = default;
116
117 [[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::DISCOVER_MTU; }
118
119 [[nodiscard]] NetCommandResults
120 internalUpdate(TransmitPacketPtr& buffPacket, IpAddress::Types addressType, Client& client) override;
121 [[nodiscard]] NetCommandResults
122 onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;
123
124 [[nodiscard]] inline std::future<uint16_t> get_future() { return this->g_promise.get_future(); }
125
126 [[nodiscard]] inline std::chrono::milliseconds getTimeoutTarget() const override { return FGE_NET_MTU_TIMEOUT_MS; }
127
128private:
129 [[nodiscard]] NetCommandResults timeout(Client& client) override;
130
131 std::promise<uint16_t> g_promise;
132 uint16_t g_currentMTU{0};
133 uint16_t g_targetMTU{0};
134 uint16_t g_maximumMTU{0};
135 uint16_t g_intervalMTU{0};
136 std::size_t g_tryCount{0};
137 enum class States
138 {
139 ASKING,
140 WAITING_RESPONSE,
141
142 DISCOVER,
143 WAITING
144 } g_state{States::ASKING};
145};
146
147class FGE_API NetConnectCommand : public NetCommand
148{
149public:
150 using NetCommand::NetCommand;
151 ~NetConnectCommand() final = default;
152
153 void setVersioningString(std::string_view versioningString);
154 [[nodiscard]] std::string const& getVersioningString() const;
155
156 [[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::CONNECT; }
157
158 [[nodiscard]] NetCommandResults
159 internalUpdate(TransmitPacketPtr& buffPacket, IpAddress::Types addressType, Client& client) override;
160 [[nodiscard]] NetCommandResults
161 onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;
162
163 [[nodiscard]] inline std::future<bool> get_future() { return this->g_promise.get_future(); }
164
165 [[nodiscard]] inline std::chrono::milliseconds getTimeoutTarget() const override
166 {
167 return FGE_NET_CONNECT_TIMEOUT_MS;
168 }
169
170private:
171 [[nodiscard]] NetCommandResults timeout(Client& client) override;
172
173 std::promise<bool> g_promise;
174 enum class States
175 {
176 TRANSMIT_FGE_HANDSHAKE,
177 WAITING_FGE_HANDSHAKE,
178
179 DEALING_WITH_MTU,
180 WAITING_SERVER_FINAL_MTU,
181
182 CRYPT_HANDSHAKE,
183 CRYPT_WAITING,
184
185 CONNECTED
186 } g_state{States::TRANSMIT_FGE_HANDSHAKE};
187 bool g_mtuTested{false};
188 std::future<uint16_t> g_mtuFuture;
189 std::string g_versioningString;
190};
191
192class FGE_API NetDisconnectCommand : public NetCommand
193{
194public:
195 using NetCommand::NetCommand;
196 ~NetDisconnectCommand() final = default;
197
198 [[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::CONNECT; }
199
200 [[nodiscard]] NetCommandResults
201 internalUpdate(TransmitPacketPtr& buffPacket, IpAddress::Types addressType, Client& client) override;
202 [[nodiscard]] NetCommandResults
203 onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;
204
205 [[nodiscard]] inline std::future<void> get_future() { return this->g_promise.get_future(); }
206
207 [[nodiscard]] inline std::chrono::milliseconds getTimeoutTarget() const override
208 {
209 return FGE_NET_DISCONNECT_TIMEOUT_MS;
210 }
211
212private:
213 [[nodiscard]] NetCommandResults timeout(Client& client) override;
214
215 std::promise<void> g_promise;
216 bool g_transmitted{false};
217};
218
219} // namespace fge::net
220
221#endif // _FGE_C_NETCOMMAND_HPP_INCLUDED
Class that represent the identity of a client.
Definition C_client.hpp:220
Definition C_netCommand.hpp:82
Definition C_netCommand.hpp:148
Definition C_netCommand.hpp:193
Definition C_netCommand.hpp:112