70 lines
2.5 KiB
C#
70 lines
2.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using JiShe.CollectBus.Core.Plugins;
|
||
using JiShe.CollectBus.Core.Services;
|
||
using TouchSocket.Core;
|
||
using TouchSocket.Sockets;
|
||
|
||
namespace Microsoft.Extensions.DependencyInjection
|
||
{
|
||
public static class ServiceCollectionExtensions
|
||
{
|
||
/// <summary>
|
||
/// 添加TcpService服务。
|
||
/// </summary>
|
||
public static IServiceCollection AddTcp(this IServiceCollection services)
|
||
{
|
||
services.AddTcpService(config =>
|
||
{
|
||
config.SetListenIPHosts(10500)
|
||
.ConfigureContainer(a => //容器的配置顺序应该在最前面
|
||
{
|
||
a.AddConsoleLogger(); //添加一个控制台日志注入(注意:在maui中控制台日志不可用)
|
||
})
|
||
.ConfigurePlugins(a =>
|
||
{
|
||
a.UseCheckClear()
|
||
.SetCheckClearType(CheckClearType.All)
|
||
.SetTick(TimeSpan.FromSeconds(60))
|
||
.SetOnClose((c, t) =>
|
||
{
|
||
c.TryShutdown();
|
||
c.SafeClose("超时无数据");
|
||
});
|
||
a.Add<TcpClosePlugin>();
|
||
a.Add<TcpServiceReceivedPlugin>();
|
||
a.Add<BusService>();
|
||
});
|
||
});
|
||
return services;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 添加UdpService服务。
|
||
/// </summary>
|
||
public static IServiceCollection AddUdp(this IServiceCollection services)
|
||
{
|
||
services.AddUdpSession(config =>
|
||
{
|
||
config.SetBindIPHost(10500)
|
||
.ConfigureContainer(a => //容器的配置顺序应该在最前面
|
||
{
|
||
a.AddConsoleLogger(); //添加一个控制台日志注入(注意:在maui中控制台日志不可用)
|
||
})
|
||
.ConfigurePlugins(a =>
|
||
{
|
||
a.Add<UdpServiceReceivedPlugin>();
|
||
a.Add<BusService>();
|
||
})
|
||
.UseBroadcast()
|
||
.SetUdpDataHandlingAdapter(() => new NormalUdpDataHandlingAdapter());
|
||
});
|
||
return services;
|
||
}
|
||
}
|
||
}
|