private void SeekPastMagicBytes(Stream @in)
{
var magicCursor = 3; // Which byte of the magic we're looking for currently.
while (true)
{
var b = @in.Read(); // Read a byte.
if (b == -1)
{
// There's no more data to read.
throw new IOException("Socket is disconnected");
}
// We're looking for a run of bytes that is the same as the packet magic but we want to ignore partial
// magics that aren't complete. So we keep track of where we're up to with magicCursor.
var expectedByte = (byte) (_params.PacketMagic >> magicCursor*8);
if (b == expectedByte)
{
magicCursor--;
if (magicCursor < 0)
{
// We found the magic sequence.
return;
}
// We still have further to go to find the next message.
}
else
{
magicCursor = 3;
}
}
}