 14575a4a6b
			
		
	
	14575a4a6b
	
	
	
		
			
			* add jwt expiry * start of 0.9 api impl * some stuff idk * some more impl * some cleanup * remove grouppair, add configuration, rework some pair drawing stuff * do some stuff * rework some ui * I don't even know anymore * add cancellationtoken * token bla * ui fixes etc * probably individual adding/removing now working fully as expected * add working report popup * I guess it's more syncshell shit or so * popup shit idk * work out most of the syncshell bullshit I guess * delete some old crap * are we actually getting closer to the end * update pair info stuff * more fixes/adjustments, idk * refactor some things * some rework * some more cleanup * cleanup * make menu buttons w i d e * better icon text buttons * add all syncshell folder and ordering fixes --------- Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
		
			
				
	
	
		
			126 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			126 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Dalamud.Plugin.Services;
 | |
| using MareSynchronos.API.SignalR;
 | |
| using MareSynchronos.Interop;
 | |
| using MareSynchronos.MareConfiguration;
 | |
| using MareSynchronos.Services.Mediator;
 | |
| using MareSynchronos.Services.ServerConfiguration;
 | |
| using MareSynchronos.WebAPI.SignalR.Utils;
 | |
| using MessagePack;
 | |
| using MessagePack.Resolvers;
 | |
| using Microsoft.AspNetCore.Http.Connections;
 | |
| using Microsoft.AspNetCore.SignalR.Client;
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using Microsoft.Extensions.Logging;
 | |
| 
 | |
| namespace MareSynchronos.WebAPI.SignalR;
 | |
| 
 | |
| public class HubFactory : MediatorSubscriberBase
 | |
| {
 | |
|     private readonly MareConfigService _configService;
 | |
|     private readonly IPluginLog _pluginLog;
 | |
|     private readonly ServerConfigurationManager _serverConfigurationManager;
 | |
|     private readonly TokenProvider _tokenProvider;
 | |
|     private HubConnection? _instance;
 | |
|     private bool _isDisposed = false;
 | |
| 
 | |
|     public HubFactory(ILogger<HubFactory> logger, MareMediator mediator,
 | |
|         ServerConfigurationManager serverConfigurationManager, MareConfigService configService,
 | |
|         TokenProvider tokenProvider, IPluginLog pluginLog) : base(logger, mediator)
 | |
|     {
 | |
|         _serverConfigurationManager = serverConfigurationManager;
 | |
|         _configService = configService;
 | |
|         _tokenProvider = tokenProvider;
 | |
|         _pluginLog = pluginLog;
 | |
|     }
 | |
| 
 | |
|     public async Task DisposeHubAsync()
 | |
|     {
 | |
|         if (_instance == null || _isDisposed) return;
 | |
| 
 | |
|         Logger.LogDebug("Disposing current HubConnection");
 | |
| 
 | |
|         _isDisposed = true;
 | |
| 
 | |
|         _instance.Closed -= HubOnClosed;
 | |
|         _instance.Reconnecting -= HubOnReconnecting;
 | |
|         _instance.Reconnected -= HubOnReconnected;
 | |
| 
 | |
|         await _instance.StopAsync().ConfigureAwait(false);
 | |
|         await _instance.DisposeAsync().ConfigureAwait(false);
 | |
| 
 | |
|         _instance = null;
 | |
| 
 | |
|         Logger.LogDebug("Current HubConnection disposed");
 | |
|     }
 | |
| 
 | |
|     public HubConnection GetOrCreate(CancellationToken ct)
 | |
|     {
 | |
|         if (!_isDisposed && _instance != null) return _instance;
 | |
| 
 | |
|         return BuildHubConnection(ct);
 | |
|     }
 | |
| 
 | |
|     private HubConnection BuildHubConnection(CancellationToken ct)
 | |
|     {
 | |
|         Logger.LogDebug("Building new HubConnection");
 | |
| 
 | |
|         _instance = new HubConnectionBuilder()
 | |
|             .WithUrl(_serverConfigurationManager.CurrentApiUrl + IMareHub.Path, options =>
 | |
|             {
 | |
|                 options.AccessTokenProvider = () => _tokenProvider.GetOrUpdateToken(ct);
 | |
|                 options.Transports = HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents | HttpTransportType.LongPolling;
 | |
|             })
 | |
|             .AddMessagePackProtocol(opt =>
 | |
|             {
 | |
|                 var resolver = CompositeResolver.Create(StandardResolverAllowPrivate.Instance,
 | |
|                     BuiltinResolver.Instance,
 | |
|                     AttributeFormatterResolver.Instance,
 | |
|                     // replace enum resolver
 | |
|                     DynamicEnumAsStringResolver.Instance,
 | |
|                     DynamicGenericResolver.Instance,
 | |
|                     DynamicUnionResolver.Instance,
 | |
|                     DynamicObjectResolver.Instance,
 | |
|                     PrimitiveObjectResolver.Instance,
 | |
|                     // final fallback(last priority)
 | |
|                     StandardResolver.Instance);
 | |
| 
 | |
|                 opt.SerializerOptions =
 | |
|                     MessagePackSerializerOptions.Standard
 | |
|                         .WithCompression(MessagePackCompression.Lz4Block)
 | |
|                         .WithResolver(resolver);
 | |
|             })
 | |
|             .WithAutomaticReconnect(new ForeverRetryPolicy(Mediator))
 | |
|             .ConfigureLogging(a =>
 | |
|             {
 | |
|                 a.ClearProviders().AddProvider(new DalamudLoggingProvider(_configService, _pluginLog));
 | |
|                 a.SetMinimumLevel(LogLevel.Information);
 | |
|             })
 | |
|             .Build();
 | |
| 
 | |
|         _instance.Closed += HubOnClosed;
 | |
|         _instance.Reconnecting += HubOnReconnecting;
 | |
|         _instance.Reconnected += HubOnReconnected;
 | |
| 
 | |
|         _isDisposed = false;
 | |
| 
 | |
|         return _instance;
 | |
|     }
 | |
| 
 | |
|     private Task HubOnClosed(Exception? arg)
 | |
|     {
 | |
|         Mediator.Publish(new HubClosedMessage(arg));
 | |
|         return Task.CompletedTask;
 | |
|     }
 | |
| 
 | |
|     private Task HubOnReconnected(string? arg)
 | |
|     {
 | |
|         Mediator.Publish(new HubReconnectedMessage(arg));
 | |
|         return Task.CompletedTask;
 | |
|     }
 | |
| 
 | |
|     private Task HubOnReconnecting(Exception? arg)
 | |
|     {
 | |
|         Mediator.Publish(new HubReconnectingMessage(arg));
 | |
|         return Task.CompletedTask;
 | |
|     }
 | |
| } |