51 lines
1.7 KiB
C#
Raw Normal View History

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
.ConfigurePlugins(a =>
{
a.Add<TcpClosePlugin>();
2024-10-30 18:01:33 +08:00
a.Add<TcpServicePlugin>();
2024-09-30 17:10:43 +08:00
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
.ConfigurePlugins(a =>
{
2024-10-30 18:01:33 +08:00
a.Add<UdpServicePlugin>();
2024-09-30 17:10:43 +08:00
a.Add<BusService>();
})
.UseBroadcast()
.SetUdpDataHandlingAdapter(() => new NormalUdpDataHandlingAdapter());
});
return services;
}
}
}