UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
NetPing.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
8
9#include "NetPing.generated.h"
10
11// Forward declarations
12enum class EPingType : uint32;
13class FInternetAddr;
14
15namespace UE::Net
16{
17
18namespace Private
19{
20
27constexpr int32 PingTypeToIdx(EPingType PingType)
28{
29 // FGenericPlatformMath::FloorLog2 as constexpr
30
31 uint32 Value = static_cast<uint32>(PingType);
32 uint32 Pos = 0;
33 if (Value >= 1<<16) { Value >>= 16; Pos += 16; }
34 if (Value >= 1<< 8) { Value >>= 8; Pos += 8; }
35 if (Value >= 1<< 4) { Value >>= 4; Pos += 4; }
36 if (Value >= 1<< 2) { Value >>= 2; Pos += 2; }
37 if (Value >= 1<< 1) { Pos += 1; }
38
39 return Pos;
40}
41
48static int32 PingTypeToIdxRuntime(EPingType PingType)
49{
50 return FMath::FloorLog2(static_cast<uint32>(PingType));
51}
52
53}
54}
55
59UENUM()
61{
62 None = 0x00000000,
63 RoundTrip = 0x00000001, // Round Trip ping which includes any server/client frame time delay
64 RoundTripExclFrame = 0x00000002, // Round Trip ping which may attempt to exclude server frame time delay (inaccurate)
65 ICMP = 0x00000004, // Standard ICMP ping (GameNetDriver client only, accuracy affected by network ICMP throttling)
66 UDPQoS = 0x00000008, // UDP based ping used with QoS ping host on server (more accurate than ICMP)
67
68 Max = UDPQoS,
70};
71
73
74
78UENUM()
80{
81 None, // No averaging
82 MovingAverage, // Moving average of the last 4 seconds of ping data
83 PlayerStateAvg // Original PlayerState ping code, calculating a moving average of the last 4 seconds of ping data. To be deprecated.
84};
85
90{
91 SetPingAddress, // Sent from the server to the client, to override the ICMP/UDP ping address
92 PingFailure, // Sent from the client to the server, to report failure to ping an ICMP/UDP address
93
95};
96
97
98namespace UE::Net
99{
103static bool IsValidPingType(EPingType PingType)
104{
105 const uint32 PingTypeVal = static_cast<uint32>(PingType);
106
108}
109
110namespace Private
111{
112 class FNetPingICMP;
113 class FNetPingUDPQoS;
114}
115
120{
122 double Current = -1.0;
123
125 double Min = -1.0;
126
128 double Max = -1.0;
129};
130
136{
137 template <typename T> friend struct UE::Core::Private::PimplPtr::TPimplHeapObjectImpl;
138
139private:
140 class FAverageStorageBase
141 {
142 public:
144 uint32 PingCount = 0;
145
147 uint32 PingTimeouts = 0;
148 };
149
150 class FNoAverageStorage : public FAverageStorageBase
151 {
152 public:
154 TSampleMinMaxAvg<EMinMaxValueMode::PerSample> PingValues;
155 };
156
157 class FMovingAverageStorage : public FAverageStorageBase
158 {
159 public:
161 TSampleMinMaxAvg<EMinMaxValueMode::PerMeasurement> PingValues;
162
164 TBinnedMovingAvg<decltype(PingValues), TBinParms::NumBins(4)> PingAccumulator{TBinParms::TimePerBin(1.0)};
165
166
167 public:
168 FMovingAverageStorage();
169 };
170
174 class FPlayerStateAvgStorage : public FAverageStorageBase
175 {
176 private:
178 uint8 CurPingBucket = 0;
179
185 PingAvgData PingBucket[4];
186
188 double CurPingBucketTimestamp = 0.0;
189
190 public:
192 TSampleMinMaxAvg<EMinMaxValueMode::PerMeasurement> PingValues;
193
194
195 public:
199 void Reset();
200
205 void UpdatePing(double TimeVal, float InPing);
206
207 private:
209 void RecalculateAvgPing();
210 };
211
212
213private:
214 FNetPing() = delete;
215
216 FNetPing(UNetConnection* InOwner);
217
218public:
219 ~FNetPing();
220
229
237 void UpdatePing(EPingType PingType, double TimeVal, double PingValue);
238
244 void UpdatePingTimeout(EPingType PingType);
245
252 ENGINE_API FPingValues GetPingValues(EPingType PingType) const;
253
259 void TickRealtime(double CurTimeSeconds);
260
268 ENGINE_API static void HandleNetPingControlMessage(UNetConnection* Connection, ENetPingControlMessage MessageType, FString MessageStr);
269
274 {
275 return PingTypes;
276 }
277
278private:
282 void Init();
283
284#if STATS
291 void UpdatePingStats(EPingType PingType, double CurrentValue);
292
301#endif
302
310 const FSampleMinMaxAvg& GetPingStorageValues(EPingType PingType) const;
311
319 FAverageStorageBase& GetPingStorageBase(EPingType PingType);
320
326 void ResetPingValues(EPingType PingType);
327
328
334 const TCHAR* GetRemoteAddr();
335
339 FString GetICMPAddr();
340
344 FString GetUDPQoSAddr();
345
351 void UpdateRemoteAddr(double CurTimeSeconds);
352
358 void PingTimerICMP(double CurTimeSeconds);
359
365 void PingTimerUDP(double CurTimeSeconds);
366
367
375 void ServerSetPingAddress(EPingType PingType, FString PingAddress);
376
377
378private:
381
383 TWeakPtr<const FInternetAddr> WeakRemoteAddr;
384
386 FString CachedRemoteAddr = TEXT("");
387
389 double NextRemoteAddrCheckTimestamp = 0.0;
390
392 EPingType PingTypes = EPingType::None;
393
395 EPingAverageType PingAverageTypes[static_cast<uint32>(EPingType::Count)] = { EPingAverageType::None };
396
398 uint8 PingAverageStorageIdx[static_cast<uint32>(EPingType::Count)] = { 0 };
399
401 TArray<FNoAverageStorage> PingNoAverageStorage;
402
404 TArray<FMovingAverageStorage> PingMovingAverageStorage;
405
407 TArray<FPlayerStateAvgStorage> PingPlayerStateAvgStorage;
408
410 double NextPingTimerICMPTimestamp = 0.0;
411
413 double NextPingTimerUDPTimestamp = 0.0;
414
417
420
422 FString OverridePingICMPAddress;
423
425 FString OverridePingUDPQoSAddress;
426
428 int32 OverridePingUDPQosPort = INDEX_NONE;
429
431 int32 OverridePingAddressCount = 0;
432};
433
434}
@ INDEX_NONE
Definition CoreMiscDefines.h:150
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define ENUM_CLASS_FLAGS(Enum)
Definition EnumClassFlags.h:6
ENetPingControlMessage
Definition NetPing.h:90
EPingType
Definition NetPing.h:61
@ RoundTripExclFrame
EPingAverageType
Definition NetPing.h:80
#define UENUM(...)
Definition ObjectMacros.h:749
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition IPAddress.h:22
Definition Array.h:670
Definition SharedPointer.h:692
Definition SharedPointer.h:1295
Definition NetPing.h:136
void UpdatePingTimeout(EPingType PingType)
Definition NetPing.cpp:518
static ENGINE_API void HandleNetPingControlMessage(UNetConnection *Connection, ENetPingControlMessage MessageType, FString MessageStr)
Definition NetPing.cpp:894
~FNetPing()
Definition NetPing.cpp:297
static TPimplPtr< FNetPing > CreateNetPing(UNetConnection *InOwner)
Definition NetPing.cpp:310
ENGINE_API FPingValues GetPingValues(EPingType PingType) const
Definition NetPing.cpp:681
void UpdatePing(EPingType PingType, double TimeVal, double PingValue)
Definition NetPing.cpp:464
void TickRealtime(double CurTimeSeconds)
Definition NetPing.cpp:744
EPingType GetPingTypes() const
Definition NetPing.h:273
Definition NetConnection.h:284
Definition OverriddenPropertySet.cpp:45
constexpr int32 PingTypeToIdx(EPingType PingType)
Definition NetPing.h:27
Definition NetworkVersion.cpp:28
TBinnedMovingValue< ConsumerType, NumBins, EBinnedValueMode::MovingAvg > TBinnedMovingAvg
Definition NetStatsUtils.h:306
static constexpr UE_FORCEINLINE_HINT bool IsPowerOfTwo(T Value)
Definition UnrealMathUtility.h:519
Definition PlayerState.h:19
Definition ObjectPtr.h:488
Definition PimplPtr.h:50
Definition NetPing.h:120
double Max
Definition NetPing.h:128
double Min
Definition NetPing.h:125
double Current
Definition NetPing.h:122