예제 #1
0
        /// <summary>
        /// Constructs a Habbo Hotel environment and tries to initialize it.
        /// </summary>
        public HabboHotel()
        {
            // Try to parse version
            IonEnvironment.Configuration.TryParseUInt32("projects.habbo.client.version", out mVersion);

            // Initialize HabboHotel project modules
            mExternalTexts      = new KeyValueDictionary("external_texts", "xkey", "xvalue");
            mExternalVariables  = new KeyValueDictionary("external_variables", "xkey", "xvalue");
            mClientManager      = new GameClientManager();
            mHabboManager       = new HabboManager();
            mAuthenticator      = new HabboAuthenticator();
            mUserRightManager   = new UserRightManager(20);
            mAchievementManager = new AchievementManager();
            mMessengerManager   = new MessengerManager();
            mCatalog            = new Catalog.Catalog();

            // Start connection checker for clients
            mClientManager.StartConnectionChecker();

            // Load external texts and external variables
            mExternalTexts.Reload();
            mExternalVariables.Reload();

            // Load user rights
            mUserRightManager.ReloadRights();

            // Load catalog
            mCatalog.ReloadPages();

            // Print that we are done!
            IonEnvironment.GetLog().WriteLine(string.Format("Initialized project 'Habbo Hotel' for version {0}.", mVersion));
        }
예제 #2
0
        /// <summary>
        /// Closes the connections of database clients that have been inactive for too long. Connections can be opened again when needed.
        /// </summary>
        private void MonitorClientsLoop()
        {
            while (true)
            {
                try
                {
                    lock (this)
                    {
                        DateTime dtNow = DateTime.Now;
                        for (int i = 0; i < mClients.Length; i++)
                        {
                            if (mClients[i].State != ConnectionState.Closed)
                            {
                                if (mClients[i].Inactivity >= 60) // Not used in the last %x% seconds
                                {
                                    mClients[i].Disconnect();     // Temporarily close connection

                                    IonEnvironment.GetLog().WriteInformation("Disconnected database client #" + mClients[i].Handle);
                                }
                            }
                        }
                    }

                    Thread.Sleep(10000); // 10 seconds
                }
                catch (ThreadAbortException) { } // Nothing special
                catch (Exception ex)
                {
                    IonEnvironment.GetLog().WriteError(ex.Message);
                }
            }
        }
예제 #3
0
 public void ReleaseClient(uint Handle)
 {
     if (mClients.Length >= (Handle - 1)) // Ensure client exists
     {
         mClientAvailable[Handle - 1] = true;
         IonEnvironment.GetLog().WriteLine("Released client #" + Handle);
     }
 }
예제 #4
0
        /// <summary>
        /// Handles a given amount of data in a given byte array, by attempting to parse messages from the received data and process them in the message handler.
        /// </summary>
        /// <param name="Data">The byte array with the data to process.</param>
        /// <param name="numBytesToProcess">The actual amount of bytes in the byte array to process.</param>
        public void HandleConnectionData(ref byte[] data)
        {
            // Gameclient protocol or policyrequest?
            if (data[0] != 64)
            {
                IonEnvironment.GetLog().WriteInformation("Client " + mID + " sent non-gameclient message: " + IonEnvironment.GetDefaultTextEncoding().GetString(data));

                string xmlPolicy =
                    "<?xml version=\"1.0\"?>\r\n" +
                    "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">\r\n" +
                    "<cross-domain-policy>\r\n" +
                    "<allow-access-from domain=\"*\" to-ports=\"1-31111\" />\r\n" +
                    "</cross-domain-policy>\x0";

                IonEnvironment.GetLog().WriteInformation("Client " + mID + ": sending XML cross domain policy file: " + xmlPolicy);
                mConnection.SendData(xmlPolicy);

                mMessageHandler.GetResponse().Initialize(ResponseOpcodes.SecretKey); // "@A"
                mMessageHandler.GetResponse().Append("ION/Deltar");
                mMessageHandler.SendResponse();
            }
            else
            {
                int pos = 0;
                while (pos < data.Length)
                {
                    try
                    {
                        // Total length of message (without this): 3 Base64 bytes
                        int messageLength = Base64Encoding.DecodeInt32(new byte[] { data[pos++], data[pos++], data[pos++] });

                        // ID of message: 2 Base64 bytes
                        uint messageID = Base64Encoding.DecodeUInt32(new byte[] { data[pos++], data[pos++] });

                        // Data of message: (messageLength - 2) bytes
                        byte[] Content = new byte[messageLength - 2];
                        for (int i = 0; i < Content.Length; i++)
                        {
                            Content[i] = data[pos++];
                        }

                        // Create message object
                        ClientMessage message = new ClientMessage(messageID, Content);

                        // Handle message object
                        mMessageHandler.HandleRequest(message);
                    }
                    catch (IndexOutOfRangeException) // Bad formatting!
                    {
                        IonEnvironment.GetHabboHotel().GetClients().StopClient(mID);
                    }
                    catch (Exception ex)
                    {
                        IonEnvironment.GetLog().WriteUnhandledExceptionError("GameClient.HandleConnectionData", ex);
                    }
                }
            }
        }
예제 #5
0
        public bool TryParseUInt32(string sField, out uint i)
        {
            bool Success = uint.TryParse(this[sField], out i);

            if (!Success)
            {
                IonEnvironment.GetLog().WriteConfigurationParseError(sField);
            }

            return(Success);
        }
예제 #6
0
        public void Destroy()
        {
            // Clear clients
            if (GetClients() != null)
            {
                GetClients().Clear();
                GetClients().StopConnectionChecker();
            }

            IonEnvironment.GetLog().WriteLine(string.Format("Destroyed project 'Habbo Hotel' for version {0}.", mVersion));
        }
예제 #7
0
        public void DropConnection(uint clientID)
        {
            IonTcpConnection connection = GetConnection(clientID);

            if (connection != null)
            {
                IonEnvironment.GetLog().WriteInformation("Dropped IonTcpConnection [" + clientID + "] of " + connection.ipAddress);

                connection.Stop();
                mConnections.Remove(clientID);
            }
        }
예제 #8
0
        private void DataReceived(IAsyncResult iAr)
        {
            // Connection not stopped yet?
            if (this.Alive == false)
            {
                return;
            }

            // Do an optional wait before processing the data
            if (RECEIVEDATA_MILLISECONDS_DELAY > 0)
            {
                Thread.Sleep(RECEIVEDATA_MILLISECONDS_DELAY);
            }

            // How many bytes has server received?
            int numReceivedBytes = 0;

            try
            {
                numReceivedBytes = mSocket.EndReceive(iAr);
            }
            catch (ObjectDisposedException)
            {
                ConnectionDead();
                return;
            }
            catch (Exception ex)
            {
                IonEnvironment.GetLog().WriteUnhandledExceptionError("IonTcpConnection.DataReceived", ex);

                ConnectionDead();
                return;
            }

            if (numReceivedBytes > 0)
            {
                // Copy received data buffer
                byte[] dataToProcess = ByteUtility.ChompBytes(mDataBuffer, 0, numReceivedBytes);

                // Decipher received data?
                if (mEncryptionStarted)
                {
                    dataToProcess = mRc4.Decipher(dataToProcess, numReceivedBytes);
                }

                // Route data to GameClient to parse and process messages
                RouteData(ref dataToProcess);
                //Environment.GetHabboHotel().GetClients().GetClient(this.ID).HandleConnectionData(ref dataToProcess);
            }

            // Wait for new data
            WaitForData();
        }
예제 #9
0
        /// <summary>
        /// Creates an IonTcpConnection instance for a given socket and assigns it an unique ID.
        /// </summary>
        /// <param name="pSocket">The System.Net.Socket.Sockets object to base the connection on.</param>
        /// <returns>IonTcpConnection</returns>
        public IonTcpConnection CreateConnection(Socket pSocket)
        {
            if (pSocket == null)
            {
                return(null);
            }

            IonTcpConnection connection = new IonTcpConnection(++mConnectionCounter, pSocket);

            IonEnvironment.GetLog().WriteInformation(string.Format("Created IonTcpConnection [{0}] for {1}.", connection.ID, connection.ipAddress));

            return(connection);
        }
예제 #10
0
        /// <summary>
        /// Constructs an IonTcpConnection listener and binds it to a given local IP address and TCP port.
        /// </summary>
        /// <param name="sLocalIP">The IP address string to parse and bind the listener to.</param>
        /// <param name="Port">The TCP port number to parse the listener to.</param>
        public IonTcpConnectionListener(string sLocalIP, int Port, IonTcpConnectionManager pManager)
        {
            IPAddress pIP = null;

            if (!IPAddress.TryParse(sLocalIP, out pIP))
            {
                pIP = IPAddress.Loopback;
                IonEnvironment.GetLog().WriteWarning(string.Format("Connection listener was unable to parse the given local IP address '{0}', now binding listener to '{1}'.", sLocalIP, pIP.ToString()));
            }

            mListener = new TcpListener(pIP, Port);
            mConnectionRequestCallback = new AsyncCallback(ConnectionRequest);
            mFactory = new IonTcpConnectionFactory();
            mManager = pManager;

            IonEnvironment.GetLog().WriteLine(string.Format("IonTcpConnectionListener initialized and bound to {0}:{1}.", pIP.ToString(), Port.ToString()));
        }
예제 #11
0
        /// <summary>
        /// Invokes the matching request handler for a given ClientMessage.
        /// </summary>
        /// <param name="request">The ClientMessage object to process.</param>
        public void HandleRequest(ClientMessage request)
        {
            IonEnvironment.GetLog().WriteLine("[" + mSession.ID + "] --> " + request.Header + request.GetContentString());

            if (request.ID > HIGHEST_MESSAGEID)
            {
                return; // Not in protocol
            }
            if (mRequestHandlers[request.ID] == null)
            {
                return; // Handler not registered
            }
            // Handle request
            Request = request;
            mRequestHandlers[request.ID].Invoke();
            Request = null;
        }
