diff --git a/Client.cs b/Client.cs index da33fd7..832cbb0 100644 --- a/Client.cs +++ b/Client.cs @@ -90,11 +90,12 @@ private async void Connect(string host) public void Disconnect() { + Debug.Log("Sending Disconnect message"); SendInternal(hostSteamID, InternalMessages.DISCONNECT); Dispose(); - cancelToken.Cancel(); + cancelToken?.Cancel(); - transport.StartCoroutine(WaitDisconnect(hostSteamID)); + WaitForClose(hostSteamID); } private void SetConnectedComplete() => connectedComplete.SetResult(connectedComplete.Task); diff --git a/Common.cs b/Common.cs index 768e2a8..174f741 100644 --- a/Common.cs +++ b/Common.cs @@ -29,10 +29,11 @@ protected Common(FizzyFacepunch transport) this.transport = transport; } - protected IEnumerator WaitDisconnect(SteamId steamID) + protected void WaitForClose(SteamId cSteamID) => transport.StartCoroutine(DelayedClose(cSteamID)); + private IEnumerator DelayedClose(SteamId cSteamID) { - yield return new WaitForSeconds(0.1f); - CloseP2PSessionWithUser(steamID); + yield return null; + CloseP2PSessionWithUser(cSteamID); } protected void Dispose() @@ -46,6 +47,7 @@ protected void Dispose() private void OnConnectFail(SteamId id, P2PSessionError err) { OnConnectionFailed(id); + CloseP2PSessionWithUser(id); switch (err) { @@ -67,7 +69,7 @@ private void OnConnectFail(SteamId id, P2PSessionError err) private bool Receive(out SteamId clientSteamID, out byte[] receiveBuffer, int channel) { if (SteamNetworking.IsP2PPacketAvailable(channel)) - { + { var data = SteamNetworking.ReadP2PPacket(channel); if (data != null) @@ -89,25 +91,26 @@ public void ReceiveData() { try { - for (int chNum = 0; chNum < channels.Length; chNum++) - { - while (Receive(out SteamId clientSteamID, out byte[] receiveBuffer, chNum)) - { - OnReceiveData(receiveBuffer, clientSteamID, chNum); - } - } - while (Receive(out SteamId clientSteamID, out byte[] internalMessage, internal_ch)) { if (internalMessage.Length == 1) { OnReceiveInternalData((InternalMessages)internalMessage[0], clientSteamID); + return; } else { Debug.Log("Incorrect package length on internal channel."); } } + + for (int chNum = 0; chNum < channels.Length; chNum++) + { + while (Receive(out SteamId clientSteamID, out byte[] receiveBuffer, chNum)) + { + OnReceiveData(receiveBuffer, clientSteamID, chNum); + } + } } catch (Exception e) { diff --git a/FizzyFacepunch.cs b/FizzyFacepunch.cs index 1be849d..9d13588 100644 --- a/FizzyFacepunch.cs +++ b/FizzyFacepunch.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using UnityEngine; namespace Mirror.FizzySteam @@ -42,7 +41,7 @@ private void Awake() catch (Exception e) { Debug.LogError($"FizzyFacepunch could not initialise: {e.Message}"); - } + } } private void LateUpdate() @@ -91,7 +90,13 @@ public override void ClientConnect(Uri uri) ClientConnect(uri.Host); } - public override bool ClientSend(int channelId, ArraySegment segment) => client.Send(segment.ToArray(), channelId); + public override bool ClientSend(int channelId, ArraySegment segment) + { + byte[] data = new byte[segment.Count]; + Array.Copy(segment.Array, segment.Offset, data, 0, segment.Count); + return client.Send(data, channelId); + } + public override void ClientDisconnect() { if (ClientActive()) @@ -143,7 +148,18 @@ public override Uri ServerUri() return steamBuilder.Uri; } - public override bool ServerSend(List connectionIds, int channelId, ArraySegment segment) => ServerActive() && server.SendAll(connectionIds, segment.ToArray(), channelId); + public override bool ServerSend(List connectionIds, int channelId, ArraySegment segment) + { + if (ServerActive()) + { + byte[] data = new byte[segment.Count]; + Array.Copy(segment.Array, segment.Offset, data, 0, segment.Count); + server.SendAll(connectionIds, data, channelId); + } + + return false; + } + public override bool ServerDisconnect(int connectionId) => ServerActive() && server.Disconnect(connectionId); public override string ServerGetClientAddress(int connectionId) => ServerActive() ? server.ServerGetClientAddress(connectionId) : string.Empty; public override void ServerStop() diff --git a/Server.cs b/Server.cs index 6e55b14..c356020 100644 --- a/Server.cs +++ b/Server.cs @@ -67,11 +67,11 @@ protected override void OnReceiveInternalData(InternalMessages type, SteamId cli Debug.Log($"Client with SteamID {clientSteamID} connected. Assigning connection id {connectionId}"); break; case InternalMessages.DISCONNECT: - if (steamToMirrorIds.Contains(clientSteamID)) + if (steamToMirrorIds.TryGetValue(clientSteamID, out int connId)) { - OnDisconnected.Invoke(steamToMirrorIds[clientSteamID]); - steamToMirrorIds.Remove(clientSteamID); + OnDisconnected.Invoke(connId); CloseP2PSessionWithUser(clientSteamID); + steamToMirrorIds.Remove(clientSteamID); Debug.Log($"Client with SteamID {clientSteamID} disconnected."); } else @@ -88,9 +88,8 @@ protected override void OnReceiveInternalData(InternalMessages type, SteamId cli protected override void OnReceiveData(byte[] data, SteamId clientSteamID, int channel) { - if (steamToMirrorIds.Contains(clientSteamID)) + if (steamToMirrorIds.TryGetValue(clientSteamID, out int connectionId)) { - int connectionId = steamToMirrorIds[clientSteamID]; OnReceivedData.Invoke(connectionId, data, channel); } else @@ -103,13 +102,9 @@ protected override void OnReceiveData(byte[] data, SteamId clientSteamID, int ch public bool Disconnect(int connectionId) { - if (steamToMirrorIds.Contains(connectionId)) + if (steamToMirrorIds.TryGetValue(connectionId, out SteamId steamID)) { - SteamId steamID = steamToMirrorIds[connectionId]; - steamToMirrorIds.Remove(connectionId); - SendInternal(steamID, InternalMessages.DISCONNECT); - transport.StartCoroutine(WaitDisconnect(steamID)); return true; } @@ -122,18 +117,25 @@ public bool Disconnect(int connectionId) public void Shutdown() { - Dispose(); + foreach (KeyValuePair client in steamToMirrorIds) + { + Disconnect(client.Value); + WaitForClose(client.Key); + } + SteamNetworking.OnP2PSessionRequest = null; + Dispose(); } + public bool SendAll(List connectionIds, byte[] data, int channelId) { bool success = true; foreach (int connId in connectionIds) { - if (steamToMirrorIds.Contains(connId)) + if (steamToMirrorIds.TryGetValue(connId, out SteamId steamId)) { - success = success && Send(steamToMirrorIds[connId], data, channelId); + success = success && Send(steamId, data, channelId); } else { @@ -147,9 +149,9 @@ public bool SendAll(List connectionIds, byte[] data, int channelId) public string ServerGetClientAddress(int connectionId) { - if (steamToMirrorIds.Contains(connectionId)) + if (steamToMirrorIds.TryGetValue(connectionId, out SteamId steamId)) { - return steamToMirrorIds[connectionId].ToString(); + return steamId.ToString(); } else { @@ -161,8 +163,8 @@ public string ServerGetClientAddress(int connectionId) protected override void OnConnectionFailed(SteamId remoteId) { - int connId = steamToMirrorIds.Contains(remoteId) ? steamToMirrorIds[remoteId] : nextConnectionID++; - OnDisconnected.Invoke(steamToMirrorIds[remoteId]); + int connectionId = steamToMirrorIds.TryGetValue(remoteId, out int connId) ? connId : nextConnectionID++; + OnDisconnected.Invoke(connectionId); } } } \ No newline at end of file