MaxPortCountToTry

MaxPortCountToTry

#Overview

name: MaxPortCountToTry

The value of this variable can be defined or overridden in .ini config files. 2 .ini config files referencing this setting variable.

It is referenced in 4 C++ source files.

#Summary

#Usage in the C++ source code

The purpose of MaxPortCountToTry is to define the number of additional ports that will be attempted for binding if the initially specified port is unavailable. This setting is primarily used in network-related operations within Unreal Engine 5.

This setting variable is relied upon by multiple network-related subsystems and modules in Unreal Engine 5, including:

  1. OnlineSubsystemSteam
  2. OnlineSubsystemUtils
  3. SocketSubsystemEOS

The value of this variable is typically set in the configuration files of the respective network drivers. It’s defined as a UPROPERTY with the Config specifier in the UIpNetDriver class, which suggests it can be set in the engine or project configuration files.

MaxPortCountToTry interacts with the local address (LocalAddr) and is used in conjunction with socket binding operations. It’s often used in a loop or iterative process to attempt binding to successive ports if the initial attempt fails.

Developers must be aware that:

  1. This variable affects network initialization and connection processes.
  2. Setting it too high might result in longer connection times if many ports are unavailable.
  3. Setting it too low might lead to connection failures if the initially specified ports are already in use.

Best practices when using this variable include:

  1. Set it to a reasonable value that balances between connection reliability and performance.
  2. Consider the typical network environment of your target audience when configuring this value.
  3. Monitor and log port binding attempts to identify any recurring issues.
  4. Ensure that firewall settings on the target systems allow for the range of ports that might be used.

#Setting Variables

#References In INI files

Location: <Workspace>/Engine/Config/BaseEngine.ini:1643, section: [/Script/OnlineSubsystemUtils.IpNetDriver]

Location: <Workspace>/Engine/Plugins/Experimental/WebSocketNetworking/Config/BaseWebSocketNetDriver.ini:24, section: [/Script/WebSocketNetworking.WebSocketNetDriver]

#References in C++ code

#Callsites

This variable is referenced in the following C++ source code:

#Loc: <Workspace>/Engine/Plugins/Online/OnlineSubsystemSteam/Source/Private/SteamNetDriver.cpp:72

Scope (from outer to inner):

file
function     bool USteamNetDriver::InitBase

Source code excerpt:


	int32 AttemptPort = LocalAddr->GetPort();
	int32 BoundPort = SocketSubsystem->BindNextPort(GetSocket(), *LocalAddr, MaxPortCountToTry + 1, 1);
	UE_LOG(LogNet, Display, TEXT("%s bound to port %d"), *GetName(), BoundPort);
	// Success.
	return true;
}

bool USteamNetDriver::InitConnect(FNetworkNotify* InNotify, const FURL& ConnectURL, FString& Error)

#Loc: <Workspace>/Engine/Plugins/Online/OnlineSubsystemUtils/Source/OnlineSubsystemUtils/Classes/IpNetDriver.h:79

Scope (from outer to inner):

file
function     class ONLINESUBSYSTEMUTILS_API UIpNetDriver : public UNetDriver { friend class FPacketIterator; GENERATED_BODY

Source code excerpt:

	uint32 bExitOnBindFailure:1;

	/** Number of ports which will be tried if current one is not available for binding (i.e. if told to bind to port N, will try from N to N+MaxPortCountToTry inclusive) */
	UPROPERTY(Config)
	uint32 MaxPortCountToTry;

	/** If pausing socket receives, the time at which this should end */
	float PauseReceiveEnd;

	/** Base constructor */
	UIpNetDriver(const FObjectInitializer& ObjectInitializer);

#Loc: <Workspace>/Engine/Plugins/Online/OnlineSubsystemUtils/Source/OnlineSubsystemUtils/Private/IpNetDriver.cpp:841

Scope (from outer to inner):

file
function     FUniqueSocket UIpNetDriver::CreateAndBindSocket

Source code excerpt:


	int32 AttemptPort = BindAddr->GetPort();
	int32 BoundPort = SocketSubsystem->BindNextPort(NewSocket.Get(), *BindAddr, MaxPortCountToTry + 1, 1);
	if (BoundPort == 0)
	{
		Error = FString::Printf(TEXT("%s: binding to port %i failed (%i)"), SocketSubsystem->GetSocketAPIName(), AttemptPort,
			(int32)SocketSubsystem->GetLastErrorCode());

		if (bExitOnBindFailure)

#Loc: <Workspace>/Engine/Plugins/Online/SocketSubsystemEOS/Source/SocketSubsystemEOS/Private/NetDriverEOSBase.cpp:120

Scope (from outer to inner):

file
function     bool UNetDriverEOSBase::InitConnect

Source code excerpt:

	FSocketSubsystemEOS* const SocketSubsystem = static_cast<FSocketSubsystemEOS*>(GetSocketSubsystem());
	check(SocketSubsystem);
	if (!SocketSubsystem->BindNextPort(CurSocket, *LocalAddr, MaxPortCountToTry + 1, 1))
	{
		// Failure
		Error = TEXT("Could not bind local port");
		UE_LOG(LogTemp, Warning, TEXT("Could not bind local port in %d attempts"), MaxPortCountToTry);
		return false;
	}

	// Create an unreal connection to the server
	UNetConnectionEOS* Connection = NewObject<UNetConnectionEOS>(NetConnectionClass);
	check(Connection);