some connection improvements
This commit is contained in:
parent
2c34c36c45
commit
c82fd14d61
@ -165,7 +165,7 @@ namespace bsmd.AIS2Service
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
_log.InfoFormat("No decoder for AIS class {0}", (AISType)type);
|
// _log.InfoFormat("No decoder for AIS class {0}", (AISType)type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -122,6 +122,7 @@ namespace bsmd.AIS2Service
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if(aisStatus != AISClass.Status.UNSUPPORTED)
|
||||||
_log.WarnFormat("failed to decode AIS data: {0}", aisStatus);
|
_log.WarnFormat("failed to decode AIS data: {0}", aisStatus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,6 +19,9 @@ namespace bsmd.AIS2Service
|
|||||||
private TcpClient tcpSocket;
|
private TcpClient tcpSocket;
|
||||||
private Thread _thread;
|
private Thread _thread;
|
||||||
|
|
||||||
|
private const int sleepRetry = 30000;
|
||||||
|
private const int maxRetries = 3;
|
||||||
|
|
||||||
private static readonly ILog _log = LogManager.GetLogger(typeof(SerialTCPReader));
|
private static readonly ILog _log = LogManager.GetLogger(typeof(SerialTCPReader));
|
||||||
|
|
||||||
|
|
||||||
@ -29,31 +32,52 @@ namespace bsmd.AIS2Service
|
|||||||
|
|
||||||
private void ReadData()
|
private void ReadData()
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
while (!_stopFlag)
|
while (!_stopFlag)
|
||||||
{
|
{
|
||||||
if (this.tcpSocket == null || !this.tcpSocket.Connected) this.Connect();
|
if (this.tcpSocket == null || !this.tcpSocket.Connected)
|
||||||
|
{
|
||||||
|
if (!this.Connect())
|
||||||
|
{
|
||||||
|
_log.Error("Cannot connect to datasource, stopping service");
|
||||||
|
this.FatalErrorOccurred?.Invoke(this, new EventArgs());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
foreach (string line in ReadLines(this.tcpSocket.GetStream(), Encoding.ASCII))
|
foreach (string line in ReadLines(this.tcpSocket.GetStream(), Encoding.ASCII))
|
||||||
{
|
{
|
||||||
_inputQueue.Enqueue(line);
|
_inputQueue.Enqueue(line);
|
||||||
if (_stopFlag) return;
|
if (_stopFlag) return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_log.ErrorFormat("Something bad has happened: {0}", ex.Message);
|
_log.WarnFormat("Exception occurred on serial reader: {0}", ex.Message);
|
||||||
this.FatalErrorOccurred?.Invoke(this, new EventArgs());
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Connect()
|
private bool Connect()
|
||||||
|
{
|
||||||
|
int retryCounter = 0;
|
||||||
|
while (retryCounter < maxRetries)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
this.tcpSocket = new TcpClient(_host, (int)_port);
|
this.tcpSocket = new TcpClient(_host, (int)_port);
|
||||||
_log.InfoFormat("TCP stream connected ({0}:{1})", _host, _port);
|
_log.InfoFormat("TCP stream connected ({0}:{1})", _host, _port);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
retryCounter++;
|
||||||
|
_log.WarnFormat("connect failed: {0}, retrying {1}", ex.Message, retryCounter);
|
||||||
|
Thread.Sleep(sleepRetry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<string> ReadLines(Stream source, Encoding encoding)
|
private static IEnumerable<string> ReadLines(Stream source, Encoding encoding)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user