//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在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.Net; using System.Text; using System.Threading.Tasks; using TouchSocket.Core; namespace TouchSocket.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 } }