Пример #1
0
        public static byte[] ReadValueBytes(Stream stream, Key key)
        {
            int offset = 0;

            switch (key.WireType)
            {
            case Wire.Varint:
                return(ProtocolParser.ReadVarIntBytes(stream));

            case Wire.Fixed64:
                byte[] buffer1 = new byte[8];
                while (offset < 8)
                {
                    offset += stream.Read(buffer1, offset, 8 - offset);
                }
                return(buffer1);

            case Wire.LengthDelimited:
                uint   val = ProtocolParser.ReadUInt32(stream);
                byte[] buffer2;
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    ProtocolParser.WriteUInt32((Stream)memoryStream, val);
                    buffer2 = new byte[(long)val + memoryStream.Length];
                    memoryStream.ToArray().CopyTo((Array)buffer2, 0);
                    offset = (int)memoryStream.Length;
                }
                while (offset < buffer2.Length)
                {
                    offset += stream.Read(buffer2, offset, buffer2.Length - offset);
                }
                return(buffer2);

            case Wire.Fixed32:
                byte[] buffer3 = new byte[4];
                while (offset < 4)
                {
                    offset += stream.Read(buffer3, offset, 4 - offset);
                }
                return(buffer3);

            default:
                throw new NotImplementedException("Unknown wire type: " + (object)key.WireType);
            }
        }
Пример #2
0
        /// <summary>
        /// Seek past the value for the previously read key.
        /// </summary>
        public static void SkipKey(Stream stream, Key key)
        {
            switch (key.WireType)
            {
            case Wire.Fixed32:
                stream.Seek(4, SeekOrigin.Current);
                return;

            case Wire.Fixed64:
                stream.Seek(8, SeekOrigin.Current);
                return;

            case Wire.LengthDelimited:
                stream.Seek(ProtocolParser.ReadUInt32(stream), SeekOrigin.Current);
                return;

            case Wire.Varint:
                ProtocolParser.ReadSkipVarInt(stream);
                return;

            default:
                throw new NotImplementedException("Unknown wire type: " + key.WireType);
            }
        }
Пример #3
0
        public static void WriteKey(Stream stream, Key key)
        {
            uint val = (uint)((Wire)((int)key.Field << 3) | key.WireType);

            ProtocolParser.WriteUInt32(stream, val);
        }
Пример #4
0
 public static Key ReadKey(byte firstByte, Stream stream)
 {
     return(firstByte < (byte)128 ? new Key((uint)firstByte >> 3, (Wire)((int)firstByte & 7)) : new Key((uint)((int)ProtocolParser.ReadUInt32(stream) << 4 | (int)firstByte >> 3 & 15), (Wire)((int)firstByte & 7)));
 }
Пример #5
0
 public static void Write(this Stream stream, float val)
 {
     ProtocolParser.WriteFloat(stream, val);
 }
Пример #6
0
 public static void WriteBytes(Stream stream, byte[] val)
 {
     ProtocolParser.WriteUInt32(stream, (uint)val.Length);
     stream.Write(val, 0, val.Length);
 }
Пример #7
0
 public static void WriteZInt64(Stream stream, long val)
 {
     ProtocolParser.WriteUInt64(stream, (ulong)(val << 1 ^ val >> 63));
 }
Пример #8
0
 public static void WriteInt64(Stream stream, int val)
 {
     ProtocolParser.WriteUInt64(stream, (ulong)val);
 }
Пример #9
0
 public static void WriteZInt32(Stream stream, int val)
 {
     ProtocolParser.WriteUInt32(stream, (uint)(val << 1 ^ val >> 31));
 }
Пример #10
0
 public static double ReadDouble(this Stream stream)
 {
     return(ProtocolParser.ReadDouble(stream));
 }
Пример #11
0
 public static float ReadFloat(this Stream stream)
 {
     return(ProtocolParser.ReadFloat(stream));
 }
Пример #12
0
 public static int ReadInt(this Stream stream)
 {
     return(ProtocolParser.ReadInt32(stream));
 }
Пример #13
0
 public static void Write(this Stream stream, byte[] bytes)
 {
     ProtocolParser.WriteBytes(stream, bytes);
 }
Пример #14
0
 public static void Write(this Stream stream, string val)
 {
     ProtocolParser.WriteString(stream, val);
 }
Пример #15
0
 public static void Write(this Stream stream, bool val)
 {
     ProtocolParser.WriteBool(stream, val);
 }
Пример #16
0
 public static void Write(this Stream stream, double val)
 {
     ProtocolParser.WriteDouble(stream, val);
 }
Пример #17
0
        public static int ReadZInt32(Stream stream)
        {
            uint num = ProtocolParser.ReadUInt32(stream);

            return((int)(num >> 1) ^ (int)num << 31 >> 31);
        }
Пример #18
0
 public static bool ReadBool(this Stream stream)
 {
     return(ProtocolParser.ReadBool(stream));
 }
Пример #19
0
 public static int ReadInt64(Stream stream)
 {
     return((int)ProtocolParser.ReadUInt64(stream));
 }
Пример #20
0
 public static string ReadString(this Stream stream)
 {
     return(ProtocolParser.ReadString(stream));
 }
Пример #21
0
        public static long ReadZInt64(Stream stream)
        {
            ulong num = ProtocolParser.ReadUInt64(stream);

            return((long)(num >> 1) ^ (long)num << 63 >> 63);
        }
Пример #22
0
 public static byte[] ReadBytes(this Stream stream)
 {
     return(ProtocolParser.ReadBytes(stream));
 }
Пример #23
0
 public static void WriteString(Stream stream, string val)
 {
     ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(val));
 }
Пример #24
0
 public static string ReadString(Stream stream)
 {
     byte[] bytes = ProtocolParser.ReadBytes(stream);
     return(Encoding.UTF8.GetString(bytes, 0, bytes.Length));
 }
Пример #25
0
        public static Key ReadKey(Stream stream)
        {
            uint num = ProtocolParser.ReadUInt32(stream);

            return(new Key(num >> 3, (Wire)((int)num & 7)));
        }
Пример #26
0
 public static void Write(this Stream stream, int val)
 {
     ProtocolParser.WriteInt32(stream, val);
 }