-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OneBot] try to support multi-connection
- Loading branch information
Linwenxuan
authored and
Linwenxuan
committed
Oct 27, 2023
1 parent
0a88ad9
commit b417adc
Showing
8 changed files
with
115 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,84 @@ | ||
using Lagrange.OneBot.Core.Network.Service; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Lagrange.OneBot.Core.Network; | ||
|
||
public class LagrangeWebSvcCollection : List<ILagrangeWebService>, IHostedService | ||
public class LagrangeWebSvcCollection : Dictionary<string, ILagrangeWebService>, IHostedService | ||
{ | ||
private const string Tag = nameof(LagrangeWebSvcCollection); | ||
|
||
public event EventHandler<MsgRecvEventArgs> OnMessageReceived = delegate { }; | ||
|
||
public LagrangeWebSvcCollection(IEnumerable<ILagrangeWebService> services) : base(services) | ||
public LagrangeWebSvcCollection(IConfiguration global, ILogger<LagrangeApp> logger) | ||
{ | ||
foreach (var service in this) service.OnMessageReceived += OnMessageReceived.Invoke; | ||
uint uin = global.GetValue<uint>("Account:Uin"); | ||
|
||
if (global.GetSection("Implementations").Exists()) | ||
{ | ||
logger.LogInformation($"[{Tag}]: Multi Connection has been configured"); | ||
|
||
foreach (var section in global.GetSection("Implementations").GetChildren()) | ||
{ | ||
ILagrangeWebService? service = section["Type"] switch | ||
{ | ||
"ReverseWebSocket" => new ReverseWSService(section, logger, uin), | ||
"ForwardWebSocket" => new ForwardWSService(section, logger, uin), | ||
_ => null | ||
}; | ||
|
||
if (service == null) logger.LogWarning($"[{Tag}]: unknown type of service of {section["Type"]} is configured, skipped"); | ||
else Add(new Guid().ToString(), service); | ||
} | ||
} | ||
else if (global.GetSection("Implementation").Exists()) | ||
{ | ||
logger.LogInformation($"[{Tag}]: Single Connection has been configured"); | ||
|
||
string identifier = new Guid().ToString(); | ||
if (global.GetSection("Implementation:ReverseWebSocket").Exists()) | ||
{ | ||
this[identifier] = new ReverseWSService(global.GetSection("Implementation:ReverseWebSocket"), logger, uin); | ||
} | ||
else if (global.GetSection("Implementation:ForwardWebSocket").Exists()) | ||
{ | ||
this[identifier] = new ForwardWSService(global.GetSection("Implementation:ForwardWebSocket"), logger, uin); | ||
} | ||
} | ||
else | ||
{ | ||
logger.LogWarning($"[{Tag}]: No implementation has been configured"); | ||
} | ||
|
||
foreach (var (identifier, service) in this) | ||
{ | ||
service.OnMessageReceived += (sender, args) => | ||
{ | ||
OnMessageReceived.Invoke(sender, new MsgRecvEventArgs(identifier, args.Data)); | ||
}; | ||
} | ||
} | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
foreach (var service in this) await service.StartAsync(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); | ||
foreach (var (_, service) in this) await service.StopAsync(cancellationToken); | ||
} | ||
|
||
public async Task SendJsonAsync<T>(T json, CancellationToken cancellationToken = default) | ||
public async Task SendJsonAsync<T>(T json, string? identifier = null, CancellationToken cancellationToken = default) | ||
{ | ||
foreach (var service in this) await service.SendJsonAsync(json, cancellationToken); | ||
if (identifier == null) | ||
{ | ||
foreach (var (_, service) in this) await service.SendJsonAsync(json, cancellationToken); | ||
} | ||
else | ||
{ | ||
if (TryGetValue(identifier, out var service)) await service.SendJsonAsync(json, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters