17#ifndef _FGE_C_CLIENT_HPP_INCLUDED
18#define _FGE_C_CLIENT_HPP_INCLUDED
20#include "FastEngine/fge_extern.hpp"
21#include "C_identity.hpp"
22#include "FastEngine/C_event.hpp"
23#include "FastEngine/C_propertyList.hpp"
24#include "FastEngine/network/C_protocol.hpp"
32#define FGE_NET_DEFAULT_LATENCY 20
33#define FGE_NET_CLIENT_TIMESTAMP_MODULO 65536
34#define FGE_NET_BAD_LATENCY std::numeric_limits<fge::net::Latency_ms>::max()
35#define FGE_NET_LATENCY_PLANNER_MEAN 6
36#define FGE_NET_DEFAULT_lOST_PACKET_THRESHOLD 15
37#define FGE_NET_STATUS_DEFAULT_TIMEOUT \
38 std::chrono::milliseconds \
42#define FGE_NET_STATUS_DEFAULT_CONNECTED_TIMEOUT \
43 std::chrono::milliseconds \
47#define FGE_NET_STATUS_DEFAULT_STATUS "none"
49#define FGE_NET_DEFAULT_RETURN_PACKET_RATE \
50 std::chrono::milliseconds \
77class FGE_API OneWayLatencyPlanner
80 OneWayLatencyPlanner() =
default;
84 HAVE_EXTERNAL_TIMESTAMP = 1 << 0
92 void pack(TransmitPacketPtr& tPacket);
157 std::optional<Latency_ms> g_latency;
158 std::optional<Latency_ms> g_otherSideLatency;
160 std::optional<FullTimestampOffset> g_meanClockOffset;
161 std::array<FullTimestampOffset, FGE_NET_LATENCY_PLANNER_MEAN> g_clockOffsets{};
162 std::size_t g_clockOffsetCount{0};
164 std::optional<Latency_ms> g_roundTripTime;
166 Timestamp g_externalStoredTimestamp{0};
167 std::underlying_type_t<Stats> g_syncStat{0};
170class FGE_API ClientStatus
173 enum class NetworkStatus
186 ClientStatus() =
default;
187 explicit ClientStatus(std::string_view status, NetworkStatus networkStatus = NetworkStatus::UNKNOWN);
189 [[nodiscard]]
bool isInEncryptedState()
const;
190 [[nodiscard]]
bool isDisconnected()
const;
191 [[nodiscard]]
bool isConnected()
const;
192 [[nodiscard]]
bool isConnecting()
const;
193 [[nodiscard]]
bool isAuthenticated()
const;
195 [[nodiscard]] std::string
const& getStatus()
const;
196 [[nodiscard]] NetworkStatus getNetworkStatus()
const;
197 [[nodiscard]] std::chrono::milliseconds getTimeout()
const;
198 [[nodiscard]] std::chrono::milliseconds getRemainingTimeout()
const;
200 void set(std::string_view status, NetworkStatus networkStatus);
201 void setStatus(std::string_view status);
202 void setNetworkStatus(NetworkStatus networkStatus);
204 void setTimeout(std::chrono::milliseconds timeout);
206 [[nodiscard]]
bool isTimeout()
const;
209 std::string g_status{FGE_NET_STATUS_DEFAULT_STATUS};
210 std::atomic<NetworkStatus> g_networkStatus{NetworkStatus::UNKNOWN};
211 std::chrono::milliseconds g_timeout{FGE_NET_STATUS_DEFAULT_TIMEOUT};
212 std::chrono::steady_clock::time_point g_currentTimeout{std::chrono::steady_clock::now()};
225 void* _rbio{
nullptr};
226 void* _wbio{
nullptr};
320 [[nodiscard]]
Latency_ms getLastPacketLatency()
const;
362 void pushForcedFrontPacket(TransmitPacketPtr pck);
376 void disconnect(
bool pushDisconnectPacket =
true);
378 [[nodiscard]] ProtocolPacket::RealmType getCurrentRealm()
const;
379 [[nodiscard]] std::chrono::milliseconds getLastRealmChangeElapsedTime()
const;
380 void setCurrentRealm(ProtocolPacket::RealmType realm);
381 ProtocolPacket::RealmType advanceCurrentRealm();
383 [[nodiscard]] ProtocolPacket::CounterType getCurrentPacketCounter()
const;
384 ProtocolPacket::CounterType advanceCurrentPacketCounter();
385 void setCurrentPacketCounter(ProtocolPacket::CounterType counter);
387 [[nodiscard]] ProtocolPacket::CounterType getClientPacketCounter()
const;
388 ProtocolPacket::CounterType advanceClientPacketCounter();
389 void setClientPacketCounter(ProtocolPacket::CounterType counter);
390 void resetLastReorderedPacketCounter();
391 [[nodiscard]] ProtocolPacket::CounterType getLastReorderedPacketCounter()
const;
396 [[nodiscard]] DataLockPair<PacketCache*, std::recursive_mutex> getPacketCache();
397 [[nodiscard]] DataLockPair<PacketCache const*, std::recursive_mutex> getPacketCache()
const;
398 void acknowledgeReception(ReceivedPacketPtr
const& packet);
399 [[nodiscard]] std::vector<PacketCache::Label>
const& getAcknowledgedList()
const;
400 void clearAcknowledgedList();
402 void clearLostPacketCount();
403 uint32_t advanceLostPacketCount();
404 void setLostPacketThreshold(uint32_t threshold);
405 [[nodiscard]] uint32_t getLostPacketThreshold()
const;
406 [[nodiscard]] uint32_t getLostPacketCount()
const;
411 [[nodiscard]]
CryptInfo const& getCryptInfo()
const;
414 [[nodiscard]] uint16_t getMTU()
const;
415 void setMTU(uint16_t mtu);
417 void setPacketReturnRate(std::chrono::milliseconds rate);
418 [[nodiscard]] std::chrono::milliseconds getPacketReturnRate()
const;
428 mutable std::optional<Timestamp> g_correctorTimestamp;
431 std::chrono::steady_clock::time_point g_lastPacketTimePoint;
433 std::deque<TransmitPacketPtr> g_pendingTransmitPackets;
434 mutable std::recursive_mutex g_mutex;
436 std::chrono::steady_clock::time_point g_lastRealmChangeTimePoint;
437 ProtocolPacket::RealmType g_currentRealm{FGE_NET_DEFAULT_REALM};
438 ProtocolPacket::CounterType g_currentPacketCounter{0};
439 ProtocolPacket::CounterType g_lastReorderedPacketCounter{0};
440 ProtocolPacket::CounterType g_clientPacketCounter{0};
442 std::vector<PacketCache::Label> g_acknowledgedPackets;
443 PacketCache g_packetCache;
444 PacketReorderer g_packetReorderer;
445 uint32_t g_lostPacketCount{0};
446 uint32_t g_lostPacketThreshold{FGE_NET_DEFAULT_lOST_PACKET_THRESHOLD};
448 std::chrono::milliseconds g_returnPacketRate{FGE_NET_DEFAULT_RETURN_PACKET_RATE};
452 ClientStatus g_status;
453 CryptInfo g_cryptInfo;
This class is used to handle callbacks in a safe way.
Definition C_callback.hpp:189
This class is a wrapper for SDL events.
Definition C_event.hpp:59
A class that map a string to a Property.
Definition C_propertyList.hpp:35
Definition C_client.hpp:171
Class that represent the identity of a client.
Definition C_client.hpp:220
void resetLastPacketTimePoint()
Reset the time point for limiting the packets sending frequency.
Latency_ms getPing_ms() const
Compute the ping.
Client(Latency_ms CTOSLatency, Latency_ms STOCLatency)
Constructor with default latencies.
OneWayLatencyPlanner _latencyPlanner
A latency planner that will help latency calculation.
Definition C_client.hpp:424
void pushPacket(TransmitPacketPtr pck)
Add a Packet to the queue.
void clearPackets()
Clear the packet queue.
std::chrono::milliseconds getLastPacketElapsedTime() const
Get the delta time between the last sent packet and the current time.
void setCorrectorTimestamp(Timestamp timestamp)
Set the corrector timestamp.
bool _mtuFinalizedFlag
A flag that indicate if the MTU has been finalized from the remote side.
Definition C_client.hpp:425
PropertyList _data
Some user-defined client properties.
Definition C_client.hpp:423
Event _event
Optional client-side event that can be synchronized with the server.
Definition C_client.hpp:422
std::optional< Timestamp > getCorrectorTimestamp() const
Get the corrector timestamp.
static Timestamp getTimestamp_ms()
Get a modulated timestamp of the current time.
Latency_ms getSTOCLatency_ms() const
Get the "Server To Client" latency.
std::optional< Latency_ms > getCorrectorLatency() const
Compute the corrector latency.
bool isPendingPacketsEmpty() const
Check if the packet queue is empty.
static FullTimestamp getFullTimestamp_ms()
Get a timestamp of the current time.
void setCTOSLatency_ms(Latency_ms latency)
Set the "Client To Server" latency.
TransmitPacketPtr popPacket()
Pop a packet from the queue.
Latency_ms getCTOSLatency_ms() const
Get the "Client To Server" latency.
void setSTOCLatency_ms(Latency_ms latency)
Set the "Server To Client" latency.
static Latency_ms computeLatency_ms(Timestamp const &sentTimestamp, Timestamp const &receivedTimestamp)
Compute the latency for the client->server / server->client with the given timestamps.
A helper class that measure latency between client/server.
Definition C_client.hpp:78
std::optional< FullTimestampOffset > getClockOffset() const
Retrieve the clock offset.
std::optional< Latency_ms > getRoundTripTime() const
Retrieve the RTT (Round Trip Time)
std::optional< Latency_ms > getLatency() const
Retrieve the latency.
void pack(TransmitPacketPtr &tPacket)
Pack the required data by the planner to the client/server.
void unpack(ProtocolPacket *packet, Client &client)
Unpack the data received by another client/server planner.
std::optional< Latency_ms > getOtherSideLatency() const
Retrieve the other side latency.
A packet reorderer.
Definition C_protocol.hpp:333
A special inheritance of Packet with a predefined communication protocol.
Definition C_protocol.hpp:85
int64_t FullTimestampOffset
An timestamp offset.
Definition C_client.hpp:64
uint64_t FullTimestamp
An timestamp represent current time in milliseconds.
Definition C_client.hpp:63
uint16_t Timestamp
An timestamp represent modulated current time in milliseconds.
Definition C_client.hpp:62
uint16_t Latency_ms
An latency represent the latency of the client->server / server->client connection.
Definition C_client.hpp:65
Definition C_client.hpp:223