//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在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.Collections.Generic; using System.Threading.Tasks; using TouchSocket.Core; using TouchSocket.Sockets; namespace TouchSocket.Rpc.TouchRpc { /// /// RPC辅助扩展 /// public static partial class TouchRpcExtensions { /// /// 创建一个直接向目标地址请求的Rpc客户端。 /// /// /// public static IRpcClient CreateIDRpcClient(this TRpcActor client, string targetId) where TRpcActor : IRpcActor { return new IDRpcActor(targetId, client); } #region 批量传输(弃用) /// /// 批量拉取文件 /// /// 终端 /// 并行数量 /// 批量操作器 [Obsolete("此方法因为特殊原因已被弃用。")] public static void PullFiles(this IRpcActor client, int multipleCount, FileOperator[] fileOperators) { if (multipleCount < 1) { throw new Exception("并行数量不能小于1。"); } if (fileOperators is null) { throw new ArgumentNullException(nameof(fileOperators)); } int index = 0; int t = 0; int complatedLen = 0; List> results = new List>(); LoopAction loopAction = LoopAction.CreateLoopAction(-1, 100, (loop) => { int st = multipleCount - t; for (int i = 0; i < st; i++) { if (index == fileOperators.Length) { break; } Task result = client.PullFileAsync(fileOperators[index]); results.Add(result); index++; t++; } List> cr = new List>(); foreach (var item in results) { if (item.IsCompleted) { cr.Add(item); t--; complatedLen++; if (complatedLen == fileOperators.Length) { loop.Dispose(); } } } foreach (var item in cr) { results.Remove(item); } }); loopAction.Run(); } /// /// 异步批量拉取文件 /// /// 终端 /// 并行数量 /// 批量操作器 [Obsolete("此方法因为特殊原因已被弃用。")] public static void PullFilesAsync(this IRpcActor client, int multipleCount, FileOperator[] fileOperators) { if (multipleCount < 1) { throw new Exception("并行数量不能小于1。"); } if (fileOperators is null) { throw new ArgumentNullException(nameof(fileOperators)); } int index = 0; int t = 0; int complatedLen = 0; List> results = new List>(); LoopAction loopAction = LoopAction.CreateLoopAction(-1, 100, (loop) => { int st = multipleCount - t; for (int i = 0; i < st; i++) { if (index == fileOperators.Length) { break; } Task result = client.PullFileAsync(fileOperators[index]); results.Add(result); index++; t++; } List> cr = new List>(); foreach (var item in results) { if (item.IsCompleted) { cr.Add(item); t--; complatedLen++; if (complatedLen == fileOperators.Length) { loop.Dispose(); } } } foreach (var item in cr) { results.Remove(item); } }); loopAction.RunAsync(); } /// /// 批量推送文件 /// /// 终端 /// 并行数量 /// 批量操作器 [Obsolete("此方法因为特殊原因已被弃用。")] public static void PushFiles(this IRpcActor client, int multipleCount, FileOperator[] fileOperators) { if (multipleCount < 1) { throw new Exception("并行数量不能小于1。"); } if (fileOperators is null) { throw new ArgumentNullException(nameof(fileOperators)); } int index = 0; int t = 0; int complatedLen = 0; List> results = new List>(); LoopAction loopAction = LoopAction.CreateLoopAction(-1, 100, (loop) => { int st = multipleCount - t; for (int i = 0; i < st; i++) { if (index == fileOperators.Length) { break; } Task result = client.PushFileAsync(fileOperators[index]); results.Add(result); index++; t++; } List> cr = new List>(); foreach (var item in results) { if (item.IsCompleted) { cr.Add(item); t--; complatedLen++; if (complatedLen == fileOperators.Length) { loop.Dispose(); } } } foreach (var item in cr) { results.Remove(item); } }); loopAction.Run(); } /// /// 异步批量推送文件 /// /// 终端 /// 并行数量 /// 批量操作器 [Obsolete("此方法因为特殊原因已被弃用。")] public static void PushFilesAsync(this IRpcActor client, int multipleCount, FileOperator[] fileOperators) { if (multipleCount < 1) { throw new Exception("并行数量不能小于1。"); } if (fileOperators is null) { throw new ArgumentNullException(nameof(fileOperators)); } int index = 0; int t = 0; int complatedLen = 0; List> results = new List>(); LoopAction loopAction = LoopAction.CreateLoopAction(-1, 100, (loop) => { int st = multipleCount - t; for (int i = 0; i < st; i++) { if (index == fileOperators.Length) { break; } Task result = client.PushFileAsync(fileOperators[index]); results.Add(result); index++; t++; } List> cr = new List>(); foreach (var item in results) { if (item.IsCompleted) { cr.Add(item); t--; complatedLen++; if (complatedLen == fileOperators.Length) { loop.Dispose(); } } } foreach (var item in cr) { results.Remove(item); } }); loopAction.RunAsync(); } #endregion 批量传输(弃用) /// /// 转化Protocol协议标识为TouchRpc /// /// public static void SwitchProtocolToTouchRpc(this ITcpClientBase client) { client.SetDataHandlingAdapter(new FixedHeaderPackageAdapter()); client.Protocol = TouchRpcUtility.TouchRpcProtocol; } /// /// 转为ResultCode /// /// /// public static ResultCode ToResultCode(this ChannelStatus channelStatus) { switch (channelStatus) { case ChannelStatus.Default: return ResultCode.Default; case ChannelStatus.Overtime: return ResultCode.Overtime; case ChannelStatus.Cancel: return ResultCode.Canceled; case ChannelStatus.Completed: return ResultCode.Success; case ChannelStatus.Moving: case ChannelStatus.Disposed: default: return ResultCode.Error; } } } }