Fix ConnectionLostException for JsonRPC v2 plugins

This commit is contained in:
Jack251970 2025-04-09 18:09:54 +08:00
parent 2e740b15ef
commit d3fdd99e10
3 changed files with 21 additions and 23 deletions

View file

@ -1,5 +1,4 @@
using Flow.Launcher.Core.Resource; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
@ -7,10 +6,10 @@ using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Plugin; using Flow.Launcher.Plugin;
using Microsoft.IO; using Microsoft.IO;
using System.Windows;
namespace Flow.Launcher.Core.Plugin namespace Flow.Launcher.Core.Plugin
{ {
@ -20,7 +19,7 @@ namespace Flow.Launcher.Core.Plugin
/// </summary> /// </summary>
internal abstract class JsonRPCPlugin : JsonRPCPluginBase internal abstract class JsonRPCPlugin : JsonRPCPluginBase
{ {
public const string JsonRPC = "JsonRPC"; public new const string JsonRPC = "JsonRPC";
protected abstract Task<Stream> RequestAsync(JsonRPCRequestModel rpcRequest, CancellationToken token = default); protected abstract Task<Stream> RequestAsync(JsonRPCRequestModel rpcRequest, CancellationToken token = default);
protected abstract string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default); protected abstract string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default);
@ -29,9 +28,6 @@ namespace Flow.Launcher.Core.Plugin
private int RequestId { get; set; } private int RequestId { get; set; }
private string SettingConfigurationPath => Path.Combine(Context.CurrentPluginMetadata.PluginDirectory, "SettingsTemplate.yaml");
private string SettingPath => Path.Combine(Context.CurrentPluginMetadata.PluginSettingsDirectoryPath, "Settings.json");
public override List<Result> LoadContextMenus(Result selectedResult) public override List<Result> LoadContextMenus(Result selectedResult)
{ {
var request = new JsonRPCRequestModel(RequestId++, var request = new JsonRPCRequestModel(RequestId++,
@ -57,13 +53,6 @@ namespace Flow.Launcher.Core.Plugin
} }
}; };
private static readonly JsonSerializerOptions settingSerializeOption = new()
{
WriteIndented = true
};
private readonly Dictionary<string, FrameworkElement> _settingControls = new();
private async Task<List<Result>> DeserializedResultAsync(Stream output) private async Task<List<Result>> DeserializedResultAsync(Stream output)
{ {
await using (output) await using (output)
@ -122,7 +111,6 @@ namespace Flow.Launcher.Core.Plugin
return !result.JsonRPCAction.DontHideAfterAction; return !result.JsonRPCAction.DontHideAfterAction;
} }
/// <summary> /// <summary>
/// Execute external program and return the output /// Execute external program and return the output
/// </summary> /// </summary>

View file

@ -1,11 +1,11 @@
using Flow.Launcher.Core.Resource; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Plugin; using Flow.Launcher.Plugin;
using YamlDotNet.Serialization; using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions; using YamlDotNet.Serialization.NamingConventions;

View file

@ -10,7 +10,6 @@ using Microsoft.VisualStudio.Threading;
using StreamJsonRpc; using StreamJsonRpc;
using IAsyncDisposable = System.IAsyncDisposable; using IAsyncDisposable = System.IAsyncDisposable;
namespace Flow.Launcher.Core.Plugin namespace Flow.Launcher.Core.Plugin
{ {
internal abstract class JsonRPCPluginV2 : JsonRPCPluginBase, IAsyncDisposable, IAsyncReloadable, IResultUpdated internal abstract class JsonRPCPluginV2 : JsonRPCPluginBase, IAsyncDisposable, IAsyncReloadable, IResultUpdated
@ -23,7 +22,6 @@ namespace Flow.Launcher.Core.Plugin
private JsonRpc RPC { get; set; } private JsonRpc RPC { get; set; }
protected override async Task<bool> ExecuteResultAsync(JsonRPCResult result) protected override async Task<bool> ExecuteResultAsync(JsonRPCResult result)
{ {
var res = await RPC.InvokeAsync<JsonRPCExecuteResponse>(result.JsonRPCAction.Method, var res = await RPC.InvokeAsync<JsonRPCExecuteResponse>(result.JsonRPCAction.Method,
@ -55,7 +53,6 @@ namespace Flow.Launcher.Core.Plugin
return results; return results;
} }
public override async Task InitAsync(PluginInitContext context) public override async Task InitAsync(PluginInitContext context)
{ {
await base.InitAsync(context); await base.InitAsync(context);
@ -88,7 +85,6 @@ namespace Flow.Launcher.Core.Plugin
protected abstract MessageHandlerType MessageHandler { get; } protected abstract MessageHandlerType MessageHandler { get; }
private void SetupJsonRPC() private void SetupJsonRPC()
{ {
var formatter = new SystemTextJsonFormatter { JsonSerializerOptions = RequestSerializeOption }; var formatter = new SystemTextJsonFormatter { JsonSerializerOptions = RequestSerializeOption };
@ -118,9 +114,16 @@ namespace Flow.Launcher.Core.Plugin
{ {
await RPC.InvokeAsync("reload_data", Context); await RPC.InvokeAsync("reload_data", Context);
} }
catch (RemoteMethodNotFoundException e) catch (RemoteMethodNotFoundException)
{ {
} }
catch (ConnectionLostException)
{
}
catch (Exception e)
{
Context.API.LogException(nameof(JsonRPCPluginV2), "Failed to call reload_data", e);
}
} }
public virtual async ValueTask DisposeAsync() public virtual async ValueTask DisposeAsync()
@ -129,9 +132,16 @@ namespace Flow.Launcher.Core.Plugin
{ {
await RPC.InvokeAsync("close"); await RPC.InvokeAsync("close");
} }
catch (RemoteMethodNotFoundException e) catch (RemoteMethodNotFoundException)
{ {
} }
catch (ConnectionLostException)
{
}
catch (Exception e)
{
Context.API.LogException(nameof(JsonRPCPluginV2), "Failed to call close", e);
}
finally finally
{ {
RPC?.Dispose(); RPC?.Dispose();