private void ClientAuthenticationed(TcpClientManager tcpClient) { lock (_clientSockets) { var unUsedCLients = _clientSockets.Where(obj => obj.ClientDevice != null && obj.DeviceGuid == tcpClient.DeviceGuid && obj.ClientGuid != tcpClient.ClientGuid) .Select(item => item.DeviceGuid) .ToList(); foreach (var client in unUsedCLients) { var unUsedCLient = _clientSockets.First(obj => obj.DeviceGuid == client); unUsedCLient.Dispose(); try { unUsedCLient.ClientAuthenticationEvent -= ClientAuthenticationed; unUsedCLient.ClientDisconnectEvent -= ClientDisconnected; _clientSockets.Remove(unUsedCLient); } catch (Exception ex) { LogService.Instance.Fatal($"尝试删除连接失效设备失败,目标设备:{unUsedCLient.DeviceGuid}", ex); } } } AdminReportService.Instance.Info($"客户端授权通过,客户端设备NODEID:{tcpClient.ClientDevice.DeviceNodeId}"); }
/// <summary> /// 添加一个客户端 /// </summary> /// <param name="client"></param> public void AddClient(Socket client) { var tcpClientManager = new TcpClientManager(client) { ReceiverName = $"UnIdentified - {client.RemoteEndPoint}" }; tcpClientManager.ClientDisconnectEvent += ClientDisconnected; tcpClientManager.ClientAuthenticationEvent += ClientAuthenticationed; client.BeginReceive(tcpClientManager.ReceiveBuffer, SocketFlags.None, tcpClientManager.Received, client); lock (_clientSockets) { _clientSockets.Add(tcpClientManager); } }
/// <summary> /// 客户端断开连接事件 /// </summary> /// <param name="tcpClient">客户端连接</param> private void ClientDisconnected(TcpClientManager tcpClient) { lock (_clientSockets) { try { tcpClient.ClientAuthenticationEvent -= ClientAuthenticationed; tcpClient.ClientDisconnectEvent -= ClientDisconnected; _clientSockets.Remove(tcpClient); } catch (Exception ex) { LogService.Instance.Fatal($"尝试删除设备失败,目标设备:{tcpClient.DeviceGuid}", ex); return; } } if (tcpClient.IsConnected) { AdminReportService.Instance.Info($"客户端连接断开,客户端信息:{tcpClient.ReceiverName}"); } }