From 3e9b33f02a71cf2c4617a1af83c86283887afade Mon Sep 17 00:00:00 2001 From: Zaitonn <103164490+Zaitonn@users.noreply.github.com> Date: Mon, 9 Dec 2024 22:58:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BA=8B=E4=BB=B6=E6=80=BB=E6=95=B0?= =?UTF-8?q?=E8=A2=AB=E6=8B=A6=E6=88=AA=20chore:=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Connection/OneBot/Messages/SubType.cs | 6 ++ .../Network/WebApi/WebSocketBroadcastType.cs | 6 ++ src/Serein.Core/Models/Plugins/Js/JsPlugin.cs | 8 +- .../Bindings/BindingRecordDbContext.cs | 2 + src/Serein.Core/Services/Commands/Matcher.cs | 98 ++++++++++++++----- .../Network/Connection/PacketHandler.cs | 2 +- src/Serein.Core/Services/Servers/Server.cs | 6 +- 7 files changed, 95 insertions(+), 33 deletions(-) diff --git a/src/Serein.Core/Models/Network/Connection/OneBot/Messages/SubType.cs b/src/Serein.Core/Models/Network/Connection/OneBot/Messages/SubType.cs index 704b343e..5f62b9ba 100644 --- a/src/Serein.Core/Models/Network/Connection/OneBot/Messages/SubType.cs +++ b/src/Serein.Core/Models/Network/Connection/OneBot/Messages/SubType.cs @@ -6,10 +6,16 @@ namespace Serein.Core.Models.Network.Connection.OneBot.Messages; public enum SubType { Unknown, + Friend, + Normal, + Anonymous, + Group, + GroupSelf, + Notice, } diff --git a/src/Serein.Core/Models/Network/WebApi/WebSocketBroadcastType.cs b/src/Serein.Core/Models/Network/WebApi/WebSocketBroadcastType.cs index 463c251a..72d1fdb0 100644 --- a/src/Serein.Core/Models/Network/WebApi/WebSocketBroadcastType.cs +++ b/src/Serein.Core/Models/Network/WebApi/WebSocketBroadcastType.cs @@ -3,10 +3,16 @@ namespace Serein.Core.Models.Network.WebApi; public enum WebSocketBroadcastType { Started, + Stopped, + Removed, + Input, + Output, + Error, + Information, } diff --git a/src/Serein.Core/Models/Plugins/Js/JsPlugin.cs b/src/Serein.Core/Models/Plugins/Js/JsPlugin.cs index 5e44b86e..3317a9f1 100644 --- a/src/Serein.Core/Models/Plugins/Js/JsPlugin.cs +++ b/src/Serein.Core/Models/Plugins/Js/JsPlugin.cs @@ -27,8 +27,6 @@ public class JsPlugin : IPlugin public TimerFactory TimerFactory { get; } public ScriptInstance ScriptInstance { get; } - // public FileSystem FileSystem { get; } - public CancellationToken CancellationToken => _cancellationTokenSource.Token; public IReadOnlyDictionary EventHandlers => _eventHandlers; public bool IsEnabled => !_cancellationTokenSource.IsCancellationRequested; @@ -97,7 +95,7 @@ internal bool Invoke(Event @event, CancellationToken cancellationToken, params o || func is null ) { - return false; + return true; } if (!Monitor.TryEnter(Engine, 1000)) { @@ -107,11 +105,11 @@ internal bool Invoke(Event @event, CancellationToken cancellationToken, params o if (cancellationToken.IsCancellationRequested) { - return false; + return true; } var result = Engine.Invoke(func, args); { - return result.IsBoolean() && result.AsBoolean(); + return !result.IsBoolean() || result.AsBoolean(); } } catch (Exception e) diff --git a/src/Serein.Core/Services/Bindings/BindingRecordDbContext.cs b/src/Serein.Core/Services/Bindings/BindingRecordDbContext.cs index c0d79275..db631c79 100644 --- a/src/Serein.Core/Services/Bindings/BindingRecordDbContext.cs +++ b/src/Serein.Core/Services/Bindings/BindingRecordDbContext.cs @@ -5,6 +5,8 @@ namespace Serein.Core.Services.Bindings; +#pragma warning disable CS8618 + internal sealed class BindingRecordDbContext : DbContext { public DbSet Records { get; private set; } diff --git a/src/Serein.Core/Services/Commands/Matcher.cs b/src/Serein.Core/Services/Commands/Matcher.cs index 38d6b29c..67a5d893 100644 --- a/src/Serein.Core/Services/Commands/Matcher.cs +++ b/src/Serein.Core/Services/Commands/Matcher.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -9,24 +10,41 @@ namespace Serein.Core.Services.Commands; -public sealed class Matcher( - MatchesProvider matchesProvider, - CommandRunner commandRunner, - SettingProvider settingProvider -) +public sealed class Matcher { - private readonly MatchesProvider _matchesProvider = matchesProvider; - private readonly CommandRunner _commandRunner = commandRunner; - private readonly SettingProvider _settingProvider = settingProvider; + private record ServerLine(string Id, string Line, bool IsInput); + + private readonly MatchesProvider _matchesProvider; + private readonly CommandRunner _commandRunner; + private readonly SettingProvider _settingProvider; + + private readonly BlockingCollection _packets; + private readonly BlockingCollection _serverLines; + + public Matcher( + MatchesProvider matchesProvider, + CommandRunner commandRunner, + SettingProvider settingProvider + ) + { + _matchesProvider = matchesProvider; + _commandRunner = commandRunner; + _settingProvider = settingProvider; + _packets = [.. new ConcurrentQueue()]; + _serverLines = [.. new ConcurrentQueue()]; + + Task.Run(StartMatchMsgLoop); + Task.Run(StartMatchServerLineLoop); + } /// /// 匹配服务器输出 /// /// 服务器Id /// 输出行 - public async Task MatchServerOutputAsync(string id, string line) + public void QueueServerOutputLine(string id, string line) { - await MatchFromServerInputOrOutput(id, line, false); + _serverLines.Add(new(id, line, false)); } /// @@ -34,12 +52,12 @@ public async Task MatchServerOutputAsync(string id, string line) /// /// 服务器Id /// 输入行 - public async Task MatchServerInputAsync(string id, string line) + public void QueueServerInputLine(string id, string line) { - await MatchFromServerInputOrOutput(id, line, true); + _serverLines.Add(new(id, line, true)); } - private async Task MatchFromServerInputOrOutput(string id, string line, bool isInput) + private void MatchServerLine(ServerLine serverLine) { var tasks = new List(); @@ -51,33 +69,56 @@ private async Task MatchFromServerInputOrOutput(string id, string line, bool isI string.IsNullOrEmpty(match.RegExp) || string.IsNullOrEmpty(match.Command) || match.FieldType - != (isInput ? MatchFieldType.ServerInput : MatchFieldType.ServerOutput) + != ( + serverLine.IsInput + ? MatchFieldType.ServerInput + : MatchFieldType.ServerOutput + ) || match.RegexObj is null || match.CommandObj is null || match.CommandObj.Type == CommandType.Invalid - || CheckExclusions(match, id) + || CheckExclusions(match, serverLine.Id) ) { continue; } - var matches = match.RegexObj.Match(line); + var matches = match.RegexObj.Match(serverLine.Line); if (matches.Success) { - tasks.Add(_commandRunner.RunAsync(match.CommandObj, new(matches))); + tasks.Add( + _commandRunner.RunAsync( + match.CommandObj, + new(matches, ServerId: serverLine.Id) + ) + ); } } } - await Task.WhenAll(tasks); + Task.WaitAll([.. tasks]); } - /// - /// 匹配消息 - /// - /// OneBot消息数据包 - public async Task MatchMsgAsync(MessagePacket messagePacket) + private void StartMatchServerLineLoop() + { + while (true) + { + var line = _serverLines.Take(); + MatchServerLine(line); + } + } + + private void StartMatchMsgLoop() + { + while (true) + { + var packet = _packets.Take(); + MatchMessagePacket(packet); + } + } + + private void MatchMessagePacket(MessagePacket messagePacket) { var tasks = new List(); @@ -121,7 +162,16 @@ public async Task MatchMsgAsync(MessagePacket messagePacket) } } - await Task.WhenAll(tasks); + Task.WaitAll([.. tasks]); + } + + /// + /// 匹配消息 + /// + /// OneBot消息数据包 + public void QueueMsg(MessagePacket messagePacket) + { + _packets.Add(messagePacket); } private static bool CheckExclusions(Match match, string serverId) diff --git a/src/Serein.Core/Services/Network/Connection/PacketHandler.cs b/src/Serein.Core/Services/Network/Connection/PacketHandler.cs index b3bfb60e..3bf409b5 100644 --- a/src/Serein.Core/Services/Network/Connection/PacketHandler.cs +++ b/src/Serein.Core/Services/Network/Connection/PacketHandler.cs @@ -117,6 +117,6 @@ private void HandleMessagePacket(MessagePacket? packet) return; } - _matcher.MatchMsgAsync(packet); + _matcher.QueueMsg(packet); } } diff --git a/src/Serein.Core/Services/Servers/Server.cs b/src/Serein.Core/Services/Servers/Server.cs index 14bca112..5fc1e0cb 100644 --- a/src/Serein.Core/Services/Servers/Server.cs +++ b/src/Serein.Core/Services/Servers/Server.cs @@ -286,7 +286,7 @@ internal void Input(string command, EncodingMap.EncodingType? encodingType, bool CommandHistoryIndex = CommandHistory.Count; - _matcher.MatchServerInputAsync(Id, command); + _matcher.QueueServerInputLine(Id, command); } public void RequestRestart() @@ -405,14 +405,14 @@ private void OnOutputDataReceived(object? sender, DataReceivedEventArgs e) ) { _cache.Add(filtered); - _matcher.MatchServerOutputAsync(Id, string.Join('\n', _cache)); + _matcher.QueueServerOutputLine(Id, string.Join('\n', _cache)); } else { _cache.Clear(); } - _matcher.MatchServerOutputAsync(Id, filtered); + _matcher.QueueServerOutputLine(Id, filtered); } private bool CancelRestart()