Skip to content

Commit

Permalink
Bugfixes regarding Transport Shutdown and Reusing
Browse files Browse the repository at this point in the history
  • Loading branch information
Chykary committed Jun 19, 2020
1 parent cf1ab06 commit 8b2e135
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
5 changes: 3 additions & 2 deletions Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 15 additions & 12 deletions Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -46,6 +47,7 @@ protected void Dispose()
private void OnConnectFail(SteamId id, P2PSessionError err)
{
OnConnectionFailed(id);
CloseP2PSessionWithUser(id);

switch (err)
{
Expand All @@ -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)
Expand All @@ -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)
{
Expand Down
24 changes: 20 additions & 4 deletions FizzyFacepunch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

namespace Mirror.FizzySteam
Expand Down Expand Up @@ -42,7 +41,7 @@ private void Awake()
catch (Exception e)
{
Debug.LogError($"FizzyFacepunch could not initialise: {e.Message}");
}
}
}

private void LateUpdate()
Expand Down Expand Up @@ -91,7 +90,13 @@ public override void ClientConnect(Uri uri)
ClientConnect(uri.Host);
}

public override bool ClientSend(int channelId, ArraySegment<byte> segment) => client.Send(segment.ToArray(), channelId);
public override bool ClientSend(int channelId, ArraySegment<byte> 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())
Expand Down Expand Up @@ -143,7 +148,18 @@ public override Uri ServerUri()
return steamBuilder.Uri;
}

public override bool ServerSend(List<int> connectionIds, int channelId, ArraySegment<byte> segment) => ServerActive() && server.SendAll(connectionIds, segment.ToArray(), channelId);
public override bool ServerSend(List<int> connectionIds, int channelId, ArraySegment<byte> 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()
Expand Down
36 changes: 19 additions & 17 deletions Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -122,18 +117,25 @@ public bool Disconnect(int connectionId)

public void Shutdown()
{
Dispose();
foreach (KeyValuePair<SteamId, int> client in steamToMirrorIds)
{
Disconnect(client.Value);
WaitForClose(client.Key);
}

SteamNetworking.OnP2PSessionRequest = null;
Dispose();
}


public bool SendAll(List<int> 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
{
Expand All @@ -147,9 +149,9 @@ public bool SendAll(List<int> 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
{
Expand All @@ -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);
}
}
}

0 comments on commit 8b2e135

Please sign in to comment.