2024-10-08 14:41:41 +08:00
|
|
|
|
using JiShe.CollectBus.Core.Plugins;
|
2024-09-30 17:10:43 +08:00
|
|
|
|
using JiShe.CollectBus.Core.Services;
|
2024-10-08 14:41:41 +08:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2024-09-30 17:10:43 +08:00
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
|
2024-10-11 11:27:57 +08:00
|
|
|
|
// ReSharper disable once CheckNamespace
|
2024-09-30 17:10:43 +08:00
|
|
|
|
namespace Microsoft.Extensions.DependencyInjection
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class ServiceCollectionExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加TcpService服务。
|
|
|
|
|
|
/// </summary>
|
2024-10-08 14:41:41 +08:00
|
|
|
|
public static IServiceCollection AddTcp(this IServiceCollection services, IConfiguration configuration)
|
2024-09-30 17:10:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
services.AddTcpService(config =>
|
|
|
|
|
|
{
|
2024-10-30 17:49:05 +08:00
|
|
|
|
config.SetListenIPHosts(int.Parse(configuration["TCP:ClientPort"] ?? "10500"))
|
2024-09-30 17:10:43 +08:00
|
|
|
|
.ConfigureContainer(a => //容器的配置顺序应该在最前面
|
|
|
|
|
|
{
|
2024-10-22 20:57:26 +08:00
|
|
|
|
a.AddConsoleLogger();
|
2024-09-30 17:10:43 +08:00
|
|
|
|
})
|
|
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
|
|
{
|
|
|
|
|
|
a.Add<TcpClosePlugin>();
|
|
|
|
|
|
a.Add<TcpServiceReceivedPlugin>();
|
|
|
|
|
|
a.Add<BusService>();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
return services;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加UdpService服务。
|
|
|
|
|
|
/// </summary>
|
2024-10-08 14:41:41 +08:00
|
|
|
|
public static IServiceCollection AddUdp(this IServiceCollection services, IConfiguration configuration)
|
2024-09-30 17:10:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
services.AddUdpSession(config =>
|
|
|
|
|
|
{
|
2024-10-30 17:49:05 +08:00
|
|
|
|
config.SetBindIPHost(int.Parse(configuration["UDP:ClientPort"] ?? "10500"))
|
2024-09-30 17:10:43 +08:00
|
|
|
|
.ConfigureContainer(a => //容器的配置顺序应该在最前面
|
|
|
|
|
|
{
|
2024-10-22 20:57:26 +08:00
|
|
|
|
//a.AddConsoleLogger();
|
2024-09-30 17:10:43 +08:00
|
|
|
|
})
|
|
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
|
|
{
|
|
|
|
|
|
a.Add<UdpServiceReceivedPlugin>();
|
|
|
|
|
|
a.Add<BusService>();
|
|
|
|
|
|
})
|
|
|
|
|
|
.UseBroadcast()
|
|
|
|
|
|
.SetUdpDataHandlingAdapter(() => new NormalUdpDataHandlingAdapter());
|
|
|
|
|
|
});
|
|
|
|
|
|
return services;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|