Skip to content

Commit

Permalink
[OneBot] Introduce WebServiceCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 27, 2023
1 parent 213229d commit a4078b8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Lagrange.OneBot/Core/Message/MessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Lagrange.Core.Message;
using Lagrange.Core.Utility.Extension;
using Lagrange.OneBot.Core.Entity.Message;
using Lagrange.OneBot.Core.Network.Service;
using Lagrange.OneBot.Core.Network;
using Lagrange.OneBot.Database;

namespace Lagrange.OneBot.Core.Message;
Expand All @@ -15,11 +15,11 @@ namespace Lagrange.OneBot.Core.Message;
/// </summary>
public sealed class MessageService
{
private readonly ILagrangeWebService _service;
private readonly LagrangeWebSvcCollection _service;
private readonly ContextBase _context;
private readonly Dictionary<Type, (string, ISegment)> _entityToSegment;

public MessageService(BotContext bot, ILagrangeWebService service, ContextBase context)
public MessageService(BotContext bot, LagrangeWebSvcCollection service, ContextBase context)
{
_service = service;
_context = context;
Expand Down
29 changes: 29 additions & 0 deletions Lagrange.OneBot/Core/Network/LagrangeWebSvcCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Lagrange.OneBot.Core.Network.Service;
using Microsoft.Extensions.Hosting;

namespace Lagrange.OneBot.Core.Network;

public class LagrangeWebSvcCollection : List<ILagrangeWebService>, IHostedService
{
public event EventHandler<MsgRecvEventArgs> OnMessageReceived = delegate { };

public LagrangeWebSvcCollection(IEnumerable<ILagrangeWebService> services) : base(services)
{
foreach (var service in this) service.OnMessageReceived += OnMessageReceived.Invoke;
}

public async Task StartAsync(CancellationToken cancellationToken)
{
foreach (var service in this) await service.StartAsync(cancellationToken);
}

public async Task StopAsync(CancellationToken cancellationToken)
{
foreach (var service in this) await service.StopAsync(cancellationToken);
}

public async Task SendJsonAsync<T>(T json, CancellationToken cancellationToken = default)
{
foreach (var service in this) await service.SendJsonAsync(json, cancellationToken);
}
}
9 changes: 7 additions & 2 deletions Lagrange.OneBot/Core/Operation/OperationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
using Lagrange.Core;
using Lagrange.Core.Utility.Extension;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Network;
using Lagrange.OneBot.Core.Network.Service;

namespace Lagrange.OneBot.Core.Operation;

public sealed class OperationService
{
private readonly BotContext _bot;
private readonly ILagrangeWebService _service;
private readonly LagrangeWebSvcCollection _service;
private readonly Dictionary<string, IOperation> _operations;

public OperationService(BotContext bot, ILagrangeWebService service)
public OperationService(BotContext bot, LagrangeWebSvcCollection service)
{
_bot = bot;
_service = service;
Expand Down Expand Up @@ -55,5 +56,9 @@ private async Task HandleOperation(string data)
throw new Exception("action deserialized failed");
}
}
catch
{
await _service.SendJsonAsync(new OneBotResult(null, 200, "failed"));
}
}
}
3 changes: 3 additions & 0 deletions Lagrange.OneBot/LagrangeAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Lagrange.Core.Common.Interface;
using Lagrange.Core.Utility.Sign;
using Lagrange.OneBot.Core.Message;
using Lagrange.OneBot.Core.Network;
using Lagrange.OneBot.Core.Network.Service;
using Lagrange.OneBot.Core.Operation;
using Lagrange.OneBot.Database;
Expand Down Expand Up @@ -85,6 +86,8 @@ public LagrangeAppBuilder ConfigureOneBot()
{
Services.AddSingleton<ILagrangeWebService, ForwardWSService>();
}

Services.AddSingleton<LagrangeWebSvcCollection>();

Services.AddSingleton<ContextBase, LiteDbContext>();
Services.AddSingleton<SignProvider, OneBotSigner>();
Expand Down

0 comments on commit a4078b8

Please sign in to comment.