protected bool ReadCommand(ref TCommands aCommand, byte[] aFixedCommandPart, TByteBuffer aPayload, byte[] aPayloadCheck) { int NumBytesRead = FNetStream.Read(aFixedCommandPart, 0, aFixedCommandPart.Length); if (NumBytesRead > 0) { while (BitConverter.ToInt64(aFixedCommandPart, 0) != MagicBytesInt64) { Array.Copy(aFixedCommandPart, 1, aFixedCommandPart, 0, aFixedCommandPart.Length - 1); int rbr = FNetStream.ReadByte(); if (rbr != -1) aFixedCommandPart[aFixedCommandPart.Length - 1] = (byte)rbr; // skipped bytes because of invalid magic in read command else return false; // error, no valid connection } // we found the magic in the stream aCommand = (TCommands)BitConverter.ToInt32(aFixedCommandPart, MagicBytes.Length); Int32 PayloadSize = BitConverter.ToInt32(aFixedCommandPart, MagicBytes.Length + sizeof(Int32)); if (PayloadSize <= MaxPayloadSize) { aPayload.Clear(PayloadSize); if (PayloadSize > 0) { int Len = ReadBytesFromNetStream(aPayload); if (Len == aPayload.Length) { NumBytesRead = FNetStream.Read(aPayloadCheck, 0, aPayloadCheck.Length); return NumBytesRead == aPayloadCheck.Length && BitConverter.ToInt32(aPayloadCheck, 0) == CheckStringMagic; } else return false; // error, payload size mismatch } else return true; // ok, no payload } else return false; // error, payload is over max size } else return false; // error, no valid connection }