Skip to content

Commit

Permalink
Merge pull request #12 from ooonush/dev
Browse files Browse the repository at this point in the history
Simplify local ClientId to TransportId, fix the multiple client failure
  • Loading branch information
ooonush authored Apr 24, 2023
2 parents f647d68 + 0478abd commit ede394e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 71 deletions.
18 changes: 9 additions & 9 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
{
public ulong ServerClientId { get; private set; }
private ulong _clientId;
private string _serverCommonName;
private string _clientCaCertificate;

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

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

return true;
}

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

public bool StopClient()
Expand Down Expand Up @@ -135,11 +135,11 @@ public bool DisconnectLocalClient()

SetLocalConnectionState(LocalConnectionState.Stopping);

FlushSendQueuesForClientId(ServerClientId);
FlushSendQueuesForClientId(_clientId);

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

SetLocalConnectionState(LocalConnectionState.Stopped);

Expand All @@ -149,7 +149,7 @@ public bool DisconnectLocalClient()
protected override void SetLocalConnectionState(LocalConnectionState state)
{
State = state;
Transport.HandleClientConnectionState(new ClientConnectionStateArgs(state, Transport.Index));
Transport.HandleClientConnectionState(state, _clientId, Transport.Index);
}

protected override void OnPushMessageFailure(int channelId, ArraySegment<byte> payload, ulong clientId)
Expand Down
119 changes: 57 additions & 62 deletions Assets/FishNet/Plugins/FishyUnityTransport/FishyUnityTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,17 @@ public int DisconnectTimeoutMS

#region ClientId And TransportId

private int _localClientTransportId;
private readonly Dictionary<int, ulong> _transportIdToClientIdMap = new();
private readonly Dictionary<ulong, int> _clientIdToTransportIdMap = new();

internal int ClientIdToTransportId(ulong clientId)
{
return clientId == _clientSocket.ServerClientId
? _localClientTransportId
: _clientIdToTransportIdMap[clientId];
}
internal int ClientIdToTransportId(ulong clientId) => _clientIdToTransportIdMap[clientId];

private ulong TransportIdToClientId(int transportId)
{
return transportId == _localClientTransportId
? _clientSocket.ServerClientId
: _transportIdToClientIdMap[transportId];
}
private ulong TransportIdToClientId(int transportId) => _transportIdToClientIdMap[transportId];

#endregion

#region Initialization and unity.

/// <summary>
/// Initializes the transport. Use this instead of Awake.
/// </summary>
Expand All @@ -236,18 +226,18 @@ private void OnDestroy()
{
Shutdown();
}

#endregion

#region ConnectionStates.

public override string GetConnectionAddress(int connectionId)
{
return _serverSocket.GetConnectionAddress(TransportIdToClientId(connectionId));
}

public override event Action<ClientConnectionStateArgs> OnClientConnectionState;

public override event Action<ClientConnectionStateArgs> OnClientConnectionState;
public override event Action<ServerConnectionStateArgs> OnServerConnectionState;

public override event Action<RemoteConnectionStateArgs> OnRemoteConnectionState;

public override LocalConnectionState GetConnectionState(bool server)
Expand All @@ -264,34 +254,63 @@ public override RemoteConnectionState GetConnectionState(int connectionId)

public override void HandleClientConnectionState(ClientConnectionStateArgs connectionStateArgs)
{
switch (connectionStateArgs.ConnectionState)
OnClientConnectionState?.Invoke(connectionStateArgs);
}

internal void HandleClientConnectionState(LocalConnectionState connectionState, ulong clientId, int transportIndex)
{
int transportId = ParseClientIdToTransportId(clientId);

switch (connectionState)
{
case LocalConnectionState.Stopped:
_localClientTransportId = _clientSocket.ServerClientId.GetHashCode();
break;
case LocalConnectionState.Starting:
break;
case LocalConnectionState.Started:
_localClientTransportId = _clientSocket.ServerClientId.GetHashCode();
_transportIdToClientIdMap[transportId] = clientId;
_clientIdToTransportIdMap[clientId] = transportId;
break;
case LocalConnectionState.Stopping:
case LocalConnectionState.Stopped:
_transportIdToClientIdMap.Remove(transportId);
_clientIdToTransportIdMap.Remove(clientId);
break;
default:
throw new ArgumentOutOfRangeException();
}

OnClientConnectionState?.Invoke(connectionStateArgs);
HandleClientConnectionState(new ClientConnectionStateArgs(connectionState, transportIndex));
}

