예제 #1
0
        /// <summary>
        ///     Spawns a new client simulation thread.
        /// </summary>
        private bool SpawnClient()
        {
            string arbitratorHost = "localhost";
            ushort arbitratorPort = 0;

            // Select all arbitrators in the database, ordered by which have hte most clients
            // registered to them.
            DBResults results = m_databaseConnection.Query(@"SELECT 
                                                                a.id,
                                                                a.ip_address,
                                                                a.port,
                                                                (
                                                                    SELECT 
                                                                        COUNT(*)
                                                                    FROM
                                                                        {0} AS c
                                                                    WHERE
                                                                        c.arbitrator_id = a.id
                                                                ) AS client_count,
                                                                a.last_active_timestamp
                                                            FROM 
                                                                {1} AS a
                                                            ORDER BY
                                                                client_count ASC,
                                                                a.last_active_timestamp DESC
                                                                ",
                                                            Settings.DB_TABLE_ACTIVE_CLIENTS,
                                                            Settings.DB_TABLE_ACTIVE_ARBITRATORS);

            // Did we get any arbitrators we can use?
            if (results.RowsAffected <= 0)
            {
                return false;
            }

            arbitratorHost = (string)results[0]["ip_address"];
            arbitratorPort = (ushort)((int)results[0]["port"]);

            if (arbitratorHost == HardwareHelper.GetLocalIPAddress())
            {
                arbitratorHost = "localhost";
            }

            // Create peer thread.
            ClientSimulatorThread new_thread = new ClientSimulatorThread(arbitratorHost, arbitratorPort);
            new_thread.Run(m_settings.Clone());
            m_threads.Add(new_thread);

            return true;
        }
예제 #2
0
        /// <summary>
        ///     Gets position information about a given client state.
        /// </summary>
        private bool GetStateInfo(SimulatorClientState state, out bool registering, out bool unregistering, out float x, out float y, out ClientSimulatorThread realClientThread)
        {
            bool found = false;

            registering = false;
            unregistering = false;
            x = state.Account.PeristentState.X;
            y = state.Account.PeristentState.Y;
            realClientThread = null;

            if (state.LastClientState != null)
            {
                x = state.LastClientState.X;
                y = state.LastClientState.Y;
            }

            foreach (SimulatorThread thread in m_simulator.Threads)
            {
                ClientSimulatorThread clientThread = (thread as ClientSimulatorThread);
                if (clientThread == null || clientThread.Service == null)
                {
                    continue;
                }

                if (clientThread.Service.CurrentZone == null ||
                    clientThread.Service.CurrentZone.ID != (int)state.DatabaseSettings["zone_id"])
                {
                    continue;
                }

                if (clientThread.Service.ClientID == (int)state.DatabaseSettings["id"])
                {
                    registering = clientThread.Service.RegisteringWithSuperPeers;
                    unregistering = clientThread.Service.UnregisteringWithSuperPeers;
                    realClientThread = clientThread;
                }

                // Look to see if the position information for this peer is in this clients world state.
                if (clientThread.Service.WorldState != null)
                {
                    foreach (SuperPeerWorldStatePlayerInfo peerInfo in clientThread.Service.WorldState.Peers)
                    {
                        if (peerInfo.ClientID == (int)state.DatabaseSettings["id"])
                        {
                            x = peerInfo.Account.PeristentState.X;
                            y = peerInfo.Account.PeristentState.Y;
                            state.LastClientState = peerInfo.Account.PeristentState;
                            found = true;
                            break;
                        }
                    }
                }
            }

            return found;
        }