UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
TcpSocketBuilder.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 "SocketSubsystem.h"
10#include "Sockets.h"
11#include "SocketTypes.h"
12
13class Error;
14
19{
20public:
21
28 : Blocking(false)
29 , Bound(false)
30 , BoundEndpoint(FIPv4Address::Any, 0)
31 , Description(InDescription)
32 , Linger(false)
33 , LingerTimeout(0)
34 , Listen(false)
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
89 {
90 Reusable = bInReusable;
91
92 return *this;
93 }
94
106 {
107 BoundEndpoint = FIPv4Endpoint(Address, BoundEndpoint.Port);
108 Bound = true;
109
110 return *this;
111 }
112
121 {
122 BoundEndpoint = Endpoint;
123 Bound = true;
124
125 return *this;
126 }
127
139 {
140 BoundEndpoint = FIPv4Endpoint(BoundEndpoint.Address, Port);
141 Bound = true;
142
143 return *this;
144 }
145
153 {
154 Linger = true;
155 LingerTimeout = Timeout;
156
157 return *this;
158 }
159
167 {
168 Listen = true;
169 ListenBacklog = MaxBacklog;
170
171 return *this;
172 }
173
185 {
186 ReceiveBufferSize = SizeInBytes;
187
188 return *this;
189 }
190
202 {
203 SendBufferSize = SizeInBytes;
204
205 return *this;
206 }
207
208public:
209
215 operator FSocket*() const
216 {
217 return Build();
218 }
219
225 FSocket* Build() const
226 {
227 FSocket* Socket = nullptr;
228
230
231 if (SocketSubsystem != nullptr)
232 {
234 Socket = SocketSubsystem->CreateSocket(NAME_Stream, *Description, BoundEndpointAddr->GetProtocolType());
235
236 if (Socket != nullptr)
237 {
238 bool Error = !Socket->SetReuseAddr(Reusable) ||
239 !Socket->SetLinger(Linger, LingerTimeout) ||
240 !Socket->SetRecvErr();
241
242 if (!Error)
243 {
244 Error = Bound && !Socket->Bind(*BoundEndpointAddr);
245 }
246
247 if (!Error)
248 {
249 Error = Listen && !Socket->Listen(ListenBacklog);
250 }
251
252 if (!Error)
253 {
254 Error = !Socket->SetNonBlocking(!Blocking);
255 }
256
257 if (!Error)
258 {
260
261 if (ReceiveBufferSize > 0)
262 {
263 Socket->SetReceiveBufferSize(ReceiveBufferSize, OutNewSize);
264 }
265
266 if (SendBufferSize > 0)
267 {
268 Socket->SetSendBufferSize(SendBufferSize, OutNewSize);
269 }
270 }
271
272 if (Error)
273 {
274 GLog->Logf(TEXT("FTcpSocketBuilder: Failed to create the socket %s as configured"), *Description);
275
276 SocketSubsystem->DestroySocket(Socket);
277
278 Socket = nullptr;
279 }
280 }
281 }
282
283 return Socket;
284 }
285
286private:
287
289 bool Blocking;
290
292 bool Bound;
293
295 FIPv4Endpoint BoundEndpoint;
296
298 FString Description;
299
301 bool Linger;
302
304 int32 LingerTimeout;
305
307 bool Listen;
308
310 int32 ListenBacklog;
311
313 int32 ReceiveBufferSize;
314
316 bool Reusable;
317
319 int32 SendBufferSize;
320};
#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
#define PLATFORM_SOCKETSUBSYSTEM
Definition SocketSubsystem.h:44
uint16_t uint16
Definition binka_ue_file_header.h:7
Definition Sockets.h:19
Definition TcpSocketBuilder.h:19
FTcpSocketBuilder(const FString &InDescription)
Definition TcpSocketBuilder.h:27
FTcpSocketBuilder Lingering(int32 Timeout)
Definition TcpSocketBuilder.h:152
FTcpSocketBuilder AsBlocking()
Definition TcpSocketBuilder.h:48
FTcpSocketBuilder AsNonBlocking()
Definition TcpSocketBuilder.h:61
FTcpSocketBuilder WithReceiveBufferSize(int32 SizeInBytes)
Definition TcpSocketBuilder.h:184
FTcpSocketBuilder BoundToPort(uint16 Port)
Definition TcpSocketBuilder.h:138
FTcpSocketBuilder AsReusable(bool bInReusable)
Definition TcpSocketBuilder.h:88
FTcpSocketBuilder Listening(int32 MaxBacklog)
Definition TcpSocketBuilder.h:166
FSocket * Build() const
Definition TcpSocketBuilder.h:225
FTcpSocketBuilder AsReusable()
Definition TcpSocketBuilder.h:74
FTcpSocketBuilder WithSendBufferSize(int32 SizeInBytes)
Definition TcpSocketBuilder.h:201
FTcpSocketBuilder BoundToAddress(const FIPv4Address &Address)
Definition TcpSocketBuilder.h:105
FTcpSocketBuilder BoundToEndpoint(const FIPv4Endpoint &Endpoint)
Definition TcpSocketBuilder.h:120
Definition SocketSubsystem.h:58
static SOCKETS_API ISocketSubsystem * Get(const FName &SubsystemName=NAME_None)
Definition SocketSubsystem.cpp:224
virtual FSocket * CreateSocket(const FName &SocketType, const FString &SocketDescription, bool bForceUDP=false)
Definition SocketSubsystem.h:98
virtual void DestroySocket(FSocket *Socket)=0
Definition SharedPointer.h:153
@ false
Definition radaudio_common.h:23
Definition IPv4Address.h:16
Definition IPv4Endpoint.h:27
TSharedRef< FInternetAddr > ToInternetAddr() const
Definition IPv4Endpoint.h:112
uint16 Port
Definition IPv4Endpoint.h:32
FIPv4Address Address
Definition IPv4Endpoint.h:29