コード例 #1
0
        internal void GetChallenge()
        {
            _sid = new Random().Next() & 0xCF;
            Packet data = this.GrabData(Packets.Challenge(this._sid));

            if (data.Read <byte>() == (byte)0x09 && data.Read <int>() == this._sid)
            {
                int.TryParse(data.Read <String>(), out this._challenge);
                byte[] iabi = BitConverter.GetBytes(this._challenge);
                Array.Reverse(iabi);
                this._challenge = BitConverter.ToInt32(iabi, 0);

                _ping = Environment.TickCount;

                this.GetInfo();
            }
            else
            {
                GetChallenge();
            }
        }
コード例 #2
0
        internal void GetInfo()
        {
            Packet data = this.GrabData(Packets.QueryData(this._sid, this._challenge));

            if (data == null)
            {
                this.GetChallenge();
                return;
            }

            if (data.Read <byte>() == (byte)0x00 && data.Read <int>() == this._sid)
            {
                _info = new ServerInfo();

                _info.Latency = Environment.TickCount - _ping;

                data.Skip(11); // Unknown padding.

                string key, value;

                while (true)
                {
                    key   = data.Read <string>();
                    value = data.Read <string>();

                    if (key.Length == 0)
                    {
                        break;
                    }

                    if (key == "hostname")
                    {
                        _info.Name = value;
                    }

                    else if (key == "gametype")
                    {
                        _info.GameType = value;
                    }

                    else if (key == "game_id")
                    {
                        _info.GameID = value;
                    }

                    else if (key == "version")
                    {
                        _info.Version = value;
                    }

                    else if (key == "plugins")
                    {
                        _info.Plugins = value;
                    }

                    else if (key == "map")
                    {
                        _info.Map = value;
                    }

                    else if (key == "numplayers")
                    {
                        if (!int.TryParse(value, out _info.OnlinePlayers))
                        {
                            return;
                        }
                    }

                    else if (key == "maxplayers")
                    {
                        if (!int.TryParse(value, out _info.MaxPlayers))
                        {
                            return;
                        }
                    }

                    else if (key == "hostport")
                    {
                        _info.HostPort = value;
                    }

                    else if (key == "hostip")
                    {
                        _info.HostIP = value;
                    }
                }

                data.Skip(1);

                _info.Players = new List <string>();

                while (true)
                {
                    key = data.Read <string>();

                    if (key.Length == 0)
                    {
                        break;
                    }

                    _info.Players.Add(key);
                }

                _success = true;
            }
            else
            {
                GetChallenge();
            }
        }