示例#1
0
        public static Payload FromArray(Rexpression[] objects)
        {
            if (objects == null || objects.Length == 0)
            {
                return new Payload(PayloadCode.Array, null);
            }

            // size of each rexpression is 1 byte for the type + data length
            int size = objects.Sum(rexpression => 1 + rexpression.DataSize);

            var bytes = new byte[size];

            for (int i = 0, offset = 0; i < objects.Length; i++)
            {
                var rexpBytes = objects[i].ToEncodedBytes();
                Array.Copy(rexpBytes, 0, bytes, offset, rexpBytes.Length);
                offset += rexpBytes.Length;
            }

            return new Payload(PayloadCode.Array, bytes);
        }
示例#2
0
 public static Payload FromRexpression(Rexpression value)
 {
     return new Payload(PayloadCode.Rexpression, value.ToEncodedBytes());
 }
示例#3
0
        public static Rexpression ParseRexpression(byte[] bytes, ref int offset)
        {
            if (bytes == null || (bytes.Length - offset) < Rexpression.RexpressionHeaderSize)
            {
                throw new ArgumentException("Invalid Rexpression header");
            }

            try
            {
                Rexpression rexp = null;
                Rexpression attr = null;

                var type = (RexpressionType)(bytes[offset] & 0x3F);
                var hasAttribute = (bytes[offset] & (byte)RexpressionType.HasAttribute) == (byte)RexpressionType.HasAttribute;

                // TODO: handle long data
                var lengthBytes = new byte[4];
                Array.Copy(bytes, offset + 1, lengthBytes, 0, Rexpression.DataSizeHeaderSize);
                var length = BitConverter.ToInt32(lengthBytes, 0);

                offset += Rexpression.RexpressionHeaderSize;
                var delta = 0;

                if (hasAttribute)
                {
                    delta = offset;
                    attr = ParseRexpression(bytes, ref offset);
                    delta = offset - delta;
                }

                length -= delta;

                var data = new byte[length];
                Array.Copy(bytes, offset, data, 0, length);

                offset += length;

                rexp = new Rexpression(type, data) { HasAttribute = hasAttribute, Attribute = attr};

                return rexp;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }