Skip to content

Commit

Permalink
fix: 事件总数被拦截
Browse files Browse the repository at this point in the history
chore: 重构匹配功能
  • Loading branch information
Zaitonn committed Dec 9, 2024
1 parent 3d22f40 commit 3e9b33f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ namespace Serein.Core.Models.Network.Connection.OneBot.Messages;
public enum SubType
{
Unknown,

Friend,

Normal,

Anonymous,

Group,

GroupSelf,

Notice,
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ namespace Serein.Core.Models.Network.WebApi;
public enum WebSocketBroadcastType
{
Started,

Stopped,

Removed,

Input,

Output,

Error,

Information,
}
8 changes: 3 additions & 5 deletions src/Serein.Core/Models/Plugins/Js/JsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event, Function> EventHandlers => _eventHandlers;
public bool IsEnabled => !_cancellationTokenSource.IsCancellationRequested;
Expand Down Expand Up @@ -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))
{
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/Serein.Core/Services/Bindings/BindingRecordDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Serein.Core.Services.Bindings;

#pragma warning disable CS8618

internal sealed class BindingRecordDbContext : DbContext
{
public DbSet<BindingRecord> Records { get; private set; }
Expand Down
98 changes: 74 additions & 24 deletions src/Serein.Core/Services/Commands/Matcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -9,37 +10,54 @@

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<MessagePacket> _packets;
private readonly BlockingCollection<ServerLine> _serverLines;

public Matcher(
MatchesProvider matchesProvider,
CommandRunner commandRunner,
SettingProvider settingProvider
)
{
_matchesProvider = matchesProvider;
_commandRunner = commandRunner;
_settingProvider = settingProvider;
_packets = [.. new ConcurrentQueue<MessagePacket>()];
_serverLines = [.. new ConcurrentQueue<ServerLine>()];

Task.Run(StartMatchMsgLoop);
Task.Run(StartMatchServerLineLoop);
}

/// <summary>
/// 匹配服务器输出
/// </summary>
/// <param name="id">服务器Id</param>
/// <param name="line">输出行</param>
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));
}

/// <summary>
/// 匹配服务器输入
/// </summary>
/// <param name="id">服务器Id</param>
/// <param name="line">输入行</param>
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<Task>();

Expand All @@ -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]);
}

/// <summary>
/// 匹配消息
/// </summary>
/// <param name="messagePacket">OneBot消息数据包</param>
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<Task>();

Expand Down Expand Up @@ -121,7 +162,16 @@ public async Task MatchMsgAsync(MessagePacket messagePacket)
}
}

await Task.WhenAll(tasks);
Task.WaitAll([.. tasks]);
}

/// <summary>
/// 匹配消息
/// </summary>
/// <param name="messagePacket">OneBot消息数据包</param>
public void QueueMsg(MessagePacket messagePacket)
{
_packets.Add(messagePacket);
}

private static bool CheckExclusions(Match match, string serverId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ private void HandleMessagePacket(MessagePacket? packet)
return;
}

_matcher.MatchMsgAsync(packet);
_matcher.QueueMsg(packet);
}
}
6 changes: 3 additions & 3 deletions src/Serein.Core/Services/Servers/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 3e9b33f

Please sign in to comment.