UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
UdpSocketBuilder.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"
7#include "Sockets.h"
8#include "SocketSubsystem.h"
11
12class Error;
13
18{
19public:
20
27 : AllowBroadcast(false)
28 , Blocking(false)
29 , Bound(false)
30 , BoundEndpoint(FIPv4Address::Any, 0)
31 , Description(InDescription)
32 , MulticastInterface(FIPv4Address::Any)
33 , MulticastLoopback(false)
34 , MulticastTtl(1)
35 , ReceiveBufferSize(0)
36 , Reusable(false)
37 , SendBufferSize(0)
38 { }
39
40public:
41
49 {
50 Blocking = true;
51
52 return *this;
53 }
54
62 {
63 Blocking = false;
64
65 return *this;
66 }
67
75 {
76 Reusable = true;
77
78 return *this;
79 }
80
92 {
93 BoundEndpoint = FIPv4Endpoint(Address, BoundEndpoint.Port);
94 Bound = true;
95
96 return *this;
97 }
98
107 {
108 BoundEndpoint = Endpoint;
109 Bound = true;
110
111 return *this;
112 }
113
125 {
126 BoundEndpoint = FIPv4Endpoint(BoundEndpoint.Address, Port);
127 Bound = true;
128
129 return *this;
130 }
131
140 {
141 return JoinedToGroup(GroupAddress, FIPv4Address::Any);
142 }
143
152 FUdpSocketBuilder& JoinedToGroup(const FIPv4Address& GroupAddress, const FIPv4Address& InterfaceAddress)
153 {
154 JoinedGroups.Add({ GroupAddress, InterfaceAddress });
155
156 return *this;
157 }
158
165 {
166 AllowBroadcast = true;
167
168 return *this;
169 }
170
180 {
181 MulticastLoopback = true;
182
183 return *this;
184 }
185
194 {
195 MulticastTtl = TimeToLive;
196
197 return *this;
198 }
199
208 {
209 MulticastInterface = InterfaceAddress;
210
211 return *this;
212 }
213
214
226 {
227 ReceiveBufferSize = SizeInBytes;
228
229 return *this;
230 }
231
243 {
244 SendBufferSize = SizeInBytes;
245
246 return *this;
247 }
248
249public:
250
256 operator FSocket*() const
257 {
258 return Build();
259 }
260
266 FSocket* Build() const
267 {
268 // load socket subsystem
270
271 if (SocketSubsystem == nullptr)
272 {
273 GLog->Log(TEXT("FUdpSocketBuilder: Failed to load socket subsystem"));
274 return nullptr;
275 }
276
277 // create socket
278 TSharedRef<FInternetAddr> RemoteAddr = BoundEndpoint.ToInternetAddr();
279 FSocket* Socket = SocketSubsystem->CreateSocket(NAME_DGram, *Description, RemoteAddr->GetProtocolType());
280
281 if (Socket == nullptr)
282 {
283 GLog->Logf(TEXT("FUdpSocketBuilder: Failed to create socket %s"), *Description);
284 return nullptr;
285 }
286
287 // configure socket
288
289 if (!Socket->SetNonBlocking(!Blocking) ||
290 !Socket->SetReuseAddr(Reusable) ||
291 !Socket->SetBroadcast(AllowBroadcast) ||
292 !Socket->SetRecvErr())
293 {
294 const ESocketErrors SocketError = SocketSubsystem->GetLastErrorCode();
295
296 GLog->Logf(TEXT("FUdpSocketBuilder: Failed to configure %s (blocking: %i, reusable: %i, broadcast: %i). Error code %d."),
297 *Description, Blocking, Reusable, AllowBroadcast, int32(SocketError));
298
299 SocketSubsystem->DestroySocket(Socket);
300 return nullptr;
301 }
302
303 // bind socket
304
305 if (Bound && !Socket->Bind(*RemoteAddr))
306 {
307 const ESocketErrors SocketError = SocketSubsystem->GetLastErrorCode();
308
309 GLog->Logf(TEXT("FUdpSocketBuilder: Failed to bind %s to %s. Error code %d."),
310 *Description, *BoundEndpoint.ToString(), int32(SocketError));
311
312 SocketSubsystem->DestroySocket(Socket);
313 return nullptr;
314 }
315
316 // configure multicast
317
318 if (!Socket->SetMulticastLoopback(MulticastLoopback) || !Socket->SetMulticastTtl(MulticastTtl))
319 {
320 const ESocketErrors SocketError = SocketSubsystem->GetLastErrorCode();
321
322 GLog->Logf(TEXT("FUdpSocketBuilder: Failed to configure multicast for %s (loopback: %i, ttl: %i). Error code %d."),
323 *Description, MulticastLoopback, MulticastTtl, int32(SocketError));
324
325 SocketSubsystem->DestroySocket(Socket);
326 return nullptr;
327 }
328
329 // join multicast groups
330
331 TSharedRef<FInternetAddr> MulticastAddress = SocketSubsystem->CreateInternetAddr(RemoteAddr->GetProtocolType());
334
335 for (const auto& Group : JoinedGroups)
336 {
337 // The Socket code no longer has the multicast address hack handled anymore, as such, we need to properly determine the address ourselves.
338 if (Group.GroupAddress.IsSessionFrontendMulticast() && MulticastAddress->GetProtocolType() != FNetworkProtocolTypes::IPv4)
339 {
340 // This will use the address protocol that we figured out earlier.
342 }
343 else
344 {
345 // Otherwise, we'll use the address we use all the time.
346 AddressToUse = FIPv4Endpoint(Group.GroupAddress, 0).ToInternetAddr();
347 }
348
349 if (!Socket->JoinMulticastGroup(*AddressToUse, *FIPv4Endpoint(Group.InterfaceAddress, 0).ToInternetAddr()))
350 {
351 const ESocketErrors SocketError = SocketSubsystem->GetLastErrorCode();
352
353 GLog->Logf(TEXT("FUdpSocketBuilder: Failed to subscribe %s to multicast group %s on interface %s. Error code %d."),
354 *Description, *Group.GroupAddress.ToString(), *Group.InterfaceAddress.ToString(), int32(SocketError));
355
356 SocketSubsystem->DestroySocket(Socket);
357 return nullptr;
358 }
359 }
360
361 // set multicast outgoing interface
362 if (MulticastInterface != FIPv4Address::Any)
363 {
364 if (!Socket->SetMulticastInterface(*FIPv4Endpoint(MulticastInterface, 0).ToInternetAddr()))
365 {
366 const ESocketErrors SocketError = SocketSubsystem->GetLastErrorCode();
367
368 GLog->Logf(TEXT("FUdpSocketBuilder: Failed to set multicast outgoing interface for %s to %s. Error code %d."),
369 *Description, *MulticastInterface.ToString(), int32(SocketError));
370
371 SocketSubsystem->DestroySocket(Socket);
372 return nullptr;
373 }
374 }
375
376 // set buffer sizes
377 {
379
380 if (ReceiveBufferSize > 0)
381 {
382 if (!Socket->SetReceiveBufferSize(ReceiveBufferSize, OutNewSize))
383 {
384 const ESocketErrors SocketError = SocketSubsystem->GetLastErrorCode();
385
386 GLog->Logf(TEXT("FUdpSocketBuilder: Warning - could not set receive buffer size to %d for %s. Error code %d."),
387 ReceiveBufferSize, *Description, int32(SocketError));
388 }
389 }
390
391 if (SendBufferSize > 0)
392 {
393 if (!Socket->SetSendBufferSize(SendBufferSize, OutNewSize))
394 {
395 const ESocketErrors SocketError = SocketSubsystem->GetLastErrorCode();
396
397 GLog->Logf(TEXT("FUdpSocketBuilder: Warning - could not set send buffer size to %d for %s. Error code %d."),
398 SendBufferSize, *Description, int32(SocketError));
399 }
400 }
401 }
402
403 return Socket;
404 }
405
406private:
407
409 bool AllowBroadcast;
410
412 bool Blocking;
413
415 bool Bound;
416
418 FIPv4Endpoint BoundEndpoint;
419
421 FString Description;
422
424 FIPv4Address MulticastInterface;
425
427 struct FMulticastGroup
428 {
429 FIPv4Address GroupAddress;
430 FIPv4Address InterfaceAddress;
431 };
432 TArray<FMulticastGroup> JoinedGroups;
433
435 bool MulticastLoopback;
436
438 uint8 MulticastTtl;
439
441 int32 ReceiveBufferSize;
442
444 bool Reusable;
445
447 int32 SendBufferSize;
448};
#define GLog
Definition CoreGlobals.h:95
#define TEXT(x)
Definition Platform.h:1272
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
ESocketErrors
Definition SocketErrors.h:9
#define PLATFORM_SOCKETSUBSYSTEM
Definition SocketSubsystem.h:44
uint8_t uint8
Definition binka_ue_file_header.h:8
uint16_t uint16
Definition binka_ue_file_header.h:7
virtual void SetBroadcastAddress()=0
Definition Sockets.h:19
Definition UdpSocketBuilder.h:18
FUdpSocketBuilder & AsBlocking()
Definition UdpSocketBuilder.h:48
FUdpSocketBuilder & AsReusable()
Definition UdpSocketBuilder.h:74
FUdpSocketBuilder & JoinedToGroup(const FIPv4Address &GroupAddress, const FIPv4Address &InterfaceAddress)
Definition UdpSocketBuilder.h:152
FSocket * Build() const
Definition UdpSocketBuilder.h:266
FUdpSocketBuilder & AsNonBlocking()
Definition UdpSocketBuilder.h:61
FUdpSocketBuilder & WithSendBufferSize(int32 SizeInBytes)
Definition UdpSocketBuilder.h:242
FUdpSocketBuilder & WithMulticastInterface(const FIPv4Address &InterfaceAddress)
Definition UdpSocketBuilder.h:207
FUdpSocketBuilder & WithMulticastLoopback()
Definition UdpSocketBuilder.h:179
FUdpSocketBuilder & WithReceiveBufferSize(int32 SizeInBytes)
Definition UdpSocketBuilder.h:225
FUdpSocketBuilder & BoundToEndpoint(const FIPv4Endpoint &Endpoint)
Definition UdpSocketBuilder.h:106
FUdpSocketBuilder & JoinedToGroup(const FIPv4Address &GroupAddress)
Definition UdpSocketBuilder.h:139
FUdpSocketBuilder & BoundToPort(uint16 Port)
Definition UdpSocketBuilder.h:124
FUdpSocketBuilder & WithBroadcast()
Definition UdpSocketBuilder.h:164
FUdpSocketBuilder & WithMulticastTtl(uint8 TimeToLive)
Definition UdpSocketBuilder.h:193
FUdpSocketBuilder(const FString &InDescription)
Definition UdpSocketBuilder.h:26
FUdpSocketBuilder & BoundToAddress(const FIPv4Address &Address)
Definition UdpSocketBuilder.h:91
Definition SocketSubsystem.h:58
static SOCKETS_API ISocketSubsystem * Get(const FName &SubsystemName=NAME_None)
Definition SocketSubsystem.cpp:224
virtual TSharedRef< FInternetAddr > CreateInternetAddr(uint32 Address, uint32 Port=0)
Definition SocketSubsystem.h:317
virtual FSocket * CreateSocket(const FName &SocketType, const FString &SocketDescription, bool bForceUDP=false)
Definition SocketSubsystem.h:98
virtual ESocketErrors GetLastErrorCode()=0
virtual void DestroySocket(FSocket *Socket)=0
Definition Array.h:670
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
Definition SharedPointer.h:692
Definition SharedPointer.h:153
const FLazyName IPv4(TEXT("IPv4"))
Definition SocketTypes.h:27
@ false
Definition radaudio_common.h:23
Definition IPv4Address.h:16
NETWORKING_API FString ToString() const
Definition IPv4Address.cpp:17
static NETWORKING_API const FIPv4Address Any
Definition IPv4Address.h:299
Definition IPv4Endpoint.h:27
TSharedRef< FInternetAddr > ToInternetAddr() const
Definition IPv4Endpoint.h:112
uint16 Port
Definition IPv4Endpoint.h:32
FIPv4Address Address
Definition IPv4Endpoint.h:29
NETWORKING_API FString ToString() const
Definition IPv4Endpoint.cpp:17