// UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
COMPANY.
THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
GOVERNMENT.
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/
using System;
using System.Threading;
namespace Raytheon.Common
{
///
/// Base class for handling incoming messages
///
public abstract class MsgDevice : IDisposable
{
///
/// A callback def for a completed message
///
///
///
///
///
unsafe public delegate void CompleteMessageCallback(uint msgId, IntPtr pData, uint numBytes, uint errorCode);
private DataBuffer _dataBuffer;
private AutoResetEvent _dataInBufferEvent;
private IWorkerInterface _msgProcessorWorker;
private bool _isProcessorThreadRunning;
private Thread _msgProcessorThread;
///
/// The constructor
///
///
///
public MsgDevice(IMsgParser msgParser, uint bufferSize)
{
try
{
_isProcessorThreadRunning = false;
_dataBuffer = new DataBuffer(bufferSize);
_dataInBufferEvent = new AutoResetEvent(false);
_msgProcessorWorker = new MsgProcessorWorker(msgParser, ref _dataBuffer, ref _dataInBufferEvent);
_msgProcessorThread = new Thread(_msgProcessorWorker.DoWork);
}
catch (Exception)
{
throw;
}
}
///
/// The finalizer
///
~MsgDevice()
{
Dispose(false);
}
///
/// The public dispose function. Necessary for commanding threads to quit
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Add data to the buffer
///
/// The data to add
/// the number of bytes to add
protected void AddToBuffer(byte[] data, uint numBytes)
{
try
{
_dataBuffer.Add(data, numBytes);
_dataInBufferEvent.Set();
}
catch (Exception)
{
throw;
}
}
///
/// Clear all data from the buffer
///
protected void ClearBuffer()
{
try
{
_dataBuffer.RemoveAll();
}
catch (Exception)
{
throw;
}
}
///
/// The local dispose function. Necessary for commanding threads to quit
///
///
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_msgProcessorWorker.QuitWork();
Thread.Sleep(500);
_msgProcessorThread.Abort();
if (_msgProcessorThread.IsAlive)
{
_msgProcessorThread.Join();
}
_dataInBufferEvent.Dispose();
_msgProcessorWorker.Dispose();
_dataBuffer.Dispose();
}
}
///
/// Run the message processor thread
///
protected void RunThread()
{
try
{
_msgProcessorThread.Start();
}
catch (Exception)
{
throw;
}
}
///
/// Set the callback that will be invoked when a complete message is parsed out
///
/// The callback function
protected void SetCompleteMessageCallback(CompleteMessageCallback callback)
{
try
{
((MsgProcessorWorker)_msgProcessorWorker).SetCallback(callback);
// now that the callback is set, we can run the thread
if (_isProcessorThreadRunning == false)
{
_msgProcessorThread.Start();
_isProcessorThreadRunning = true;
}
}
catch (Exception)
{
throw;
}
}
///
/// Stop the message processor thread
///
protected void QuitThread()
{
try
{
_msgProcessorWorker.QuitWork();
if (_msgProcessorThread.IsAlive)
{
_msgProcessorThread.Join();
}
}
catch (Exception)
{
throw;
}
}
///
/// abstract function for the children to implement
///
/// The data to add
/// The number of bytes to add
public abstract void AddData(byte[] data, uint numBytes);
}
}