From f4d693d544f2e5ee4dc9e567aee1f7dcba480d88 Mon Sep 17 00:00:00 2001 From: Andreas Stange Date: Sun, 11 Apr 2021 13:57:28 +0200 Subject: [PATCH] - use ClientId sent by companion app - 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 --- .../Common/Network/ConnectedClientHandler.cs | 12 ++++++---- .../Common/Network/Dto/ConnectRequestDto.cs | 1 + .../Common/Network/Dto/ConnectResponseDto.cs | 1 + .../ServerSideConnectRequestManager.cs | 24 ++++++++++--------- .../RecordingOptions/RecordingDeviceSlider.cs | 4 +--- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/UltraStar Play/Assets/Common/Network/ConnectedClientHandler.cs b/UltraStar Play/Assets/Common/Network/ConnectedClientHandler.cs index e2701c4e8..e33efa9c6 100644 --- a/UltraStar Play/Assets/Common/Network/ConnectedClientHandler.cs +++ b/UltraStar Play/Assets/Common/Network/ConnectedClientHandler.cs @@ -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; @@ -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]; diff --git a/UltraStar Play/Assets/Common/Network/Dto/ConnectRequestDto.cs b/UltraStar Play/Assets/Common/Network/Dto/ConnectRequestDto.cs index e6952b68b..cd8aed877 100644 --- a/UltraStar Play/Assets/Common/Network/Dto/ConnectRequestDto.cs +++ b/UltraStar Play/Assets/Common/Network/Dto/ConnectRequestDto.cs @@ -2,5 +2,6 @@ { public int ProtocolVersion { get; set; } public string ClientName { get; set; } + public string ClientId { get; set; } public int MicrophoneSampleRate { get; set; } } diff --git a/UltraStar Play/Assets/Common/Network/Dto/ConnectResponseDto.cs b/UltraStar Play/Assets/Common/Network/Dto/ConnectResponseDto.cs index f47eab041..0a7a165e6 100644 --- a/UltraStar Play/Assets/Common/Network/Dto/ConnectResponseDto.cs +++ b/UltraStar Play/Assets/Common/Network/Dto/ConnectResponseDto.cs @@ -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; } diff --git a/UltraStar Play/Assets/Common/Network/ServerSideConnectRequestManager.cs b/UltraStar Play/Assets/Common/Network/ServerSideConnectRequestManager.cs index 60544af12..5aa7e5e34 100644 --- a/UltraStar Play/Assets/Common/Network/ServerSideConnectRequestManager.cs +++ b/UltraStar Play/Assets/Common/Network/ServerSideConnectRequestManager.cs @@ -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; @@ -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) @@ -182,6 +186,7 @@ private void HandleClientMessageWithNoMicrophone(IPEndPoint clientIpEndPoint, Co ConnectResponseDto connectResponseDto = new ConnectResponseDto { ClientName = connectRequestDto.ClientName, + ClientId = connectRequestDto.ClientId, HttpServerPort = httpServer.port, }; serverUdpClient.Send(connectResponseDto.ToJson(), clientIpEndPoint); @@ -189,12 +194,13 @@ private void HandleClientMessageWithNoMicrophone(IPEndPoint clientIpEndPoint, Co 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(), }; @@ -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); @@ -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(); - } } diff --git a/UltraStar Play/Assets/Scenes/Options/RecordingOptions/RecordingDeviceSlider.cs b/UltraStar Play/Assets/Scenes/Options/RecordingOptions/RecordingDeviceSlider.cs index c6a27a023..7b90e7c97 100644 --- a/UltraStar Play/Assets/Scenes/Options/RecordingOptions/RecordingDeviceSlider.cs +++ b/UltraStar Play/Assets/Scenes/Options/RecordingOptions/RecordingDeviceSlider.cs @@ -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; } }