Skip to content

Commit

Permalink
fix(session): fix session timeout detection and tcp listener exceptio…
Browse files Browse the repository at this point in the history
…n not released
  • Loading branch information
yedajiang44 committed Jun 4, 2024
1 parent ce6ed8a commit eb0ddd9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/JT808.Gateway/JT808TcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ await Task.Factory.StartNew(async (state) =>
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);
}
Expand Down Expand Up @@ -245,7 +245,7 @@ private void ReaderBuffer(ref ReadOnlySequence<byte> buffer, JT808TcpSession ses
}
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;
Expand Down
14 changes: 10 additions & 4 deletions src/JT808.Gateway/Services/JT808TcpReceiveTimeoutHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -37,9 +35,17 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
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]");
Expand Down

0 comments on commit eb0ddd9

Please sign in to comment.