public async Task ReadsSinglePacketString()
        {
            var bytes = new byte[] { 65, 65, 65, 00 };
            var reader = new MultiPacketStringReader(new BufferReader(bytes), () => { throw new Exception("Should not call this method"); });

            Assert.AreEqual("AAA", await reader.ReadStringAsync());
        }
        public async Task ReadsTwoPacketString()
        {
            var bytes = new byte[] { 65, 65, 65 };
            var bytes2 = new byte[] { 66, 66, 66, 00 };

            var reader = new MultiPacketStringReader(new BufferReader(bytes), () => Task.FromResult(new BufferReader(bytes2)));

            Assert.AreEqual("AAABBB", await reader.ReadStringAsync());
        }
        public async Task ReadsDelayedTwoPacketString()
        {
            var bytes = new byte[] { 66, 65, 65 };
            var bytes2 = new byte[] { 67, 66, 66, 00 };

            var reader = new MultiPacketStringReader(new BufferReader(bytes), async () =>
            {
                await Task.Delay(2000);
                return new BufferReader(bytes2);
            });

            Assert.AreEqual("BAACBB", await reader.ReadStringAsync());
        }
        public async Task ReadsDelayedMultiPacketString()
        {
            var bytes = new byte[] { 65, 65, 65 };
            var extraPackets = new[]
            {
                new byte[] { 66, 66, 66 },
                new byte[] { 67, 67 },
                new byte[] { 67 },
                new byte[] { 68, 68, 68, 00 },
            };

            var index = 0;

            var reader = new MultiPacketStringReader(new BufferReader(bytes), async () =>
            {
                await Task.Delay(1000);
                return new BufferReader(extraPackets[index++]);
            });

            Assert.AreEqual("AAABBBCCCDDD", await reader.ReadStringAsync());
        }
        public async Task ReadsMultiPacketStringExcludingPacketHeaders()
        {
            var bytes = new byte[] { 65, 65, 65 };
            var extraPackets = new[]
            {
                new byte[] { 00, 66, 66, 66 },
                new byte[] { 00, 67, 67 },
                new byte[] { 00, 67 },
                new byte[] { 00, 68, 68, 68, 00 },
            };

            var index = 0;

            var reader = new MultiPacketStringReader(new BufferReader(bytes), async () =>
            {
                await Task.Delay(1000);

                var sequelReader = new BufferReader(extraPackets[index++]);
                sequelReader.Skip(1);

                return sequelReader;
            });

            Assert.AreEqual("AAABBBCCCDDD", await reader.ReadStringAsync());
        }
Example #6
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);
        }