From 9b2c8fbfbc2d86ae15313d1a8d7896598bc8a22f Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Wed, 30 Nov 2016 13:52:16 +0100 Subject: [PATCH] Ignore invalid frames Closes #30 --- Lawo.EmberPlusSharp/S101/DeframingStream.cs | 10 +++++++--- Lawo.EmberPlusSharpTest/S101/S101ReaderTest.cs | 15 +++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Lawo.EmberPlusSharp/S101/DeframingStream.cs b/Lawo.EmberPlusSharp/S101/DeframingStream.cs index f4e54eff..07e2966f 100644 --- a/Lawo.EmberPlusSharp/S101/DeframingStream.cs +++ b/Lawo.EmberPlusSharp/S101/DeframingStream.cs @@ -112,18 +112,22 @@ private bool ReadByte(ReadBuffer readBuffer) case Frame.EscapeByte: this.state = State.InFrameEscaped; break; + case Frame.BeginOfFrame: + this.decodedQueue.Clear(); + break; case Frame.EndOfFrame: this.state = State.AfterFrame; if (this.crc != 0xF0B8) { - throw new S101Exception("CRC failed."); + this.decodedQueue.Clear(); } return false; default: this.state = State.AfterFrame; - throw new S101Exception("Invalid byte in frame."); + this.decodedQueue.Clear(); + break; } } @@ -132,7 +136,7 @@ private bool ReadByte(ReadBuffer readBuffer) if (currentByte >= Frame.InvalidStart) { this.state = State.AfterFrame; - throw new S101Exception("Invalid escaped byte."); + this.decodedQueue.Clear(); } currentByte = (byte)(currentByte ^ Frame.EscapeXor); diff --git a/Lawo.EmberPlusSharpTest/S101/S101ReaderTest.cs b/Lawo.EmberPlusSharpTest/S101/S101ReaderTest.cs index 494585f3..57017ece 100644 --- a/Lawo.EmberPlusSharpTest/S101/S101ReaderTest.cs +++ b/Lawo.EmberPlusSharpTest/S101/S101ReaderTest.cs @@ -80,17 +80,17 @@ public void ExceptionTest() () => reader.Message.Ignore(), () => reader.Payload.Ignore()); } - await AssertS101Exception("CRC failed.", 0xFE, 0xFF); - await AssertS101Exception("Invalid byte in frame.", 0xFE, 0xFE); + await AssertEmpty(0xFE, 0xFF); + await AssertEmpty(0xFE, 0xFE); for (byte invalid = 0xF8; invalid < 0xFD; ++invalid) { - await AssertS101Exception("Invalid byte in frame.", 0xFE, invalid); + await AssertEmpty(0xFE, invalid); } for (ushort invalid = 0xF8; invalid < 0x100; ++invalid) { - await AssertS101Exception("Invalid escaped byte.", 0xFE, 0xFD, (byte)invalid); + await AssertEmpty(0xFE, 0xFD, (byte)invalid); } await AssertS101Exception("Unexpected end of stream.", 0xFE, 0x00, 0x00, 0x00); @@ -186,6 +186,13 @@ await AssertThrowAsync( } } + private static async Task AssertEmpty(params byte[] input) + { + var reader = CreateAsyncReader(input); + Assert.IsFalse(await reader.ReadAsync(CancellationToken.None)); + await reader.DisposeAsync(CancellationToken.None); + } + private static async Task AssertS101Exception(string message, params byte[] input) { var reader = CreateAsyncReader(input);