Example #1
0
        public void Listen()
        {
            string username;
            string recv;
            bool   fireCallback = true;

            active = true;
            try
            {
                Interlocked.Increment(ref Lib.connections);
                string clientip = userSocket.ClientEndpointId;
                Lib.log.Add(userSocket, null, "CONNECT (" + userSocket.ClientEndpointId + ") Online:" + Lib.connections);
                Commandprocessor cp = new Commandprocessor();

                // Send the welcome screen to the user.
                userSocket.Send(Lib.Welcomescreen);
                userSocket.Send(Lib.Ansifboldyellow + "Developers: " + Lib.Ansifboldwhite + Lib.Creditsdev + "\r\n");
                userSocket.Send(Lib.Ansifboldyellow + "Special Thanks: " + Lib.Ansifboldwhite + Lib.Creditsother + "\r\n\r\n" + Lib.Ansifwhite);

                // Authenticate this user.
                username = Lib.Authenticate(userSocket, cp);
                if (username == null)
                {
                    // detected some socket error
                    return;
                }

                if (username == null)
                {
                    try
                    {
                        // If the user is already disconnected, this might fail so catch the exception
                        userSocket.SendLine("Too many bad login attempts. Byte!");
                        Lib.Disco(userSocket, "too many bad logon attempts");
                    }
                    catch
                    {
                        return;
                    }
                    return;
                }

                // Log this user as connected
                Actor theUser = Lib.GetByName(username);
                //theUser["connected"] = true;
                // Set the user's client socket
                theUser.UserSocket = this.userSocket;

                //theUser.Save();

                while (enabled)
                {
                    try
                    {
                        // if user disco, then socket is gone and we need to catch this exception
                        recv = userSocket.GetResponse();
                    }
                    catch
                    {
                        //Lib.PrintLine(DateTime.Now + " EXCEPTION in ClientListener (Waiting for client response): " + ex.Message + ex.StackTrace);
                        // We should rather log these exceptions to the console
                        // or to a log file
                        return;
                    }
                    if (recv == null)
                    {
                        return;
                    }
                    cp.processcommand(userSocket, username, recv, false);
                }
                Lib.Disco(userSocket, "client listener switched off with the stoplistening method");
            }
            catch (ThreadAbortException ex)
            {
                // Signal that we should just quit and not fire the
                // callback
                fireCallback = false;
                //Lib.PrintLine(DateTime.Now + " EXCEPTION in ClientListener (Thread aborted): " + ex.Message + ex.StackTrace);
            }
            finally
            {
                if (fireCallback)
                {
                    // Fire the callback event
                    IAsyncResult ar = clientDisconnectedEventHandler.BeginInvoke(this,
                                                                                 null,
                                                                                 null);
                }

                // We are no longer active
                active = false;
            }
        }