Showing the changes to Source/Libraries/GSF.Core/Parsing/MultiSourceFrameImageParserBase.cs (1 of 22 files). Show all
Unified · Split
| @@ -78,17 +78,10 @@ namespace GSF.Parsing · 77 unmodified lines | |||
| 78 | /// automatic object pool handling, e.g., returning object to pool when disposed. | 78 | /// automatic object pool handling, e.g., returning object to pool when disposed. |
| 79 | /// </remarks> | 79 | /// </remarks> |
| 80 | [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] | 80 | [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] |
| 81 | public class SourceIdentifiableBuffer : ISupportLifecycle | 81 | public class SourceIdentifiableBuffer |
| 82 | { | 82 | { |
| 83 | #region [ Members ] | 83 | #region [ Members ] |
| 84 | 84 | ||
| 85 | // Events | ||
| 86 | |||
| 87 | /// <summary> | ||
| 88 | /// Occurs when <see cref="SourceIdentifiableBuffer"/> is disposed. | ||
| 89 | /// </summary> | ||
| 90 | public event EventHandler Disposed; | ||
| 91 | |||
| 92 | // Fields | 85 | // Fields |
| 93 | 86 | ||
| 94 | /// <summary> | 87 | /// <summary> |
| @@ -102,19 +95,6 @@ namespace GSF.Parsing · 7 unmodified lines | |||
| 102 | public byte[] Buffer; | 95 | public byte[] Buffer; |
| 103 | 96 | ||
| 104 | private int m_count; | 97 | private int m_count; |
| 105 | private bool m_disposed; | ||
| 106 | |||
| 107 | #endregion | ||
| 108 | |||
| 109 | #region [ Constructors ] | ||
| 110 | |||
| 111 | /// <summary> | ||
| 112 | /// Releases the unmanaged resources before the <see cref="SourceIdentifiableBuffer"/> object is reclaimed by <see cref="GC"/>. | ||
| 113 | /// </summary> | ||
| 114 | ~SourceIdentifiableBuffer() | ||
| 115 | { | ||
| 116 | Dispose(false); | ||
| 117 | } | ||
| 118 | 98 | ||
| 119 | #endregion | 99 | #endregion |
| 120 | 100 | ||
| @@ -144,63 +124,7 @@ namespace GSF.Parsing · 23 unmodified lines | |||
| 144 | set | 124 | set |
| 145 | { | 125 | { |
| 146 | m_count = value; | 126 | m_count = value; |
| 147 | 127 | Buffer = new byte[m_count]; | |
| 148 | if ((object)Buffer != null) | ||
| 149 | BufferPool.ReturnBuffer(Buffer); | ||
| 150 | |||
| 151 | if (m_count > 0) | ||
| 152 | Buffer = BufferPool.TakeBuffer(m_count); | ||
| 153 | else | ||
| 154 | Buffer = null; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | #endregion | ||
| 159 | |||
| 160 | #region [ Methods ] | ||
| 161 | |||
| 162 | /// <summary> | ||
| 163 | /// Releases all the resources used by the <see cref="SourceIdentifiableBuffer"/> object. | ||
| 164 | /// </summary> | ||
| 165 | public void Dispose() | ||
| 166 | { | ||
| 167 | Dispose(true); | ||
| 168 | GC.SuppressFinalize(this); | ||
| 169 | } | ||
| 170 | |||
| 171 | /// <summary> | ||
| 172 | /// Releases the unmanaged resources used by the <see cref="SourceIdentifiableBuffer"/> object and optionally releases the managed resources. | ||
| 173 | /// </summary> | ||
| 174 | /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param> | ||
| 175 | protected virtual void Dispose(bool disposing) | ||
| 176 | { | ||
| 177 | if (!m_disposed) | ||
| 178 | { | ||
| 179 | try | ||
| 180 | { | ||
| 181 | // Return any existing buffer to the pool | ||
| 182 | Count = 0; | ||
| 183 | } | ||
| 184 | finally | ||
| 185 | { | ||
| 186 | m_disposed = true; // Prevent duplicate dispose. | ||
| 187 | |||
| 188 | if (Disposed != null) | ||
| 189 | Disposed(this, EventArgs.Empty); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | /// <summary> | ||
| 195 | /// Initializes (or reinitializes) <see cref="SourceIdentifiableBuffer"/> state. | ||
| 196 | /// </summary> | ||
| 197 | public void Initialize() | ||
| 198 | { | ||
| 199 | // Un-dispose class instance if it is being reused | ||
| 200 | if (m_disposed) | ||
| 201 | { | ||
| 202 | m_disposed = false; | ||
| 203 | GC.ReRegisterForFinalize(this); | ||
| 204 | } | 128 | } |
| 205 | } | 129 | } |
| 206 | 130 | ||
| @@ -323,31 +247,20 @@ namespace GSF.Parsing · 116 unmodified lines | |||
| 323 | 247 | ||
| 324 | buffer.ValidateParameters(offset, count); | 248 | buffer.ValidateParameters(offset, count); |
| 325 | 249 | ||
| 326 | try | 250 | if (count > 0) |
| 327 | { | ||
| 328 | if (count > 0) | ||
| 329 | { | ||
| 330 | // Get an identifiable buffer object | ||
| 331 | identifiableBuffer = FastObjectFactory<SourceIdentifiableBuffer>.CreateObjectFunction(); | ||
| 332 | identifiableBuffer.Source = source; | ||
| 333 | identifiableBuffer.Count = count; | ||
| 334 | |||
| 335 | // Copy buffer data for processing (destination buffer pre-allocated from buffer pool) | ||
| 336 | Buffer.BlockCopy(buffer, offset, identifiableBuffer.Buffer, 0, count); | ||
| 337 | |||
| 338 | // Add buffer to the queue for parsing. Note that buffer is queued for parsing instead | ||
| 339 | // of handling parse on this thread - this has become necessary to reduce UDP data loss | ||
| 340 | // that can happen in-process when system has UDP buffers building up for processing. | ||
| 341 | m_bufferQueue.Enqueue(new[] { identifiableBuffer }); | ||
| 342 | } | ||
| 343 | } | ||
| 344 | catch | ||
| 345 | { | 251 | { |
| 346 | // Properly dispose of the source buffer if we failed to queue it for processing | 252 | // Get an identifiable buffer object |
| 347 | if ((object)identifiableBuffer != null) | 253 | identifiableBuffer = FastObjectFactory<SourceIdentifiableBuffer>.CreateObjectFunction(); |
| 348 | identifiableBuffer.Dispose(); | 254 | identifiableBuffer.Source = source; |
| 349 | 255 | identifiableBuffer.Count = count; | |
| 350 | throw; | 256 | |
| 257 | // Copy buffer data for processing | ||
| 258 | Buffer.BlockCopy(buffer, offset, identifiableBuffer.Buffer, 0, count); | ||
| 259 | |||
| 260 | // Add buffer to the queue for parsing. Note that buffer is queued for parsing instead | ||
| 261 | // of handling parse on this thread - this has become necessary to reduce UDP data loss | ||
| 262 | // that can happen in-process when system has UDP buffers building up for processing. | ||
| 263 | m_bufferQueue.Enqueue(new[] { identifiableBuffer }); | ||
| 351 | } | 264 | } |
| 352 | } | 265 | } |
| 353 | 266 | ||
| @@ -430,16 +343,6 @@ namespace GSF.Parsing · 76 unmodified lines | |||
| 430 | } | 343 | } |
| 431 | finally | 344 | finally |
| 432 | { | 345 | { |
| 433 | // Dispose of source buffers | ||
| 434 | if ((object)buffers != null) | ||
| 435 | { | ||
| 436 | foreach (SourceIdentifiableBuffer buffer in buffers) | ||
| 437 | { | ||
| 438 | if ((object)buffer != null) | ||
| 439 | buffer.Dispose(); | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | // If user has attached to SourceDataParsed event, expose list of parsed data per source | 346 | // If user has attached to SourceDataParsed event, expose list of parsed data per source |
| 444 | if ((object)SourceDataParsed != null) | 347 | if ((object)SourceDataParsed != null) |
| 445 | { | 348 | { |