diff --git a/com.unity.multiplayer.mlapi/Runtime/Transports/MultiplexTransportAdapter.cs b/com.unity.multiplayer.mlapi/Runtime/Transports/MultiplexTransportAdapter.cs index 892930159c..82e33de06f 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Transports/MultiplexTransportAdapter.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Transports/MultiplexTransportAdapter.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using MLAPI.Transports.Tasks; namespace MLAPI.Transports.Multiplex @@ -53,6 +54,21 @@ public enum ConnectionIdSpreadMethod private byte m_LastProcessedTransportIndex; + /// + public override string NetworkAddress + { + get => Transports.Any() ? Transports.First().NetworkAddress : default; + set => Array.ForEach(Transports, t => t.NetworkAddress = value); + } + + /// + public override ushort NetworkPort + { + get => Transports.Any() ? Transports.First().NetworkPort : default; + set => Array.ForEach(Transports, t => t.NetworkPort = value); + } + + public override bool IsSupported => true; public override void DisconnectLocalClient() @@ -327,4 +343,4 @@ public byte GetFirstSupportedTransportIndex() #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member } -} \ No newline at end of file +} diff --git a/com.unity.multiplayer.mlapi/Runtime/Transports/NetworkTransport.cs b/com.unity.multiplayer.mlapi/Runtime/Transports/NetworkTransport.cs index e3ef3b76c0..09a5554d6a 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Transports/NetworkTransport.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Transports/NetworkTransport.cs @@ -41,6 +41,19 @@ public abstract class NetworkTransport : MonoBehaviour /// public abstract ulong ServerClientId { get; } + /// + /// Gets or sets the IP address the client uses to connect to the server. + /// For transports which don't use an IP address to connect this property is used to pass a transport specific connection identifier such as a room name. + /// + public abstract string NetworkAddress { get; set; } + + /// + /// Gets or sets the port this transport should use for networking. + /// In server/host mode this is the port on which the server is exposed. + /// In client mode this is the port + /// + public abstract ushort NetworkPort { get; set; } + /// /// Gets a value indicating whether this is supported in the current runtime context. /// This is used by multiplex adapters. diff --git a/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/UNetTransport.cs b/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/UNetTransport.cs index 040b6d6440..245022c122 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/UNetTransport.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/UNetTransport.cs @@ -6,6 +6,7 @@ using MLAPI.Logging; using MLAPI.Profiling; using MLAPI.Transports.Tasks; +using UnityEngine; using UnityEngine.Networking; namespace MLAPI.Transports.UNET @@ -26,8 +27,10 @@ public enum SendMode public int MaxConnections = 100; public int MaxSentMessageQueueSize = 128; - public string ConnectAddress = "127.0.0.1"; - public int ConnectPort = 7777; + [SerializeField] + string m_ConnectAddress = "127.0.0.1"; + [SerializeField] + ushort m_ConnectPort = 7777; public int ServerListenPort = 7777; public int ServerWebsocketListenPort = 8887; public bool SupportWebsocket = false; @@ -64,6 +67,12 @@ public enum SendMode private SocketTask m_ConnectTask; public override ulong ServerClientId => GetMLAPIClientId(0, 0, true); + /// + public override string NetworkAddress { get { return m_ConnectAddress; } set { m_ConnectAddress = value; } } + + /// + public override ushort NetworkPort { get { return m_ConnectPort; } set { m_ConnectPort = value; } } + protected void LateUpdate() { if (UnityEngine.Networking.NetworkTransport.IsStarted && MessageSendMode == SendMode.Queued) @@ -143,7 +152,6 @@ public override void Send(ulong clientId, ArraySegment data, NetworkChanne } } - public void SendQueued(ulong clientId) { if (ProfilerEnabled) @@ -161,9 +169,11 @@ public override NetworkEvent PollEvent(out ulong clientId, out NetworkChannel ne var eventType = RelayTransport.Receive(out int hostId, out int connectionId, out int channelId, m_MessageBuffer, m_MessageBuffer.Length, out int receivedSize, out byte error); clientId = GetMLAPIClientId((byte)hostId, (ushort)connectionId, false); + receiveTime = UnityEngine.Time.realtimeSinceStartup; var networkError = (NetworkError)error; + if (networkError == NetworkError.MessageToLong) { byte[] tempBuffer; @@ -254,7 +264,7 @@ public override SocketTasks StartClient() var socketTask = SocketTask.Working; m_ServerHostId = RelayTransport.AddHost(new HostTopology(GetConfig(), 1), false); - m_ServerConnectionId = RelayTransport.Connect(m_ServerHostId, ConnectAddress, ConnectPort, 0, out byte error); + m_ServerConnectionId = RelayTransport.Connect(m_ServerHostId, NetworkAddress, NetworkPort, 0, out byte error); var connectError = (NetworkError)error; @@ -472,4 +482,4 @@ public IReadOnlyDictionary GetTransportProfilerData() } } #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member -#pragma warning restore 618 \ No newline at end of file +#pragma warning restore 618 diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/RpcQueueTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/RpcQueueTests.cs index db9eae713f..8c7b950ca2 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/RpcQueueTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/RpcQueueTests.cs @@ -37,8 +37,8 @@ public IEnumerator RpcQueueUnitTest() AllowRuntimeSceneChanges = true, EnableSceneManagement = false }; - unetTransport.ConnectAddress = "127.0.0.1"; - unetTransport.ConnectPort = 7777; + unetTransport.NetworkAddress = "127.0.0.1"; + unetTransport.NetworkPort = 7777; unetTransport.ServerListenPort = 7777; unetTransport.MessageBufferSize = 65535; unetTransport.MaxConnections = 100; @@ -107,4 +107,4 @@ public IEnumerator RpcQueueUnitTest() #endif } } -} \ No newline at end of file +}