forked from shiftwinting/FastGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcpInterceptHostedService.cs
58 lines (55 loc) · 1.8 KB
/
TcpInterceptHostedService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.PacketIntercept
{
/// <summary>
/// tcp拦截后台服务
/// </summary>
[SupportedOSPlatform("windows")]
sealed class TcpInterceptHostedService : BackgroundService
{
private readonly IEnumerable<ITcpInterceptor> tcpInterceptors;
private readonly ILogger<TcpInterceptHostedService> logger;
private readonly IHost host;
/// <summary>
/// tcp拦截后台服务
/// </summary>
/// <param name="tcpInterceptors"></param>
/// <param name="logger"></param>
/// <param name="host"></param>
public TcpInterceptHostedService(
IEnumerable<ITcpInterceptor> tcpInterceptors,
ILogger<TcpInterceptHostedService> logger,
IHost host)
{
this.tcpInterceptors = tcpInterceptors;
this.logger = logger;
this.host = host;
}
/// <summary>
/// https后台
/// </summary>
/// <param name="stoppingToken"></param>
/// <returns></returns>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken));
await Task.WhenAll(tasks);
}
catch (Exception ex)
{
stoppingToken.ThrowIfCancellationRequested();
this.logger.LogError(ex, "tcp拦截器异常");
await this.host.StopAsync(stoppingToken);
}
}
}
}