//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在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://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.Security.Authentication;
using System.Threading;
using TouchSocket.Core;
namespace TouchSocket.Sockets
{
///
/// TouchSocketConfigBuilder配置扩展
///
public static class TouchSocketConfigExtension
{
#region 数据
///
/// 接收缓存容量,默认1024*64,其作用有两个:
///
/// - 指示单次可接受的最大数据量
/// - 指示常规申请内存块的长度
///
/// 所需类型
///
public static readonly DependencyProperty BufferLengthProperty =
DependencyProperty.Register("BufferLength", typeof(TouchSocketConfigExtension), 1024 * 64);
///
/// 数据处理适配器,默认为获取
/// 所需类型
///
public static readonly DependencyProperty> DataHandlingAdapterProperty = DependencyProperty>.Register("DataHandlingAdapter", typeof(TouchSocketConfigExtension), (Func)(() => { return new NormalDataHandlingAdapter(); }));
///
/// 接收类型,默认为
/// 为自动接收数据,然后主动触发。
/// 为不投递IO接收申请,用户可通过,获取到流以后,自己处理接收。注意:连接端不会感知主动断开
/// 所需类型
///
public static readonly DependencyProperty ReceiveTypeProperty = DependencyProperty.Register("ReceiveType", typeof(TouchSocketConfigExtension), ReceiveType.Auto);
///
/// 数据处理适配器,默认为获取
/// 所需类型
///
public static readonly DependencyProperty> UdpDataHandlingAdapterProperty = DependencyProperty>.Register("UdpDataHandlingAdapter", typeof(TouchSocketConfigExtension), (Func)(() => { return new NormalUdpDataHandlingAdapter(); }));
///
/// 接收缓存容量,默认1024*64,其作用有两个:
///
/// - 指示单次可接受的最大数据量
/// - 指示常规申请内存块的长度
///
///
///
///
///
public static TouchSocketConfig SetBufferLength(this TouchSocketConfig config, int value)
{
config.SetValue(BufferLengthProperty, value);
return config;
}
///
/// 设置(Tcp系)数据处理适配器。
///
///
///
///
public static TouchSocketConfig SetDataHandlingAdapter(this TouchSocketConfig config, Func value)
{
config.SetValue(DataHandlingAdapterProperty, value);
return config;
}
///
/// 接收类型,默认为
/// 为自动接收数据,然后主动触发。
/// 为不投递IO接收申请,用户可通过,获取到流以后,自己处理接收。注意:连接端不会感知主动断开
///
///
///
///
public static TouchSocketConfig SetReceiveType(this TouchSocketConfig config, ReceiveType value)
{
config.SetValue(ReceiveTypeProperty, value);
return config;
}
///
/// 设置(Udp系)数据处理适配器。
///
///
///
///
public static TouchSocketConfig SetUdpDataHandlingAdapter(this TouchSocketConfig config, Func value)
{
config.SetValue(UdpDataHandlingAdapterProperty, value);
return config;
}
#endregion 数据
#region ServiceBase
///
/// 服务名称,用于标识,无实际意义,所需类型
///
public static readonly DependencyProperty ServerNameProperty = DependencyProperty.Register("ServerName", typeof(TouchSocketConfigExtension), "TouchSocketServer");
///
/// 多线程数量。默认-1缺省。
/// TCP模式中,该值等效于
/// UDP模式中,该值为重叠IO并发数
/// 所需类型
///
public static readonly DependencyProperty ThreadCountProperty = DependencyProperty.Register("ThreadCount", typeof(TouchSocketConfigExtension), -1);
///
/// 服务名称,用于标识,无实际意义
///
///
///
///
public static TouchSocketConfig SetServerName(this TouchSocketConfig config, string value)
{
config.SetValue(ServerNameProperty, value);
return config;
}
///
/// 多线程数量,默认为-1缺省,实际上在tcp中相当于值10,udp中相当于1。
/// TCP模式中,该值等效于
/// UDP模式中,该值为重叠IO并发数
///
///
///
///
public static TouchSocketConfig SetThreadCount(this TouchSocketConfig config, int value)
{
config.SetValue(ThreadCountProperty, value);
return config;
}
#endregion ServiceBase
#region 适配器配置
///
/// 适配器数据包缓存启用。默认为缺省(null),如果有正常值会在设置适配器时,直接作用于
///
public static readonly DependencyProperty CacheTimeoutEnableProperty = DependencyProperty.Register("CacheTimeoutEnable", typeof(TouchSocketConfigExtension), null);
///
/// 适配器数据包缓存启用。默认为缺省(null),如果有正常值会在设置适配器时,直接作用于
///
///
///
///
public static TouchSocketConfig SetCacheTimeoutEnable(this TouchSocketConfig config, bool value)
{
config.SetValue(CacheTimeoutEnableProperty, value);
return config;
}
///
/// 适配器数据包缓存时长。默认为缺省()。当该值有效时会在设置适配器时,直接作用于
///
public static readonly DependencyProperty CacheTimeoutProperty = DependencyProperty.Register("CacheTimeout", typeof(TouchSocketConfigExtension), TimeSpan.Zero);
///
/// 适配器数据包缓存时长。默认为缺省()。当该值有效时会在设置适配器时,直接作用于
///
///
///
///
public static TouchSocketConfig SetCacheTimeout(this TouchSocketConfig config, TimeSpan value)
{
config.SetValue(CacheTimeoutProperty, value);
return config;
}
///
/// 适配器数据包缓存策略。默认缺省(null),当该值有效时会在设置适配器时,直接作用于
///
public static readonly DependencyProperty UpdateCacheTimeWhenRevProperty = DependencyProperty.Register("UpdateCacheTimeWhenRev", typeof(TouchSocketConfigExtension), null);
///
/// 适配器数据包缓存策略。默认缺省(null),当该值有效时会在设置适配器时,直接作用于
///
///
///
///
public static TouchSocketConfig SetUpdateCacheTimeWhenRev(this TouchSocketConfig config, bool value)
{
config.SetValue(UpdateCacheTimeWhenRevProperty, value);
return config;
}
///
/// 适配器数据包最大值。默认缺省(null),当该值有效时会在设置适配器时,直接作用于
///
public static readonly DependencyProperty MaxPackageSizeProperty = DependencyProperty.Register("MaxPackageSize", typeof(TouchSocketConfigExtension), null);
///
/// 适配器数据包最大值。默认缺省(null),当该值有效时会在设置适配器时,直接作用于
///
///
///
///
public static TouchSocketConfig SetMaxPackageSize(this TouchSocketConfig config, int value)
{
config.SetValue(MaxPackageSizeProperty, value);
return config;
}
#endregion 适配器配置
#region TcpClient
///
/// TCP固定端口绑定,
/// 所需类型
///
public static readonly DependencyProperty BindIPHostProperty = DependencyProperty.Register("BindIPHost", typeof(TouchSocketConfigExtension), null);
///
/// 在Socket配置KeepAlive属性,这个是操作tcp底层的,如果你对底层不了解,建议不要动。
/// 所需类型
///
public static readonly DependencyProperty KeepAliveValueProperty = DependencyProperty.Register("KeepAliveValue", typeof(TouchSocketConfigExtension), new KeepAliveValue());
///
/// 设置Socket不使用Delay算法,
/// 所需类型
///
public static readonly DependencyProperty NoDelayProperty = DependencyProperty.Register("NoDelay", typeof(TouchSocketConfigExtension), false);
///
/// 远程目标地址,所需类型
///
public static readonly DependencyProperty RemoteIPHostProperty = DependencyProperty.Register("RemoteIPHost", typeof(TouchSocketConfigExtension), null);
///
/// Ssl配置,为Null时则不启用
/// 所需类型
///
public static readonly DependencyProperty SslOptionProperty = DependencyProperty.Register("SslOption", typeof(TouchSocketConfigExtension), null);
///
/// 是否使用延迟合并发送。默认null。不开启
/// 所需类型
///
public static readonly DependencyProperty DelaySenderProperty = DependencyProperty.Register("DelaySender", typeof(TouchSocketConfigExtension), null);
///
/// 使用默认配置延迟合并发送。
/// 所需类型
///
///
///
///
public static TouchSocketConfig UseDelaySender(this TouchSocketConfig config, DelaySenderOption option = default)
{
if (option == default)
{
option = new DelaySenderOption();
}
config.SetValue(DelaySenderProperty, option);
return config;
}
///
/// 固定端口绑定。
/// 在中表示本地监听地址
/// 在中表示固定客户端端口号。
///
///
///
///
public static TouchSocketConfig SetBindIPHost(this TouchSocketConfig config, IPHost value)
{
config.SetValue(BindIPHostProperty, value);
return config;
}
///
/// 固定端口绑定。
/// 在中表示本地监听地址
/// 在中表示固定客户端端口号。
///
///
///
///
public static TouchSocketConfig SetBindIPHost(this TouchSocketConfig config, int value)
{
config.SetValue(BindIPHostProperty, new IPHost(value));
return config;
}
///
/// 固定端口绑定。
/// 在中表示本地监听地址
/// 在中表示固定客户端端口号。
///
///
///
///
public static TouchSocketConfig SetBindIPHost(this TouchSocketConfig config, string value)
{
config.SetValue(BindIPHostProperty, new IPHost(value));
return config;
}
///
/// 设置客户端Ssl配置,为Null时则不启用。
///
///
///
///
public static TouchSocketConfig SetClientSslOption(this TouchSocketConfig config, ClientSslOption value)
{
config.SetValue(SslOptionProperty, value);
return config;
}
///
/// 在Socket的KeepAlive属性。
/// 注意:这个是操作tcp底层的,如果你对底层不了解,建议不要动。
///
///
///
///
public static TouchSocketConfig SetKeepAliveValue(this TouchSocketConfig config, KeepAliveValue value)
{
config.SetValue(KeepAliveValueProperty, value);
return config;
}
///
/// 设置远程目标地址。在中,表示默认发送时的目标地址。
///
///
///
///
public static TouchSocketConfig SetRemoteIPHost(this TouchSocketConfig config, IPHost value)
{
config.SetValue(RemoteIPHostProperty, value);
if (value.IsUri)
{
if (value.Uri.Scheme.Equals("https", StringComparison.CurrentCultureIgnoreCase)
|| value.Uri.Scheme.Equals("wss", StringComparison.CurrentCultureIgnoreCase)
|| value.Uri.Scheme.Equals("ssl", StringComparison.CurrentCultureIgnoreCase)
|| value.Uri.Scheme.Equals("tls", StringComparison.CurrentCultureIgnoreCase))
{
config.SetClientSslOption(new ClientSslOption()
{
TargetHost = value.Host
});
}
}
return config;
}
///
/// 设置远程目标地址。在中,表示默认发送时的目标地址。
///
///
///
///
public static TouchSocketConfig SetRemoteIPHost(this TouchSocketConfig config, string value)
{
return SetRemoteIPHost(config, new IPHost(value));
}
///
/// 设置Socket的NoDelay属性,默认false。
///
///
///
public static TouchSocketConfig UseNoDelay(this TouchSocketConfig config)
{
config.SetValue(NoDelayProperty, true);
return config;
}
#endregion TcpClient
#region TcpService
///
/// 挂起连接队列的最大长度,所需类型
///
public static readonly DependencyProperty BacklogProperty = DependencyProperty.Register("Backlog", typeof(TouchSocketConfigExtension), 100);
///
/// 设置默认ID的获取方式,所需类型
///
public static readonly DependencyProperty> GetDefaultNewIDProperty = DependencyProperty>.Register("GetDefaultNewID", typeof(TouchSocketConfigExtension), null);
///
/// 服务器负责监听的地址组。所需类型数组
///
public static readonly DependencyProperty ListenIPHostsProperty = DependencyProperty.Register("ListenIPHosts", typeof(TouchSocketConfigExtension), null);
///
/// 最大可连接数,默认为10000,所需类型
///
public static readonly DependencyProperty MaxCountProperty = DependencyProperty.Register("MaxCount", typeof(TouchSocketConfigExtension), 10000);
///
/// 端口复用,默认为false,所需类型
///
public static readonly DependencyProperty ReuseAddressProperty = DependencyProperty.Register("ReuseAddress", typeof(TouchSocketConfigExtension), false);
///
/// 启用端口复用。
/// 该配置可在服务器、或客户端在监听端口时,运行监听同一个端口。可以一定程度缓解端口来不及释放的问题
///
///
///
public static TouchSocketConfig UseReuseAddress(this TouchSocketConfig config)
{
config.SetValue(ReuseAddressProperty, true);
return config;
}
///
/// 挂起连接队列的最大长度,默认100。
///
///
///
///
public static TouchSocketConfig SetBacklog(this TouchSocketConfig config, int value)
{
config.SetValue(BacklogProperty, value);
return config;
}
///
/// 设置清理无数据交互的SocketClient,默认60*1000 ms。如果不想清除,可使用-1
///
///
///
///
[Obsolete("该操作已被弃用,请使用CheckClearPlugin插件,或者在插件中,配置UseCheckClear。", true)]
public static TouchSocketConfig SetClearInterval(this TouchSocketConfig config, int value)
{
throw new NotImplementedException();
}
///
/// 清理统计类型。
/// 为在收到数据时,刷新统计,如果一直有数据接收,则不会被主动清理断开
/// 为在发送数据时,刷新统计,如果一直有数据发送,则不会被主动清理断开
/// 二者可叠加使用。
///
///
///
///
[Obsolete("该操作已被弃用,请使用CheckClearPlugin插件,或者在插件中,配置UseCheckClear。", true)]
public static TouchSocketConfig SetClearType(this TouchSocketConfig config, CheckClearType value)
{
throw new NotImplementedException();
}
///
/// 设置默认ID的获取方式。仅服务器生效。
///
///
///
///
public static TouchSocketConfig SetGetDefaultNewID(this TouchSocketConfig config, Func value)
{
config.SetValue(GetDefaultNewIDProperty, value);
return config;
}
///
/// 服务器负责监听的地址组。
///
///
///
///
public static TouchSocketConfig SetListenIPHosts(this TouchSocketConfig config, IPHost[] value)
{
config.SetValue(ListenIPHostsProperty, value);
return config;
}
///
/// 最大可连接数,默认为10000。
///
///
///
///
public static TouchSocketConfig SetMaxCount(this TouchSocketConfig config, int value)
{
config.SetValue(MaxCountProperty, value);
return config;
}
///
/// 设置客户端Ssl配置,为Null时则不启用。
///
///
///
///
public static TouchSocketConfig SetServiceSslOption(this TouchSocketConfig config, ServiceSslOption value)
{
config.SetValue(SslOptionProperty, value);
return config;
}
#endregion TcpService
#region UDP
///
/// 该值指定 System.Net.Sockets.Socket可以发送或接收广播数据包。
///
public static readonly DependencyProperty EnableBroadcastProperty = DependencyProperty.Register("EnableBroadcast", typeof(TouchSocketConfigExtension), false);
///
/// 该值指定 System.Net.Sockets.Socket可以发送或接收广播数据包。
///
///
///
public static TouchSocketConfig UseBroadcast(this TouchSocketConfig config)
{
config.SetValue(EnableBroadcastProperty, true);
return config;
}
#endregion UDP
#region 创建
///
/// 构建Tcp类客户端,并连接
///
///
///
///
public static TClient BuildWithTcpClient(this TouchSocketConfig config) where TClient : ITcpClient
{
TClient service = config.Container.Resolve();
service.Setup(config);
service.Connect();
return service;
}
///
/// 构建Tcp类客户端,并连接
///
///
///
public static TcpClient BuildWithTcpClient(this TouchSocketConfig config)
{
return BuildWithTcpClient(config);
}
///
/// 构建Tcp类服务器,并启动。
///
///
///
///
public static TService BuildWithTcpService(this TouchSocketConfig config) where TService : ITcpService
{
TService service = config.Container.Resolve();
service.Setup(config);
service.Start();
return service;
}
///
/// 构建Tcp类服务器,并启动。
///
///
///
public static TcpService BuildWithTcpService(this TouchSocketConfig config)
{
return BuildWithTcpService(config);
}
///
/// 构建UDP类,并启动。
///
///
///
///
public static TSession BuildWithUdpSession(this TouchSocketConfig config) where TSession : IUdpSession
{
TSession service = config.Container.Resolve();
service.Setup(config);
service.Start();
return service;
}
///
/// 构建UDP类,并启动。
///
///
///
public static UdpSession BuildWithUdpSession(this TouchSocketConfig config)
{
return BuildWithUdpSession(config);
}
#endregion 创建
}
}