public Task StartAsync(CancellationToken cancellationToken)
 {
     Logger.LogInformation($"JT1078 Tcp Server start at {IPAddress.Any}:{Configuration.TcpPort}.");
     Task.Factory.StartNew(async() =>
     {
         while (!cancellationToken.IsCancellationRequested)
         {
             var socket = await server.AcceptAsync();
             JT1078TcpSession jT808TcpSession = new JT1078TcpSession(socket);
             SessionManager.TryAdd(jT808TcpSession);
             await Task.Factory.StartNew(async(state) =>
             {
                 var session = (JT1078TcpSession)state;
                 if (Logger.IsEnabled(LogLevel.Information))
                 {
                     Logger.LogInformation($"[Connected]:{session.Client.RemoteEndPoint}");
                 }
                 var pipe     = new Pipe();
                 Task writing = FillPipeAsync(session, pipe.Writer);
                 Task reading = ReadPipeAsync(session, pipe.Reader);
                 await Task.WhenAll(reading, writing);
                 SessionManager.RemoveBySessionId(session.SessionID);
             }, jT808TcpSession);
         }
     }, cancellationToken);
     return(Task.CompletedTask);
 }
Exemple #2
0
 protected override async Task ExecuteAsync(CancellationToken stoppingToken)
 {
     while (!stoppingToken.IsCancellationRequested)
     {
         try
         {
             List <string> sessionIds = new List <string>();
             foreach (var item in SessionManager.GetUdpAll())
             {
                 if (item.ActiveTime.AddSeconds(Configuration.CurrentValue.UdpReaderIdleTimeSeconds) < DateTime.Now)
                 {
                     sessionIds.Add(item.SessionID);
                 }
             }
             foreach (var item in sessionIds)
             {
                 SessionManager.RemoveBySessionId(item);
             }
             if (Logger.IsEnabled(LogLevel.Information))
             {
                 Logger.LogInformation($"[Check Receive Timeout]");
                 Logger.LogInformation($"[Session Online Count]:{SessionManager.UdpSessionCount}");
             }
         }
         catch (Exception ex)
         {
             Logger.LogError(ex, $"[Receive Timeout]");
         }
         finally
         {
             await Task.Delay(TimeSpan.FromSeconds(Configuration.CurrentValue.UdpReceiveTimeoutCheckTimeSeconds), stoppingToken);
         }
     }
 }