private async Task CleanupHangClients2() { await Task.Delay(1000).ConfigureAwait(false); int count = clients.Count; if (count == 0) { this._cleanupHangClients = null; return; } var buffer = new UdpClientEx[count]; clients.Values.CopyTo(buffer, 0); foreach (var udpClient in buffer) { if (udpClient.lastActivity + TimeSpan.FromSeconds(60) < DateTime.UtcNow) { if (clients.TryRemove(udpClient.ClientEndpoint, out var c)) { udpClient.Stop(); } } } await CleanupHangClients2(); }
internal bool Start() { if (_client != null) { _client.Close(); } _client = new UdpClientEx(_context) { Port = this.Port, IP = this.IP, }; _client.SendPacket += this.SendPacketEvent; _client.ReceivePacket += this.ReceivePacketEvent; //监听端口开始收信 if (!_client.Listen()) { return(false); } return(true); }
public bool Listen() { this.SecurityKeys = SecurityFactory.GenerateKeys(); if (_client != null) { _client.Close(); } _client = new UdpClientEx(_context) { Port = this.Port, IP = this.IP, SecurityKeys = SecurityKeys }; _client.SendPacket += this.SendPacketEvent; _client.ReceivePacket += this.ReceivePacketEvent; //监听端口开始收信 if (!_client.Listen()) { return(false); } //文件或者大图片发送监听启动 if (_fileTransTcpListener != null) { _fileTransTcpListener.Close(); } _fileTransTcpListener = new FileTransportTcpListener(_context); _fileTransTcpListener.SecurityKeys = this.SecurityKeys; _fileTransTcpListener.ProgressChanged += FileSendProgressChanged; _fileTransTcpListener.Completed += FileSendCompleted; _fileTransTcpListener.Error += FileSendError; _fileTransTcpListener.Listen(IP, this.Port); return(true); }