You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: MCPForUnity/Editor/Helpers/PortManager.cs
+13-7Lines changed: 13 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,10 @@ public class PortConfig
38
38
/// Get the port to use - either from storage or discover a new one
39
39
/// Will try stored port first, then fallback to discovering new port
40
40
/// </summary>
41
-
/// <returns>Port number to use</returns>
41
+
/// <summary>
42
+
/// Selects a TCP port for the current Unity project, preferring a previously saved project-specific port when it is valid and free; if the saved port is busy the method waits briefly for release and otherwise finds and persists an alternative.
43
+
/// </summary>
44
+
/// <returns>The port number to use. Returns the stored project port if it exists and is available (or becomes available after a short wait); otherwise returns a newly discovered available port which is saved for future use.</returns>
42
45
publicstaticintGetPortWithFallback()
43
46
{
44
47
// Try to load stored port first, but only if it's from the current project
@@ -60,14 +63,17 @@ public static int GetPortWithFallback()
60
63
if(IsDebugEnabled())Debug.Log($"<b><color=#2EA3FF>MCP-FOR-UNITY</color></b>: Stored port {storedConfig.unity_port} became available after short wait");
61
64
returnstoredConfig.unity_port;
62
65
}
63
-
// Prefer sticking to the same port; let the caller handle bind retries/fallbacks
64
-
returnstoredConfig.unity_port;
66
+
// Port is still busy after waiting - find a new available port instead
67
+
if(IsDebugEnabled())Debug.Log($"<b><color=#2EA3FF>MCP-FOR-UNITY</color></b>: Stored port {storedConfig.unity_port} is occupied by another instance, finding alternative...");
68
+
intnewPort=FindAvailablePort();
69
+
SavePort(newPort);
70
+
returnnewPort;
65
71
}
66
72
67
73
// If no valid stored port, find a new one and save it
/// Starts the MCPForUnity bridge: binds a local TCP listener, marks the bridge running, starts the background listener loop, registers editor update and lifecycle handlers, and emits an initial heartbeat.
302
+
/// </summary>
303
+
/// <remarks>
304
+
/// The method prefers a persisted per-project port and will attempt short retries; if the preferred port is occupied it will obtain an alternative port and continue. On success it sets bridge runtime state (including <c>isRunning</c> and <c>currentUnityPort</c>), initializes command handling, and schedules regular heartbeats. Errors encountered while binding the listener are logged.
/// Writes a per-project status JSON file for external monitoring containing bridge state and metadata.
1226
+
/// </summary>
1227
+
/// <param name="reloading">If true, indicates the editor is reloading; affects the default reason value.</param>
1228
+
/// <param name="reason">Optional custom reason to include in the status file. If null, defaults to "reloading" when <paramref name="reloading"/> is true, otherwise "ready".</param>
0 commit comments