//******************************************************************************************************
// FrameImageCollector.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 12/30/2008 - J. Ritchie Carroll
// Generated original version of source code.
// 08/07/2009 - Josh L. Patterson
// Edited Comments.
// 09/15/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/17/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.IO;
namespace GSF.PhasorProtocols.IEEE1344
{
///
/// Collects frame images until a full IEEE 1344 frame has been received.
///
public class FrameImageCollector
{
#region [ Members ]
// Fields
private readonly MemoryStream m_frameQueue;
#endregion
#region [ Constructors ]
///
/// Creates a new .
///
public FrameImageCollector()
{
// As an optimization in context of usage, we don't implement IDisposable for
// this class just for the memory stream since its Close method (i.e., Dispose)
// does nothing (reflect it and look for yourself). Additionally, we go ahead
// and suppress the finalizer for this stream to reduce that overhead too.
m_frameQueue = new MemoryStream();
GC.SuppressFinalize(m_frameQueue);
}
#endregion
#region [ Properties ]
///
/// Gets the combined binary image of all the collected frame images.
///
public byte[] BinaryImage => m_frameQueue.ToArray();
///
/// Gets the length of the combined binary image of all the collected frame images.
///
public int BinaryLength => (int)m_frameQueue.Length;
///
/// Gets the total number frames appended to .
///
public int Count { get; private set; }
#endregion
#region [ Methods ]
///
/// Appends the current frame image to the frame image collection.
///
/// A array to append to the collection.
/// An value indicating the number of bytes to read from the .
/// An value indicating the offset to read from.
public void AppendFrameImage(byte[] buffer, int offset, int length)
{
// Validate CRC of frame image being appended
if (!CommonFrameHeader.ChecksumIsValid(buffer, offset, length))
throw new InvalidOperationException("Invalid binary image detected - check sum of individual IEEE 1344 interleaved frame transmission did not match");
// Include initial header in new stream...
if (m_frameQueue.Length == 0)
m_frameQueue.Write(buffer, offset, CommonFrameHeader.FixedLength);
// Skip past header
offset += CommonFrameHeader.FixedLength;
// Include frame image
m_frameQueue.Write(buffer, offset, length - CommonFrameHeader.FixedLength);
// Track total frame images
Count++;
}
#endregion
}
}