public override void HandleServerConnectionState(ServerConnectionStateArgs connectionStateArgs)
{
OnServerConnectionState?.Invoke(connectionStateArgs);
}

internal void HandleRemoteConnectionState(RemoteConnectionState state, ulong clientId, int transportIndex)
{
int transportId = ParseClientIdToTransportId(clientId);
switch (state)
{
case RemoteConnectionState.Started:
_transportIdToClientIdMap[transportId] = clientId;
_clientIdToTransportIdMap[clientId] = transportId;
break;
case RemoteConnectionState.Stopped:
_transportIdToClientIdMap.Remove(transportId);
_clientIdToTransportIdMap.Remove(clientId);
break;
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}

HandleRemoteConnectionState(new RemoteConnectionStateArgs(state, transportId, transportIndex));
}

public override void HandleRemoteConnectionState(RemoteConnectionStateArgs connectionStateArgs)
{
OnRemoteConnectionState?.Invoke(connectionStateArgs);
}

internal static int ParseClientIdToTransportId(ulong clientId)
{
return clientId.GetHashCode();
}

#endregion

#region Iterating.
Expand Down Expand Up @@ -325,17 +344,28 @@ public override void IterateOutgoing(bool server)

public override event Action<ClientReceivedDataArgs> OnClientReceivedData;

internal void HandleClientReceivedData(ArraySegment<byte> message, Channel channel, int transportIndex)
{
HandleClientReceivedDataArgs(new ClientReceivedDataArgs(message, channel, transportIndex));
}

public override void HandleClientReceivedDataArgs(ClientReceivedDataArgs receivedDataArgs)
{
OnClientReceivedData?.Invoke(receivedDataArgs);
}

public override event Action<ServerReceivedDataArgs> OnServerReceivedData;

internal void HandleServerReceivedData(ArraySegment<byte> message, Channel channel, ulong clientId, int transportIndex)
{
HandleServerReceivedDataArgs(new ServerReceivedDataArgs(message, channel, ClientIdToTransportId(clientId), transportIndex));
}

public override void HandleServerReceivedDataArgs(ServerReceivedDataArgs receivedDataArgs)
{
OnServerReceivedData?.Invoke(receivedDataArgs);
}

#endregion

#region Sending.
Expand Down Expand Up @@ -563,40 +593,5 @@ private bool StopClient(int connectionId)
}

#endregion

internal static int ParseClientIdToTransportId(ulong clientId)
{
return clientId.GetHashCode();
}

internal void HandleRemoteConnectionState(RemoteConnectionState state, ulong clientId, int transportIndex)
{
int transportId = ParseClientIdToTransportId(clientId);
switch (state)
{
case RemoteConnectionState.Started:
_transportIdToClientIdMap[transportId] = clientId;
_clientIdToTransportIdMap[clientId] = transportId;
break;
case RemoteConnectionState.Stopped:
_transportIdToClientIdMap.Remove(transportId);
_clientIdToTransportIdMap.Remove(clientId);
break;
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}

HandleRemoteConnectionState(new RemoteConnectionStateArgs(state, transportId, transportIndex));
}

internal void HandleServerReceivedData(ArraySegment<byte> message, Channel channel, ulong clientId, int transportIndex)
{
HandleServerReceivedDataArgs(new ServerReceivedDataArgs(message, channel, ClientIdToTransportId(clientId), transportIndex));
}

internal void HandleClientReceivedData(ArraySegment<byte> message, Channel channel, int transportIndex)
{
HandleClientReceivedDataArgs(new ClientReceivedDataArgs(message, channel, transportIndex));
}
}
}

0 comments on commit ede394e

Please sign in to comment.