From d0ac4343bd3bcb591cf535aece7426a9be694dae Mon Sep 17 00:00:00 2001 From: Marco Hoffmann Date: Sat, 20 Feb 2021 14:50:27 +0100 Subject: [PATCH] Logging, Bugfix, Default channels --- Client.cs | 30 ++++++++++++++++++++++-------- FizzyFacepunch.cs | 26 ++++++++++++++++---------- Server.cs | 2 +- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/Client.cs b/Client.cs index 7dfb06d..8858dda 100644 --- a/Client.cs +++ b/Client.cs @@ -57,11 +57,19 @@ private async void Connect(string host) OnConnected += SetConnectedComplete; SendInternal(hostSteamID, InternalMessages.CONNECT); - Task connectedCompleteTask = connectedComplete.Task; + Task timeOutTask = Task.Delay(ConnectionTimeout, cancelToken.Token); - if (await Task.WhenAny(connectedCompleteTask, Task.Delay(ConnectionTimeout, cancelToken.Token)) != connectedCompleteTask) + if (await Task.WhenAny(connectedCompleteTask, timeOutTask) != connectedCompleteTask) { + if (cancelToken.IsCancellationRequested) + { + Debug.LogError($"The connection attempt was cancelled."); + } + else if (timeOutTask.IsCompleted) + { + Debug.LogError($"Connection to {host} timed out."); + } OnConnected -= SetConnectedComplete; Debug.LogError("Connection timed out."); OnConnectionFailed(hostSteamID); @@ -128,14 +136,20 @@ protected override void OnReceiveInternalData(InternalMessages type, SteamId cli switch (type) { case InternalMessages.ACCEPT_CONNECT: - Connected = true; - Debug.Log("Connection established."); - OnConnected.Invoke(); + if (!Connected) + { + Connected = true; + Debug.Log("Connection established."); + OnConnected.Invoke(); + } break; case InternalMessages.DISCONNECT: - Connected = false; - Debug.Log("Disconnected."); - OnDisconnected.Invoke(); + if (Connected) + { + Connected = false; + Debug.Log("Disconnected."); + OnDisconnected.Invoke(); + } break; default: Debug.Log("Received unknown message type"); diff --git a/FizzyFacepunch.cs b/FizzyFacepunch.cs index ecb19c1..9f223c7 100644 --- a/FizzyFacepunch.cs +++ b/FizzyFacepunch.cs @@ -11,13 +11,13 @@ public class FizzyFacepunch : Transport { private const string STEAM_SCHEME = "steam"; - private Client client; - private Server server; + private static Client client; + private static Server server; - private Common activeNode; + private static Common activeNode; [SerializeField] - public P2PSend[] Channels = new P2PSend[1] { P2PSend.Reliable }; + public P2PSend[] Channels = new P2PSend[2] { P2PSend.Reliable , P2PSend.UnreliableNoDelay }; [Tooltip("Timeout for connecting in seconds.")] public int Timeout = 25; @@ -180,13 +180,19 @@ public override void ServerStop() public override void Shutdown() { - server?.Shutdown(); - client?.Disconnect(); + if (server != null) + { + server.Shutdown(); + server = null; + Debug.Log("Transport shut down - was server."); + } - server = null; - client = null; - activeNode = null; - Debug.Log("Transport shut down."); + if (client != null) + { + client.Disconnect(); + client = null; + Debug.Log("Transport shut down - was client."); + } } public override int GetMaxPacketSize(int channelId) diff --git a/Server.cs b/Server.cs index 518f854..94b5d4b 100644 --- a/Server.cs +++ b/Server.cs @@ -76,7 +76,7 @@ protected override void OnReceiveInternalData(InternalMessages type, SteamId cli } else { - OnReceivedError.Invoke(-1, new Exception("ERROR Unknown SteamID")); + OnReceivedError.Invoke(-1, new Exception("ERROR Unknown SteamID while receiving disconnect message.")); } break;