Skip to content

Commit

Permalink
Merge pull request #13 from ooonush/dev
Browse files Browse the repository at this point in the history
Stability Improvement and bug fix
  • Loading branch information
ooonush authored May 2, 2023
2 parents e213400 + 08fee6c commit 334e333
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 190 deletions.
64 changes: 35 additions & 29 deletions Assets/FishNet/Plugins/FishyUnityTransport/Core/ClientSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace FishNet.Transporting.FishyUnityTransport
[Serializable]
internal class ClientSocket : CommonSocket
{
private ulong _clientId;
private NetworkConnection _connection;
private string _serverCommonName;
private string _clientCaCertificate;

Expand All @@ -37,7 +37,7 @@ public void SetClientSecrets(string serverCommonName, string caCertificate = nul

public bool StartConnection()
{
if (Driver.IsCreated || State != LocalConnectionState.Stopped)
if (Driver.IsCreated)
{
if (Transport.NetworkManager.CanLog(LoggingType.Error))
Debug.LogError("Attempting to start a client that is already active.");
Expand All @@ -56,8 +56,6 @@ public bool StartConnection()
Driver.Dispose();
}

// SetLocalConnectionState(LocalConnectionState.Started, false);

return false;
}

Expand Down Expand Up @@ -93,25 +91,38 @@ private bool ClientBindAndConnect()
return false;
}

NetworkConnection connection = Driver.Connect(serverEndpoint);
_clientId = ParseClientId(connection);
_connection = Driver.Connect(serverEndpoint);

return true;
}

public void SendToServer(byte channelId, ArraySegment<byte> segment)
{
Send(channelId, segment, _clientId);
Send(channelId, segment, _connection);
}

public bool StopClient()
{
if (!DisconnectLocalClient()) return false;
LocalConnectionState state = State;
if (State is LocalConnectionState.Stopped or LocalConnectionState.Stopping) return false;

SetLocalConnectionState(LocalConnectionState.Stopping);

FlushSendQueuesForClientId(_connection);

if (_connection.Disconnect(Driver) != 0)
{
SetLocalConnectionState(state);
return false;
}
Shutdown();

SetLocalConnectionState(LocalConnectionState.Stopped);

return true;
}

protected override void HandleDisconnectEvent(ulong clientId)
protected override void HandleDisconnectEvent(NetworkConnection connection)
{
// Handle cases where we're a client receiving a Disconnect event. The
// meaning of the event depends on our current state. If we were connected
Expand All @@ -120,44 +131,39 @@ protected override void HandleDisconnectEvent(ulong clientId)
if (State == LocalConnectionState.Started)
{
SetLocalConnectionState(LocalConnectionState.Stopped);
// m_ServerClientId = default;
_connection = default;
}
else if (State == LocalConnectionState.Stopped)
{
Debug.LogError("Failed to connect to server.");
// m_ServerClientId = default;
_connection = default;
}
}

public bool DisconnectLocalClient()
protected override void Shutdown()
{
if (State is LocalConnectionState.Stopped or LocalConnectionState.Stopping) return false;

SetLocalConnectionState(LocalConnectionState.Stopping);

FlushSendQueuesForClientId(_clientId);

if (ParseClientId(_clientId).Disconnect(Driver) != 0) return false;
ReliableReceiveQueues.Remove(_clientId);
ClearSendQueuesForClientId(_clientId);

SetLocalConnectionState(LocalConnectionState.Stopped);

return true;
base.Shutdown();
_connection = default;
}

protected override void SetLocalConnectionState(LocalConnectionState state)
{
State = state;
Transport.HandleClientConnectionState(state, _clientId, Transport.Index);
if (Transport)
{
Transport.HandleClientConnectionState(state, _connection, Transport.Index);
}
}

protected override void OnPushMessageFailure(int channelId, ArraySegment<byte> payload, ulong clientId)
protected override void OnPushMessageFailure(int channelId, ArraySegment<byte> payload, NetworkConnection connection)
{
DisconnectLocalClient();
if (connection == _connection)
{
StopClient();
}
}

protected override void HandleReceivedData(ArraySegment<byte> message, Channel channel, ulong clientId)
protected override void HandleReceivedData(ArraySegment<byte> message, Channel channel, NetworkConnection connection)
{
Transport.HandleClientReceivedData(message, channel, Transport.Index);
}
Expand Down
Loading

0 comments on commit 334e333

Please sign in to comment.