using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.PacketIntercept
{
///
/// tcp拦截后台服务
///
[SupportedOSPlatform("windows")]
sealed class TcpInterceptHostedService : BackgroundService
{
private readonly IEnumerable tcpInterceptors;
private readonly ILogger logger;
private readonly IHost host;
///
/// tcp拦截后台服务
///
///
///
///
public TcpInterceptHostedService(
IEnumerable tcpInterceptors,
ILogger logger,
IHost host)
{
this.tcpInterceptors = tcpInterceptors;
this.logger = logger;
this.host = host;
}
///
/// https后台
///
///
///
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken));
await Task.WhenAll(tasks);
}
catch (OperationCanceledException)
{
}
catch (Win32Exception ex) when (ex.NativeErrorCode == 995)
{
}
catch (Exception ex)
{
this.logger.LogError(ex, "tcp拦截器异常");
await this.host.StopAsync(stoppingToken);
}
}
}
}