예제 #1
0
 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();
     }
 }
예제 #2
0
        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();
        }
예제 #3
0
 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();
     }
 }
예제 #4
0
        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();
        }