Skip to content

Instantly share code, notes, and snippets.

@Ilchert
Last active October 10, 2023 21:05
Show Gist options
  • Save Ilchert/4822293517a796e208b4fa05e2364a8c to your computer and use it in GitHub Desktop.
Save Ilchert/4822293517a796e208b4fa05e2364a8c to your computer and use it in GitHub Desktop.
// See https://aka.ms/new-console-template for more information
using System.Buffers.Binary;
var data = new byte[] {
100,
100,100,10,10,
11,11,11,11,
12,00,
4,
4,
100,
100,
10,10,10,10,
11,11,11,11,
13,00,
4,
4
};
var packetTemplateData = new byte[] {
100,
100,
0,0,0,0,
0,0,0,0,
0,0,
4,
4,
0,0
};
var packetBitmask = new byte[] {
255,
255,
0,0,0,0,
0,0,0,0,
0,0,
255,
255,
0,0
};
var template = BinaryPrimitives.ReadInt128BigEndian(packetTemplateData);
var bitmask = BinaryPrimitives.ReadInt128BigEndian(packetBitmask);
using var ms = new MemoryStream(data);
var buffer = new byte[16].AsMemory();
ms.ReadExactly(buffer.Span[..14]);
var packet = BinaryPrimitives.ReadInt128BigEndian(buffer.Span);
while ((packet & bitmask) != template)
{
packet <<= 8;
packet |= ms.ReadByte() << 16;
}
//parse package
while ((packet & bitmask) != template)
{
ms.ReadExactly(buffer.Span[..14]);
packet = BinaryPrimitives.ReadInt128BigEndian(buffer.Span);
// parse package
}
//error. reset or read end package
Console.ReadLine();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment