Files
TouchSocket/src/TouchSocket/Extensions/TouchSocketConfigExtension.cs
若汝棋茗 d2ef2e941d 发布:4.0.2
2025-12-06 15:25:46 +08:00

209 lines
8.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//------------------------------------------------------------------------------
// 此代码版权除特别声明或在XREF结尾的命名空间的代码归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议若本仓库没有设置则按MIT开源协议授权
// CSDN博客https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频https://space.bilibili.com/94253567
// Gitee源代码仓库https://gitee.com/RRQM_Home
// Github源代码仓库https://github.com/RRQM
// API首页https://touchsocket.net/
// 交流QQ群234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
namespace TouchSocket.Sockets;
/// <summary>
/// 触摸套接字配置扩展类
/// </summary>
public static class TouchSocketConfigExtension
{
#region
/// <summary>
/// 传输选项配置属性,类型为<see cref="TransportOption"/>。
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig), ActionMode = true)]
public static readonly DependencyProperty<TransportOption> TransportOptionProperty = new("TransportOption", new TransportOption());
/// <summary>
/// 数据处理适配器
/// 所需类型<see cref="Func{TResult}"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<Func<SingleStreamDataHandlingAdapter>> TcpDataHandlingAdapterProperty = new("TcpDataHandlingAdapter", null);
/// <summary>
/// 数据处理适配器
/// 所需类型<see cref="Func{TResult}"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<Func<UdpDataHandlingAdapter>> UdpDataHandlingAdapterProperty = new("UdpDataHandlingAdapter", null);
#endregion
#region ServiceBase
/// <summary>
/// 服务名称,用于标识,无实际意义,所需类型<see cref="string"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<string> ServerNameProperty = new("ServerName", "TouchSocketServer");
/// <summary>
/// 重叠IO并发数。默认1。
/// <para>UDP模式中该值为重叠IO并发数</para>
/// 所需类型<see cref="int"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<int> UdpOverlappedCountProperty = new("UdpOverlappedCount", 1);
#endregion ServiceBase
#region TcpClient
/// <summary>
/// Tcp固定端口绑定
/// 所需类型<see cref="IPHost"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<IPHost> BindIPHostProperty = new("BindIPHost", null);
/// <summary>
/// 在Socket配置KeepAlive属性这个是操作tcp底层的如果你对底层不了解建议不要动。
/// 所需类型<see cref="bool"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig), ActionMode = true)]
public static readonly DependencyProperty<KeepAliveValue> KeepAliveValueProperty = new("KeepAliveValue", default);
/// <summary>
/// 设置Socket不使用Delay算法
/// 所需类型<see cref="bool"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<bool> NoDelayProperty = new("NoDelay", true);
/// <summary>
/// 远程目标地址,所需类型<see cref="IPHost"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<IPHost> RemoteIPHostProperty = new("RemoteIPHost", null);
/// <summary>
/// ClientSslOption配置为Null时则不启用
/// 所需类型<see cref="TouchSocket.Sockets.SslOption"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig), ActionMode = true)]
public static readonly DependencyProperty<ClientSslOption> ClientSslOptionProperty = new("ClientSslOption", null);
/// <summary>
/// ServiceSslOption配置为Null时则不启用
/// 所需类型<see cref="TouchSocket.Sockets.SslOption"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig), ActionMode = true)]
public static readonly DependencyProperty<ServiceSslOption> ServiceSslOptionProperty = new("ServiceSslOption", null);
#endregion TcpClient
#region TcpService
/// <summary>
/// 挂起连接队列的最大长度,所需类型<see cref="int"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<int> BacklogProperty = new("Backlog", 100);
/// <summary>
/// 设置默认Id的获取方式所需类型<see cref="Func{T, TResult}"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<Func<IClient, string>> GetDefaultNewIdProperty = new("GetDefaultNewId", null);
/// <summary>
/// 服务器负责监听的地址组。所需类型<see cref="IPHost"/>数组
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<IPHost[]> ListenIPHostsProperty = new("ListenIPHosts", null);
/// <summary>
/// 直接单个配置服务器监听的地址组。所需类型<see cref="Action"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig), ActionMode = true)]
public static readonly DependencyProperty<List<TcpListenOption>> ListenOptionsProperty = new("ListenOptions", null);
/// <summary>
/// 最大可连接数默认为10000所需类型<see cref="int"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<int> MaxCountProperty = new("MaxCount", 10000);
/// <summary>
/// 端口复用,默认为<see langword="false"/>,所需类型<see cref="bool"/>
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<bool> ReuseAddressProperty = new("ReuseAddress", false);
#endregion TcpService
#region UDP
/// <summary>
/// 该值指定 System.Net.Sockets.Socket可以发送或接收广播数据包。
/// </summary>
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<bool> EnableBroadcastProperty = new("EnableBroadcast", false);
/// <summary>
/// 当udp作为客户端时开始接收数据。起作用相当于<see cref="BindIPHostProperty"/>0端口。
/// </summary>
/// <param name="config"></param>
/// <returns></returns>
public static TouchSocketConfig UseUdpReceive(this TouchSocketConfig config)
{
return config.SetBindIPHost(0);
}
/// <summary>
/// 解决Windows下UDP连接被重置错误10054。
/// </summary>
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
[GeneratorProperty(TargetType = typeof(TouchSocketConfig))]
public static readonly DependencyProperty<bool> UdpConnResetProperty = new("UdpConnReset", false);
#endregion UDP
#region
/// <summary>
/// 构建可配置,可连接类客户端,并连接
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <param name="config"></param>
/// <returns></returns>
public static async Task<TClient> BuildClientAsync<TClient>(this TouchSocketConfig config) where TClient : ISetupConfigObject, IConnectableClient, new()
{
var client = new TClient();
await client.SetupAsync(config).ConfigureAwait(EasyTask.ContinueOnCapturedContext);
await client.ConnectAsync().ConfigureAwait(EasyTask.ContinueOnCapturedContext);
return client;
}
/// <summary>
/// 构建Tcp类服务器并启动。
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <param name="config"></param>
/// <returns></returns>
public static async Task<TService> BuildServiceAsync<TService>(this TouchSocketConfig config) where TService : IServiceBase, new()
{
var service = new TService();
await service.SetupAsync(config).ConfigureAwait(EasyTask.ContinueOnCapturedContext);
await service.StartAsync().ConfigureAwait(EasyTask.ContinueOnCapturedContext);
return service;
}
#endregion
}