From 5132fa8bfc7a955c21737c5d3abd8711f038c07a Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Tue, 12 Dec 2023 04:23:35 -0500 Subject: [PATCH] feat(HUD): Support for MultiplexTransport Port (#3662) * Moved Multiplex to Core * Updated Available and ToString in Transports - Available now uses `#if UNITY_WEBGL` because `Application.platform != RuntimePlatform.WebGLPlayer;` doesn't work in Editor - ToString was shortened and includes port for presentation in HUD * NetworkManagerHUD: Added support for MultiplexTransport * Moved Multiplex back to Transports * Implemented PortTransport in Multiplex * NetworkManagerHUD: reverted width change * Revamped Port handler * Changed LogError to LogWarning in Port setter * Moved ToString change to separate PR * Use Utils.IsHeadless * Use Utils.IsDebug * Removed console logging --- .../Multiplex/MultiplexTransport.cs | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs b/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs index 7690e5970..c968bf26e 100644 --- a/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs +++ b/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs @@ -7,7 +7,7 @@ namespace Mirror { // a transport that can listen to multiple underlying transport at the same time [DisallowMultipleComponent] - public class MultiplexTransport : Transport + public class MultiplexTransport : Transport, PortTransport { public Transport[] transports; @@ -42,6 +42,44 @@ public class MultiplexTransport : Transport // next multiplexed id counter. start at 1 because 0 is reserved for host. int nextMultiplexedId = 1; + // prevent log flood from OnGUI or similar per-frame updates + bool alreadyWarned; + + public ushort Port + { + get + { + foreach (Transport transport in transports) + if (transport.Available() && transport is PortTransport portTransport) + return portTransport.Port; + + return 0; + } + set + { + if (Utils.IsHeadless() && !alreadyWarned) + { + // prevent log flood from OnGUI or similar per-frame updates + alreadyWarned = true; + Debug.LogWarning($"MultiplexTransport: Server cannot set the same listen port for all transports! Set them directly instead."); + } + + else + { + // We can't set the same port for all transports because + // listen ports have to be different for each transport + // so we just set the first available one. + // This depends on the selected build platform. + foreach (Transport transport in transports) + if (transport.Available() && transport is PortTransport portTransport) + { + portTransport.Port = value; + break; + } + } + } + } + // add to bidirection lookup. returns the multiplexed connectionId. public int AddToLookup(int originalConnectionId, int transportIndex) {