4242
4343#define FGE_NET_BAD_ID 0
4444
45- #define FGE_NET_PACKET_CACHE_DELAY_FACTOR 1 .2f
45+ #define FGE_NET_PACKET_CACHE_DELAY_FACTOR 2 .2f
4646#define FGE_NET_PACKET_CACHE_MAX 100
4747#define FGE_NET_PACKET_CACHE_MIN_LATENCY_MS 10
4848
4949#define FGE_NET_DEFAULT_REALM 0
5050#define FGE_NET_DEFAULT_PACKET_REORDERER_CACHE_SIZE 5
5151#define FGE_NET_PACKET_REORDERER_CACHE_COMPUTE (_clientReturnRate, _serverTickRate ) \
52- (((_clientReturnRate) * FGE_NET_PACKET_CACHE_DELAY_FACTOR / (_serverTickRate) + 1 ) * 2 )
52+ ((static_cast <unsigned >(static_cast <float >(_clientReturnRate) * FGE_NET_PACKET_CACHE_DELAY_FACTOR / \
53+ static_cast <float >(_serverTickRate)) + \
54+ 1 ) * \
55+ 2 )
5356
5457#define FGE_NET_HANDSHAKE_STRING " FGE:HANDSHAKE:AZCgMVg4d4Sl2xYvZcqXqljIOqSrKX6H"
5558
@@ -62,7 +65,7 @@ class Compressor;
6265
6366namespace fge ::net
6467{
65-
68+ class NetFluxUdp ;
6669class Client ;
6770
6871using Timestamp = uint16_t ;
@@ -122,7 +125,7 @@ class FGE_API ProtocolPacket : public Packet, public std::enable_shared_from_thi
122125 constexpr static std::size_t IdPosition = 0 ;
123126 constexpr static std::size_t RealmPosition = sizeof (IdType);
124127 constexpr static std::size_t CounterPosition = sizeof (IdType) + sizeof (RealmType);
125- constexpr static std::size_t LastCounterPosition = sizeof (IdType) + sizeof (RealmType) + sizeof (CounterType);
128+ constexpr static std::size_t ReorderedCounterPosition = sizeof (IdType) + sizeof (RealmType) + sizeof (CounterType);
126129
127130 inline ProtocolPacket (Packet const & pck,
128131 Identity const & id,
@@ -152,7 +155,7 @@ class FGE_API ProtocolPacket : public Packet, public std::enable_shared_from_thi
152155 [[nodiscard]] inline std::optional<IdType> retrieveFullHeaderId () const ;
153156 [[nodiscard]] inline std::optional<RealmType> retrieveRealm () const ;
154157 [[nodiscard]] inline std::optional<CounterType> retrieveCounter () const ;
155- [[nodiscard]] inline std::optional<CounterType> retrieveLastCounter () const ;
158+ [[nodiscard]] inline std::optional<CounterType> retrieveReorderedCounter () const ;
156159 [[nodiscard]] inline std::optional<Header> retrieveHeader () const ;
157160
158161 [[nodiscard]] inline bool isFragmented () const ;
@@ -170,7 +173,7 @@ class FGE_API ProtocolPacket : public Packet, public std::enable_shared_from_thi
170173
171174 inline ProtocolPacket& setRealm (RealmType realm);
172175 inline ProtocolPacket& setCounter (CounterType counter);
173- inline ProtocolPacket& setLastReorderedPacketCounter (CounterType counter);
176+ inline ProtocolPacket& setReorderedCounter (CounterType counter);
174177
175178 inline void setTimestamp (Timestamp timestamp);
176179 [[nodiscard]] inline Timestamp getTimeStamp () const ;
@@ -356,16 +359,26 @@ class FGE_API PacketReorderer
356359
357360 void clear ();
358361
362+ bool process (Client& client, NetFluxUdp& flux, bool ignoreRealm);
363+
359364 void push (ReceivedPacketPtr&& packet);
365+
366+ [[nodiscard]] static Stats checkStat (ReceivedPacketPtr const & packet, Client const & client, bool ignoreRealm);
360367 [[nodiscard]] static Stats checkStat (ReceivedPacketPtr const & packet,
361- ProtocolPacket::CounterType currentCounter,
362- ProtocolPacket::RealmType currentRealm);
363- [[nodiscard]] bool isForced () const ;
364- [[nodiscard]] std::optional<Stats> checkStat (ProtocolPacket::CounterType currentCounter,
365- ProtocolPacket::RealmType currentRealm) const ;
368+ ProtocolPacket::CounterType peerCounter,
369+ ProtocolPacket::CounterType peerReorderedCounter,
370+ ProtocolPacket::RealmType peerRealm,
371+ bool ignoreRealm);
372+ [[nodiscard]] std::optional<Stats> checkStat (Client const & client, bool ignoreRealm) const ;
373+ [[nodiscard]] std::optional<Stats> checkStat (ProtocolPacket::CounterType peerCounter,
374+ ProtocolPacket::CounterType peerReorderedCounter,
375+ ProtocolPacket::RealmType peerRealm,
376+ bool ignoreRealm) const ;
377+
366378 [[nodiscard]] ReceivedPacketPtr pop ();
367379
368380 [[nodiscard]] bool isEmpty () const ;
381+ [[nodiscard]] bool isForced () const ;
369382
370383 void setMaximumSize (std::size_t size);
371384 [[nodiscard]] std::size_t getMaximumSize () const ;
@@ -381,12 +394,14 @@ class FGE_API PacketReorderer
381394 Data& operator =(Data const & r) = delete ;
382395 Data& operator =(Data&& r) noexcept ;
383396
384- [[nodiscard]] Stats checkStat (ProtocolPacket::CounterType currentCounter,
385- ProtocolPacket::RealmType currentRealm) const ;
397+ [[nodiscard]] Stats checkStat (ProtocolPacket::CounterType peerCounter,
398+ ProtocolPacket::CounterType peerReorderedCounter,
399+ ProtocolPacket::RealmType peerRealm,
400+ bool ignoreRealm) const ;
386401
387402 ReceivedPacketPtr _packet;
388403 ProtocolPacket::CounterType _counter;
389- ProtocolPacket::CounterType _lastCounter ;
404+ ProtocolPacket::CounterType _reorderedCounter ;
390405 ProtocolPacket::RealmType _realm;
391406
392407 struct Compare
@@ -395,7 +410,7 @@ class FGE_API PacketReorderer
395410 {
396411 if (l._realm == r._realm )
397412 {
398- return l._counter > r._counter ;
413+ return l._reorderedCounter > r._reorderedCounter ;
399414 }
400415 return l._realm > r._realm ;
401416 }
@@ -438,7 +453,7 @@ class FGE_API PacketCache
438453 };
439454 };
440455
441- PacketCache () = default ;
456+ PacketCache ();
442457 PacketCache (PacketCache const & r) = delete ;
443458 PacketCache (PacketCache&& r) noexcept ;
444459 ~PacketCache () = default ;
@@ -449,6 +464,7 @@ class FGE_API PacketCache
449464 void clear ();
450465 [[nodiscard]] bool isEmpty () const ;
451466 [[nodiscard]] bool isEnabled () const ;
467+ [[nodiscard]] bool isAlarmed () const ;
452468 void enable (bool enable);
453469
454470 // Transmit
@@ -458,9 +474,7 @@ class FGE_API PacketCache
458474 void acknowledgeReception (std::span<Label> labels);
459475
460476 // Check for unacknowledged packet
461- [[nodiscard]] bool check (std::chrono::steady_clock::time_point const & timePoint,
462- std::chrono::milliseconds clientDelay);
463- [[nodiscard]] TransmitPacketPtr pop ();
477+ bool process (std::chrono::steady_clock::time_point const & timePoint, Client& client);
464478
465479private:
466480 struct Data
@@ -473,15 +487,14 @@ class FGE_API PacketCache
473487 TransmitPacketPtr _packet;
474488 Label _label;
475489 std::chrono::steady_clock::time_point _time{};
490+ unsigned int _tryCount{0 };
476491 };
477492
478493 mutable std::mutex g_mutex;
479494
480- // Circular buffer
481- std::vector<Data> g_cache{FGE_NET_PACKET_CACHE_MAX};
482- std::size_t g_start{0 };
483- std::size_t g_end{0 };
495+ std::vector<Data> g_cache;
484496
497+ bool g_alarm{false };
485498 bool g_enable{false };
486499};
487500
0 commit comments