예제 #1
0
파일: TimedLock.cs 프로젝트: radtek/Pos
        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);
        }
예제 #2
0
파일: TCP.cs 프로젝트: radtek/Pos
        /// <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);
            }
        }