diff --git a/Assets/Mirror/Components/Discovery/NetworkDiscovery.cs b/Assets/Mirror/Components/Discovery/NetworkDiscovery.cs index cc9f1167b..23a5a3e58 100644 --- a/Assets/Mirror/Components/Discovery/NetworkDiscovery.cs +++ b/Assets/Mirror/Components/Discovery/NetworkDiscovery.cs @@ -14,27 +14,6 @@ public class NetworkDiscovery : NetworkDiscoveryBase /// Process the request from a client /// @@ -68,9 +47,11 @@ protected override ServerResponse ProcessRequest(ServerRequest request, IPEndPoi throw; } } + #endregion #region Client + /// /// Create a message that will be broadcasted on the network to discover servers /// @@ -106,6 +87,7 @@ protected override void ProcessResponse(ServerResponse response, IPEndPoint endp OnServerFound.Invoke(response); } + #endregion } } diff --git a/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs b/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs index de74fbdc5..432316ea9 100644 --- a/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs +++ b/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs @@ -23,33 +23,46 @@ public abstract class NetworkDiscoveryBase : MonoBehaviour { public static bool SupportedOnThisPlatform { get { return Application.platform != RuntimePlatform.WebGLPlayer; } } - // each game should have a random unique handshake, this way you can tell if this is the same game or not - [HideInInspector] - public long secretHandshake; + [SerializeField] + [Tooltip("If true, broadcasts a discovery request every ActiveDiscoveryInterval seconds")] + public bool enableActiveDiscovery = true; + + // broadcast address needs to be configurable on iOS: + // https://github.com/vis2k/Mirror/pull/3255 + [Tooltip("iOS may require LAN IP address here (e.g. 192.168.x.x), otherwise leave blank.")] + public string BroadcastAddress = ""; [SerializeField] [Tooltip("The UDP port the server will listen for multi-cast messages")] protected int serverBroadcastListenPort = 47777; - [SerializeField] - [Tooltip("If true, broadcasts a discovery request every ActiveDiscoveryInterval seconds")] - public bool enableActiveDiscovery = true; - [SerializeField] [Tooltip("Time in seconds between multi-cast messages")] [Range(1, 60)] float ActiveDiscoveryInterval = 3; - // broadcast address needs to be configurable on iOS: - // https://github.com/vis2k/Mirror/pull/3255 - public string BroadcastAddress = ""; + [Tooltip("Transport to be advertised during discovery")] + public Transport transport; + + [Tooltip("Invoked when a server is found")] + public ServerFoundUnityEvent OnServerFound; + + // Each game should have a random unique handshake, + // this way you can tell if this is the same game or not + [HideInInspector] + public long secretHandshake; + + public long ServerId { get; private set; } protected UdpClient serverUdpClient; protected UdpClient clientUdpClient; #if UNITY_EDITOR - void OnValidate() + public virtual void OnValidate() { + if (transport == null) + transport = GetComponent(); + if (secretHandshake == 0) { secretHandshake = RandomLong(); @@ -58,24 +71,32 @@ void OnValidate() } #endif - public static long RandomLong() - { - int value1 = UnityEngine.Random.Range(int.MinValue, int.MaxValue); - int value2 = UnityEngine.Random.Range(int.MinValue, int.MaxValue); - return value1 + ((long)value2 << 32); - } - /// /// virtual so that inheriting classes' Start() can call base.Start() too /// public virtual void Start() { + ServerId = RandomLong(); + + // active transport gets initialized in Awake + // so make sure we set it here in Start() after Awake + // Or just let the user assign it in the inspector + if (transport == null) + transport = Transport.active; + // Server mode? then start advertising #if UNITY_SERVER AdvertiseServer(); #endif } + public static long RandomLong() + { + int value1 = UnityEngine.Random.Range(int.MinValue, int.MaxValue); + int value2 = UnityEngine.Random.Range(int.MinValue, int.MaxValue); + return value1 + ((long)value2 << 32); + } + // Ensure the ports are cleared no matter when Game/Unity UI exits void OnApplicationQuit() { @@ -166,9 +187,7 @@ public async Task ServerListenAsync() // socket has been closed break; } - catch (Exception) - { - } + catch (Exception) {} } } @@ -247,6 +266,7 @@ protected virtual void ProcessClientRequest(Request request, IPEndPoint endpoint AndroidJavaObject multicastLock; bool hasMulticastLock; #endif + void BeginMulticastLock() { #if UNITY_ANDROID