You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
using System; using System.Collections.Generic; using System.Linq;
namespace Mirle.Component.SocketDirver.FrameBuilder { /// <summary>
/// Google Protobuffer 訊息加解密類別
/// </summary>
public class ProtobufferFrameBulder : IFrameBuilder { /// <inheritdoc/>
public byte[] EncodeFrame(byte[] payload) { List<byte> list = new List<byte>(); long num = payload.LongLength; if (num <= 125) { byte item = (byte)num; list.Add(item); } else if (num <= 65535) { byte item2 = 126; list.Add(item2); byte[] bytes = BitConverter.GetBytes(num); list.Add(bytes[1]); list.Add(bytes[0]); } else { byte item3 = 127; list.Add(item3); byte[] bytes2 = BitConverter.GetBytes(num); list.Add(bytes2[7]); list.Add(bytes2[6]); list.Add(bytes2[5]); list.Add(bytes2[4]); list.Add(bytes2[3]); list.Add(bytes2[2]); list.Add(bytes2[1]); list.Add(bytes2[0]); }
list.AddRange(payload); return list.ToArray(); }
/// <inheritdoc/>
public bool TryDecodeFrame(byte[] buffer, out int receiveCount, out byte[] payload) { try { int num = buffer.AsEnumerable().First() & 0x7F; int num2 = 1; int num3 = 0; if (num <= 125) { num2 = 1; num3 = num; } else { switch (num) { case 126: { num2 = 3; byte[] array2 = buffer.Skip(1).Take(2).ToArray(); num3 = array2[0] * 256 + array2[1]; break; } case 127: { num2 = 9; byte[] array = buffer.Skip(1).Take(8).ToArray(); int num4 = 1; for (int i = 0; i < 8; i++) { num3 += array[8 - i - 1] * num4; num4 *= 256; } break; } } } receiveCount = num2 + num3; payload = buffer.Skip(num2).Take(num3).ToArray(); return true; } catch (Exception ex) { Console.WriteLine(ex.ToString()); receiveCount = 0; payload = null; return false; } } } }
|