Skip to content

Commit 0731409

Browse files
authored
Merge pull request #183 from immutable/feat/volstro-webview-multiple-instances
[DX-2771] feat: check for available ports when setting up uwb tcp
2 parents 981113a + b21df57 commit 0731409

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/Packages/Passport/Runtime/ThirdParty/UnityWebBrowser/Runtime/Communication/TCPCommunicationLayer.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// This project is under the MIT license. See the LICENSE.md file for more details.
77

88
using System.Net;
9+
using System.Net.NetworkInformation;
910
using UnityEngine;
1011
using VoltRpc.Communication;
1112
using VoltRpc.Communication.TCP;
@@ -32,6 +33,18 @@ public sealed class TCPCommunicationLayer : CommunicationLayer
3233
[Tooltip("The out port to communicate on")]
3334
public int outPort = 5556;
3435

36+
public TCPCommunicationLayer()
37+
{
38+
// If ports are not available, use different ports
39+
System.Random rnd = new System.Random();
40+
while (!CheckAvailableServerPort(inPort) || !CheckAvailableServerPort(outPort))
41+
{
42+
int port = rnd.Next(1024, 65353);
43+
inPort = port;
44+
outPort = port + 1;
45+
}
46+
}
47+
3548
public override Client CreateClient()
3649
{
3750
IPEndPoint ipEndPoint = new(IPAddress.Loopback, inPort);
@@ -50,6 +63,25 @@ public override void GetIpcSettings(out object outLocation, out object inLocatio
5063
inLocation = inPort;
5164
assemblyLocation = null;
5265
}
66+
67+
private bool CheckAvailableServerPort(int port) {
68+
Debug.Log($"Checking Port {port}");
69+
bool isAvailable = true;
70+
71+
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
72+
IPEndPoint[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpListeners();
73+
74+
foreach (IPEndPoint endpoint in tcpConnInfoArray) {
75+
if (endpoint.Port == port) {
76+
isAvailable = false;
77+
break;
78+
}
79+
}
80+
81+
Debug.Log($"Port {port} available = {isAvailable}");
82+
83+
return isAvailable;
84+
}
5385
}
5486
}
5587

0 commit comments

Comments
 (0)