Browse Source

fix(session): fix session timeout detection and tcp listener exception not released

master
yedajiang44 11 months ago
parent
commit
eb0ddd90a6
2 changed files with 12 additions and 6 deletions
  1. +2
    -2
      src/JT808.Gateway/JT808TcpServer.cs
  2. +10
    -4
      src/JT808.Gateway/Services/JT808TcpReceiveTimeoutHostedService.cs

+ 2
- 2
src/JT808.Gateway/JT808TcpServer.cs View File

@@ -114,7 +114,7 @@ namespace JT808.Gateway
var pipe = new Pipe(); var pipe = new Pipe();
Task writing = FillPipeAsync(session, pipe.Writer); Task writing = FillPipeAsync(session, pipe.Writer);
Task reading = ReadPipeAsync(session, pipe.Reader); Task reading = ReadPipeAsync(session, pipe.Reader);
await Task.WhenAll(reading, writing);
await Task.WhenAny(reading, writing);
SessionManager.RemoveBySessionId(session.SessionID); SessionManager.RemoveBySessionId(session.SessionID);
}, jT808TcpSession); }, jT808TcpSession);
} }
@@ -245,7 +245,7 @@ namespace JT808.Gateway
} }
catch (JT808Exception ex) 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; totalConsumed += seqReader.Consumed - totalConsumed;
if (seqReader.End) break; if (seqReader.End) break;


+ 10
- 4
src/JT808.Gateway/Services/JT808TcpReceiveTimeoutHostedService.cs View File

@@ -4,8 +4,6 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.Collections.Generic;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;


@@ -37,9 +35,17 @@ namespace JT808.Gateway.Services
{ {
foreach (var item in SessionManager.GetTcpAll()) 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]"); Logger.LogInformation($"[Check Receive Timeout]");


Loading…
Cancel
Save