Esempio n. 1
0
        private void AcceptLoop()
        {
            try
            {
                _listener = new TcpListener(_ipAddress, _port);
                _listener.Start();

                _clients = new List <LircClient>();

                Logger.LogInfo("LircOutputPin: Succesfully started on address {0}:{1} ...", _ipAddress, _port);

                while (!_acceptThreadStopEvt.WaitOne(50))
                {
                    while (_listener.Pending() && !_acceptThreadStopEvt.WaitOne(50))
                    {
                        // There are some queued connection attempts
                        // Proceed with accept
                        TcpClient client = _listener.AcceptTcpClient();
                        if (_clients.Count >= MaxClientsCount - 1)
                        {
                            // Sorry ... no more clients allowed.

                            Logger.LogInfo("LircOutputPin: Server is full. Rejecting address {0} ...",
                                           client.Client.RemoteEndPoint);

                            client.Close();
                        }
                        else
                        {
                            LircClient lircClient = new LircClient(client);
                            _clients.Add(lircClient);

                            Logger.LogInfo("LircOutputPin: New incoming connection from address {0} (Total active: {1})",
                                           lircClient.RemoteEPAddress, _clients.Count);
                        }
                    }

                    foreach (LircClient client in _clients.ToArray())
                    {
                        if (client != null)
                        {
                            if (client.Inactive)
                            {
                                client.Close();
                                _clients.Remove(client);

                                Logger.LogInfo("LircOutputPin: Connection from address {0} is no longer active, closing it (Total active: {1})",
                                               client.RemoteEPAddress, _clients.Count);
                            }
                        }
                        else
                        {
                            _clients.Remove(client);
                            Logger.LogWarning("LircOutputPin: Found null address in clients table. This usually indicates a problem, removing the garbage. (Total active: {0})", _clients.Count);
                        }
                    }
                }

                Logger.LogInfo("LircOutputPin: Preparing to stop. Closing all connected clients.");

                // Thread is stopping. Disconnect all accepted clients.
                foreach (LircClient client in _clients)
                {
                    if (client != null)
                    {
                        Logger.LogInfo("LircOutputPin: Disconnecting from address {0} ...",
                                       client.RemoteEPAddress);

                        client.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }
Esempio n. 2
0
        private void AcceptLoop()
        {
            try
            {
                _listener = new TcpListener(_ipAddress, _port);
                _listener.Start();

                _clients = new List<LircClient>();

                Logger.LogInfo("LircOutputPin: Succesfully started on address {0}:{1} ...", _ipAddress, _port);

                while (!_acceptThreadStopEvt.WaitOne(50))
                {
                    while (_listener.Pending() && !_acceptThreadStopEvt.WaitOne(50))
                    {
                        // There are some queued connection attempts
                        // Proceed with accept
                        TcpClient client = _listener.AcceptTcpClient();
                        if (_clients.Count >= MaxClientsCount - 1)
                        {
                            // Sorry ... no more clients allowed.

                            Logger.LogInfo("LircOutputPin: Server is full. Rejecting address {0} ...",
                                client.Client.RemoteEndPoint);

                            client.Close();
                        }
                        else
                        {
                            LircClient lircClient = new LircClient(client);
                            _clients.Add(lircClient);

                            Logger.LogInfo("LircOutputPin: New incoming connection from address {0} (Total active: {1})",
                                lircClient.RemoteEPAddress, _clients.Count);
                        }
                    }

                    foreach (LircClient client in _clients.ToArray())
                    {
                        if (client != null)
                        {
                            if (client.Inactive)
                            {
                                client.Close();
                                _clients.Remove(client);
                                
                                Logger.LogInfo("LircOutputPin: Connection from address {0} is no longer active, closing it (Total active: {1})",
                                    client.RemoteEPAddress, _clients.Count);

                            }
                        }
                        else
                        {
                            _clients.Remove(client);
                            Logger.LogWarning("LircOutputPin: Found null address in clients table. This usually indicates a problem, removing the garbage. (Total active: {0})", _clients.Count);
                        }
                    }
                  
                }

                Logger.LogInfo("LircOutputPin: Preparing to stop. Closing all connected clients.");

                // Thread is stopping. Disconnect all accepted clients.
                foreach (LircClient client in _clients)
                {
                    if (client != null)
                    {
                        Logger.LogInfo("LircOutputPin: Disconnecting from address {0} ...",
                                    client.RemoteEPAddress);

                        client.Close();
                    }
                }

            }
            catch(Exception ex)
            {
                Logger.LogException(ex);
            }
        }