generated from gemstone/gemtem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrameImageCollector.cs
124 lines (103 loc) · 4.48 KB
/
FrameImageCollector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//******************************************************************************************************
// FrameImageCollector.cs - Gbtc
//
// Copyright © 2021, 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://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:
// ----------------------------------------------------------------------------------------------------
// 01/05/2021 - J. Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.IO;
namespace Gemstone.PhasorProtocols.IEEEC37_118
{
/// <summary>
/// Collects frame images until a full IEEE C37.118 frame has been received.
/// </summary>
public sealed class FrameImageCollector : IDisposable
{
#region [ Members ]
// Fields
private readonly MemoryStream m_frameQueue;
private bool m_disposed;
#endregion
#region [ Constructors ]
/// <summary>
/// Creates a new <see cref="FrameImageCollector"/>.
/// </summary>
/// <param name="validateFrameCheckSum">Indicates if frames are selected for check-sum validation.</param>
public FrameImageCollector(bool validateFrameCheckSum)
{
m_frameQueue = new MemoryStream();
ValidateFrameCheckSum = validateFrameCheckSum;
}
#endregion
#region [ Properties ]
/// <summary>
/// Gets the combined binary image of all the collected frame images.
/// </summary>
public byte[] BinaryImage => m_frameQueue.ToArray();
/// <summary>
/// Gets the length of the combined binary image of all the collected frame images.
/// </summary>
public int BinaryLength => (int)m_frameQueue.Length;
/// <summary>
/// Gets the total number frames appended to <see cref="FrameImageCollector"/>.
/// </summary>
public int Count { get; private set; }
/// <summary>
/// Gets flag based on <see cref="CheckSumValidationFrameTypes"/> property that determines if frames are selected for check-sum validation.
/// </summary>
public bool ValidateFrameCheckSum { get; }
#endregion
#region [ Methods ]
/// <summary>
/// Releases all the resources used by the <see cref="FrameImageCollector"/> object.
/// </summary>
public void Dispose()
{
if (m_disposed)
return;
try
{
m_frameQueue?.Dispose();
}
finally
{
m_disposed = true; // Prevent duplicate dispose.
}
}
/// <summary>
/// Appends the current frame image to the frame image collection.
/// </summary>
/// <param name="buffer">A <see cref="byte"/> array to append to the collection.</param>
/// <param name="length">An <see cref="int"/> value indicating the number of bytes to read from the <paramref name="buffer"/>.</param>
/// <param name="offset">An <see cref="int"/> value indicating the offset to read from.</param>
public void AppendFrameImage(byte[] buffer, int offset, int length)
{
// Include initial header in new stream
if (m_frameQueue.Length == 0)
m_frameQueue.Write(buffer, offset, ConfigurationFrame3.FrameHeaderLength);
// Skip past header, including CONT_IDX
offset += ConfigurationFrame3.FrameHeaderLength;
// Include frame image
m_frameQueue.Write(buffer, offset, length - ConfigurationFrame3.FrameHeaderLength);
// Track total frame images
Count++;
}
#endregion
}
}