From ad1c7e1fd356f58890dc9c30127f91d0ab7c1621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Kaan=20K=C3=B6se?= Date: Wed, 17 Jul 2024 00:20:40 +0200 Subject: [PATCH] Fix server-client connection. --- .../Services/WinoServerConnectionManager.cs | 45 +++++++++++++++++-- .../BackgroundActivationHandlerEx.cs | 41 ----------------- Wino.Mail/App.xaml.cs | 23 +++++++++- Wino.Mail/Wino.Mail.csproj | 1 - Wino.Server/App.xaml.cs | 1 - Wino.Server/ServerContext.cs | 8 ++++ Wino.Server/TrayIconViewModel.cs | 1 + 7 files changed, 72 insertions(+), 48 deletions(-) delete mode 100644 Wino.Mail/Activation/BackgroundActivationHandlerEx.cs diff --git a/Wino.Core.UWP/Services/WinoServerConnectionManager.cs b/Wino.Core.UWP/Services/WinoServerConnectionManager.cs index 536b5aa4..717e178a 100644 --- a/Wino.Core.UWP/Services/WinoServerConnectionManager.cs +++ b/Wino.Core.UWP/Services/WinoServerConnectionManager.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; +using Windows.ApplicationModel; using Windows.ApplicationModel.AppService; using Windows.Foundation.Metadata; using Wino.Core.Domain.Enums; @@ -10,9 +11,34 @@ namespace Wino.Core.UWP.Services { public partial class WinoServerConnectionManager : ObservableObject, IWinoServerConnectionManager { - 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] private WinoServerConnectionStatus _status; @@ -27,10 +53,13 @@ namespace Wino.Core.UWP.Services { 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) { + Status = WinoServerConnectionStatus.Failed; return false; } @@ -55,5 +84,15 @@ namespace Wino.Core.UWP.Services // 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. + } } } diff --git a/Wino.Mail/Activation/BackgroundActivationHandlerEx.cs b/Wino.Mail/Activation/BackgroundActivationHandlerEx.cs deleted file mode 100644 index e2fcf54d..00000000 --- a/Wino.Mail/Activation/BackgroundActivationHandlerEx.cs +++ /dev/null @@ -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 - { - private readonly IWinoServerConnectionManager _winoServerConnectionManager; - - public BackgroundActivationHandlerEx(IWinoServerConnectionManager 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; - } - } -} diff --git a/Wino.Mail/App.xaml.cs b/Wino.Mail/App.xaml.cs index 264e6fc5..732a7edf 100644 --- a/Wino.Mail/App.xaml.cs +++ b/Wino.Mail/App.xaml.cs @@ -39,6 +39,8 @@ namespace Wino public new static App Current => (App)Application.Current; public IServiceProvider Services { get; } + private BackgroundTaskDeferral backgroundTaskDeferral; + private readonly IWinoServerConnectionManager _appServiceConnectionManager; private readonly ILogInitializer _logInitializer; private readonly IThemeService _themeService; @@ -113,7 +115,6 @@ namespace Wino private void RegisterActivationHandlers(IServiceCollection services) { services.AddTransient(); - services.AddTransient(); // services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -234,6 +235,20 @@ namespace Wino { 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"}"); await ActivateWinoAsync(args); @@ -308,7 +323,6 @@ namespace Wino private IEnumerable GetActivationHandlers() { yield return Services.GetService(); - yield return Services.GetService(); // New app service background task handler. // yield return Services.GetService(); // Old UWP background task handler. yield return Services.GetService(); yield return Services.GetService(); @@ -317,6 +331,11 @@ namespace Wino public void OnBackgroundTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { Log.Information($"Background task {sender.Task.Name} was canceled. Reason: {reason}"); + + backgroundTaskDeferral?.Complete(); + backgroundTaskDeferral = null; + + _appServiceConnectionManager.Connection = null; } } } diff --git a/Wino.Mail/Wino.Mail.csproj b/Wino.Mail/Wino.Mail.csproj index c4846c02..03453b38 100644 --- a/Wino.Mail/Wino.Mail.csproj +++ b/Wino.Mail/Wino.Mail.csproj @@ -229,7 +229,6 @@ - diff --git a/Wino.Server/App.xaml.cs b/Wino.Server/App.xaml.cs index d2bab884..6a48a883 100644 --- a/Wino.Server/App.xaml.cs +++ b/Wino.Server/App.xaml.cs @@ -11,7 +11,6 @@ namespace Wino.Server { base.OnStartup(e); - //create the notifyicon (it's a resource declared in NotifyIconResources.xaml notifyIcon = (TaskbarIcon)FindResource("NotifyIcon"); notifyIcon.ForceCreate(enablesEfficiencyMode: true); } diff --git a/Wino.Server/ServerContext.cs b/Wino.Server/ServerContext.cs index 353dc3c8..c1a40b23 100644 --- a/Wino.Server/ServerContext.cs +++ b/Wino.Server/ServerContext.cs @@ -1,6 +1,7 @@ using System; using Windows.ApplicationModel; using Windows.ApplicationModel.AppService; +using Windows.Foundation.Collections; 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) { // TODO: Handle connection closed. diff --git a/Wino.Server/TrayIconViewModel.cs b/Wino.Server/TrayIconViewModel.cs index 2b2a41a5..145f1cd9 100644 --- a/Wino.Server/TrayIconViewModel.cs +++ b/Wino.Server/TrayIconViewModel.cs @@ -11,6 +11,7 @@ namespace Wino.Server [RelayCommand] public void LaunchWino() { + _context.SendMessage(); } ///