Beispiel #1
0
        private async Task <IGameServerRules> QueryServerRulesAsync(TimeSpan timeout)
        {
            var task = Task.Run(async() =>
            {
                using (var client = SteamAgent.GetUdpClient(EndPoint))
                {
                    var requestPacket = GetRequestPacket(GameServerQueryPacketType.RulesRequest);
                    requestPacket.WriteLong(-1);

                    var reader = await SteamAgent.RequestResponseAsync(client, requestPacket.ToArray(), 4);

                    var responseType = reader.ReadEnum <GameServerQueryPacketType>();

                    if (responseType == GameServerQueryPacketType.RulesResponse)
                    {
                        throw new NotImplementedException();
                    }

                    if (responseType != GameServerQueryPacketType.RequestChallenge)
                    {
                        throw new ProtocolViolationException();
                    }

                    var challengeNumber = reader.ReadLong();
                    requestPacket       = GetRequestPacket(GameServerQueryPacketType.RulesRequest);
                    requestPacket.WriteLong(challengeNumber);

                    reader = await SteamAgent.RequestResponseAsync(client, requestPacket.ToArray(), 16); //seems not to agree with protocol, would expect this 11 bytes earlier...

                    responseType = reader.ReadEnum <GameServerQueryPacketType>();

                    if (responseType != GameServerQueryPacketType.RulesResponse)
                    {
                        throw new ProtocolViolationException();
                    }

                    var ruleCount = reader.ReadShort();
                    var rules     = new List <GameServerRule>(ruleCount);

                    var packetsReceived = 1;
                    Func <Task <BufferReader> > sequelRequestAsyncFunc = async() =>
                    {
                        var next = await SteamAgent.ReceiveBufferReaderAsync(client, 12); //protocol unclear, header size determined manually
                        packetsReceived++;
                        return(next);
                    };

                    var multiPacketStringReader = new MultiPacketStringReader(reader, sequelRequestAsyncFunc);

                    for (var i = 0; i < ruleCount; i++)
                    {
                        var key   = await multiPacketStringReader.ReadStringAsync();
                        var value = await multiPacketStringReader.ReadStringAsync();

                        rules.Add(new GameServerRule(key, value));
                    }

                    _rules = new GameServerRules(rules);
                    return(_rules);
                }
            });

            return(await task.TimeoutAfter(timeout));
        }
Beispiel #2
0
        private async Task<IGameServerRules> QueryServerRulesAsync(TimeSpan timeout)
        {
            var task = Task.Run(async () =>
            {
                using (var client = SteamAgent.GetUdpClient(EndPoint))
                {
                    var requestPacket = GetRequestPacket(GameServerQueryPacketType.RulesRequest);
                    requestPacket.WriteLong(-1);

                    var reader = await SteamAgent.RequestResponseAsync(client, requestPacket.ToArray(), 4);

                    var responseType = reader.ReadEnum<GameServerQueryPacketType>();

                    if (responseType == GameServerQueryPacketType.RulesResponse)
                        throw new NotImplementedException();

                    if (responseType != GameServerQueryPacketType.RequestChallenge)
                        throw new ProtocolViolationException();

                    var challengeNumber = reader.ReadLong();
                    requestPacket = GetRequestPacket(GameServerQueryPacketType.RulesRequest);
                    requestPacket.WriteLong(challengeNumber);

                    reader = await SteamAgent.RequestResponseAsync(client, requestPacket.ToArray(), 16); //seems not to agree with protocol, would expect this 11 bytes earlier...

                    responseType = reader.ReadEnum<GameServerQueryPacketType>();

                    if (responseType != GameServerQueryPacketType.RulesResponse)
                        throw new ProtocolViolationException();

                    var ruleCount = reader.ReadShort();
                    var rules = new List<GameServerRule>(ruleCount);

                    var packetsReceived = 1;
                    Func<Task<BufferReader>> sequelRequestAsyncFunc = async () =>
                    {
                        var next = await SteamAgent.ReceiveBufferReaderAsync(client, 12); //protocol unclear, header size determined manually
                        packetsReceived++;
                        return next;
                    };

                    var multiPacketStringReader = new MultiPacketStringReader(reader, sequelRequestAsyncFunc);

                    for (var i = 0; i < ruleCount; i++)
                    {
                        var key = await multiPacketStringReader.ReadStringAsync();
                        var value = await multiPacketStringReader.ReadStringAsync();

                        rules.Add(new GameServerRule(key, value));
                    }

                    _rules = new GameServerRules(rules);
                    return _rules;
                }
            });

            return await task.TimeoutAfter(timeout);
        }