UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
UdpSocketSender.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"
6#include "Containers/Queue.h"
7#include "HAL/Event.h"
9#include "HAL/Runnable.h"
10#include "HAL/RunnableThread.h"
12#include "Sockets.h"
13
15
22 : public FRunnable
23 , private FSingleThreadRunnable
24{
25 // Structure for outbound packets.
26 struct FPacket
27 {
30
32 FIPv4Endpoint Recipient;
33
35 FPacket() { }
36
39 : Data(InData)
40 , Recipient(InRecipient)
41 { }
42 };
43
44public:
45
53 : SendRate(0)
54 , Socket(InSocket)
55 , Stopping(false)
56 , WaitTime(FTimespan::FromMilliseconds(100))
57 {
58 check(Socket != nullptr);
60
61 int32 NewSize = 0;
62 Socket->SetSendBufferSize(512 * 1024, NewSize);
63
66 }
67
70 {
71 if (Thread != nullptr)
72 {
73 Thread->Kill(true);
74 delete Thread;
75 }
76
78 WorkEvent = nullptr;
79 }
80
81public:
82
89 {
90 return SendRate;
91 }
92
99 {
100 return Throughput;
101 }
102
110 bool Send(const TSharedRef<TArray<uint8>, ESPMode::ThreadSafe>& Data, const FIPv4Endpoint& Recipient)
111 {
112 if (!Stopping && SendQueue.Enqueue(FPacket(Data, Recipient)))
113 {
114 WorkEvent->Trigger();
115
116 return true;
117 }
118
119 return false;
120 }
121
129 {
130 SendRate = Rate;
131 }
132
140 {
141 WaitTime = Timespan;
142 }
143
144public:
145
146 //~ FRunnable interface
147
149 {
150 return this;
151 }
152
153 virtual bool Init() override
154 {
155 return true;
156 }
157
158 virtual uint32 Run() override
159 {
160 while (!Stopping)
161 {
162 if (!Update(WaitTime))
163 {
164 Stopping = true;
165
166 return 0;
167 }
168
169 WorkEvent->Wait(WaitTime);
170 }
171
172 return 0;
173 }
174
175 virtual void Stop() override
176 {
177 Stopping = true;
178 WorkEvent->Trigger();
179 }
180
181 virtual void Exit() override { }
182
183protected:
184
192 {
193 while (!SendQueue.IsEmpty())
194 {
196 {
197 break;
198 }
199
200 FPacket Packet;
201 int32 Sent = 0;
202
203 SendQueue.Dequeue(Packet);
204 Socket->SendTo(Packet.Data->GetData(), Packet.Data->Num(), Sent, *Packet.Recipient.ToInternetAddr());
205
206 if (Sent != Packet.Data->Num())
207 {
208 return false;
209 }
210 }
211
212 return true;
213 }
214
215protected:
216
217 //~ FSingleThreadRunnable interface
218
219 virtual void Tick() override
220 {
222 }
223
224private:
225
228
230 uint32 SendRate;
231
233 FSocket* Socket;
234
236 bool Stopping;
237
239 FRunnableThread* Thread;
240
242 uint32 Throughput;
243
245 FTimespan WaitTime;
246
248 FEvent* WorkEvent;
249};
#define check(expr)
Definition AssertionMacros.h:314
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
@ TPri_AboveNormal
Definition GenericPlatformAffinity.h:28
@ SOCKTYPE_Datagram
Definition SocketTypes.h:37
uint32_t uint32
Definition binka_ue_file_header.h:6
static const uint64 GetPoolThreadMask()
Definition AndroidPlatformAffinity.h:38
Definition Event.h:21
virtual bool Wait(uint32 WaitTime, const bool bIgnoreThreadIdleStats=false)=0
virtual void Trigger()=0
Definition RunnableThread.h:20
virtual bool Kill(bool bShouldWait=true)=0
static CORE_API FRunnableThread * Create(class FRunnable *InRunnable, const TCHAR *ThreadName, uint32 InStackSize=0, EThreadPriority InThreadPri=TPri_Normal, uint64 InThreadAffinityMask=FPlatformAffinity::GetNoAffinityMask(), EThreadCreateFlags InCreateFlags=EThreadCreateFlags::None)
Definition ThreadingBase.cpp:862
Definition Runnable.h:20
Definition SingleThreadRunnable.h:12
Definition Sockets.h:19
virtual SOCKETS_API bool SendTo(const uint8 *Data, int32 Count, int32 &BytesSent, const FInternetAddr &Destination)
Definition Sockets.cpp:47
virtual bool Wait(ESocketWaitConditions::Type Condition, FTimespan WaitTime)=0
virtual bool SetSendBufferSize(int32 Size, int32 &NewSize)=0
ESocketType GetSocketType() const
Definition Sockets.h:458
Definition UdpSocketSender.h:24
virtual void Exit() override
Definition UdpSocketSender.h:181
uint32 GetThroughput() const
Definition UdpSocketSender.h:98
virtual bool Init() override
Definition UdpSocketSender.h:153
void SetWaitTime(const FTimespan &Timespan)
Definition UdpSocketSender.h:139
FUdpSocketSender(FSocket *InSocket, const TCHAR *ThreadDescription)
Definition UdpSocketSender.h:52
bool Send(const TSharedRef< TArray< uint8 >, ESPMode::ThreadSafe > &Data, const FIPv4Endpoint &Recipient)
Definition UdpSocketSender.h:110
virtual void Stop() override
Definition UdpSocketSender.h:175
uint32 GetSendRate() const
Definition UdpSocketSender.h:88
virtual void Tick() override
Definition UdpSocketSender.h:219
virtual uint32 Run() override
Definition UdpSocketSender.h:158
virtual FSingleThreadRunnable * GetSingleThreadInterface() override
Definition UdpSocketSender.h:148
virtual ~FUdpSocketSender()
Definition UdpSocketSender.h:69
bool Update(const FTimespan &SocketWaitTime)
Definition UdpSocketSender.h:191
void SetSendRate(uint32 Rate)
Definition UdpSocketSender.h:128
Definition Array.h:670
Definition Queue.h:48
bool IsEmpty() const
Definition Queue.h:206
bool Enqueue(const FElementType &Item)
Definition Queue.h:123
bool Dequeue(FElementType &OutItem)
Definition Queue.h:80
Definition SharedPointer.h:692
Definition SharedPointer.h:153
@ WaitForWrite
Definition SocketTypes.h:94
@ false
Definition radaudio_common.h:23
static CORE_API class FEvent * GetSynchEventFromPool(bool bIsManualReset=false)
Definition GenericPlatformProcess.cpp:576
static CORE_API void ReturnSynchEventToPool(FEvent *Event)
Definition GenericPlatformProcess.cpp:589
Definition IPv4Endpoint.h:27
Definition Timespan.h:76
static FTimespan Zero()
Definition Timespan.h:747