Ejemplo n.º 1
0
        private void RunListener()
        {
            while (_mIsOpen && _mPort >= 0)
            {
                try
                {
                    if (_listener.Pending())
                    {
                        TcpClient           socket = _listener.AcceptTcpClient();
                        TcpServerConnection conn   = new TcpServerConnection(socket, _mEncoding);

                        if (OnConnect != null)
                        {
                            lock (_activeThreadsLock)
                            {
                                _activeThreads++;
                            }
                            conn.CallbackThread = new Thread(() =>
                            {
                                OnConnect(conn);
                            });
                            conn.CallbackThread.Start();
                        }

                        lock (_connections)
                        {
                            _connections.Add(conn);
                        }
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(_mIdleTime);
                    }
                }
                catch (ThreadInterruptedException) { } //thread is interrupted when we quit
                catch (Exception e)
                {
                    if (_mIsOpen && OnError != null)
                    {
                        OnError(this, e);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private bool ProcessConnection(TcpServerConnection conn)
        {
            bool moreWork = false;

            if (conn.ProcessOutgoing(_mMaxSendAttempts))
            {
                moreWork = true;
            }

            if (OnDataAvailable != null && _activeThreads < _mMaxCallbackThreads && conn.Socket.Available > 0)
            {
                lock (_activeThreadsLock)
                {
                    _activeThreads++;
                }
                conn.CallbackThread = new Thread(() =>
                {
                    OnDataAvailable(conn);
                });
                conn.CallbackThread.Start();
                Thread.Yield();
            }
            return(moreWork);
        }