Beispiel #1
0
 public static void LongMemberToBytes(byte[] data, int fieldnumber, ref int offset, long val, bool useDef = true, long defval = 0L)
 {
     if (!useDef || val != defval)
     {
         int tag = fieldnumber << 3;
         offset += ProtoUtil.IntToBytes(data, offset, tag);
         offset += ProtoUtil.LongToBytes(data, offset, val);
     }
 }
Beispiel #2
0
 public static void ListIntToBytes(byte[] data, int member, ref int offset, List <int> lst)
 {
     if (lst != null && lst.Count > 0)
     {
         int tag = member << 3 | 2;
         offset += ProtoUtil.IntToBytes(data, offset, tag);
         offset += ProtoUtil.IntToBytes(data, offset, lst.Count);
         for (int i = 0; i < lst.Count; i++)
         {
             offset += ProtoUtil.IntToBytes(data, offset, lst[i]);
         }
     }
 }
Beispiel #3
0
 public static void DoubleMemberToBytes(byte[] data, int fieldnumber, ref int offset, double val, bool useDef = true, double valdef = 0.0)
 {
     if (!useDef || val != valdef)
     {
         int tag = fieldnumber << 3 | 1;
         offset += ProtoUtil.IntToBytes(data, offset, tag);
         long value = BitConverter.ToInt64(BitConverter.GetBytes(val), 0);
         data[offset++] = (byte)value;
         data[offset++] = (byte)(value >> 8);
         data[offset++] = (byte)(value >> 16);
         data[offset++] = (byte)(value >> 24);
         data[offset++] = (byte)(value >> 32);
         data[offset++] = (byte)(value >> 40);
         data[offset++] = (byte)(value >> 48);
         data[offset++] = (byte)(value >> 56);
     }
 }
Beispiel #4
0
 public static void StringMemberToBytes(byte[] data, int fieldnumber, ref int offset, string val)
 {
     if (val != null)
     {
         int tag = fieldnumber << 3 | 2;
         offset += ProtoUtil.IntToBytes(data, offset, tag);
         int len = val.Length;
         if (len == 0)
         {
             data[offset++] = 0;
         }
         else
         {
             int predicted = new UTF8Encoding().GetByteCount(val);
             offset += ProtoUtil.IntToBytes(data, offset, predicted);
             int actual = new UTF8Encoding().GetBytes(val, 0, val.Length, data, offset);
             offset += predicted;
         }
     }
 }
Beispiel #5
0
 public static byte[] ListToBytes <T>(List <T> lst, int member, ref int offset, byte[] data) where T : IProtoBuffData
 {
     byte[] result;
     if (lst == null || lst.Count <= 0)
     {
         result = data;
     }
     else
     {
         int tag = member << 3 | 2;
         offset += ProtoUtil.IntToBytes(data, offset, tag);
         offset += ProtoUtil.IntToBytes(data, offset, lst.Count);
         for (int i = 0; i < lst.Count; i++)
         {
             T      t     = lst[i];
             byte[] bytes = t.toBytes();
             int    ts    = ProtoUtil.CalcIntSize(bytes.Length);
             if (bytes.Length + offset + ts > data.Length)
             {
                 byte[] data2 = new byte[data.Length * 2];
                 Array.Copy(data, data2, data.Length);
                 data = data2;
             }
             offset += ProtoUtil.IntToBytes(data, offset, bytes.Length);
             if (bytes.Length > 0)
             {
                 Array.Copy(bytes, 0, data, offset, bytes.Length);
                 offset += bytes.Length;
             }
         }
         if (offset < data.Length)
         {
             Array.Resize <byte>(ref data, offset);
         }
         result = data;
     }
     return(result);
 }