public void ReadByteArrayCheckedTest() { var stream = new FastStreamReader(new byte[] { 1, 2, 3, 4, 5, 6 }); byte[] actual = stream.ReadByteArrayChecked(0); byte[] expected = new byte[0]; Assert.Equal(expected, actual); Helper.ComparePrivateField(stream, "position", 0); actual = stream.ReadByteArrayChecked(2); expected = new byte[] { 1, 2 }; Assert.Equal(expected, actual); Helper.ComparePrivateField(stream, "position", 2); actual = stream.ReadByteArrayChecked(3); expected = new byte[] { 3, 4, 5 }; Assert.Equal(expected, actual); Helper.ComparePrivateField(stream, "position", 5); actual = stream.ReadByteArrayChecked(1); expected = new byte[] { 6 }; Assert.Equal(expected, actual); Helper.ComparePrivateField(stream, "position", 6); }
/// <summary> /// Reads and processes the given bytes from the given buffer and provided length. /// </summary> /// <param name="buffer">Buffer containing the received bytes</param> /// <param name="offset">Offset inside <paramref name="buffer"/> parameter where the data begins</param> /// <param name="rcvLen">Number of bytes received</param> public void ReadBytes(byte[] buffer, int offset, int rcvLen) { if (rcvLen > 0 && buffer != null) { if (IsReceiveCompleted && rcvLen >= Constants.MessageHeaderSize) { FastStreamReader stream = new FastStreamReader(buffer, offset, rcvLen); if (stream.FindAndSkip(magicBytes)) { int rem = stream.GetRemainingBytesCount(); if (rem >= Constants.MessageHeaderSize) { var msg = new Message(netType); Message.ReadResult res = msg.Read(stream); if (res == Message.ReadResult.Success) { IsReceiveCompleted = true; Message[] reply = replyManager.GetReply(msg); if (reply != null) { foreach (var item in reply) { toSendQueue.Enqueue(item); } } // TODO: handle received message here. eg. write received block to disk,... // Handle remaining data rem = stream.GetRemainingBytesCount(); if (rem > 0) { rcvHolder = stream.ReadByteArrayChecked(rem); if (rem >= Constants.MessageHeaderSize) { IsReceiveCompleted = true; ReadBytes(rcvHolder, 0, rcvHolder.Length); } else { IsReceiveCompleted = false; } } else { rcvHolder = null; } } else if (res == Message.ReadResult.NotEnoughBytes) { IsReceiveCompleted = false; rcvHolder = new byte[rcvLen]; Buffer.BlockCopy(buffer, offset, rcvHolder, 0, rcvLen); } else { // Invalid message was received (checksum, network or payload size overflow) NodeStatus.AddSmallViolation(); rcvHolder = null; } } else if (rem != 0) { rcvHolder = stream.ReadByteArrayChecked(rem); IsReceiveCompleted = false; } } else { // There are always 3 bytes remaining in stream that failed to find magic and has to be stored in holder rcvHolder = stream.ReadByteArrayChecked(stream.GetRemainingBytesCount()); IsReceiveCompleted = false; // There were at least 21 bytes of garbage in this buffer NodeStatus.AddSmallViolation(); } } else { if (rcvHolder == null) { rcvHolder = new byte[rcvLen]; Buffer.BlockCopy(buffer, offset, rcvHolder, 0, rcvLen); } else { byte[] temp = new byte[rcvHolder.Length + rcvLen]; Buffer.BlockCopy(rcvHolder, 0, temp, 0, rcvHolder.Length); Buffer.BlockCopy(buffer, offset, temp, rcvHolder.Length, rcvLen); rcvHolder = temp; } if (rcvHolder.Length >= Constants.MessageHeaderSize) { IsReceiveCompleted = true; ReadBytes(rcvHolder, 0, rcvHolder.Length); } else { IsReceiveCompleted = false; } } } }