Пример #1
0
        internal override void Parse(byte[] buffer, ref int offset)
        {
            while (offset < buffer.Length)
            {
                VariableInt messageNameLength = VariableInt.DecodeVariableInt(buffer.Skip(offset).ToArray());
                offset += messageNameLength.Length;

                string messageName = Encoding.ASCII.GetString(buffer, offset, (int)messageNameLength.Value);
                offset += messageName.Length;

                byte numberOfArgs = buffer.Skip(offset).Take(1).First();
                offset++;

                var message = new SpoeMessage(messageName);

                for (byte i = 0; i < numberOfArgs; i++)
                {
                    VariableInt keyNameLength = VariableInt.DecodeVariableInt(buffer.Skip(offset).ToArray());
                    offset += keyNameLength.Length;

                    string keyname = Encoding.ASCII.GetString(buffer, offset, (int)keyNameLength.Value);
                    offset += keyname.Length;

                    TypedData data = TypedDataParser.ParseNext(buffer, ref offset);
                    message.Args.Add(keyname, data);
                }

                this.Messages.Add(message);
            }
        }
Пример #2
0
        internal override void Parse(byte[] buffer, ref int offset)
        {
            while (offset < buffer.Length)
            {
                // key-value format: [length-of-keyname(varint)][keyname][datatype][value]
                VariableInt keyNameLength = VariableInt.DecodeVariableInt(buffer.Skip(offset).ToArray());
                offset += keyNameLength.Length;

                string keyname = Encoding.ASCII.GetString(buffer, offset, (int)keyNameLength.Value);
                offset += keyname.Length;

                TypedData data = TypedDataParser.ParseNext(buffer, ref offset);
                this.KeyValueItems.Add(keyname, data);
            }
        }
        public void DecodeVariableInt_WhenGivenBuffer_ThenReturnsExpectedValue()
        {
            // arrange
            // 0001111111110100
            BitArray ba = new BitArray(
                new bool[] { false, false, false, true, true, true, true, true, true, true, true, true, false, true, false, false });

            byte[] buffer = new byte[2];
            ba.CopyTo(buffer, 0);

            // act
            VariableInt result = VariableInt.DecodeVariableInt(buffer);

            // assert
            Assert.AreEqual(1000, result.Value);
        }