From c10505cde864bdac85eb17097ce0e00d1bfbb006 Mon Sep 17 00:00:00 2001 From: vis2k Date: Fri, 13 May 2022 12:46:57 +0800 Subject: [PATCH 1/2] KcpConnection.SendReliable: added OnError instead of logs --- kcp2k/Assets/Tests/Editor/ClientServerTests.cs | 6 +++--- kcp2k/Assets/kcp2k/highlevel/KcpConnection.cs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/kcp2k/Assets/Tests/Editor/ClientServerTests.cs b/kcp2k/Assets/Tests/Editor/ClientServerTests.cs index 5de24043..f3848ab0 100644 --- a/kcp2k/Assets/Tests/Editor/ClientServerTests.cs +++ b/kcp2k/Assets/Tests/Editor/ClientServerTests.cs @@ -370,7 +370,7 @@ public void ClientToServerTooLargeReliableMessage() byte[] message = new byte[KcpConnection.ReliableMaxMessageSize(ReceiveWindowSize) + 1]; #if UNITY_2018_3_OR_NEWER - UnityEngine.TestTools.LogAssert.Expect(UnityEngine.LogType.Error, new Regex($".*Failed to send reliable message of size {message.Length} because it's larger than ReliableMaxMessageSize={KcpConnection.ReliableMaxMessageSize(ReceiveWindowSize)}")); + UnityEngine.TestTools.LogAssert.Expect(UnityEngine.LogType.Warning, new Regex($".*Failed to send reliable message of size {message.Length} because it's larger than ReliableMaxMessageSize={KcpConnection.ReliableMaxMessageSize(ReceiveWindowSize)}")); #endif SendClientToServerBlocking(new ArraySegment(message), KcpChannel.Reliable); Assert.That(serverReceived.Count, Is.EqualTo(0)); @@ -660,7 +660,7 @@ public void ServerToClientTooLargeReliableMessage() byte[] message = new byte[KcpConnection.ReliableMaxMessageSize(ReceiveWindowSize) + 1]; #if UNITY_2018_3_OR_NEWER - UnityEngine.TestTools.LogAssert.Expect(UnityEngine.LogType.Error, new Regex($".*Failed to send reliable message of size {message.Length} because it's larger than ReliableMaxMessageSize={KcpConnection.ReliableMaxMessageSize(ReceiveWindowSize)}")); + UnityEngine.TestTools.LogAssert.Expect(UnityEngine.LogType.Warning, new Regex($".*Failed to send reliable message of size {message.Length} because it's larger than ReliableMaxMessageSize={KcpConnection.ReliableMaxMessageSize(ReceiveWindowSize)}")); #endif SendServerToClientBlocking(connectionId, new ArraySegment(message), KcpChannel.Reliable); Assert.That(clientReceived.Count, Is.EqualTo(0)); @@ -677,7 +677,7 @@ public void ServerToClientTooLargeUnreliableMessage() byte[] message = new byte[KcpConnection.UnreliableMaxMessageSize + 1]; #if UNITY_2018_3_OR_NEWER - UnityEngine.TestTools.LogAssert.Expect(UnityEngine.LogType.Error, new Regex($".*Failed to send unreliable message of size {message.Length} because it's larger than UnreliableMaxMessageSize={KcpConnection.UnreliableMaxMessageSize}")); + UnityEngine.TestTools.LogAssert.Expect(UnityEngine.LogType.Warning, new Regex($".*Failed to send unreliable message of size {message.Length} because it's larger than UnreliableMaxMessageSize={KcpConnection.UnreliableMaxMessageSize}")); #endif SendServerToClientBlocking(connectionId, new ArraySegment(message), KcpChannel.Unreliable); Assert.That(clientReceived.Count, Is.EqualTo(0)); diff --git a/kcp2k/Assets/kcp2k/highlevel/KcpConnection.cs b/kcp2k/Assets/kcp2k/highlevel/KcpConnection.cs index 29402cb1..7d81b5c9 100644 --- a/kcp2k/Assets/kcp2k/highlevel/KcpConnection.cs +++ b/kcp2k/Assets/kcp2k/highlevel/KcpConnection.cs @@ -578,12 +578,12 @@ void SendReliable(KcpHeader header, ArraySegment content) if (sent < 0) { // GetType() shows Server/ClientConn instead of just Connection. - Log.Warning($"{GetType()}: Send failed with error={sent} for content with length={content.Count}"); + OnError(ErrorCode.InvalidSend, $"{GetType()}: Send failed with error={sent} for content with length={content.Count}"); } } // otherwise content is larger than MaxMessageSize. let user know! - // GetType() shows Server/ClientConn instead of just Connection. - else Log.Error($"{GetType()}: Failed to send reliable message of size {content.Count} because it's larger than ReliableMaxMessageSize={ReliableMaxMessageSize(kcp.rcv_wnd)}"); + // GetType() shows Server/ClientConn instead of just Connection. + else OnError(ErrorCode.InvalidSend, $"{GetType()}: Failed to send reliable message of size {content.Count} because it's larger than ReliableMaxMessageSize={ReliableMaxMessageSize(kcp.rcv_wnd)}"); } void SendUnreliable(ArraySegment message) From 926af1c814771e29a40dfa0fb6774b7ddfcc0495 Mon Sep 17 00:00:00 2001 From: Robin Rolf Date: Tue, 16 Aug 2022 10:41:49 +0200 Subject: [PATCH 2/2] Add OnError for SocketException On windows, when trying to connect to localhost, there seems to be an edge case where instead of timing out or anything like that, windows knows better and raises a socket exception here: "System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host.", which of course doesnt make any sense whatsoever if there's not a server listening to begin with (should be a connection refused really), but thats how it is --- kcp2k/Assets/kcp2k/highlevel/KcpClientConnection.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/kcp2k/Assets/kcp2k/highlevel/KcpClientConnection.cs b/kcp2k/Assets/kcp2k/highlevel/KcpClientConnection.cs index a843a8dd..da76639a 100644 --- a/kcp2k/Assets/kcp2k/highlevel/KcpClientConnection.cs +++ b/kcp2k/Assets/kcp2k/highlevel/KcpClientConnection.cs @@ -134,10 +134,7 @@ public void RawReceive() // this is fine, the socket might have been closed in the other end catch (SocketException ex) { - // the other end closing the connection is not an 'error'. - // but connections should never just end silently. - // at least log a message for easier debugging. - Log.Info($"KCP ClientConnection: looks like the other end has closed the connection. This is fine: {ex}"); + OnError(ErrorCode.ConnectionClosed, $"KCP ClientConnection: looks like the other end has closed the connection. This is fine: {ex}"); Disconnect(); } }