private bool RemoveClientThreadSafe(TcpClientInfo client) { try { clientsLocker.AcquireWriterLock(LockTimeout); } catch (ApplicationException) { Logger.Error("RemoveClientThreadSafe - lock timeout"); return(false); } try { clients.Remove(client); return(true); } finally { clientsLocker.ReleaseWriterLock(); } }
private void DoAcceptTcpClientCallback(IAsyncResult ar) { var lst = (TcpListener)ar.AsyncState; TcpClient client = lst.EndAcceptTcpClient(ar); try { clientsLocker.AcquireWriterLock(LockTimeout); } catch { Logger.Error("TCPDistributor: deadlock при добавлении клиента"); tcpClientConnected.Set(); return; } try { var cliInf = new TcpClientInfo(client); clients.Add(cliInf); Logger.InfoFormat("Клиент [{0}:{1}] подключился", ((IPEndPoint)client.Client.LocalEndPoint).Address, ((IPEndPoint)client.Client.LocalEndPoint).Port); cliInf.receiveThread = new Thread(ThreadReceiveLoop); cliInf.receiveThread.Start(cliInf); } catch (Exception ex) { Logger.Error("TCPDistributor:DoAcceptTcpClientCallback", ex); } finally { clientsLocker.ReleaseWriterLock(); } // signal the calling thread to continue. tcpClientConnected.Set(); }
private bool RemoveClientThreadSafe(TcpClientInfo client) { try { clientsLocker.AcquireWriterLock(LockTimeout); } catch (ApplicationException) { Logger.Error("RemoveClientThreadSafe - lock timeout"); return false; } try { clients.Remove(client); return true; } finally { clientsLocker.ReleaseWriterLock(); } }