Esempio n. 1
0
        public static void PackBytes(WriteBuffer wb, byte[] buf)
        {
            if (buf == null)
            {
                PackNil(wb);
                return;
            }
            long slen    = buf.Length;
            byte topbyte = 0;

            if (slen < 256)
            {
                topbyte = 0xc4;
                wb.Add(topbyte, ReverseBytes((byte)slen), buf);
            }
            else if (slen < 65536)
            {
                topbyte = 0xc5;
                wb.Add(topbyte, ReverseBytes((ushort)slen), buf);
            }
            else if (slen < 4294967296 - 1)  // TODO: -1 for avoiding (condition is always true warning)
            {
                topbyte = 0xc6;
                wb.Add(topbyte, ReverseBytes((uint)slen), buf);
            }
            else
            {
                PackNil(wb);
                Console.WriteLine("PackBytes length is out of uint " + slen);
            }
        }
Esempio n. 2
0
 public static void PackBool(WriteBuffer wb, bool value)
 {
     if (value)
     {
         wb.Add(0xc3);
     }
     else
     {
         wb.Add(0xc2);
     }
 }
Esempio n. 3
0
 public static void PackInteger(WriteBuffer wb, long value)
 {
     if (value >= 0)
     {
         if (value < 128)
         {
             wb.Add((byte)value);
         }
         else if (value < 256)
         {
             wb.Add(0xcc, (byte)value);
         }
         else if (value < 65536)
         {
             wb.Add(0xcd, ReverseBytes((ushort)value));
         }
         else if (value < 4294967296)
         {
             wb.Add(0xce, ReverseBytes((uint)value));
         }
         else
         {
             wb.Add(0xcf, ReverseBytes((ulong)value));
         }
     }
     else
     {
         if (value >= -32)
         {
             byte v = (byte)(0xe0 | (byte)value);
             wb.Add(v);
         }
         else if (value >= -128)
         {
             wb.Add(0xd0, (byte)value);
         }
         else if (value >= -32768)
         {
             wb.Add(0xd1, ReverseBytes((short)(value & 0xffff)));
         }
         else if (value >= -2147483648)
         {
             wb.Add(0xd2, ReverseBytes((int)(value & 0xffffffff)));
         }
         else
         {
             wb.Add(0xd3, ReverseBytes((long)value));
         }
     }
 }
Esempio n. 4
0
        public static void PackMap(WriteBuffer wb, long length)
        {
            byte topbyte;

            // map fixmap, 16,32 : 0x80|num, 0xde+2byte, 0xdf+4byte
            if (length < 16)
            {
                topbyte = (byte)(0x80 | (byte)length);
                wb.Add(topbyte);
            }
            else if (length < 65536)
            {
                topbyte = 0xde;
                wb.Add(topbyte, ReverseBytes((ushort)length));
            }
            else if (length < 4294967296 - 1)
            {
                topbyte = 0xdf;
                wb.Add(topbyte, ReverseBytes((uint)length));
            }
        }
Esempio n. 5
0
        public static void PackArray(WriteBuffer wb, long length)
        {
            byte topbyte;

            // array!(ignore map part.) 0x90|n , 0xdc+2byte, 0xdd+4byte
            if (length < 16)
            {
                topbyte = (byte)(0x90 | (byte)length);
                wb.Add(topbyte);
            }
            else if (length < 65536)
            {
                topbyte = 0xdc;
                wb.Add(topbyte, ReverseBytes((ushort)length));
            }
            else if (length < 4294967296 - 1)  // TODO: avoid C warn
            {
                topbyte = 0xdd;
                wb.Add(topbyte, ReverseBytes((uint)length));
            }
        }
Esempio n. 6
0
        public static void PackString(WriteBuffer wb, string str)
        {
            if (str == null)
            {
                PackNil(wb);
                return;
            }
            long slen    = str.Length;
            byte topbyte = 0;

            if (slen < 32)
            {
                topbyte = (byte)(0xa0 | (byte)slen);
                wb.Add(topbyte, Eproto.StringToBytes(str));
            }
            else if (slen < 256)
            {
                topbyte = 0xd9;
                wb.Add(topbyte, ReverseBytes((byte)slen), Eproto.StringToBytes(str));
            }
            else if (slen < 65536)
            {
                topbyte = 0xda;
                wb.Add(topbyte, ReverseBytes((ushort)slen), Eproto.StringToBytes(str));
            }
            else if (slen < 4294967296 - 1)  // TODO: -1 for avoiding (condition is always true warning)
            {
                topbyte = 0xdb;
                wb.Add(topbyte, ReverseBytes((uint)slen), Eproto.StringToBytes(str));
            }
            else
            {
                PackNil(wb);
                Console.WriteLine("PackString length is out of uint " + slen);
            }
        }
Esempio n. 7
0
 public static void PackDouble(WriteBuffer wb, double value)
 {
     wb.Add(0xcb, ReverseBytes(value));
 }
Esempio n. 8
0
 public static void PackDouble(WriteBuffer wb, float value)
 {
     wb.Add(0xca, ReverseBytes(value));
 }
Esempio n. 9
0
 public static void PackNil(WriteBuffer wb)
 {
     wb.Add(0xc0);
 }