Fixed Reconnection Problem

This commit is contained in:
TheJoKlLa 2022-05-19 17:47:49 +02:00
parent 14552bf929
commit 224891cadb
8 changed files with 131 additions and 68 deletions

View File

@ -11,6 +11,7 @@ using Borepin.Page.AddServerProcess;
using Borepin.PageModel.AddServerProcess;
using System;
using Borepin.Service.Storage;
using NLog;
namespace Borepin
{
@ -18,7 +19,10 @@ namespace Borepin
{
public App(IPlatformInitializer platformInitializer) : base(platformInitializer)
{
var config = new NLog.Config.LoggingConfiguration();
var logconsole = new NLog.Targets.ConsoleTarget("logconsole");
config.AddRule(LogLevel.Trace, LogLevel.Fatal, logconsole);
LogManager.Configuration = config;
}
protected override async void OnInitialized()

View File

@ -26,18 +26,29 @@ namespace Borepin.Base
#endregion
#region Methods
public void OnConnectionStatusChanged(object sender, ConnectionStatusChange args)
public async void OnConnectionStatusChanged(object sender, ConnectionStatusChange args)
{
switch(args)
{
case ConnectionStatusChange.Connected:
IsConnected = true;
LoadAPIData();
await LoadAPIData().ConfigureAwait(false);
break;
case ConnectionStatusChange.Reconnected:
ReloadAPIData();
await ReloadAPIData().ConfigureAwait(false);
break;
case ConnectionStatusChange.ConnectionLoss:
try
{
await _API.Reconnect().ConfigureAwait(false);
}
catch
{
IsConnected = false;
await _API.Disconnect().ConfigureAwait(false);
_API.UnbindAllEvents();
}
break;
case ConnectionStatusChange.Disconnected:
IsConnected = false;
break;

View File

@ -8,6 +8,7 @@ using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace Borepin.PageModel.AddServerProcess
{
@ -98,7 +99,7 @@ namespace Borepin.PageModel.AddServerProcess
try
{
API api = new API();
await api.TestConnection(_ConnectionData).ConfigureAwait(true);
await api.TestConnection(_ConnectionData).ConfigureAwait(false);
}
catch(ConnectingFailedException)
{
@ -108,11 +109,14 @@ namespace Borepin.PageModel.AddServerProcess
return;
}
Device.BeginInvokeOnMainThread(async () =>
{
INavigationResult result = await _NavigationService.NavigateAsync("AddServerProcess_ChooseAuthTypePage").ConfigureAwait(false);
if (result.Exception != null)
{
Log.Fatal(result.Exception, "Navigating failed");
}
});
}
private ICommand _ScanCodeCommand;

View File

@ -12,6 +12,7 @@ using System;
using NaturalSort.Extension;
using System.Linq;
using Borepin.Service;
using Xamarin.Forms;
namespace Borepin.PageModel
{
@ -101,7 +102,7 @@ namespace Borepin.PageModel
get => _SelectInstanceCommand;
set => SetProperty(ref _SelectInstanceCommand, value);
}
public async void SelectInstanceCommandExecute(object obj)
public void SelectInstanceCommandExecute(object obj)
{
MachineListItemViewModel viewmodel = obj as MachineListItemViewModel;
@ -110,7 +111,14 @@ namespace Borepin.PageModel
{ "instance", viewmodel.Instance.Id },
};
await _NavigationService.NavigateAsync($"MachinePage", parameters).ConfigureAwait(false);
Device.BeginInvokeOnMainThread(async () =>
{
INavigationResult result = await _NavigationService.NavigateAsync($"MachinePage", parameters).ConfigureAwait(false);
if (result.Exception != null)
{
Log.Fatal(result.Exception, "Navigating failed");
}
});
}
private ICommand _ScanCodeCommand;
@ -154,7 +162,7 @@ namespace Borepin.PageModel
#endregion
#region IPageLifecycleAware
public async void OnAppearing()
public void OnAppearing()
{
if(_NextMachine != null)
{
@ -165,7 +173,14 @@ namespace Borepin.PageModel
_NextMachine = null;
await _NavigationService.NavigateAsync("MachinePage", parameters).ConfigureAwait(false);
Device.BeginInvokeOnMainThread(async () =>
{
INavigationResult result = await _NavigationService.NavigateAsync("MachinePage", parameters).ConfigureAwait(false);
if (result.Exception != null)
{
Log.Fatal(result.Exception, "Navigating failed");
}
});
}
}

View File

@ -8,6 +8,8 @@ using Borepin.Model;
using Prism.Services;
using Borepin.Service;
using Borepin.Base.Exceptions;
using Capnp.Rpc;
using System;
namespace Borepin.PageModel
{
@ -73,12 +75,19 @@ namespace Borepin.PageModel
IsBusy = true;
if(_API.IsConnected)
{
try
{
Machine.IUseInterface useInterface = _Machine.Use;
await useInterface.Use().ConfigureAwait(false);
await LoadAPIData().ConfigureAwait(false);
}
catch (RpcException exception) when (string.Equals(exception.Message, "RPC connection is broken. Task would never return.", StringComparison.Ordinal))
{
Log.Debug("RPC Connection Loss");
}
}
IsBusy = false;
}
@ -95,12 +104,19 @@ namespace Borepin.PageModel
IsBusy = true;
if (_API.IsConnected)
{
try
{
Machine.IInUseInterface inUseInterface = _Machine.Inuse;
await inUseInterface.GiveBack().ConfigureAwait(false);
await LoadAPIData().ConfigureAwait(false);
}
catch(RpcException exception) when(string.Equals(exception.Message, "RPC connection is broken. Task would never return.", StringComparison.Ordinal))
{
Log.Debug("RPC Connection Loss");
}
}
IsBusy = false;
}

View File

@ -2,8 +2,8 @@
using Prism.Commands;
using Prism.Navigation;
using Prism.Services;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace Borepin.PageModel.SetUpProcess
{
@ -23,9 +23,17 @@ namespace Borepin.PageModel.SetUpProcess
get => _NextCommand;
set => SetProperty(ref _NextCommand, value);
}
public async void NextCommandCommandExecute(object obj)
public void NextCommandCommandExecute(object obj)
{
await _NavigationService.NavigateAsync("AddServerProcess_SelectServerPage").ConfigureAwait(false);
Device.BeginInvokeOnMainThread(async () =>
{
INavigationResult result = await _NavigationService.NavigateAsync("AddServerProcess_SelectServerPage").ConfigureAwait(false);
if (result.Exception != null)
{
Log.Fatal(result.Exception, "Navigating failed");
}
});
}
#endregion
}

View File

@ -9,6 +9,7 @@ using System.Linq;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
namespace FabAccessAPI
@ -22,6 +23,8 @@ namespace FabAccessAPI
#region Private Members
private TcpRpcClient _TcpRpcClient;
private IBootstrap _Bootstrap;
private static SemaphoreSlim _ConnectSemaphore = new SemaphoreSlim(1, 1);
private static SemaphoreSlim _ReconnectSemaphore = new SemaphoreSlim(1, 1);
#endregion
#region Constructors
@ -36,22 +39,10 @@ namespace FabAccessAPI
public void OnTcpRpcConnectionChanged(object sender, ConnectionStateChange args)
{
EventHandler<ConnectionStatusChange> eventHandler = ConnectionStatusChanged;
if (eventHandler == null)
{
return;
}
if (args.LastState == ConnectionState.Initializing && args.NewState == ConnectionState.Active)
{
Log.Trace("TcpRpcClient Event Connected");
eventHandler(this, ConnectionStatusChange.Connected);
}
if (args.LastState == ConnectionState.Active && args.NewState == ConnectionState.Down)
{
Log.Trace("TcpRpcClient Event ConnectionLoss");
eventHandler(this, ConnectionStatusChange.ConnectionLoss);
ConnectionStatusChanged?.Invoke(this, ConnectionStatusChange.ConnectionLoss);
_TcpRpcClient = null;
}
}
@ -91,6 +82,9 @@ namespace FabAccessAPI
/// <exception cref="AuthenticationException"></exception>
/// <exception cref="ConnectingFailedException"></exception>
public async Task Connect(ConnectionData connectionData, TcpRpcClient tcpRpcClient = null)
{
await _ConnectSemaphore.WaitAsync();
try
{
if (IsConnected)
{
@ -99,25 +93,22 @@ namespace FabAccessAPI
if (tcpRpcClient == null)
{
_TcpRpcClient = new TcpRpcClient();
}
else
{
_TcpRpcClient = tcpRpcClient;
tcpRpcClient = new TcpRpcClient();
}
try
{
_TcpRpcClient.ConnectionStateChanged += OnTcpRpcConnectionChanged;
await _ConnectAsync(tcpRpcClient, connectionData).ConfigureAwait(false);
await _ConnectAsync(_TcpRpcClient, connectionData).ConfigureAwait(false);
_Bootstrap = _TcpRpcClient.GetMain<IBootstrap>();
_Bootstrap = tcpRpcClient.GetMain<IBootstrap>();
ConnectionInfo = await _GetConnectionInfo(_Bootstrap);
Session = await _Authenticate(connectionData).ConfigureAwait(false);
ConnectionData = connectionData;
_TcpRpcClient = tcpRpcClient;
tcpRpcClient.ConnectionStateChanged += OnTcpRpcConnectionChanged;
ConnectionStatusChanged?.Invoke(this, ConnectionStatusChange.Connected);
Log.Info("API connected");
}
catch (System.Exception ex)
@ -127,6 +118,11 @@ namespace FabAccessAPI
throw ex;
}
}
finally
{
_ConnectSemaphore.Release();
}
}
public Task Disconnect()
{
@ -150,7 +146,10 @@ namespace FabAccessAPI
public async Task Reconnect()
{
if (ConnectionData != null)
await _ReconnectSemaphore.WaitAsync();
try
{
if (ConnectionData != null && IsConnected == false)
{
await Connect(ConnectionData);
}
@ -158,6 +157,12 @@ namespace FabAccessAPI
ConnectionStatusChanged?.Invoke(this, ConnectionStatusChange.Reconnected);
Log.Info("API reconnected");
}
finally
{
_ReconnectSemaphore.Release();
}
}
public async Task<ConnectionInfo> TestConnection(ConnectionData connectionData, TcpRpcClient tcpRpcClient = null)
{

@ -1 +1 @@
Subproject commit 0176a503c8d0b0be66e0212e04200c8324e11fd9
Subproject commit 086bbc2497785d2cc63e9252df6f6d3ee7599579