Fix server-client connection.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using Windows.ApplicationModel;
|
||||||
using Windows.ApplicationModel.AppService;
|
using Windows.ApplicationModel.AppService;
|
||||||
using Windows.Foundation.Metadata;
|
using Windows.Foundation.Metadata;
|
||||||
using Wino.Core.Domain.Enums;
|
using Wino.Core.Domain.Enums;
|
||||||
@@ -10,9 +11,34 @@ namespace Wino.Core.UWP.Services
|
|||||||
{
|
{
|
||||||
public partial class WinoServerConnectionManager : ObservableObject, IWinoServerConnectionManager<AppServiceConnection>
|
public partial class WinoServerConnectionManager : ObservableObject, IWinoServerConnectionManager<AppServiceConnection>
|
||||||
{
|
{
|
||||||
public AppServiceConnection Connection { get; set; }
|
private AppServiceConnection _connection;
|
||||||
|
|
||||||
private Guid? _activeConnectionSessionId;
|
public AppServiceConnection Connection
|
||||||
|
{
|
||||||
|
get { return _connection; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_connection != null)
|
||||||
|
{
|
||||||
|
_connection.RequestReceived -= ServerMessageReceived;
|
||||||
|
_connection.ServiceClosed -= ServerDisconnected;
|
||||||
|
}
|
||||||
|
|
||||||
|
_connection = value;
|
||||||
|
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
Status = WinoServerConnectionStatus.Disconnected;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value.RequestReceived += ServerMessageReceived;
|
||||||
|
value.ServiceClosed += ServerDisconnected;
|
||||||
|
|
||||||
|
Status = WinoServerConnectionStatus.Connected;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private WinoServerConnectionStatus _status;
|
private WinoServerConnectionStatus _status;
|
||||||
@@ -27,10 +53,13 @@ namespace Wino.Core.UWP.Services
|
|||||||
{
|
{
|
||||||
Status = WinoServerConnectionStatus.Connecting;
|
Status = WinoServerConnectionStatus.Connecting;
|
||||||
|
|
||||||
// await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
|
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
|
||||||
|
|
||||||
|
// If the server connection is success, Status will be updated to Connected by BackgroundActivationHandlerEx.
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
|
Status = WinoServerConnectionStatus.Failed;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,5 +84,15 @@ namespace Wino.Core.UWP.Services
|
|||||||
|
|
||||||
// TODO: Log connection status
|
// TODO: Log connection status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ServerMessageReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
|
||||||
|
{
|
||||||
|
// TODO: Handle server messsages.
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ServerDisconnected(AppServiceConnection sender, AppServiceClosedEventArgs args)
|
||||||
|
{
|
||||||
|
// TODO: Handle server disconnection.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using Windows.ApplicationModel;
|
|
||||||
using Windows.ApplicationModel.Activation;
|
|
||||||
using Windows.ApplicationModel.AppService;
|
|
||||||
using Wino.Core.Domain.Interfaces;
|
|
||||||
|
|
||||||
namespace Wino.Activation
|
|
||||||
{
|
|
||||||
internal class BackgroundActivationHandlerEx : ActivationHandler<BackgroundActivatedEventArgs>
|
|
||||||
{
|
|
||||||
private readonly IWinoServerConnectionManager<AppServiceConnection> _winoServerConnectionManager;
|
|
||||||
|
|
||||||
public BackgroundActivationHandlerEx(IWinoServerConnectionManager<AppServiceConnection> winoServerConnectionManager)
|
|
||||||
{
|
|
||||||
_winoServerConnectionManager = winoServerConnectionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override Task HandleInternalAsync(BackgroundActivatedEventArgs args)
|
|
||||||
{
|
|
||||||
if (args.TaskInstance == null || args.TaskInstance.TriggerDetails == null) return Task.CompletedTask;
|
|
||||||
|
|
||||||
if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails appServiceTriggerDetails)
|
|
||||||
{
|
|
||||||
// only accept connections from callers in the same package
|
|
||||||
if (appServiceTriggerDetails.CallerPackageFamilyName == Package.Current.Id.FamilyName)
|
|
||||||
{
|
|
||||||
// Connection established from the fulltrust process
|
|
||||||
_winoServerConnectionManager.Connection = appServiceTriggerDetails.AppServiceConnection;
|
|
||||||
|
|
||||||
var deferral = args.TaskInstance.GetDeferral();
|
|
||||||
|
|
||||||
args.TaskInstance.Canceled += App.Current.OnBackgroundTaskCanceled;
|
|
||||||
|
|
||||||
// AppServiceConnected?.Invoke(this, args.TaskInstance.TriggerDetails as AppServiceTriggerDetails);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -39,6 +39,8 @@ namespace Wino
|
|||||||
public new static App Current => (App)Application.Current;
|
public new static App Current => (App)Application.Current;
|
||||||
public IServiceProvider Services { get; }
|
public IServiceProvider Services { get; }
|
||||||
|
|
||||||
|
private BackgroundTaskDeferral backgroundTaskDeferral;
|
||||||
|
|
||||||
private readonly IWinoServerConnectionManager<AppServiceConnection> _appServiceConnectionManager;
|
private readonly IWinoServerConnectionManager<AppServiceConnection> _appServiceConnectionManager;
|
||||||
private readonly ILogInitializer _logInitializer;
|
private readonly ILogInitializer _logInitializer;
|
||||||
private readonly IThemeService _themeService;
|
private readonly IThemeService _themeService;
|
||||||
@@ -113,7 +115,6 @@ namespace Wino
|
|||||||
private void RegisterActivationHandlers(IServiceCollection services)
|
private void RegisterActivationHandlers(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddTransient<ProtocolActivationHandler>();
|
services.AddTransient<ProtocolActivationHandler>();
|
||||||
services.AddTransient<BackgroundActivationHandlerEx>();
|
|
||||||
// services.AddTransient<BackgroundActivationHandler>();
|
// services.AddTransient<BackgroundActivationHandler>();
|
||||||
services.AddTransient<ToastNotificationActivationHandler>();
|
services.AddTransient<ToastNotificationActivationHandler>();
|
||||||
services.AddTransient<FileActivationHandler>();
|
services.AddTransient<FileActivationHandler>();
|
||||||
@@ -234,6 +235,20 @@ namespace Wino
|
|||||||
{
|
{
|
||||||
base.OnBackgroundActivated(args);
|
base.OnBackgroundActivated(args);
|
||||||
|
|
||||||
|
if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails appServiceTriggerDetails)
|
||||||
|
{
|
||||||
|
// Only accept connections from callers in the same package
|
||||||
|
if (appServiceTriggerDetails.CallerPackageFamilyName == Package.Current.Id.FamilyName)
|
||||||
|
{
|
||||||
|
// Connection established from the fulltrust process
|
||||||
|
|
||||||
|
backgroundTaskDeferral = args.TaskInstance.GetDeferral();
|
||||||
|
args.TaskInstance.Canceled += OnBackgroundTaskCanceled;
|
||||||
|
|
||||||
|
_appServiceConnectionManager.Connection = appServiceTriggerDetails.AppServiceConnection;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LogActivation($"OnBackgroundActivated -> {args.GetType().Name}, TaskInstanceIdName -> {args.TaskInstance?.Task?.Name ?? "NA"}");
|
LogActivation($"OnBackgroundActivated -> {args.GetType().Name}, TaskInstanceIdName -> {args.TaskInstance?.Task?.Name ?? "NA"}");
|
||||||
|
|
||||||
await ActivateWinoAsync(args);
|
await ActivateWinoAsync(args);
|
||||||
@@ -308,7 +323,6 @@ namespace Wino
|
|||||||
private IEnumerable<ActivationHandler> GetActivationHandlers()
|
private IEnumerable<ActivationHandler> GetActivationHandlers()
|
||||||
{
|
{
|
||||||
yield return Services.GetService<ProtocolActivationHandler>();
|
yield return Services.GetService<ProtocolActivationHandler>();
|
||||||
yield return Services.GetService<BackgroundActivationHandlerEx>(); // New app service background task handler.
|
|
||||||
// yield return Services.GetService<BackgroundActivationHandler>(); // Old UWP background task handler.
|
// yield return Services.GetService<BackgroundActivationHandler>(); // Old UWP background task handler.
|
||||||
yield return Services.GetService<ToastNotificationActivationHandler>();
|
yield return Services.GetService<ToastNotificationActivationHandler>();
|
||||||
yield return Services.GetService<FileActivationHandler>();
|
yield return Services.GetService<FileActivationHandler>();
|
||||||
@@ -317,6 +331,11 @@ namespace Wino
|
|||||||
public void OnBackgroundTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
|
public void OnBackgroundTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
|
||||||
{
|
{
|
||||||
Log.Information($"Background task {sender.Task.Name} was canceled. Reason: {reason}");
|
Log.Information($"Background task {sender.Task.Name} was canceled. Reason: {reason}");
|
||||||
|
|
||||||
|
backgroundTaskDeferral?.Complete();
|
||||||
|
backgroundTaskDeferral = null;
|
||||||
|
|
||||||
|
_appServiceConnectionManager.Connection = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,7 +229,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Activation\ActivationHandler.cs" />
|
<Compile Include="Activation\ActivationHandler.cs" />
|
||||||
<Compile Include="Activation\BackgroundActivationHandler.cs" />
|
<Compile Include="Activation\BackgroundActivationHandler.cs" />
|
||||||
<Compile Include="Activation\BackgroundActivationHandlerEx.cs" />
|
|
||||||
<Compile Include="Activation\DefaultActivationHandler.cs" />
|
<Compile Include="Activation\DefaultActivationHandler.cs" />
|
||||||
<Compile Include="Activation\FileActivationHandler.cs" />
|
<Compile Include="Activation\FileActivationHandler.cs" />
|
||||||
<Compile Include="Activation\ProtocolActivationHandler.cs" />
|
<Compile Include="Activation\ProtocolActivationHandler.cs" />
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ namespace Wino.Server
|
|||||||
{
|
{
|
||||||
base.OnStartup(e);
|
base.OnStartup(e);
|
||||||
|
|
||||||
//create the notifyicon (it's a resource declared in NotifyIconResources.xaml
|
|
||||||
notifyIcon = (TaskbarIcon)FindResource("NotifyIcon");
|
notifyIcon = (TaskbarIcon)FindResource("NotifyIcon");
|
||||||
notifyIcon.ForceCreate(enablesEfficiencyMode: true);
|
notifyIcon.ForceCreate(enablesEfficiencyMode: true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using Windows.ApplicationModel;
|
using Windows.ApplicationModel;
|
||||||
using Windows.ApplicationModel.AppService;
|
using Windows.ApplicationModel.AppService;
|
||||||
|
using Windows.Foundation.Collections;
|
||||||
|
|
||||||
namespace Wino.Server
|
namespace Wino.Server
|
||||||
{
|
{
|
||||||
@@ -35,6 +36,13 @@ namespace Wino.Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async void SendMessage()
|
||||||
|
{
|
||||||
|
var set = new ValueSet();
|
||||||
|
set.Add("Hello", "World");
|
||||||
|
await connection.SendMessageAsync(set);
|
||||||
|
}
|
||||||
|
|
||||||
private void OnConnectionClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
|
private void OnConnectionClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
|
||||||
{
|
{
|
||||||
// TODO: Handle connection closed.
|
// TODO: Handle connection closed.
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ namespace Wino.Server
|
|||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
public void LaunchWino()
|
public void LaunchWino()
|
||||||
{
|
{
|
||||||
|
_context.SendMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user