From 3cbaa58caf68bdb6f5a61d3b69eff561d43f712f Mon Sep 17 00:00:00 2001 From: mischa Date: Thu, 4 Jul 2024 09:59:24 +0200 Subject: [PATCH] things --- Assets/Mirror/Core/LocalConnectionToClient.cs | 4 +-- .../Mirror/Core/NetworkConnectionToClient.cs | 24 ++++++++++++++- Assets/Mirror/Core/NetworkServer.cs | 7 +++-- Assets/Mirror/Core/Transport.cs | 8 ++++- .../Common/FakeNetworkConnectionToClient.cs | 3 +- Assets/Mirror/Tests/Common/MemoryTransport.cs | 2 +- .../InterestManagementTests_Common.cs | 4 +-- .../NetworkConnectionToClientTests.cs | 4 +-- .../NetworkIdentity/NetworkIdentityTests.cs | 20 ++++++------- .../Editor/NetworkServer/NetworkServerTest.cs | 30 +++++++++---------- .../Transports/MiddlewareTransportTest.cs | 6 ++-- .../Transports/MultiplexTransportTest.cs | 10 +++---- .../Tests/Runtime/NetworkServerRuntimeTest.cs | 2 +- .../Edgegap/EdgegapRelay/EdgegapKcpServer.cs | 2 +- .../EdgegapRelay/EdgegapKcpTransport.cs | 2 +- .../Encryption/EncryptionTransport.cs | 4 +-- Assets/Mirror/Transports/KCP/KcpTransport.cs | 2 +- .../KCP/kcp2k/highlevel/KcpServer.cs | 8 +++-- .../Multiplex/MultiplexTransport.cs | 12 ++++---- .../SimpleWeb/Server/SimpleWebServer.cs | 5 ++-- .../Transports/Telepathy/Telepathy/Server.cs | 21 +++++++++---- .../Telepathy/TelepathyTransport.cs | 2 +- .../Transports/Threaded/ThreadedTransport.cs | 15 ++++++---- 23 files changed, 119 insertions(+), 78 deletions(-) diff --git a/Assets/Mirror/Core/LocalConnectionToClient.cs b/Assets/Mirror/Core/LocalConnectionToClient.cs index c41c6ae29..f0aac1187 100644 --- a/Assets/Mirror/Core/LocalConnectionToClient.cs +++ b/Assets/Mirror/Core/LocalConnectionToClient.cs @@ -12,9 +12,7 @@ public class LocalConnectionToClient : NetworkConnectionToClient // packet queue internal readonly Queue queue = new Queue(); - public LocalConnectionToClient() : base(LocalConnectionId) {} - - public override string address => "localhost"; + public LocalConnectionToClient() : base(LocalConnectionId, "localhost") {} internal override void Send(ArraySegment segment, int channelId = Channels.Reliable) { diff --git a/Assets/Mirror/Core/NetworkConnectionToClient.cs b/Assets/Mirror/Core/NetworkConnectionToClient.cs index 361b00c34..a9771331e 100644 --- a/Assets/Mirror/Core/NetworkConnectionToClient.cs +++ b/Assets/Mirror/Core/NetworkConnectionToClient.cs @@ -7,6 +7,8 @@ namespace Mirror { public class NetworkConnectionToClient : NetworkConnection { + // connection address, passed from transport + // rpcs are collected in a buffer, and then flushed out together. // this way we don't need one NetworkMessage per rpc. // => prepares for LocalWorldState as well. @@ -14,7 +16,7 @@ public class NetworkConnectionToClient : NetworkConnection readonly NetworkWriter reliableRpcs = new NetworkWriter(); readonly NetworkWriter unreliableRpcs = new NetworkWriter(); - public virtual string address => Transport.active.ServerGetClientAddress(connectionId); + public string address { get; private set; } /// NetworkIdentities that this connection can see // TODO move to server's NetworkConnectionToClient? @@ -50,9 +52,29 @@ public class NetworkConnectionToClient : NetworkConnection /// Round trip time (in seconds) that it takes a message to go server->client->server. public double rtt => _rtt.Value; + public NetworkConnectionToClient(int networkConnectionId, string address) + : base(networkConnectionId) + { + this.address = address; + + // initialize EMA with 'emaDuration' seconds worth of history. + // 1 second holds 'sendRate' worth of values. + // multiplied by emaDuration gives n-seconds. + driftEma = new ExponentialMovingAverage(NetworkServer.sendRate * NetworkClient.snapshotSettings.driftEmaDuration); + deliveryTimeEma = new ExponentialMovingAverage(NetworkServer.sendRate * NetworkClient.snapshotSettings.deliveryTimeEmaDuration); + + // buffer limit should be at least multiplier to have enough in there + snapshotBufferSizeLimit = Mathf.Max((int)NetworkClient.snapshotSettings.bufferTimeMultiplier, snapshotBufferSizeLimit); + } + + // keep the old contstructor for a while in order to not break all projects. + // DEPRECATED 2024-05-16 + [Obsolete("'new NetworkConnection(connectionId)' constructor was changed to 'new NetworkConnection(connectionId, address)'")] public NetworkConnectionToClient(int networkConnectionId) : base(networkConnectionId) { + this.address = Transport.active.ServerGetClientAddress(connectionId); + // initialize EMA with 'emaDuration' seconds worth of history. // 1 second holds 'sendRate' worth of values. // multiplied by emaDuration gives n-seconds. diff --git a/Assets/Mirror/Core/NetworkServer.cs b/Assets/Mirror/Core/NetworkServer.cs index 716a0fe47..5e31f3b9a 100644 --- a/Assets/Mirror/Core/NetworkServer.cs +++ b/Assets/Mirror/Core/NetworkServer.cs @@ -634,8 +634,9 @@ public static void SendToReadyObservers(NetworkIdentity identity, T message, } // transport events //////////////////////////////////////////////////// - // called by transport - static void OnTransportConnected(int connectionId) + // called by transport. + // address is the (IP) address without port. useful for IP bans etc. + static void OnTransportConnected(int connectionId, string address) { // Debug.Log($"Server accepted client:{connectionId}"); @@ -665,7 +666,7 @@ static void OnTransportConnected(int connectionId) if (connections.Count < maxConnections) { // add connection - NetworkConnectionToClient conn = new NetworkConnectionToClient(connectionId); + NetworkConnectionToClient conn = new NetworkConnectionToClient(connectionId, address); OnConnected(conn); } else diff --git a/Assets/Mirror/Core/Transport.cs b/Assets/Mirror/Core/Transport.cs index a20d84f78..1de22030f 100644 --- a/Assets/Mirror/Core/Transport.cs +++ b/Assets/Mirror/Core/Transport.cs @@ -3,6 +3,9 @@ // Connecting: // * Transports are responsible to call either OnConnected || OnDisconnected // in a certain time after a Connect was called. It can not end in limbo. +// * OnConnected should always pass the connection address. +// This makes threaded transports easier. +// Otherwise we would need to keep a main thread connections copy. // // Disconnecting: // * Connections might disconnect voluntarily by the other end. @@ -67,7 +70,8 @@ public abstract class Transport : MonoBehaviour // server ////////////////////////////////////////////////////////////// /// Called by Transport when a new client connected to the server. - public Action OnServerConnected; + // parameters: to pass address directly instead of having a separate ServerGetClientAddress() function. + public Action OnServerConnected; /// Called by Transport when the server received a message from a client. public Action, int> OnServerDataReceived; @@ -131,6 +135,8 @@ public virtual void ClientConnect(Uri uri) /// Get a client's address on the server. // Can be useful for Game Master IP bans etc. + // DEPRECATED 2024-05-16 + [Obsolete("Transport.ServerGetClientAddress() was deprecated. Each connection's address is now passed only once in OnServerConnected instead.")] public abstract string ServerGetClientAddress(int connectionId); /// Stop listening and disconnect all connections. diff --git a/Assets/Mirror/Tests/Common/FakeNetworkConnectionToClient.cs b/Assets/Mirror/Tests/Common/FakeNetworkConnectionToClient.cs index 7188bb98c..ad16ccac8 100644 --- a/Assets/Mirror/Tests/Common/FakeNetworkConnectionToClient.cs +++ b/Assets/Mirror/Tests/Common/FakeNetworkConnectionToClient.cs @@ -4,8 +4,7 @@ namespace Mirror.Tests { public class FakeNetworkConnectionToClient : NetworkConnectionToClient { - public FakeNetworkConnectionToClient() : base(1) {} - public override string address => "Test"; + public FakeNetworkConnectionToClient() : base(1, "localhost") {} public override void Disconnect() {} internal override void Send(ArraySegment segment, int channelId = 0) {} } diff --git a/Assets/Mirror/Tests/Common/MemoryTransport.cs b/Assets/Mirror/Tests/Common/MemoryTransport.cs index 82760a161..b29fb6683 100644 --- a/Assets/Mirror/Tests/Common/MemoryTransport.cs +++ b/Assets/Mirror/Tests/Common/MemoryTransport.cs @@ -201,7 +201,7 @@ public override void ServerEarlyUpdate() case EventType.Connected: Debug.Log("MemoryTransport Server Message: Connected"); // event might be null in tests if no NetworkClient is used. - OnServerConnected?.Invoke(message.connectionId); + OnServerConnected?.Invoke(message.connectionId, "localhost"); break; case EventType.Data: Debug.Log($"MemoryTransport Server Message: Data: {BitConverter.ToString(message.data)}"); diff --git a/Assets/Mirror/Tests/Editor/InterestManagement/InterestManagementTests_Common.cs b/Assets/Mirror/Tests/Editor/InterestManagement/InterestManagementTests_Common.cs index a3ccb6f4b..0be36ccf1 100644 --- a/Assets/Mirror/Tests/Editor/InterestManagement/InterestManagementTests_Common.cs +++ b/Assets/Mirror/Tests/Editor/InterestManagement/InterestManagementTests_Common.cs @@ -21,7 +21,7 @@ public override void SetUp() // A with connectionId = 0x0A, netId = 0xAA CreateNetworked(out gameObjectA, out identityA); - connectionA = new NetworkConnectionToClient(0x0A); + connectionA = new NetworkConnectionToClient(0x0A, ""); connectionA.isAuthenticated = true; connectionA.isReady = true; connectionA.identity = identityA; @@ -29,7 +29,7 @@ public override void SetUp() // B CreateNetworked(out gameObjectB, out identityB); - connectionB = new NetworkConnectionToClient(0x0B); + connectionB = new NetworkConnectionToClient(0x0B, ""); connectionB.isAuthenticated = true; connectionB.isReady = true; connectionB.identity = identityB; diff --git a/Assets/Mirror/Tests/Editor/NetworkConnection/NetworkConnectionToClientTests.cs b/Assets/Mirror/Tests/Editor/NetworkConnection/NetworkConnectionToClientTests.cs index 75b349c92..b4f3b66e1 100644 --- a/Assets/Mirror/Tests/Editor/NetworkConnection/NetworkConnectionToClientTests.cs +++ b/Assets/Mirror/Tests/Editor/NetworkConnection/NetworkConnectionToClientTests.cs @@ -34,7 +34,7 @@ public override void TearDown() public void Send_BatchesUntilUpdate() { // create connection and send - NetworkConnectionToClient connection = new NetworkConnectionToClient(42); + NetworkConnectionToClient connection = new NetworkConnectionToClient(42, ""); NetworkTime.PingInterval = float.MaxValue; // disable ping for this test byte[] message = {0x01, 0x02}; connection.Send(new ArraySegment(message)); @@ -63,7 +63,7 @@ public void SendBatchingResetsPreviousWriter() const int BatchHeader = 8; // create connection - NetworkConnectionToClient connection = new NetworkConnectionToClient(42); + NetworkConnectionToClient connection = new NetworkConnectionToClient(42, ""); NetworkTime.PingInterval = float.MaxValue; // disable ping for this test // send and update big message diff --git a/Assets/Mirror/Tests/Editor/NetworkIdentity/NetworkIdentityTests.cs b/Assets/Mirror/Tests/Editor/NetworkIdentity/NetworkIdentityTests.cs index e6632a5c5..e674b4f4e 100644 --- a/Assets/Mirror/Tests/Editor/NetworkIdentity/NetworkIdentityTests.cs +++ b/Assets/Mirror/Tests/Editor/NetworkIdentity/NetworkIdentityTests.cs @@ -123,11 +123,11 @@ public void RemoveObserver() identity.OnStartServer(); // add an observer connection - NetworkConnectionToClient connection = new NetworkConnectionToClient(42); + NetworkConnectionToClient connection = new NetworkConnectionToClient(42, ""); identity.observers[connection.connectionId] = connection; // RemoveObserver with invalid connection should do nothing - identity.RemoveObserver(new NetworkConnectionToClient(43)); + identity.RemoveObserver(new NetworkConnectionToClient(43, "")); Assert.That(identity.observers.Count, Is.EqualTo(1)); // RemoveObserver with existing connection should remove it @@ -324,7 +324,7 @@ public void AssignAndRemoveClientAuthority() // another connection // error log is expected LogAssert.ignoreFailingMessages = true; - result = identity.AssignClientAuthority(new NetworkConnectionToClient(43)); + result = identity.AssignClientAuthority(new NetworkConnectionToClient(43, "")); LogAssert.ignoreFailingMessages = false; Assert.That(result, Is.False); Assert.That(identity.connectionToClient, Is.EqualTo(owner)); @@ -552,8 +552,8 @@ public void AddObserver() CreateNetworked(out GameObject _, out NetworkIdentity identity); // create some connections - NetworkConnectionToClient connection1 = new NetworkConnectionToClient(42); - NetworkConnectionToClient connection2 = new NetworkConnectionToClient(43); + NetworkConnectionToClient connection1 = new NetworkConnectionToClient(42, ""); + NetworkConnectionToClient connection2 = new NetworkConnectionToClient(43, ""); // call AddObservers identity.AddObserver(connection1); @@ -565,7 +565,7 @@ public void AddObserver() Assert.That(identity.observers[connection2.connectionId], Is.EqualTo(connection2)); // adding a duplicate connectionId shouldn't overwrite the original - NetworkConnectionToClient duplicate = new NetworkConnectionToClient(connection1.connectionId); + NetworkConnectionToClient duplicate = new NetworkConnectionToClient(connection1.connectionId, ""); identity.AddObserver(duplicate); Assert.That(identity.observers.Count, Is.EqualTo(2)); Assert.That(identity.observers.ContainsKey(connection1.connectionId)); @@ -583,8 +583,8 @@ public void ClearObservers() identity.OnStartServer(); // add some observers - identity.observers[42] = new NetworkConnectionToClient(42); - identity.observers[43] = new NetworkConnectionToClient(43); + identity.observers[42] = new NetworkConnectionToClient(42, ""); + identity.observers[43] = new NetworkConnectionToClient(43, ""); // call ClearObservers identity.ClearObservers(); @@ -632,9 +632,9 @@ public void ResetState() identity.isClient = true; // creates .observers and generates a netId identity.OnStartServer(); - identity.connectionToClient = new NetworkConnectionToClient(1); + identity.connectionToClient = new NetworkConnectionToClient(1, ""); identity.connectionToServer = new NetworkConnectionToServer(); - identity.observers[43] = new NetworkConnectionToClient(2); + identity.observers[43] = new NetworkConnectionToClient(2, ""); // mark for reset and reset identity.ResetState(); diff --git a/Assets/Mirror/Tests/Editor/NetworkServer/NetworkServerTest.cs b/Assets/Mirror/Tests/Editor/NetworkServer/NetworkServerTest.cs index 0c8b77788..1cb06c86f 100644 --- a/Assets/Mirror/Tests/Editor/NetworkServer/NetworkServerTest.cs +++ b/Assets/Mirror/Tests/Editor/NetworkServer/NetworkServerTest.cs @@ -67,11 +67,11 @@ public void MaxConnections() Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // connect first: should work - transport.OnServerConnected.Invoke(42); + transport.OnServerConnected.Invoke(42, ""); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); // connect second: should fail - transport.OnServerConnected.Invoke(43); + transport.OnServerConnected.Invoke(43, ""); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); } @@ -84,7 +84,7 @@ public void OnConnectedEventCalled() // listen & connect NetworkServer.Listen(1); - transport.OnServerConnected.Invoke(42); + transport.OnServerConnected.Invoke(42, ""); Assert.That(connectCalled, Is.True); } @@ -97,7 +97,7 @@ public void OnDisconnectedEventCalled() // listen & connect NetworkServer.Listen(1); - transport.OnServerConnected.Invoke(42); + transport.OnServerConnected.Invoke(42, ""); // disconnect transport.OnServerDisconnected.Invoke(42); @@ -112,12 +112,12 @@ public void ConnectionsDict() Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // connect first - transport.OnServerConnected.Invoke(42); + transport.OnServerConnected.Invoke(42, ""); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); Assert.That(NetworkServer.connections.ContainsKey(42), Is.True); // connect second - transport.OnServerConnected.Invoke(43); + transport.OnServerConnected.Invoke(43, ""); Assert.That(NetworkServer.connections.Count, Is.EqualTo(2)); Assert.That(NetworkServer.connections.ContainsKey(43), Is.True); @@ -144,7 +144,7 @@ public void OnConnectedOnlyAllowsNonZeroConnectionIds() // connect with connectionId == 0 should fail // (it will show an error message, which is expected) LogAssert.ignoreFailingMessages = true; - transport.OnServerConnected.Invoke(0); + transport.OnServerConnected.Invoke(0, ""); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); LogAssert.ignoreFailingMessages = false; } @@ -156,12 +156,12 @@ public void ConnectDuplicateConnectionIds() NetworkServer.Listen(2); // connect first - transport.OnServerConnected.Invoke(42); + transport.OnServerConnected.Invoke(42, ""); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); NetworkConnectionToClient original = NetworkServer.connections[42]; // connect duplicate - shouldn't overwrite first one - transport.OnServerConnected.Invoke(42); + transport.OnServerConnected.Invoke(42, ""); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); Assert.That(NetworkServer.connections[42], Is.EqualTo(original)); } @@ -230,13 +230,13 @@ public void AddConnection() NetworkServer.Listen(1); // add first connection - NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42); + NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42, ""); Assert.That(NetworkServer.AddConnection(conn42), Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); // add second connection - NetworkConnectionToClient conn43 = new NetworkConnectionToClient(43); + NetworkConnectionToClient conn43 = new NetworkConnectionToClient(43, ""); Assert.That(NetworkServer.AddConnection(conn43), Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(2)); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); @@ -250,13 +250,13 @@ public void AddConnection_PreventsDuplicates() NetworkServer.Listen(1); // add a connection - NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42); + NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42, ""); Assert.That(NetworkServer.AddConnection(conn42), Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); // add duplicate connectionId - NetworkConnectionToClient connDup = new NetworkConnectionToClient(42); + NetworkConnectionToClient connDup = new NetworkConnectionToClient(42, ""); Assert.That(NetworkServer.AddConnection(connDup), Is.False); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); @@ -269,7 +269,7 @@ public void RemoveConnection() NetworkServer.Listen(1); // add connection - NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42); + NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42, ""); Assert.That(NetworkServer.AddConnection(conn42), Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); @@ -285,7 +285,7 @@ public void DisconnectAllTest_RemoteConnection() NetworkServer.Listen(1); // add connection - NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42); + NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42, ""); NetworkServer.AddConnection(conn42); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); diff --git a/Assets/Mirror/Tests/Editor/Transports/MiddlewareTransportTest.cs b/Assets/Mirror/Tests/Editor/Transports/MiddlewareTransportTest.cs index 32af6cbf7..8a6b089e7 100644 --- a/Assets/Mirror/Tests/Editor/Transports/MiddlewareTransportTest.cs +++ b/Assets/Mirror/Tests/Editor/Transports/MiddlewareTransportTest.cs @@ -298,7 +298,7 @@ public void TestClientExceptionCallback() public void TestServerConnectedCallback(int id) { int called = 0; - middleware.OnServerConnected = (i) => + middleware.OnServerConnected = (i, addr) => { called++; Assert.That(i, Is.EqualTo(id)); @@ -306,10 +306,10 @@ public void TestServerConnectedCallback(int id) // start to give callback to inner middleware.ServerStart(); - inner.OnServerConnected.Invoke(id); + inner.OnServerConnected.Invoke(id, ""); Assert.That(called, Is.EqualTo(1)); - inner.OnServerConnected.Invoke(id); + inner.OnServerConnected.Invoke(id, ""); Assert.That(called, Is.EqualTo(2)); } diff --git a/Assets/Mirror/Tests/Editor/Transports/MultiplexTransportTest.cs b/Assets/Mirror/Tests/Editor/Transports/MultiplexTransportTest.cs index c4609f4bf..700454866 100644 --- a/Assets/Mirror/Tests/Editor/Transports/MultiplexTransportTest.cs +++ b/Assets/Mirror/Tests/Editor/Transports/MultiplexTransportTest.cs @@ -238,7 +238,7 @@ public void TestServerConnected() ArraySegment segment = new ArraySegment(data); // on connect, send a message back - void SendMessage(int connectionId) + void SendMessage(int connectionId, string address) { transport.ServerSend(connectionId, segment, 5); } @@ -247,7 +247,7 @@ void SendMessage(int connectionId) transport.OnServerConnected = SendMessage; transport.ServerStart(); - transport1.OnServerConnected.Invoke(1); + transport1.OnServerConnected.Invoke(1, ""); transport1.Received().ServerSend(1, segment, 5); } @@ -260,14 +260,14 @@ public void TestServerSend() transport.ServerStart(); transport.ClientConnect("some.server.com"); - transport.OnServerConnected = _ => {}; + transport.OnServerConnected = (connId, address) => {}; transport.OnServerDisconnected = _ => {}; // connect two connectionIds. // one of them very large to prevent // https://github.com/vis2k/Mirror/issues/3280 - transport1.OnServerConnected(10); - transport2.OnServerConnected(int.MaxValue); + transport1.OnServerConnected(10, ""); + transport2.OnServerConnected(int.MaxValue, ""); byte[] data = { 1, 2, 3 }; ArraySegment segment = new ArraySegment(data); diff --git a/Assets/Mirror/Tests/Runtime/NetworkServerRuntimeTest.cs b/Assets/Mirror/Tests/Runtime/NetworkServerRuntimeTest.cs index 3587003a6..186fb1ed0 100644 --- a/Assets/Mirror/Tests/Runtime/NetworkServerRuntimeTest.cs +++ b/Assets/Mirror/Tests/Runtime/NetworkServerRuntimeTest.cs @@ -95,7 +95,7 @@ public IEnumerator DisconnectTimeoutTest() NetworkServer.disconnectInactiveTimeout = 1; GameObject remotePlayer = new GameObject("RemotePlayer", typeof(NetworkIdentity)); - NetworkConnectionToClient remoteConnection = new NetworkConnectionToClient(1); + NetworkConnectionToClient remoteConnection = new NetworkConnectionToClient(1, "localhost"); NetworkServer.OnConnected(remoteConnection); NetworkServer.AddPlayerForConnection(remoteConnection, remotePlayer); diff --git a/Assets/Mirror/Transports/Edgegap/EdgegapRelay/EdgegapKcpServer.cs b/Assets/Mirror/Transports/Edgegap/EdgegapRelay/EdgegapKcpServer.cs index 9aaf75f75..cc87b14f3 100644 --- a/Assets/Mirror/Transports/Edgegap/EdgegapRelay/EdgegapKcpServer.cs +++ b/Assets/Mirror/Transports/Edgegap/EdgegapRelay/EdgegapKcpServer.cs @@ -28,7 +28,7 @@ public class EdgegapKcpServer : KcpServer bool relayActive; public EdgegapKcpServer( - Action OnConnected, + Action OnConnected, Action, KcpChannel> OnData, Action OnDisconnected, Action OnError, diff --git a/Assets/Mirror/Transports/Edgegap/EdgegapRelay/EdgegapKcpTransport.cs b/Assets/Mirror/Transports/Edgegap/EdgegapRelay/EdgegapKcpTransport.cs index a7e2ff352..c8b5bf649 100644 --- a/Assets/Mirror/Transports/Edgegap/EdgegapRelay/EdgegapKcpTransport.cs +++ b/Assets/Mirror/Transports/Edgegap/EdgegapRelay/EdgegapKcpTransport.cs @@ -60,7 +60,7 @@ protected override void Awake() // server server = new EdgegapKcpServer( - (connectionId) => OnServerConnected.Invoke(connectionId), + (connectionId, endPoint) => OnServerConnected.Invoke(connectionId, endPoint.PrettyAddress()), (connectionId, message, channel) => OnServerDataReceived.Invoke(connectionId, message, FromKcpChannel(channel)), (connectionId) => OnServerDisconnected.Invoke(connectionId), (connectionId, error, reason) => OnServerError.Invoke(connectionId, ToTransportError(error), reason), diff --git a/Assets/Mirror/Transports/Encryption/EncryptionTransport.cs b/Assets/Mirror/Transports/Encryption/EncryptionTransport.cs index 5d9d9bb9a..7213ca563 100644 --- a/Assets/Mirror/Transports/Encryption/EncryptionTransport.cs +++ b/Assets/Mirror/Transports/Encryption/EncryptionTransport.cs @@ -76,7 +76,7 @@ private void HandleInnerServerDataReceived(int connId, ArraySegment data, } } - private void HandleInnerServerConnected(int connId) + private void HandleInnerServerConnected(int connId, string address) { Debug.Log($"[EncryptionTransport] New connection #{connId}"); EncryptedConnection ec = null; @@ -89,7 +89,7 @@ private void HandleInnerServerConnected(int connId) { Debug.Log($"[EncryptionTransport] Connection #{connId} is ready"); ServerRemoveFromPending(ec); - OnServerConnected?.Invoke(connId); + OnServerConnected?.Invoke(connId, address); }, (type, msg) => { diff --git a/Assets/Mirror/Transports/KCP/KcpTransport.cs b/Assets/Mirror/Transports/KCP/KcpTransport.cs index bec9c1381..b6f0f34fb 100644 --- a/Assets/Mirror/Transports/KCP/KcpTransport.cs +++ b/Assets/Mirror/Transports/KCP/KcpTransport.cs @@ -122,7 +122,7 @@ protected virtual void Awake() // server server = new KcpServer( - (connectionId) => OnServerConnected.Invoke(connectionId), + (connectionId, endPoint) => OnServerConnected.Invoke(connectionId, endPoint.PrettyAddress()), (connectionId, message, channel) => OnServerDataReceived.Invoke(connectionId, message, FromKcpChannel(channel)), (connectionId) => OnServerDisconnected.Invoke(connectionId), (connectionId, error, reason) => OnServerError.Invoke(connectionId, ToTransportError(error), reason), diff --git a/Assets/Mirror/Transports/KCP/kcp2k/highlevel/KcpServer.cs b/Assets/Mirror/Transports/KCP/kcp2k/highlevel/KcpServer.cs index 5b8ce4d56..bc838eef1 100644 --- a/Assets/Mirror/Transports/KCP/kcp2k/highlevel/KcpServer.cs +++ b/Assets/Mirror/Transports/KCP/kcp2k/highlevel/KcpServer.cs @@ -18,7 +18,7 @@ public class KcpServer // events are readonly, set in constructor. // this ensures they are always initialized when used. // fixes https://github.com/MirrorNetworking/Mirror/issues/3337 and more - protected readonly Action OnConnected; + protected readonly Action OnConnected; // connectionId, address protected readonly Action, KcpChannel> OnData; protected readonly Action OnDisconnected; protected readonly Action OnError; @@ -43,7 +43,7 @@ public class KcpServer public Dictionary connections = new Dictionary(); - public KcpServer(Action OnConnected, + public KcpServer(Action OnConnected, Action, KcpChannel> OnData, Action OnDisconnected, Action OnError, @@ -184,6 +184,7 @@ public void Disconnect(int connectionId) } // expose the whole IPEndPoint, not just the IP address. some need it. + [Obsolete("KcpServer.GetClientEndPoint() isn't needed anymore. Connection endpoints are now passed in the KcpServer.OnConnected event in order to make threaded transports easier.")] public IPEndPoint GetClientEndPoint(int connectionId) { if (connections.TryGetValue(connectionId, out KcpServerConnection connection)) @@ -285,7 +286,8 @@ void OnConnectedCallback(KcpServerConnection conn) // finally, call mirror OnConnected event Log.Info($"[KCP] Server: OnConnected({connectionId})"); - OnConnected(connectionId); + IPEndPoint endPoint = conn.remoteEndPoint as IPEndPoint; + OnConnected(connectionId, endPoint); } void OnDisconnectedCallback() diff --git a/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs b/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs index d567115f8..a7384fe3b 100644 --- a/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs +++ b/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs @@ -261,12 +261,12 @@ void AddServerCallbacks() int transportIndex = i; Transport transport = transports[i]; - transport.OnServerConnected = (originalConnectionId => + transport.OnServerConnected = (originalConnectionId, originalAddress) => { // invoke Multiplex event with multiplexed connectionId int multiplexedId = AddToLookup(originalConnectionId, transportIndex); - OnServerConnected.Invoke(multiplexedId); - }); + OnServerConnected.Invoke(multiplexedId, originalAddress); + }; transport.OnServerDataReceived = (originalConnectionId, data, channel) => { @@ -282,7 +282,7 @@ void AddServerCallbacks() } else Debug.LogWarning($"[Multiplexer] Received data for unknown connectionId={originalConnectionId} on transport={transportIndex}"); - + return; } OnServerDataReceived.Invoke(multiplexedId, data, channel); @@ -302,7 +302,7 @@ void AddServerCallbacks() } else Debug.LogError($"[Multiplexer] Received error for unknown connectionId={originalConnectionId} on transport={transportIndex}"); - + return; } OnServerError.Invoke(multiplexedId, error, reason); @@ -329,7 +329,7 @@ void AddServerCallbacks() } else Debug.LogWarning($"[Multiplexer] Received disconnect for unknown connectionId={originalConnectionId} on transport={transportIndex}"); - + return; } OnServerDisconnected.Invoke(multiplexedId); diff --git a/Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Server/SimpleWebServer.cs b/Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Server/SimpleWebServer.cs index 6ededebc4..e8d55ce75 100644 --- a/Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Server/SimpleWebServer.cs +++ b/Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Server/SimpleWebServer.cs @@ -6,7 +6,7 @@ namespace Mirror.SimpleWeb { public class SimpleWebServer { - public event Action onConnect; + public event Action onConnect; // connectionId, address public event Action onDisconnect; public event Action> onData; public event Action onError; @@ -91,7 +91,8 @@ public void ProcessMessageQueue(MonoBehaviour behaviour) switch (next.type) { case EventType.Connected: - onConnect?.Invoke(next.connId); + string address = GetClientAddress(next.connId); + onConnect?.Invoke(next.connId, address); break; case EventType.Data: onData?.Invoke(next.connId, next.data.ToSegment()); diff --git a/Assets/Mirror/Transports/Telepathy/Telepathy/Server.cs b/Assets/Mirror/Transports/Telepathy/Telepathy/Server.cs index 08af60629..1f045ba90 100644 --- a/Assets/Mirror/Transports/Telepathy/Telepathy/Server.cs +++ b/Assets/Mirror/Transports/Telepathy/Telepathy/Server.cs @@ -11,7 +11,7 @@ public class Server : Common { // events to hook into // => OnData uses ArraySegment for allocation free receives later - public Action OnConnected; + public Action OnConnected; // connectionId, IPEndPoint public Action> OnData; public Action OnDisconnected; @@ -316,16 +316,16 @@ public bool Send(int connectionId, ArraySegment message) } // client's ip is sometimes needed by the server, e.g. for bans - public string GetClientAddress(int connectionId) + IPEndPoint GetClientEndPoint(int connectionId) { try { // find the connection if (clients.TryGetValue(connectionId, out ConnectionState connection)) { - return ((IPEndPoint)connection.client.Client.RemoteEndPoint).Address.ToString(); + return (IPEndPoint)connection.client.Client.RemoteEndPoint; } - return ""; + return null; } catch (SocketException) { @@ -337,10 +337,17 @@ public string GetClientAddress(int connectionId) // incompatible with the requested protocol was used at // System.Net.Sockets.Socket.get_LocalEndPoint () // so let's at least catch it and recover - return "unknown"; + return null; } } + [Obsolete("Telepathy.Server.GetClientEndPoint() isn't needed anymore. Connection endpoints are now passed in the Telepathy.Server.OnConnected event in order to make threaded transports easier.")] + public string GetClientAddress(int connectionId) + { + IPEndPoint endPoint = GetClientEndPoint(connectionId); + return endPoint != null ? endPoint.Address.ToString() : "unknown"; + } + // disconnect (kick) a client public bool Disconnect(int connectionId) { @@ -388,7 +395,9 @@ public int Tick(int processLimit, Func checkEnabled = null) switch (eventType) { case EventType.Connected: - OnConnected?.Invoke(connectionId); + // pass address in OnConnected for easier ThreadedTransport support + IPEndPoint endPoint = GetClientEndPoint(connectionId); + OnConnected?.Invoke(connectionId, endPoint); break; case EventType.Data: OnData?.Invoke(connectionId, message); diff --git a/Assets/Mirror/Transports/Telepathy/TelepathyTransport.cs b/Assets/Mirror/Transports/Telepathy/TelepathyTransport.cs index f4276884e..5d42fe4cd 100644 --- a/Assets/Mirror/Transports/Telepathy/TelepathyTransport.cs +++ b/Assets/Mirror/Transports/Telepathy/TelepathyTransport.cs @@ -178,7 +178,7 @@ public override void ServerStart() // system's hook (e.g. statistics OnData) was added is to wrap // them all in a lambda and always call the latest hook. // (= lazy call) - server.OnConnected = (connectionId) => OnServerConnected.Invoke(connectionId); + server.OnConnected = (connectionId, endPoint) => OnServerConnected.Invoke(connectionId, endPoint.PrettyAddress()); server.OnData = (connectionId, segment) => OnServerDataReceived.Invoke(connectionId, segment, Channels.Reliable); server.OnDisconnected = (connectionId) => OnServerDisconnected.Invoke(connectionId); diff --git a/Assets/Mirror/Transports/Threaded/ThreadedTransport.cs b/Assets/Mirror/Transports/Threaded/ThreadedTransport.cs index a931d161d..62fe5b4cf 100644 --- a/Assets/Mirror/Transports/Threaded/ThreadedTransport.cs +++ b/Assets/Mirror/Transports/Threaded/ThreadedTransport.cs @@ -5,6 +5,7 @@ // note that ThreadLog.cs is required for Debug.Log from threads to work in builds. using System; using System.Collections.Concurrent; +using System.Net; using System.Runtime.CompilerServices; using System.Threading; using UnityEngine; @@ -317,9 +318,11 @@ protected void OnThreadedClientDisconnected() EnqueueClientMain(ClientMainEventType.OnClientDisconnected, null, null, null); } - protected void OnThreadedServerConnected(int connectionId) + protected void OnThreadedServerConnected(int connectionId, IPEndPoint endPoint) { - EnqueueServerMain(ServerMainEventType.OnServerConnected, null, connectionId, null, null); + // create string copy of address immediately before sending to another thread + string address = endPoint.PrettyAddress(); + EnqueueServerMain(ServerMainEventType.OnServerConnected, address, connectionId, null, null); } protected void OnThreadedServerSend(int connectionId, ArraySegment message, int channelId) @@ -515,9 +518,9 @@ public override void ServerEarlyUpdate() // SERVER EVENTS /////////////////////////////////////////// case ServerMainEventType.OnServerConnected: { - // call original transport event - // TODO pass client address in OnConnect here later - OnServerConnected?.Invoke(elem.connectionId.Value);//, (string)elem.param); + // call original transport event with connectionId, address + string address = (string)elem.param; + OnServerConnected?.Invoke(elem.connectionId.Value, address); break; } case ServerMainEventType.OnServerSent: @@ -612,7 +615,7 @@ public override void ServerDisconnect(int connectionId) // querying this at runtime won't work for threaded transports. public override string ServerGetClientAddress(int connectionId) { - throw new NotImplementedException(); + throw new NotImplementedException("ThreadedTransport passes each connection's address in OnServerConnected. Don't use ServerGetClientAddress."); } public override void ServerStop()