#region copyright //------------------------------------------------------------------------------ // 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充 // 此代码版权(除特别声明外的代码)归作者本人Diego所有 // 源代码使用协议遵循本仓库的开源协议及附加协议 // Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway // Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway // 使用文档:https://diego2098.gitee.io/thingsgateway-docs/ // QQ群:605534569 //------------------------------------------------------------------------------ #endregion //------------------------------------------------------------------------------ // 此代码版权(除特别声明或在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首页:http://rrqm_home.gitee.io/touchsocket/ // 交流QQ群:234762506 // 感谢您的下载和使用 //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ using System.Net; using System.Text; namespace ThingsGateway.Foundation.Sockets { /// /// SenderExtension /// public static class SenderExtension { #region ISend /// /// 同步发送数据。 /// /// /// /// public static void Send(this TClient client, byte[] buffer) where TClient : ISender { client.Send(buffer, 0, buffer.Length); } /// /// 同步发送数据。 /// /// /// /// public static void Send(this TClient client, ByteBlock byteBlock) where TClient : ISender { client.Send(byteBlock.Buffer, 0, byteBlock.Len); } /// /// 以UTF-8的编码同步发送字符串。 /// /// /// /// public static void Send(this TClient client, string value) where TClient : ISender { client.Send(Encoding.UTF8.GetBytes(value)); } /// /// 异步发送数据。 /// /// /// /// public static Task SendAsync(this TClient client, byte[] buffer) where TClient : ISender { return client.SendAsync(buffer, 0, buffer.Length); } /// /// 以UTF-8的编码异步发送字符串。 /// /// /// /// public static Task SendAsync(this TClient client, string value) where TClient : ISender { return client.SendAsync(Encoding.UTF8.GetBytes(value)); } #endregion ISend #region IDefaultSender /// /// 以UTF-8的编码同步发送字符串。 /// /// /// /// public static void DefaultSend(this TClient client, string value) where TClient : IDefaultSender { client.DefaultSend(Encoding.UTF8.GetBytes(value)); } /// /// 同步发送数据。 /// /// /// /// public static void DefaultSend(this TClient client, byte[] buffer) where TClient : IDefaultSender { client.DefaultSend(buffer, 0, buffer.Length); } /// /// 同步发送数据。 /// /// /// /// public static void DefaultSend(this TClient client, ByteBlock byteBlock) where TClient : IDefaultSender { client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len); } /// /// 以UTF-8的编码异步发送字符串。 /// /// /// /// public static Task DefaultSendAsync(this TClient client, string value) where TClient : IDefaultSender { return client.DefaultSendAsync(Encoding.UTF8.GetBytes(value)); } /// /// 异步发送数据。 /// /// /// /// public static Task DefaultSendAsync(this TClient client, byte[] buffer) where TClient : IDefaultSender { return client.DefaultSendAsync(buffer, 0, buffer.Length); } #endregion IDefaultSender #region IIdSender /// /// 以UTF-8的编码同步发送字符串。 /// /// /// /// /// public static void Send(this TClient client, string id, string value) where TClient : IIdSender { client.Send(id, Encoding.UTF8.GetBytes(value)); } /// /// 同步发送数据。 /// /// /// /// /// public static void Send(this TClient client, string id, byte[] buffer) where TClient : IIdSender { client.Send(id, buffer, 0, buffer.Length); } /// /// 同步发送数据。 /// /// /// /// /// public static void Send(this TClient client, string id, ByteBlock byteBlock) where TClient : IIdSender { client.Send(id, byteBlock.Buffer, 0, byteBlock.Len); } /// /// 以UTF-8的编码异步发送字符串。 /// /// /// /// /// public static Task SendAsync(this TClient client, string id, string value) where TClient : IIdSender { return client.SendAsync(id, Encoding.UTF8.GetBytes(value)); } /// /// 异步发送数据。 /// /// /// /// /// public static Task SendAsync(this TClient client, string id, byte[] buffer) where TClient : IIdSender { return client.SendAsync(id, buffer, 0, buffer.Length); } #endregion IIdSender #region IUdpDefaultSender /// /// 以UTF-8的编码同步发送字符串。 /// /// /// /// /// public static void DefaultSend(this TClient client, EndPoint endPoint, string value) where TClient : IUdpDefaultSender { if (value is null) { throw new ArgumentNullException(nameof(value)); } client.DefaultSend(endPoint, Encoding.UTF8.GetBytes(value)); } /// /// 绕过适配器,直接发送字节流 /// /// /// 目的终结点 /// 数据区 /// 发送数据超长 /// 其他异常 public static void DefaultSend(this TClient client, EndPoint endPoint, byte[] buffer) where TClient : IUdpDefaultSender { client.DefaultSend(endPoint, buffer, 0, buffer.Length); } /// /// 以UTF-8的编码异步发送字符串。 /// /// /// /// /// public static Task DefaultSendAsync(this TClient client, EndPoint endPoint, string value) where TClient : IUdpDefaultSender { return client.DefaultSendAsync(endPoint, Encoding.UTF8.GetBytes(value)); } /// /// 绕过适配器,直接发送字节流 /// /// /// 目的终结点 /// 数据缓存区 /// 发送数据超长 /// 其他异常 public static Task DefaultSendAsync(this TClient client, EndPoint endPoint, byte[] buffer) where TClient : IUdpDefaultSender { return client.DefaultSendAsync(endPoint, buffer, 0, buffer.Length); } /// /// 绕过适配器,直接发送字节流 /// /// /// 目的终结点 /// /// 发送数据超长 /// 其他异常 public static Task DefaultSendAsync(this TClient client, EndPoint endPoint, ByteBlock byteBlock) where TClient : IUdpDefaultSender { return client.DefaultSendAsync(endPoint, byteBlock.Buffer, 0, byteBlock.Len); } #endregion IUdpDefaultSender #region IUdpClientSender /// /// 以UTF-8的编码同步发送字符串。 /// /// /// /// /// public static void Send(this TClient client, EndPoint endPoint, string value) where TClient : IUdpClientSender { client.Send(endPoint, Encoding.UTF8.GetBytes(value)); } /// /// 发送字节流 /// /// /// 目的终结点 /// 数据区 /// 发送数据超长 /// 其他异常 public static void Send(this TClient client, EndPoint endPoint, byte[] buffer) where TClient : IUdpClientSender { client.Send(endPoint, buffer, 0, buffer.Length); } /// /// 发送字节流 /// /// /// 目的终结点 /// 数据区 /// 发送数据超长 /// 其他异常 public static void Send(this TClient client, EndPoint endPoint, ByteBlock byteBlock) where TClient : IUdpClientSender { client.Send(endPoint, byteBlock.Buffer, 0, byteBlock.Len); } /// /// 以UTF-8的编码异步发送字符串。 /// /// /// /// /// public static Task SendAsync(this TClient client, EndPoint endPoint, string value) where TClient : IUdpClientSender { return client.SendAsync(endPoint, Encoding.UTF8.GetBytes(value)); } /// /// 发送字节流 /// /// /// 目的终结点 /// 数据缓存区 /// 发送数据超长 /// 其他异常 public static Task SendAsync(this TClient client, EndPoint endPoint, byte[] buffer) where TClient : IUdpClientSender { return client.SendAsync(endPoint, buffer, 0, buffer.Length); } #endregion IUdpClientSender #region IWaitSender /// /// 发送字节流 /// /// /// /// /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public static byte[] SendThenReturn(this IWaitSender client, string msg, int timeout = 5000) { using (var tokenSource = new CancellationTokenSource(timeout)) { return client.SendThenReturn(Encoding.UTF8.GetBytes(msg), tokenSource.Token); } } /// /// 发送字节流 /// /// /// 数据缓存区 /// /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public static byte[] SendThenReturn(this IWaitSender client, byte[] buffer, int timeout = 5000) { using (var tokenSource = new CancellationTokenSource(timeout)) { return client.SendThenReturn(buffer, 0, buffer.Length, tokenSource.Token); } } /// /// 发送流中的有效数据 /// /// /// 数据块载体 /// /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public static byte[] SendThenReturn(this IWaitSender client, ByteBlock byteBlock, int timeout = 5000) { using (var tokenSource = new CancellationTokenSource(timeout)) { return client.SendThenReturn(byteBlock.Buffer, 0, byteBlock.Len, tokenSource.Token); } } /// /// 异步发送 /// /// /// 数据缓存区 /// /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public static Task SendThenReturnAsync(this IWaitSender client, byte[] buffer, int timeout = 5000) { using (var tokenSource = new CancellationTokenSource(timeout)) { return client.SendThenReturnAsync(buffer, 0, buffer.Length, tokenSource.Token); } } /// /// 异步发送 /// /// /// /// /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public static Task SendThenReturnAsync(this IWaitSender client, string msg, int timeout = 5000) { using (var tokenSource = new CancellationTokenSource(timeout)) { return client.SendThenReturnAsync(Encoding.UTF8.GetBytes(msg), tokenSource.Token); } } /// /// 发送字节流 /// /// /// 数据缓存区 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public static byte[] SendThenReturn(this IWaitSender client, byte[] buffer, CancellationToken token) { return client.SendThenReturn(buffer, 0, buffer.Length, token); } /// /// 发送流中的有效数据 /// /// /// 数据块载体 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public static byte[] SendThenReturn(this IWaitSender client, ByteBlock byteBlock, CancellationToken token) { return client.SendThenReturn(byteBlock.Buffer, 0, byteBlock.Len, token); } /// /// 异步发送 /// /// /// 数据缓存区 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public static Task SendThenReturnAsync(this IWaitSender client, byte[] buffer, CancellationToken token) { return client.SendThenReturnAsync(buffer, 0, buffer.Length, token); } #endregion } }