2019-11-07 14:26:46 +01:00
|
|
|
using System;
|
2019-10-31 13:08:42 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2019-11-01 19:04:24 +01:00
|
|
|
using Squirrowse.Client.Service;
|
2019-11-06 17:02:06 +01:00
|
|
|
using Squirrowse.Core.Models;
|
|
|
|
using Squirrowse.Core.Services;
|
2019-10-31 13:08:42 +01:00
|
|
|
|
|
|
|
namespace Squirrowse.Client
|
|
|
|
{
|
2019-11-06 11:49:39 +01:00
|
|
|
public class Worker : IHostedService
|
2019-10-31 13:08:42 +01:00
|
|
|
{
|
2019-11-05 17:00:36 +01:00
|
|
|
private readonly IConnectionManager _connectionManager;
|
2019-11-06 13:47:00 +01:00
|
|
|
private readonly ILogger<Worker> logger;
|
2019-11-07 14:26:46 +01:00
|
|
|
private readonly IActionDispatcher _actionDispatcher;
|
|
|
|
public Worker(ILogger<Worker> logger, IConnectionManager connectionManager,IActionDispatcher act)
|
2019-10-31 13:08:42 +01:00
|
|
|
{
|
2019-11-04 14:28:45 +01:00
|
|
|
this.logger = logger;
|
2019-11-06 13:47:00 +01:00
|
|
|
_connectionManager = connectionManager;
|
2019-11-07 14:26:46 +01:00
|
|
|
_actionDispatcher = act;
|
2019-10-31 13:08:42 +01:00
|
|
|
}
|
|
|
|
|
2019-11-06 11:49:39 +01:00
|
|
|
public async Task StartAsync(CancellationToken cancellationToken)
|
2019-10-31 13:08:42 +01:00
|
|
|
{
|
2019-11-07 14:26:46 +01:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await _connectionManager.InitConnection(ConnectionType.Client);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
await Task.Delay(1111);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// await Task.Delay(10000);//for debug
|
|
|
|
|
2019-11-06 11:49:39 +01:00
|
|
|
}
|
2019-11-05 10:30:31 +01:00
|
|
|
|
2019-11-06 11:49:39 +01:00
|
|
|
public async Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
await _connectionManager.Disconnect();
|
2019-10-31 13:08:42 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 09:29:37 +01:00
|
|
|
}
|