TizzyT

PayloadHeader - TizzyT

Jul 10th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1.             private class PayloadHeader
  2.             {
  3.                 private readonly uint value;
  4.  
  5.                 public ushort Length => (ushort)((value >> 20) << 4);
  6.                 public uint Channel => 0b000_0000_0000_1111_1111_1111_1111_1111 & value;
  7.  
  8.                 public PayloadHeader(uint length, uint channel)
  9.                 {
  10.                     if ((length & 15) > 0) throw new Exception("Length must be a multiple of 16"); else length >>= 4;
  11.                     if (length > 3840) throw new Exception("Length cannot be greater than 61,440");
  12.                     if (channel > 1048575) throw new Exception("Channel cannot be greater than 1,048,575");
  13.                     value = (length << 20) | channel;
  14.                 }
  15.  
  16.                 public PayloadHeader(byte[] data)
  17.                 {
  18.                     if (data.Length == 4) value = BitConverter.ToUInt32(data, 0);
  19.                     else throw new Exception("PayloadHeader must be 4 bytes in length");
  20.                 }
  21.  
  22.                 public byte[] ToArray() => BitConverter.GetBytes(value);
  23.             }
Advertisement
Add Comment
Please, Sign In to add comment