Skip to content

Commit

Permalink
- use ClientId sent by companion app
Browse files Browse the repository at this point in the history
    - the ip-addess was used as ClientId before. Bus this can change when the device re-connects or the router is reset
    - a UUID is created now on first start of the companion app to identify the device
  • Loading branch information
achimmihca committed Apr 11, 2021
1 parent c3e0789 commit f4d693d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
12 changes: 8 additions & 4 deletions UltraStar Play/Assets/Common/Network/ConnectedClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class ConnectedClientHandler : IDisposable

public IPEndPoint ClientIpEndPoint { get; private set; }
public string ClientName { get; private set; }
public string ClientId { get; private set; }
public TcpListener MicTcpListener { get; private set; }
public string ClientId => ServerSideConnectRequestManager.GetClientId(ClientIpEndPoint);
public int SampleRateHz => micSampleBuffer.Length;

private bool isDisposed;
Expand All @@ -31,15 +31,19 @@ public class ConnectedClientHandler : IDisposable
private readonly Thread receiveDataThread;
private readonly Thread clientStillAliveCheckThread;

public ConnectedClientHandler(ServerSideConnectRequestManager serverSideConnectRequestManager, IPEndPoint clientIpEndPoint, string clientName, int microphoneSampleRate)
public ConnectedClientHandler(ServerSideConnectRequestManager serverSideConnectRequestManager, IPEndPoint clientIpEndPoint, string clientName, string clientId, int microphoneSampleRate)
{
this.serverSideConnectRequestManager = serverSideConnectRequestManager;
ClientIpEndPoint = clientIpEndPoint;
ClientName = clientName;
ClientId = clientId;
if (ClientId.IsNullOrEmpty())
{
throw new ArgumentException("Attempt to create ConnectedClientHandler without ClientId");
}
if (microphoneSampleRate <= 0)
{
Debug.LogWarning("Attempt to create ConnectedClientHandler without microphoneSampleRate");
return;
throw new ArgumentException("Attempt to create ConnectedClientHandler without microphoneSampleRate");
}

micSampleBuffer = new float[microphoneSampleRate];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
{
public int ProtocolVersion { get; set; }
public string ClientName { get; set; }
public string ClientId { get; set; }
public int MicrophoneSampleRate { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
public class ConnectResponseDto : JsonSerializable
{
public string ClientName { get; set; }
public string ClientId { get; set; }
public int MicrophonePort { get; set; }
public string ErrorMessage { get; set; }
public int HttpServerPort { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static ServerSideConnectRequestManager Instance
/**
* This version number must to be increased when introducing breaking changes.
*/
public const int ProtocolVersion = 1;
public const int ProtocolVersion = 2;

private UdpClient serverUdpClient;
private const int ConnectPortOnServer = 34567;
Expand Down Expand Up @@ -155,7 +155,11 @@ private void HandleClientMessage(IPEndPoint clientIpEndPoint, string message)
}
if (connectRequestDto.ClientName.IsNullOrEmpty())
{
throw new ConnectRequestException("Malformed ConnectRequest: missing clientName.");
throw new ConnectRequestException("Malformed ConnectRequest: missing ClientName.");
}
if (connectRequestDto.ClientId.IsNullOrEmpty())
{
throw new ConnectRequestException("Malformed ConnectRequest: missing ClientId.");
}

if (connectRequestDto.MicrophoneSampleRate > 0)
Expand All @@ -182,19 +186,21 @@ private void HandleClientMessageWithNoMicrophone(IPEndPoint clientIpEndPoint, Co
ConnectResponseDto connectResponseDto = new ConnectResponseDto
{
ClientName = connectRequestDto.ClientName,
ClientId = connectRequestDto.ClientId,
HttpServerPort = httpServer.port,
};
serverUdpClient.Send(connectResponseDto.ToJson(), clientIpEndPoint);
}

private void HandleClientMessageWithMicrophone(IPEndPoint clientIpEndPoint, ConnectRequestDto connectRequestDto)
{
ConnectedClientHandler newConnectedClientHandler = RegisterClient(clientIpEndPoint, connectRequestDto.ClientName, connectRequestDto.MicrophoneSampleRate);
ConnectedClientHandler newConnectedClientHandler = RegisterClient(clientIpEndPoint, connectRequestDto.ClientName, connectRequestDto.ClientId, connectRequestDto.MicrophoneSampleRate);
clientConnectedEventQueue.Enqueue(new ClientConnectionEvent(newConnectedClientHandler, true));

ConnectResponseDto connectResponseDto = new ConnectResponseDto
{
ClientName = connectRequestDto.ClientName,
ClientId = connectRequestDto.ClientId,
HttpServerPort = httpServer.port,
MicrophonePort = newConnectedClientHandler.MicTcpListener.GetPort(),
};
Expand Down Expand Up @@ -235,16 +241,17 @@ public void RemoveConnectedClientHandler(ConnectedClientHandler connectedClientH
private ConnectedClientHandler RegisterClient(
IPEndPoint clientIpEndPoint,
string clientName,
string clientId,
int microphoneSampleRate)
{
// Dispose any currently registered client with the same IP-Address.
if (idToConnectedClientMap.TryGetValue(GetClientId(clientIpEndPoint), out ConnectedClientHandler existingConnectedClientHandler))
if (idToConnectedClientMap.TryGetValue(clientId, out ConnectedClientHandler existingConnectedClientHandler))
{
existingConnectedClientHandler.Dispose();
}

ConnectedClientHandler connectedClientHandler = new ConnectedClientHandler(this, clientIpEndPoint, clientName, microphoneSampleRate);
idToConnectedClientMap[GetClientId(clientIpEndPoint)] = connectedClientHandler;
ConnectedClientHandler connectedClientHandler = new ConnectedClientHandler(this, clientIpEndPoint, clientName, clientId, microphoneSampleRate);
idToConnectedClientMap[clientId] = connectedClientHandler;

Debug.Log("New number of connected clients: " + idToConnectedClientMap.Count);

Expand All @@ -260,9 +267,4 @@ public static bool TryGetConnectedClientHandler(string clientIpEndPointId, out C
{
return idToConnectedClientMap.TryGetValue(clientIpEndPointId, out connectedClientHandler);
}

public static string GetClientId(IPEndPoint clientIpEndPoint)
{
return clientIpEndPoint.Address.ToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ protected override string GetDisplayString(MicProfile micProfile)
}
else
{
return micProfile.IsInputFromConnectedClient && micProfile.IsConnected
? micProfile.Name + $"\n({micProfile.ConnectedClientId})"
: micProfile.Name;
return micProfile.Name;
}
}

Expand Down

0 comments on commit f4d693d

Please sign in to comment.