FastTunnel/FastTunnel.Core.Client/Services/ServiceFastTunnelClient.cs

56 lines
1.8 KiB
C#
Raw Permalink Normal View History

2022-01-02 00:23:39 +08:00
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
2020-11-04 19:05:04 +08:00
using FastTunnel.Core.Client;
2020-11-01 12:27:29 +08:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
2021-06-20 11:35:21 +08:00
using System.Runtime.ExceptionServices;
using System.IO;
2020-11-01 12:27:29 +08:00
namespace FastTunnel.Core.Services
{
public class ServiceFastTunnelClient : IHostedService
{
2021-06-12 15:29:22 +08:00
readonly ILogger<ServiceFastTunnelClient> _logger;
2021-07-30 00:54:53 +08:00
readonly IFastTunnelClient _fastTunnelClient;
2020-11-01 12:27:29 +08:00
2021-07-30 00:54:53 +08:00
public ServiceFastTunnelClient(ILogger<ServiceFastTunnelClient> logger, IFastTunnelClient fastTunnelClient)
2020-11-01 12:27:29 +08:00
{
_logger = logger;
_fastTunnelClient = fastTunnelClient;
2021-08-07 23:54:48 +08:00
2021-06-20 11:35:21 +08:00
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
2020-11-01 12:27:29 +08:00
}
2021-08-02 00:04:13 +08:00
public async Task StartAsync(CancellationToken cancellationToken)
2020-11-01 12:27:29 +08:00
{
2022-07-10 22:34:13 +08:00
await _fastTunnelClient.StartAsync(cancellationToken);
2020-11-01 12:27:29 +08:00
}
2022-07-10 22:34:13 +08:00
public async Task StopAsync(CancellationToken cancellationToken)
2020-11-01 12:27:29 +08:00
{
2022-07-10 22:34:13 +08:00
await _fastTunnelClient.StopAsync(cancellationToken);
2020-11-01 12:27:29 +08:00
}
2021-06-20 11:35:21 +08:00
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
_logger.LogError("【UnhandledException】" + e.ExceptionObject);
var type = e.ExceptionObject.GetType();
_logger.LogError("ExceptionObject GetType " + type);
}
catch
{
}
}
2020-11-01 12:27:29 +08:00
}
}