Skip to content

Commit

Permalink
Added command line option support for overriding the port
Browse files Browse the repository at this point in the history
  • Loading branch information
samlucas-unity committed Sep 20, 2023
1 parent ca577a6 commit 70158c1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
39 changes: 39 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@ public NetworkPrefabHandler PrefabHandler
internal NetworkConnectionManager ConnectionManager = new NetworkConnectionManager();
internal NetworkMessageManager MessageManager = null;

internal struct Override<T>
{
private T m_Value;
public bool Overidden { get; private set; }
internal T Value { get { return Overidden ? m_Value : default(T); }
set { Overidden = true; m_Value = value; } }
};

internal Override<ushort> PortOverride;

#if UNITY_EDITOR
internal static INetworkManagerHelper NetworkManagerHelper;

Expand Down Expand Up @@ -643,6 +653,8 @@ internal void Initialize(bool server)
return;
}

ParseCommandLineOptions();

if (NetworkConfig.NetworkTransport == null)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
Expand Down Expand Up @@ -1085,5 +1097,32 @@ private void OnDestroy()
Singleton = null;
}
}

// Command line options
private const string k_OverridePortArg = "-port";

private string GetArg( string[] commandLineArgs, string arg )
{
var argIndex = Array.IndexOf(commandLineArgs, arg);
if (argIndex >= 0 && argIndex < commandLineArgs.Length - 1 )
{
return commandLineArgs[argIndex + 1];
}

return null;
}

private void ParseArg<T>(string arg, ref Override<T> value)
{
if (GetArg(Environment.GetCommandLineArgs(), arg) is string argValue)
{
value.Value = (T)Convert.ChangeType(argValue, typeof(T));
}
}

private void ParseCommandLineOptions()
{
ParseArg(k_OverridePortArg, ref PortOverride);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ public override NetworkEvent PollEvent(out ulong clientId, out ArraySegment<byte
public override bool StartClient()
{
m_ServerHostId = UnityEngine.Networking.NetworkTransport.AddHost(new HostTopology(GetConfig(), 1), 0, null);
m_ServerConnectionId = UnityEngine.Networking.NetworkTransport.Connect(m_ServerHostId, ConnectAddress, ConnectPort, 0, out byte error);
m_ServerConnectionId = UnityEngine.Networking.NetworkTransport.Connect(m_ServerHostId, ConnectAddress, GetConnectPort(), 0, out byte error);
return (NetworkError)error == NetworkError.Ok;
}

public override bool StartServer()
{
var topology = new HostTopology(GetConfig(), MaxConnections);
// Undocumented, but AddHost returns -1 in case of any type of failure. See UNET::NetLibraryManager::AddHost
return -1 != UnityEngine.Networking.NetworkTransport.AddHost(topology, ServerListenPort, null);
return -1 != UnityEngine.Networking.NetworkTransport.AddHost(topology, GetServerListenPort(), null);
}

public override void DisconnectRemoteClient(ulong clientId)
Expand Down Expand Up @@ -281,6 +281,26 @@ private ConnectionConfig GetConfig()

return connectionConfig;
}

public int GetConnectPort()
{
if ( NetworkManager && NetworkManager.PortOverride.Overidden )
{
return NetworkManager.PortOverride.Value;
}

return ConnectPort;
}

public int GetServerListenPort()
{
if (NetworkManager && NetworkManager.PortOverride.Overidden)
{
return NetworkManager.PortOverride.Value;
}

return ServerListenPort;
}
}
}
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,11 @@ public override void Initialize(NetworkManager networkManager = null)

NetworkManager = networkManager;

if ( NetworkManager && NetworkManager.PortOverride.Overidden )
{
ConnectionData.Port = NetworkManager.PortOverride.Value;
}

m_RealTimeProvider = NetworkManager ? NetworkManager.RealTimeProvider : new RealTimeProvider();

m_NetworkSettings = new NetworkSettings(Allocator.Persistent);
Expand Down

0 comments on commit 70158c1

Please sign in to comment.