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