// 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.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NLog;
using Raytheon.Common;
namespace Raytheon.Instruments
{
///
/// Class for controlling a TCP client communication device
///
public class CommDeviceTcpClient : ICommDevice, IDisposable
{
#region PrivateClassMembers
private const uint _DEFAULT_READ_TIMEOUT = 25;
private const uint _DEFAULT_SEND_TIMEOUT = 5000;
private const uint _DEFAULT_READ_BUFFER_SIZE = 1024;
private static object _syncObj = new object();
private TcpClient _tcpClient;
private NetworkStream _tcpIpStream;
private readonly int _remotePort;
private readonly string _remoteAddress;
private readonly string _name;
private State _state;
private readonly ILogger _logger;
private readonly IConfigurationManager _configurationManager;
private readonly IConfiguration _configuration;
#endregion
public bool ClearErrors() => false;
public bool FrontPanelEnabled { get => false; set => throw new NotImplementedException(); }
public bool DisplayEnabled { get => false; set => throw new NotImplementedException(); }
public string DetailedStatus => $"This is a TCP/IP Device called {_name}";
public InstrumentMetadata Info => throw new NotImplementedException();
public State Status => _state;
public string Name => _name;
public SelfTestResult PerformSelfTest() => SelfTestResult;
public SelfTestResult SelfTestResult => SelfTestResult.Unknown;
public void Open() => Initialize();
public void Close() => Shutdown();
public void Reset()
{
Close();
Open();
}
#region Private Functions
///
/// Dispose of the resources contained by this object
///
public void Dispose()
{
try
{
lock (_syncObj)
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
catch (Exception err)
{
_logger.Error(err.Message + "\r\n" + err.StackTrace);
}
}
///
/// Dispose of the resources contained by this object
///
///
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// close the socket
try
{
Shutdown();
}
catch (Exception err)
{
_logger.Error(err.Message + "\r\n" + err.StackTrace);
}
}
}
#endregion
#region Public Functions
///
/// CommDevice factory constructor
///
///
///
public CommDeviceTcpClient(string deviceName, IConfigurationManager configurationManager, string remoteAddress = "", int remotePort = 0)
{
_name = deviceName;
// _tcpClient is created in Initialize()
_tcpClient = null;
_tcpIpStream = null;
_state = State.Uninitialized;
_logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
_configurationManager = configurationManager;
_configuration = _configurationManager.GetConfiguration(Name);
if (string.IsNullOrEmpty(remoteAddress))
{
_remoteAddress = _configuration.GetConfigurationValue("TcpClient", "RemoteAddress", "127.0.0.1");
}
else
{
_remoteAddress = remoteAddress;
}
if (remotePort == 0)
{
_remotePort = _configuration.GetConfigurationValue("TcpClient", "RemotePort", 0);
}
else
{
_remotePort = remotePort;
}
}
///
/// legacy constructor
///
/// The name of this device
/// The address of the server
/// The port that the server is listening on
public CommDeviceTcpClient(string deviceName, string remoteAddress, int remotePort)
{
_name = deviceName;
_remotePort = remotePort;
_remoteAddress = remoteAddress;
// _tcpClient is created in Initialize()
_tcpClient = null;
_tcpIpStream = null;
_logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
_state = State.Uninitialized;
}
///
/// initialize instrument
///
public void Initialize()
{
lock (_syncObj)
{
if (_state == State.Uninitialized)
{
_tcpClient = new TcpClient(_remoteAddress, _remotePort);
// set timeouts
_tcpClient.Client.SendTimeout = (int)_DEFAULT_SEND_TIMEOUT;
_tcpClient.Client.ReceiveTimeout = (int)_DEFAULT_READ_TIMEOUT;
// get the stream
_tcpIpStream = _tcpClient.GetStream();
_state = State.Ready;
}
else
{
throw new Exception($"expected the state to be Uninitialized, state was: {_state} on device {_name}");
}
}
}
///
/// shuts down the device
///
public void Shutdown()
{
lock (_syncObj)
{
if (_state == State.Ready)
{
_tcpIpStream.Dispose();
_tcpClient.Dispose();
_state = State.Uninitialized;
}
}
}
///
/// Read data from the device.
///
/// The buffer to put the data in
/// The number of bytes read
public uint Read(ref byte[] dataRead)
{
lock (_syncObj)
{
if (_tcpIpStream.DataAvailable == true)
{
_state = State.Busy;
uint numBytesRead = (uint)(_tcpIpStream.Read(dataRead, 0, dataRead.Length));
_state = State.Ready;
return numBytesRead;
}
else
{
return 0;
}
}
}
///
/// Read data from the device asynchronously.
///
/// The buffer to put the data in
/// The number of bytes read
public async Task ReadAsync(byte[] dataRead)
{
if (_tcpIpStream.DataAvailable == true)
{
_state = State.Busy;
var bytesRead = await _tcpIpStream.ReadAsync(dataRead, 0, dataRead.Length);
_state = State.Ready;
return (uint)bytesRead;
}
else
{
return 0;
}
}
///
/// Read string from the device asynchronously.
///
/// The buffer to put the data in
/// The number of bytes read
public async Task ReadAsync(CancellationToken token = default)
{
if (_tcpIpStream.DataAvailable == true)
{
_state = State.Busy;
var buffer = new byte[_DEFAULT_READ_BUFFER_SIZE];
var bytesRead = await _tcpIpStream.ReadAsync(buffer, 0, buffer.Length, token);
_state = State.Ready;
return Encoding.UTF8.GetString(buffer, 0, bytesRead);
}
else
{
return null;
}
}
///
/// Sets the read timeout
///
///
public void SetReadTimeout(uint timeoutMs)
{
_tcpClient.Client.ReceiveTimeout = (int)timeoutMs;
}
///
/// Write data to the device
///
/// The data to write
/// The number of bytes to write
/// The number of bytes that were written
public uint Write(byte[] dataToSend, uint numBytesToWrite)
{
lock (_syncObj)
{
_state = State.Busy;
_tcpIpStream.Write(dataToSend, 0, (int)numBytesToWrite);
_state = State.Ready;
return numBytesToWrite;
}
}
///
/// Write data to the device asynchronously
///
///
///
///
public async Task WriteAsync(byte[] dataToSend, uint numBytesToWrite)
{
_state = State.Busy;
await _tcpIpStream.WriteAsync(dataToSend, 0, (int)numBytesToWrite);
_state = State.Ready;
return numBytesToWrite;
}
///
/// Write string data to the device asynchronously
///
///
///
public async Task WriteAsync(string message)
{
_state = State.Busy;
var buffer = Encoding.UTF8.GetBytes(message);
await _tcpIpStream.WriteAsync(buffer, 0, buffer.Length);
_state = State.Ready;
}
///
/// Send Command and Get Response asynchronously
///
///
///
///
public async Task SendCommandGetResponseAsync(string message, int timeoutInMs = 5000)
{
_logger.Info($"Sending command waiting for response ({_remoteAddress}:{_remotePort}), message: {message}");
await WriteAsync(message);
CancellationTokenSource tokenSource = new CancellationTokenSource(new TimeSpan(0, 0, 0, 0, milliseconds: timeoutInMs));
string readResponse = await ReadAsync(tokenSource.Token);
_logger.Info($"Received response: {readResponse}");
return readResponse;
}
///
/// keeps reading until canceled via token,
/// received messages sent to dataReceived function
///
///
///
///
public async Task KeepReadingAsync(CancellationToken cancellationToken, Action dataReceived)
{
_logger.Info($"About to start continuous reading from {_remoteAddress}:{_remotePort} ...");
byte[] buffer = new byte[_DEFAULT_READ_BUFFER_SIZE];
while (!cancellationToken.IsCancellationRequested)
{
int bytesRead = await _tcpIpStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
if (bytesRead > 0)
{
string data = Encoding.UTF8.GetString(buffer, 0, bytesRead);
_logger.Info($"Message received from {_remoteAddress}:{_remotePort}: {data}");
dataReceived(data);
Array.Clear(buffer, 0, bytesRead);
}
}
}
#endregion
}
}