Exemple #1
0
        public static TimedLock Lock(object o, TimeSpan timeout)
        {
            TimedLock tl = new TimedLock(o);

            if (!Monitor.TryEnter(o, timeout))
            {
#if DEBUG
                System.GC.SuppressFinalize(tl.leakDetector);
#endif
                throw new LockTimeoutException();
            }

            return(tl);
        }
Exemple #2
0
        /// <summary>
        /// Wait for client connections and start new communication thread
        /// </summary>
        private void ListenForClients()
        {
            try
            {
                tcpListener.Start();
                while (Listenthread.ThreadState == System.Threading.ThreadState.Running && !Halt)
                {
                    try
                    {
                        // AcceptSocket blocks until new connection has established
                        LogMsg = "Thread Listening on " + serverAddress + ":" + port.ToString();
                        Console.WriteLine(LogMsg);
                        TcpClient client = tcpListener.AcceptTcpClient();

                        LogMsg = "Client connected : " + client.Client.RemoteEndPoint;
                        Console.WriteLine(LogMsg);
                        using (TimedLock.Lock(ClientSockets.SyncRoot))
                        {
                            int i = ClientSockets.Add(new ClientHandler(client));
                            ((ClientHandler)ClientSockets[i]).Start();
                        }
                    }
                    catch (SystemException ex)
                    {
                        // An exception will be thrown when tcpListerner.Stop() is called
                        LogMsg = "Listen Thread SystemException: " + port.ToString() + " " + ex.Message;
                        Console.WriteLine(LogMsg);
                    }
                }
            }
            finally
            {
                LogMsg = "Server Listen Thread EXITED! " + port.ToString();
                Console.WriteLine(LogMsg);
            }
        }