diff --git a/src/JT808.Gateway/JT808TcpServer.cs b/src/JT808.Gateway/JT808TcpServer.cs index c2dd454..fc6ad03 100644 --- a/src/JT808.Gateway/JT808TcpServer.cs +++ b/src/JT808.Gateway/JT808TcpServer.cs @@ -114,7 +114,7 @@ namespace JT808.Gateway var pipe = new Pipe(); Task writing = FillPipeAsync(session, pipe.Writer); Task reading = ReadPipeAsync(session, pipe.Reader); - await Task.WhenAll(reading, writing); + await Task.WhenAny(reading, writing); SessionManager.RemoveBySessionId(session.SessionID); }, jT808TcpSession); } @@ -245,7 +245,7 @@ namespace JT808.Gateway } catch (JT808Exception ex) { - Logger.LogError($"[HeaderDeserialize ErrorCode]:{ ex.ErrorCode},[ReaderBuffer]:{data?.ToHexString()},{session.Client.RemoteEndPoint},{session.TerminalPhoneNo}"); + Logger.LogError($"[HeaderDeserialize ErrorCode]:{ex.ErrorCode},[ReaderBuffer]:{data?.ToHexString()},{session.Client.RemoteEndPoint},{session.TerminalPhoneNo}"); } totalConsumed += seqReader.Consumed - totalConsumed; if (seqReader.End) break; diff --git a/src/JT808.Gateway/Services/JT808TcpReceiveTimeoutHostedService.cs b/src/JT808.Gateway/Services/JT808TcpReceiveTimeoutHostedService.cs index 86a4124..370b7a9 100644 --- a/src/JT808.Gateway/Services/JT808TcpReceiveTimeoutHostedService.cs +++ b/src/JT808.Gateway/Services/JT808TcpReceiveTimeoutHostedService.cs @@ -4,8 +4,6 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; -using System.Collections.Generic; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -37,9 +35,17 @@ namespace JT808.Gateway.Services { foreach (var item in SessionManager.GetTcpAll()) { - if (item.ActiveTime.AddSeconds(Configuration.TcpReaderIdleTimeSeconds) < DateTime.Now) + try { - item.ReceiveTimeout.Cancel(); + if (item.ActiveTime.AddSeconds(Configuration.TcpReaderIdleTimeSeconds) < DateTime.Now) + { + item.ReceiveTimeout.Cancel(); + } + } + catch (Exception e) + { + Logger.LogError(e, "Check whether the session with id {id} and sim number {terminal} has timed out and failed.", item.SessionID, item.TerminalPhoneNo); + continue; } } Logger.LogInformation($"[Check Receive Timeout]");