17#ifndef _FGE_C_SERVER_HPP_INCLUDED
18#define _FGE_C_SERVER_HPP_INCLUDED
20#include "FastEngine/fge_extern.hpp"
21#include "C_socket.hpp"
22#include "FastEngine/network/C_clientList.hpp"
23#include "FastEngine/network/C_packet.hpp"
24#include "FastEngine/network/C_packetBZ2.hpp"
25#include "FastEngine/network/C_packetLZ4.hpp"
26#include "FastEngine/network/C_protocol.hpp"
27#include <condition_variable>
33#if defined(FGE_ENABLE_SERVER_NETWORK_RANDOM_LOST) || defined(FGE_ENABLE_CLIENT_NETWORK_RANDOM_LOST)
34 #include "FastEngine/C_random.hpp"
37#define FGE_SERVER_DEFAULT_MAXPACKET 200
38#define FGE_SERVER_MAX_TIME_DIFFERENCE_REALM \
39 std::chrono::milliseconds \
47class ServerSideNetUdp;
48class ClientSideNetUdp;
66 [[nodiscard]]
inline Timestamp getTimeStamp()
const;
67 [[nodiscard]]
inline Identity const& getIdentity()
const;
76 std::size_t g_fluxIndex;
77 std::size_t g_fluxCount;
79using FluxPacketPtr = std::unique_ptr<FluxPacket>;
81enum class FluxProcessResults
112 [[nodiscard]] FluxPacketPtr popNextPacket();
114 [[nodiscard]] std::size_t getPacketsSize()
const;
115 [[nodiscard]]
bool isEmpty()
const;
117 void setMaxPackets(std::size_t n);
118 [[nodiscard]] std::size_t getMaxPackets()
const;
121 bool pushPacket(FluxPacketPtr&& fluxPck);
122 void forcePushPacket(FluxPacketPtr fluxPck);
123 void forcePushPacketFront(FluxPacketPtr fluxPck);
124 [[nodiscard]] FluxProcessResults processReorder(
Client& client,
125 FluxPacketPtr& refFluxPacket,
126 ProtocolPacket::CountId currentCountId,
129 mutable std::mutex _g_mutexFlux;
130 std::deque<FluxPacketPtr> _g_packets;
131 std::size_t _g_remainingPackets{0};
134 std::size_t g_maxPackets = FGE_SERVER_DEFAULT_MAXPACKET;
148 [[nodiscard]] FluxProcessResults
149 process(ClientSharedPtr& refClient, FluxPacketPtr& refFluxPacket,
bool allowUnknownClient);
156 [[nodiscard]]
bool verifyRealm(ClientSharedPtr
const& refClient, FluxPacketPtr
const& refFluxPacket);
177 explicit ServerSideNetUdp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
185 template<
class TPacket = Packet>
187 start(Port bindPort,
IpAddress const& bindIp, IpAddress::Types addressType = IpAddress::Types::None);
188 template<
class TPacket = Packet>
189 [[nodiscard]]
bool start(IpAddress::Types addressType = IpAddress::Types::None);
212 [[nodiscard]] std::size_t getFluxSize()
const;
214 [[nodiscard]] IpAddress::Types getAddressType()
const;
219 void repushPacket(FluxPacketPtr&& fluxPck);
229 [[nodiscard]]
bool isRunning()
const;
231 void sendTo(TransmissionPacketPtr& pck,
Client const& client,
Identity const&
id);
232 void sendTo(TransmissionPacketPtr& pck,
Identity const&
id);
235 template<
class TPacket>
236 void threadReception();
237 template<
class TPacket>
238 void threadTransmission();
240 std::unique_ptr<std::thread> g_threadReception;
241 std::unique_ptr<std::thread> g_threadTransmission;
243 std::condition_variable g_transmissionNotifier;
245 mutable std::mutex g_mutexServer;
247 std::vector<std::unique_ptr<ServerNetFluxUdp>> g_fluxes;
249 std::queue<std::pair<TransmissionPacketPtr, Identity>> g_transmissionQueue;
265 explicit ClientSideNetUdp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
273 template<
class TPacket = Packet>
274 [[nodiscard]]
bool start(Port bindPort,
276 Port connectRemotePort,
278 IpAddress::Types addressType = IpAddress::Types::None);
281 void notifyTransmission();
282 [[nodiscard]]
bool isRunning()
const;
284 [[nodiscard]] IpAddress::Types getAddressType()
const;
286 [[nodiscard]] std::size_t waitForPackets(std::chrono::milliseconds time_ms);
288 [[nodiscard]]
Identity const& getClientIdentity()
const;
290 [[nodiscard]] FluxProcessResults process(FluxPacketPtr& refFluxPacket);
295 template<
class TPacket>
296 void threadReception();
297 template<
class TPacket>
298 void threadTransmission();
300 std::unique_ptr<std::thread> g_threadReception;
301 std::unique_ptr<std::thread> g_threadTransmission;
303 std::condition_variable g_transmissionNotifier;
304 std::condition_variable g_receptionNotifier;
314#include "C_server.inl"
This class is used to handle callbacks in a safe way.
Definition C_callback.hpp:189
A list of clients used by a server.
Definition C_clientList.hpp:57
A client side network manager.
Definition C_server.hpp:263
Class that represent the identity of a client.
Definition C_client.hpp:246
A received packet from a network flux.
Definition C_server.hpp:59
A class to represent an IP address.
Definition C_ipAddress.hpp:57
A network flux.
Definition C_server.hpp:101
Definition C_packet.hpp:70
A special inheritance of Packet with a predefined communication protocol.
Definition C_protocol.hpp:52
Definition C_server.hpp:140
A server side network manager.
Definition C_server.hpp:175
void notifyTransmission()
Notify the transmission thread.
ServerNetFluxUdp * newFlux()
Create a new flux.
A wrapper for UDP sockets inheriting from Socket.
Definition C_socket.hpp:267
uint16_t Timestamp
An timestamp represent modulated current time in milliseconds.
Definition C_client.hpp:49
A class to represent a client or server identity with an IP address and a port.
Definition C_identity.hpp:31