//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在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 TouchSocket.Core;
namespace TouchSocket.Rpc.TouchRpc
{
///
/// Rpc配置扩展
///
public static class TouchRpcConfigExtensions
{
///
/// 默认使用Id。
///
public static readonly DependencyProperty DefaultIdProperty =
DependencyProperty.Register("DefaultId", typeof(TouchRpcConfigExtensions), null);
///
/// TouchClient连接时的元数据, 所需类型
///
public static readonly DependencyProperty MetadataProperty = DependencyProperty.Register("Metadata", typeof(TouchRpcConfigExtensions), null);
///
/// 序列化转换器, 所需类型
///
public static readonly DependencyProperty SerializationSelectorProperty =
DependencyProperty.Register("SerializationSelector", typeof(TouchRpcConfigExtensions), new DefaultSerializationSelector());
///
/// 验证超时时间,默认为3000ms, 所需类型
///
public static readonly DependencyProperty VerifyTimeoutProperty =
DependencyProperty.Register("VerifyTimeout", typeof(TouchRpcConfigExtensions), 3000);
///
/// 连接令箭,当为null或空时,重置为默认值“rrqm”, 所需类型
///
public static readonly DependencyProperty VerifyTokenProperty =
DependencyProperty.Register("VerifyToken", typeof(TouchRpcConfigExtensions), "rrqm");
///
/// 设置默认的使用Id。仅在TouchRpc组件适用。
///
/// 使用该功能时,仅在服务器的Handshaking之后生效。且如果id重复,则会连接失败。
///
///
///
///
///
public static TouchSocketConfig SetDefaultId(this TouchSocketConfig config, string value)
{
config.SetValue(DefaultIdProperty, value);
return config;
}
///
/// 心跳频率,默认为间隔2000ms,3次。(设置为null时禁止心跳)
/// 仅适用于TouchRpcClient系类
///
///
///
///
[Obsolete("该方法已被弃用。请使用插件UseTouchRpcHeartbeat替代。", true)]
public static TouchSocketConfig SetHeartbeatFrequency(this TouchSocketConfig config, object value)
{
throw new NotImplementedException();
}
///
/// 设置TouchClient连接时的元数据
/// 仅适用于TouchRpcClient系类
///
///
///
///
public static TouchSocketConfig SetMetadata(this TouchSocketConfig config, Metadata value)
{
config.SetValue(MetadataProperty, value);
return config;
}
///
/// 设置序列化转换器
///
///
///
///
public static TouchSocketConfig SetSerializationSelector(this TouchSocketConfig config, SerializationSelector value)
{
config.SetValue(SerializationSelectorProperty, value);
return config;
}
///
/// 验证超时时间,默认为3000ms.
/// 该配置仅有效
///
///
///
///
public static TouchSocketConfig SetVerifyTimeout(this TouchSocketConfig config, int value)
{
config.SetValue(VerifyTimeoutProperty, value);
return config;
}
///
/// 连接令箭,当为null或空时,重置为默认值“rrqm”
///
///
///
///
public static TouchSocketConfig SetVerifyToken(this TouchSocketConfig config, string value)
{
config.SetValue(VerifyTokenProperty, value);
return config;
}
#region FileTransfer
///
/// 根目录
/// 所需类型
///
public static readonly DependencyProperty RootPathProperty = DependencyProperty.Register("RootPath", typeof(TouchRpcConfigExtensions), string.Empty);
///
/// 设置根路径
///
///
///
///
public static TouchSocketConfig SetRootPath(this TouchSocketConfig config, string value)
{
config.SetValue(RootPathProperty, value);
return config;
}
#endregion FileTransfer
#region 创建TcpTouchRpc
///
/// 构建TcpTouchRpc类客户端,并连接
///
///
///
///
public static TClient BuildWithTcpTouchRpcClient(this TouchSocketConfig config) where TClient : ITcpTouchRpcClient
{
TClient client = config.Container.Resolve();
client.Setup(config);
client.Connect();
return client;
}
///
/// 构建TcpTouchRpc类客户端,并连接
///
///
///
public static TcpTouchRpcClient BuildWithTcpTouchRpcClient(this TouchSocketConfig config)
{
return BuildWithTcpTouchRpcClient(config);
}
///
/// 构建TcpTouchRpc类服务器,并启动。
///
///
///
///
public static TService BuildWithTcpTouchRpcService(this TouchSocketConfig config) where TService : ITcpTouchRpcService
{
TService service = config.Container.Resolve();
service.Setup(config);
service.Start();
return service;
}
///
/// 构建TcpTouchRpc类服务器,并启动。
///
///
///
public static TcpTouchRpcService BuildWithTcpTouchRpcService(this TouchSocketConfig config)
{
return BuildWithTcpTouchRpcService(config);
}
#endregion 创建TcpTouchRpc
#region 创建HttpTouchRpc
///
/// 构建HttpTouchRpc类客户端,并连接
///
///
///
///
public static TClient BuildWithHttpTouchRpcClient(this TouchSocketConfig config) where TClient : IHttpTouchRpcClient
{
TClient client = config.Container.Resolve();
client.Setup(config);
client.Connect();
return client;
}
///
/// 构建HttpTouchRpc类客户端,并连接
///
///
///
public static HttpTouchRpcClient BuildWithHttpTouchRpcClient(this TouchSocketConfig config)
{
return BuildWithHttpTouchRpcClient(config);
}
///
/// 构建HttpTouchRpc类服务器,并启动。
///
///
///
///
public static TService BuildWithHttpTouchRpcService(this TouchSocketConfig config) where TService : IHttpTouchRpcService
{
TService service = config.Container.Resolve();
service.Setup(config);
service.Start();
return service;
}
///
/// 构建HttpTouchRpc类服务器,并启动。
///
///
///
public static HttpTouchRpcService BuildWithHttpTouchRpcService(this TouchSocketConfig config)
{
return BuildWithHttpTouchRpcService(config);
}
#endregion 创建HttpTouchRpc
#region 创建UdpTouchRpc
///
/// 构建UdpTouchRpc类
///
///
///
///
public static TClient BuildWithUdpTouchRpc(this TouchSocketConfig config) where TClient : IUdpTouchRpc
{
TClient client = config.Container.Resolve();
client.Setup(config);
client.Start();
return client;
}
///
/// 构建UdpTouchRpc类客户端
///
///
///
public static UdpTouchRpc BuildWithUdpTouchRpc(this TouchSocketConfig config)
{
return BuildWithUdpTouchRpc(config);
}
#endregion 创建UdpTouchRpc
}
}