예제 #12
0
        public void StopClient(uint clientID)
        {
            GameClient client = GetClient(clientID);

            if (client != null)
            {
                // Stop & drop connection
                IonEnvironment.GetTcpConnections().DropConnection(clientID);

                // Stop client
                client.Stop();

                // Drop client
                mClients.Remove(clientID);

                // Log event
                IonEnvironment.GetLog().WriteInformation("Stopped client " + clientID);
            }
        }
예제 #13
0
        public static MessengerBuddy Parse(DataRow row)
        {
            MessengerBuddy buddy = new MessengerBuddy();

            try
            {
                buddy.mID       = (uint)row["id"];
                buddy.mUsername = (string)row["username"];
                buddy.mFigure   = (string)row["figure"];
                buddy.mMotto    = (string)row["motto"];

                return(buddy);
            }
            catch (Exception ex)
            {
                IonEnvironment.GetLog().WriteUnhandledExceptionError("MessengerBuddy.Parse", ex);
            }

            return(null);
        }
예제 #14
0
 public void SendData(byte[] Data)
 {
     if (this.Alive)
     {
         try
         {
             mSocket.Send(Data);
         }
         catch (SocketException)
         {
             ConnectionDead();
         }
         catch (ObjectDisposedException)
         {
             ConnectionDead();
         }
         catch (Exception ex)
         {
             IonEnvironment.GetLog().WriteUnhandledExceptionError("IonTcpConnection.Send", ex);
         }
     }
 }
예제 #15
0
        public static CatalogPage Parse(DataRow row)
        {
            try
            {
                CatalogPage page = new CatalogPage();
                page.mID         = (uint)row["id"];
                page.mParentID   = (uint)row["parentid"];
                page.mTreeID     = (uint)row["treeid"];
                page.mVisible    = ((byte)row["visible"] == 1);
                page.mComingSoon = ((byte)row["comingsoon"] == 1);
                page.mName       = (string)row["name"];
                page.mIconColor  = (byte)row["icon_color"];
                page.mIconImage  = (byte)row["icon_image"];

                return(page);
            }
            catch (Exception ex)
            {
                IonEnvironment.GetLog().WriteUnhandledExceptionError("CatalogPage.Parse", ex);
            }

            return(null);
        }
예제 #16
0
 /// <summary>
 /// Starts the asynchronous wait for new data.
 /// </summary>
 private void WaitForData()
 {
     if (this.Alive)
     {
         try
         {
             mSocket.BeginReceive(mDataBuffer, 0, RECEIVEDATA_BUFFER_SIZE, SocketFlags.None, mDataReceivedCallback, null);
         }
         catch (SocketException)
         {
             ConnectionDead();
         }
         catch (ObjectDisposedException)
         {
             ConnectionDead();
         }
         catch (Exception ex)
         {
             IonEnvironment.GetLog().WriteUnhandledExceptionError("IonTcpConnection.WaitForData", ex);
             ConnectionDead();
         }
     }
 }
예제 #17
0
        public void SendMessage(ServerMessage message)
        {
            IonEnvironment.GetLog().WriteLine(" [" + mID + "] <-- " + message.Header + message.GetContentString());

            SendData(message.GetBytes());
        }
예제 #18
0
        public DatabaseClient GetClient()
        {
            // Let other threads wait if they contact this DatabaseManager while we're busy with it
            lock (this)
            {
                // Try to find an available client
                for (uint i = 0; i < mClients.Length; i++)
                {
                    // Somebody here?
                    if (mClientAvailable[i] == true)
                    {
                        // No starvation anymore
                        mClientStarvationCounter = 0;

                        // Is this connection broken?
                        //if (mClients[i].State == ConnectionState.Broken)
                        //{
                        //    mClients[i] = new DatabaseClient((i + 1), this); // Create new client
                        //}

                        // Is this connection closed?
                        if (mClients[i].State == ConnectionState.Closed)
                        {
                            // TODO: exception handling
                            mClients[i].Connect();

                            IonEnvironment.GetLog().WriteInformation("Opening connection for database client #" + mClients[i].Handle);
                        }

                        // Is this client ready?
                        if (mClients[i].State == ConnectionState.Open)
                        {
                            IonEnvironment.GetLog().WriteLine("Handed out client #" + mClients[i].Handle);

                            mClientAvailable[i] = false; // BRB

                            mClients[i].UpdateLastActivity();
                            return(mClients[i]);
                        }
                    }
                }

                // No clients?
                mClientStarvationCounter++;

                // Are we having a structural lack of clients?
                if (mClientStarvationCounter >= ((mClients.Length + 1) / 2)) // Database hungry much?
                {
                    // Heal starvation
                    mClientStarvationCounter = 0;

                    // Increase client amount by 0.3
                    SetClientAmount((uint)(mClients.Length + 1 * 1.3f));

                    // Re-enter this method
                    return(GetClient());
                }

                DatabaseClient pAnonymous = new DatabaseClient(0, this);
                pAnonymous.Connect();

                IonEnvironment.GetLog().WriteLine("Handed out anonymous client.");

                return(pAnonymous);
            }
        }