protected static bool CanStartWith(byte[] buffer, PacketLengthHandler packetLengthCallback, int bufferLength = -1) { if (bufferLength < 0) { bufferLength = buffer.Length; } int packetLength = packetLengthCallback(buffer, bufferLength); if (packetLength > -1 && packetLength < PacketLengthMin) { return(false); } if (bufferLength < PacketLengthMin) { return(true); } if (bufferLength >= packetLength && !IsValid(buffer, packetLengthCallback, bufferLength)) { return(false); } return(true); }
protected static new bool IsValid(byte[] buffer, PacketLengthHandler packetLengthCallback, int bufferLength = -1) { if (bufferLength < 0) { bufferLength = buffer.Length; } if (bufferLength < PacketLengthMin) { return(false); } byte packetLength = (byte)ParsePacketLength(buffer, bufferLength); if (bufferLength < packetLength || packetLength < PacketLengthMin) { return(false); } byte check = 0x00; for (byte i = 0; i < packetLength - 1; i++) { check ^= buffer[i]; } return(check == buffer[packetLength - 1]); }
protected static bool IsValid(byte[] buffer, PacketLengthHandler packetLengthCallback, int bufferLength = -1) { if (bufferLength < 0) { bufferLength = buffer.Length; } if (bufferLength < PacketLengthMin) { return(false); } int packetLength = packetLengthCallback(buffer, bufferLength); if (bufferLength < packetLength || packetLength < PacketLengthMin) { return(false); } var packetCheck = buffer[packetLength - 1]; if (packetCheck == 0x00 && packetLength > 8) { // find corrupted packet as a part of current packet for (int i = packetLength / 2; i < packetLength - 1; i++) { var theSame = true; for (int ic = i, io = 0; ic < packetLength - 1 && io < i; ic++, io++) { if (buffer[ic] != buffer[io]) { theSame = false; break; } } if (theSame) { return(false); } } } byte check = 0x00; for (int i = 0; i < packetLength - 1; i++) { check ^= buffer[i]; } return(check == packetCheck); }