|
@@ -24,8 +24,8 @@ namespace Edge.Core.Processor.Communicator
|
|
|
private CancellationTokenSource readAsyncCancellationTokenSource;
|
|
|
private DateTime? lastReceiveMsgDataFromTcpClientDateTime;
|
|
|
private System.Timers.Timer clientSideActiveDetectionByCheckingLongTimeNoSeeDataIncomingWatchTimer;
|
|
|
- //protected static NLog.Logger logger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("Communicator");
|
|
|
- protected ILogger logger = NullLogger.Instance;
|
|
|
+ private static NLog.Logger logger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("TcpServerCommunicator");
|
|
|
+ //protected ILogger logger = NullLogger.Instance;
|
|
|
public static int tcpReceiveBufferSize = 1500;
|
|
|
|
|
|
private Edge.Core.Parser.BinaryParser.ParserBase parser;
|
|
@@ -71,16 +71,17 @@ namespace Edge.Core.Processor.Communicator
|
|
|
IServiceProvider services)
|
|
|
{
|
|
|
this.Identity = "*:" + localTcpServerTcpListeningPortNumber;
|
|
|
- if (services != null)
|
|
|
- {
|
|
|
- var loggerFactory = services.GetRequiredService<ILoggerFactory>();
|
|
|
- this.logger = loggerFactory.CreateLogger("Communicator");
|
|
|
- if (!string.IsNullOrEmpty(customLoggerFileName))
|
|
|
- if (customLoggerFileName == "*")
|
|
|
- this.logger = loggerFactory.CreateLogger("DynamicPrivate_Comm_" + localTcpServerTcpListeningPortNumber);
|
|
|
- else
|
|
|
- this.logger = loggerFactory.CreateLogger("DynamicPrivate_Comm_" + customLoggerFileName);
|
|
|
- }
|
|
|
+ //if (services != null)
|
|
|
+ //{
|
|
|
+ // var loggerFactory = services.GetRequiredService<ILoggerFactory>();
|
|
|
+ // this.logger = loggerFactory.CreateLogger("Communicator");
|
|
|
+ // if (!string.IsNullOrEmpty(customLoggerFileName))
|
|
|
+ // if (customLoggerFileName == "*")
|
|
|
+ // this.logger = loggerFactory.CreateLogger("DynamicPrivate_Comm_" + localTcpServerTcpListeningPortNumber);
|
|
|
+ // else
|
|
|
+ // this.logger = loggerFactory.CreateLogger("DynamicPrivate_Comm_" + customLoggerFileName);
|
|
|
+ //}
|
|
|
+
|
|
|
|
|
|
this.messageCutter = binaryMsgCutter;
|
|
|
|
|
@@ -92,7 +93,8 @@ namespace Edge.Core.Processor.Communicator
|
|
|
this.messageCutter.OnInvalidMessageRead += (____, ______) =>
|
|
|
{
|
|
|
var loggingStr = $"Bytes msg from tcp client: {exclusiveTcpClient_ClientRemoteEndPoint_Str} Read Invalid data, detail: {(______?.Message ?? "")}";
|
|
|
- this.logger.LogInformation(loggingStr);
|
|
|
+ //this.logger.LogInformation(loggingStr);
|
|
|
+ logger.Info(loggingStr);
|
|
|
this.OnErrorMessageRead?.Invoke(this, new CommunicatorErrorMessageReadEventArg(null, loggingStr));
|
|
|
};
|
|
|
this.messageCutter.OnMessageCut += (s, _) =>
|
|
@@ -103,14 +105,16 @@ namespace Edge.Core.Processor.Communicator
|
|
|
{
|
|
|
eventArg.Data = this.messageCutter.Message;
|
|
|
eventArg.Message = this.parser.Deserialize(this.messageCutter.Message.ToArray()) as T;
|
|
|
- if (logger.IsEnabled(LogLevel.Debug))
|
|
|
- this.logger.LogDebug(" Parsed: " + eventArg.Message.ToLogString());
|
|
|
+ //if (logger.IsEnabled(LogLevel.Debug))
|
|
|
+ //this.logger.LogDebug(" Parsed: " + eventArg.Message.ToLogString());
|
|
|
+ logger.Debug(" Parsed: " + eventArg.Message.ToLogString());
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
var loggingStr = "Message from " + exclusiveTcpClient_ClientRemoteEndPoint_Str
|
|
|
+ " exceptioned in deserilaizing bytes:\r\n 0x" + this.messageCutter.Message.ToHexLogString() + "\r\n exception detail:\r\n" + ex;
|
|
|
- this.logger.LogError(loggingStr);
|
|
|
+ //this.logger.LogError(loggingStr);
|
|
|
+ logger.Error(loggingStr);
|
|
|
this.OnErrorMessageRead?.Invoke(this, new CommunicatorErrorMessageReadEventArg(this.messageCutter.Message, loggingStr));
|
|
|
return;
|
|
|
}
|
|
@@ -121,7 +125,9 @@ namespace Edge.Core.Processor.Communicator
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- this.logger.LogError("Message from " + exclusiveTcpClient_ClientRemoteEndPoint_Str
|
|
|
+ //this.logger.LogError("Message from " + exclusiveTcpClient_ClientRemoteEndPoint_Str
|
|
|
+ // + " exceptioned in handle message:\r\n" + eventArg.Message.ToLogString() + "\r\n exceptioned detail: \r\n" + ex);
|
|
|
+ logger.Error("Message from " + exclusiveTcpClient_ClientRemoteEndPoint_Str
|
|
|
+ " exceptioned in handle message:\r\n" + eventArg.Message.ToLogString() + "\r\n exceptioned detail: \r\n" + ex);
|
|
|
}
|
|
|
};
|
|
@@ -138,7 +144,8 @@ namespace Edge.Core.Processor.Communicator
|
|
|
if (DateTime.Now.Subtract(this.lastReceiveMsgDataFromTcpClientDateTime ?? DateTime.MinValue).TotalSeconds >= enableClientSideActiveDetection
|
|
|
&& this.exclusiveTcpClient != null)
|
|
|
{
|
|
|
- this.logger.LogInformation($"Long time no see data from tcp client with: { this.exclusiveTcpClient_ClientRemoteEndPoint_Str }, will actively disconnect it...");
|
|
|
+ //this.logger.LogInformation($"Long time no see data from tcp client with: { this.exclusiveTcpClient_ClientRemoteEndPoint_Str }, will actively disconnect it...");
|
|
|
+ logger.Info($"Long time no see data from tcp client with: { this.exclusiveTcpClient_ClientRemoteEndPoint_Str }, will actively disconnect it...");
|
|
|
this.readAsyncCancellationTokenSource?.Cancel();
|
|
|
}
|
|
|
};
|
|
@@ -173,18 +180,22 @@ namespace Edge.Core.Processor.Communicator
|
|
|
{
|
|
|
this.tcpListener = new TcpListener(IPAddress.Any, this.localTcpServerListeningPort);
|
|
|
this.tcpListener.Start();
|
|
|
- this.logger.LogInformation($"TcpListener listened on localPort: {this.localTcpServerListeningPort}");
|
|
|
+ //this.logger.LogInformation($"TcpListener listened on localPort: {this.localTcpServerListeningPort}");
|
|
|
+ logger.Info($"TcpListener listened on localPort: {this.localTcpServerListeningPort}");
|
|
|
var _ = Task.Run(async () =>
|
|
|
{
|
|
|
while (this.isStarted == 1)
|
|
|
{
|
|
|
- logger.LogInformation($"Waitting for connection on localPort: {this.localTcpServerListeningPort}");
|
|
|
+ //logger.LogInformation($"Waitting for connection on localPort: {this.localTcpServerListeningPort}");
|
|
|
+ logger.Info($"Waitting for connection on localPort: {this.localTcpServerListeningPort}");
|
|
|
var newTcpClient = await this.tcpListener.AcceptTcpClientAsync();
|
|
|
- this.logger.LogInformation($" A tcp client with remote ip/port: {newTcpClient.Client.RemoteEndPoint} has connected in");
|
|
|
+ //this.logger.LogInformation($" A tcp client with remote ip/port: {newTcpClient.Client.RemoteEndPoint} has connected in");
|
|
|
+ logger.Info($" A tcp client with remote ip/port: {newTcpClient.Client.RemoteEndPoint} has connected in");
|
|
|
|
|
|
if (this.exclusiveTcpClient != null)
|
|
|
{
|
|
|
- logger.LogInformation($" There's already a previous TcpClient established as exclusive, so close this new one with remote ip/port: {newTcpClient.Client.RemoteEndPoint}");
|
|
|
+ //logger.LogInformation($" There's already a previous TcpClient established as exclusive, so close this new one with remote ip/port: {newTcpClient.Client.RemoteEndPoint}");
|
|
|
+ logger.Info($" There's already a previous TcpClient established as exclusive, so close this new one with remote ip/port: {newTcpClient.Client.RemoteEndPoint}");
|
|
|
try
|
|
|
{
|
|
|
newTcpClient.Close();
|
|
@@ -195,7 +206,8 @@ namespace Edge.Core.Processor.Communicator
|
|
|
|
|
|
this.exclusiveTcpClient = newTcpClient;
|
|
|
this.exclusiveTcpClient_ClientRemoteEndPoint_Str = this.exclusiveTcpClient.Client.RemoteEndPoint.ToString();
|
|
|
- this.logger.LogInformation($" Tcp client with remote ip/port: {this.exclusiveTcpClient_ClientRemoteEndPoint_Str} has been chosen as exclusive");
|
|
|
+ //this.logger.LogInformation($" Tcp client with remote ip/port: {this.exclusiveTcpClient_ClientRemoteEndPoint_Str} has been chosen as exclusive");
|
|
|
+ logger.Info($" Tcp client with remote ip/port: {this.exclusiveTcpClient_ClientRemoteEndPoint_Str} has been chosen as exclusive");
|
|
|
var ___ = Task.Run(async () =>
|
|
|
{
|
|
|
|
|
@@ -231,12 +243,15 @@ namespace Edge.Core.Processor.Communicator
|
|
|
bytesReadCount = await this.exclusiveTcpClient.GetStream().ReadAsync(buffer, this.readAsyncCancellationTokenSource.Token);
|
|
|
if (bytesReadCount == 0)
|
|
|
throw new Exception("tcp server received 0 count data indicates the connection is broken");
|
|
|
- if (logger.IsEnabled(LogLevel.Debug))
|
|
|
- this.logger.LogDebug($"TCP from { this.exclusiveTcpClient_ClientRemoteEndPoint_Str } <---Incoming: 0x" + buffer.Take(bytesReadCount).ToHexLogString());
|
|
|
+ //if (logger.IsEnabled(LogLevel.Debug))
|
|
|
+ // this.logger.LogDebug($"TCP from { this.exclusiveTcpClient_ClientRemoteEndPoint_Str } <---Incoming: 0x" + buffer.Take(bytesReadCount).ToHexLogString());
|
|
|
+
|
|
|
+ logger.Debug($"TCP from {this.exclusiveTcpClient_ClientRemoteEndPoint_Str} <---Incoming: 0x" + buffer.Take(bytesReadCount).ToHexLogString());
|
|
|
}
|
|
|
catch (Exception eeee)
|
|
|
{
|
|
|
- logger.LogError($"tcp client with remote ip/port: {this.exclusiveTcpClient_ClientRemoteEndPoint_Str} exceptioned in GetStream().ReadAsync(), treat as tcp disconnection, detail: {eeee}");
|
|
|
+ //logger.LogError($"tcp client with remote ip/port: {this.exclusiveTcpClient_ClientRemoteEndPoint_Str} exceptioned in GetStream().ReadAsync(), treat as tcp disconnection, detail: {eeee}");
|
|
|
+ logger.Error($"tcp client with remote ip/port: {this.exclusiveTcpClient_ClientRemoteEndPoint_Str} exceptioned in GetStream().ReadAsync(), treat as tcp disconnection, detail: {eeee}");
|
|
|
try
|
|
|
{
|
|
|
this.exclusiveTcpClient.Close();
|
|
@@ -266,7 +281,8 @@ namespace Edge.Core.Processor.Communicator
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- this.logger.LogError($"Exception in Parsing msg bytes: 0x{ buffer.Take(bytesReadCount).ToHexLogString()}, detail: {Environment.NewLine}{ ex.ToString()}");
|
|
|
+ //this.logger.LogError($"Exception in Parsing msg bytes: 0x{ buffer.Take(bytesReadCount).ToHexLogString()}, detail: {Environment.NewLine}{ ex.ToString()}");
|
|
|
+ logger.Error($"Exception in Parsing msg bytes: 0x{ buffer.Take(bytesReadCount).ToHexLogString()}, detail: {Environment.NewLine}{ ex.ToString()}");
|
|
|
}
|
|
|
}
|
|
|
});
|
|
@@ -277,7 +293,8 @@ namespace Edge.Core.Processor.Communicator
|
|
|
}
|
|
|
catch (Exception exxx)
|
|
|
{
|
|
|
- logger.LogError($"Start tcp listener on port: {this.localTcpServerListeningPort} exceptioned: {exxx}");
|
|
|
+ //logger.LogError($"Start tcp listener on port: {this.localTcpServerListeningPort} exceptioned: {exxx}");
|
|
|
+ logger.Error($"Start tcp listener on port: {this.localTcpServerListeningPort} exceptioned: {exxx}");
|
|
|
return Task.FromResult(false);
|
|
|
}
|
|
|
}
|
|
@@ -302,7 +319,8 @@ namespace Edge.Core.Processor.Communicator
|
|
|
//rawData = this.parser.Serialize(message);
|
|
|
var arg = new CommunicatorEventArg<byte[], T>() { Data = rawData, Message = message, Continue = true };
|
|
|
this.OnRawDataWriting?.Invoke(this, arg);
|
|
|
- if (this.exclusiveTcpClient == null || !arg.Continue) { this.logger.LogError("Write failed, this.tcpClient is null: " + (this.exclusiveTcpClient is null)); return false; }
|
|
|
+ //if (this.exclusiveTcpClient == null || !arg.Continue) { this.logger.LogError("Write failed, this.tcpClient is null: " + (this.exclusiveTcpClient is null)); return false; }
|
|
|
+ if (this.exclusiveTcpClient == null || !arg.Continue) { logger.Error("Write failed, this.tcpClient is null: " + (this.exclusiveTcpClient is null)); return false; }
|
|
|
}
|
|
|
catch (Exception exx)
|
|
|
{
|
|
@@ -316,7 +334,8 @@ namespace Edge.Core.Processor.Communicator
|
|
|
msgLogStr = "exceptioned for get ToLogString()";
|
|
|
}
|
|
|
|
|
|
- this.logger.LogError("Tcp Write failed in serialize or event raise for msg: " + message.GetType() + " -> " + msgLogStr + "\r\n detail: " + exx);
|
|
|
+ //this.logger.LogError("Tcp Write failed in serialize or event raise for msg: " + message.GetType() + " -> " + msgLogStr + "\r\n detail: " + exx);
|
|
|
+ logger.Error("Tcp Write failed in serialize or event raise for msg: " + message.GetType() + " -> " + msgLogStr + "\r\n detail: " + exx);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -324,15 +343,19 @@ namespace Edge.Core.Processor.Communicator
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- if (logger.IsEnabled(LogLevel.Debug))
|
|
|
- this.logger.LogDebug("TCP to " + (this.exclusiveTcpClient_ClientRemoteEndPoint_Str) + " Outgoing--->: " + message.ToLogString() + "\r\n 0x" + rawData.ToHexLogString());
|
|
|
+ //if (logger.IsEnabled(LogLevel.Debug))
|
|
|
+ // this.logger.LogDebug("TCP to " + (this.exclusiveTcpClient_ClientRemoteEndPoint_Str) + " Outgoing--->: " + message.ToLogString() + "\r\n 0x" + rawData.ToHexLogString());
|
|
|
+ if(rawData.Length>8 && rawData[6] != 0x55 && rawData[7] != 0x10)
|
|
|
+ logger.Info("TCP to " + (this.exclusiveTcpClient_ClientRemoteEndPoint_Str) + " Outgoing--->: " + message.ToLogString() + "\r\n 0x" + rawData.ToHexLogString());
|
|
|
var sendCount = this.exclusiveTcpClient.Client.Send(rawData);
|
|
|
if (sendCount == 0)
|
|
|
throw new InvalidOperationException("the send count in this.exclusiveTcpClient.Client.Send is 0");
|
|
|
}
|
|
|
catch (Exception exx)
|
|
|
{
|
|
|
- this.logger.LogError("Send tcp msg to "
|
|
|
+ //this.logger.LogError("Send tcp msg to "
|
|
|
+ // + (this.exclusiveTcpClient_ClientRemoteEndPoint_Str) + " Write(...) exceptioned, treat as a broken tcp connection, will cancel the data read as well, detail: " + exx);
|
|
|
+ logger.Error("Send tcp msg to "
|
|
|
+ (this.exclusiveTcpClient_ClientRemoteEndPoint_Str) + " Write(...) exceptioned, treat as a broken tcp connection, will cancel the data read as well, detail: " + exx);
|
|
|
try
|
|
|
{
|