Bauglir.Ex.WebSocketServer.Execute C# (CSharp) Method

Execute() protected method

thread function this function actually waits for incomming connections and spawns new server threads
protected Execute ( ) : void
return void
        protected void Execute()
        {
            WebSocketServerConnection c;
            X509Certificate2 serverCertificate = null;
            TcpClient client;
            SslStream sslStream;

            if (fSsl)
            {
                serverCertificate = new X509Certificate2(fSslCertificate);
            }
            fIsRunning = true;
            while (!fTerminated)
            {
                try
                {

                    if (listener.Pending())
                    {

                        client = listener.AcceptTcpClient();
                        sslStream = null;
                        try
                        {

                            if (fSsl)
                            {
                                sslStream = new SslStream(client.GetStream(), false);
                                sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls | SslProtocols.Ssl3 | SslProtocols.Ssl2, true);
                            }
                        }
                        catch
                        {
                            client.Close();
                            client = null;
                        }
                        if (client != null)
                        {
                            c = AddConnection(client, sslStream);
                            if (c != null)
                            {
                                LockConnections();
                                fConnections.Add(c);
                                UnlockConnections();
                                if (AfterAddConnection != null) AfterAddConnection(this, c);
                                c.StartRead();
                            }
                        }
                    }
                    else
                    {
                        Thread.Sleep(250);
                    }
                }
                catch (SocketException e)
                {
                    if (SocketError != null) SocketError(this, e);
                    break;
                }
            }
            LockConnections();
            for (int i = fConnections.Count - 1; i >= 0; i--)
            {
                c = fConnections[i];
                fConnections.Remove(c);
                c.Close(WebSocketCloseCode.Shutdown);
            }
            UnlockConnections();
            listener.Stop();
            fIsRunning = false;
        }