//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在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.Threading; using System.Threading.Tasks; using TouchSocket.Core; using TouchSocket.Resources; namespace TouchSocket.Sockets { internal class WaitingClient : DisposableObject, IWaitingClient where TClient : IClient, IDefaultSender, ISender { private readonly WaitData m_waitData; private volatile bool breaked; public WaitingClient(TClient client, WaitingOptions waitingOptions) { Client = client ?? throw new ArgumentNullException(nameof(client)); m_waitData = new WaitData(); WaitingOptions = waitingOptions; } public bool CanSend { get { if (Client is ITcpClientBase tcpClient) { return tcpClient.Online; } else if (Client is IUdpSession) { return true; } else { return false; } } } public TClient Client { get; private set; } public WaitingOptions WaitingOptions { get; set; } /// /// 发送字节流 /// /// 数据缓存区 /// 偏移 /// 长度 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public ResponsedData SendThenResponse(byte[] buffer, int offset, int length, int timeout = 1000 * 5, CancellationToken token = default) { lock (this) { try { breaked = false; m_waitData.Reset(); if (WaitingOptions.BreakTrigger && this.Client is ITcpClientBase tcpClient) { tcpClient.Disconnected += this.OnDisconnected; } if (WaitingOptions.AdapterFilter == AdapterFilter.AllAdapter || WaitingOptions.AdapterFilter == AdapterFilter.WaitAdapter) { Client.OnHandleReceivedData += OnHandleReceivedData; } else { Client.OnHandleRawBuffer += OnHandleRawBuffer; } if (WaitingOptions.RemoteIPHost != null && Client is IUdpSession session) { if (WaitingOptions.AdapterFilter == AdapterFilter.AllAdapter || WaitingOptions.AdapterFilter == AdapterFilter.SendAdapter) { session.Send(WaitingOptions.RemoteIPHost.EndPoint, buffer, offset, length); } else { session.DefaultSend(WaitingOptions.RemoteIPHost.EndPoint, buffer, offset, length); } } else { if (WaitingOptions.AdapterFilter == AdapterFilter.AllAdapter || WaitingOptions.AdapterFilter == AdapterFilter.SendAdapter) { Client.Send(buffer, offset, length); } else { Client.DefaultSend(buffer, offset, length); } } m_waitData.SetCancellationToken(token); switch (m_waitData.Wait(timeout)) { case WaitDataStatus.SetRunning: return m_waitData.WaitResult; case WaitDataStatus.Overtime: throw new TimeoutException(); case WaitDataStatus.Canceled: { if (this.WaitingOptions.ThrowBreakException && this.breaked) { throw new Exception("等待已终止。可能是客户端已掉线,或者被注销。"); } return default; } case WaitDataStatus.Default: case WaitDataStatus.Disposed: default: throw new Exception(TouchSocketStatus.UnknownError.GetDescription()); } } finally { if (this.WaitingOptions.BreakTrigger && this.Client is ITcpClientBase tcpClient) { tcpClient.Disconnected -= this.OnDisconnected; } if (WaitingOptions.AdapterFilter == AdapterFilter.AllAdapter || WaitingOptions.AdapterFilter == AdapterFilter.WaitAdapter) { Client.OnHandleReceivedData -= OnHandleReceivedData; } else { Client.OnHandleRawBuffer -= OnHandleRawBuffer; } } } } /// /// 发送字节流 /// /// 数据缓存区 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public ResponsedData SendThenResponse(byte[] buffer, int timeout = 1000 * 5, CancellationToken token = default) { return SendThenResponse(buffer, 0, buffer.Length, timeout, token); } /// /// 发送流中的有效数据 /// /// 数据块载体 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public ResponsedData SendThenResponse(ByteBlock byteBlock, int timeout = 1000 * 5, CancellationToken token = default) { return SendThenResponse(byteBlock.Buffer, 0, byteBlock.Len, timeout, token); } /// /// 异步发送 /// /// 数据缓存区 /// 偏移 /// 长度 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public Task SendThenResponseAsync(byte[] buffer, int offset, int length, int timeout = 1000 * 5, CancellationToken token = default) { return EasyTask.Run(() => { return SendThenResponse(buffer, offset, length, timeout, token); }); } /// /// 异步发送 /// /// 数据缓存区 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public Task SendThenResponseAsync(byte[] buffer, int timeout = 1000 * 5, CancellationToken token = default) { return EasyTask.Run(() => { return SendThenResponse(buffer, timeout, token); }); } /// /// 异步发送 /// /// 数据块载体 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public Task SendThenResponseAsync(ByteBlock byteBlock, int timeout = 1000 * 5, CancellationToken token = default) { return EasyTask.Run(() => { return SendThenResponse(byteBlock, timeout, token); }); } /// /// 发送字节流 /// /// 数据缓存区 /// 偏移 /// 长度 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public byte[] SendThenReturn(byte[] buffer, int offset, int length, int timeout = 1000 * 5, CancellationToken token = default) { return SendThenResponse(buffer, offset, length, timeout, token).Data; } /// /// 发送字节流 /// /// 数据缓存区 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public byte[] SendThenReturn(byte[] buffer, int timeout = 1000 * 5, CancellationToken token = default) { return SendThenReturn(buffer, 0, buffer.Length, timeout, token); } /// /// 发送流中的有效数据 /// /// 数据块载体 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public byte[] SendThenReturn(ByteBlock byteBlock, int timeout = 1000 * 5, CancellationToken token = default) { return SendThenReturn(byteBlock.Buffer, 0, byteBlock.Len, timeout, token); } /// /// 异步发送 /// /// 数据缓存区 /// 偏移 /// 长度 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public Task SendThenReturnAsync(byte[] buffer, int offset, int length, int timeout = 1000 * 5, CancellationToken token = default) { return EasyTask.Run(() => { return SendThenReturn(buffer, offset, length, timeout, token); }); } /// /// 异步发送 /// /// 数据缓存区 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public Task SendThenReturnAsync(byte[] buffer, int timeout = 1000 * 5, CancellationToken token = default) { return EasyTask.Run(() => { return SendThenReturn(buffer, timeout, token); }); } /// /// 异步发送 /// /// 数据块载体 /// 超时时间 /// 取消令箭 /// 客户端没有连接 /// 发送数据超长 /// 其他异常 /// 返回的数据 public Task SendThenReturnAsync(ByteBlock byteBlock, int timeout = 1000 * 5, CancellationToken token = default) { return EasyTask.Run(() => { return SendThenReturn(byteBlock, timeout, token); }); } protected override void Dispose(bool disposing) { this.Client = default; this.m_waitData.SafeDispose(); base.Dispose(disposing); } private void OnDisconnected(ITcpClientBase client, DisconnectEventArgs e) { breaked = true; this.m_waitData.Cancel(); } private bool OnHandleRawBuffer(ByteBlock byteBlock) { ResponsedData responsedData = new ResponsedData(byteBlock.ToArray(), null); return !m_waitData.Set(responsedData); } /// /// /// /// /// private bool OnHandleReceivedData(ByteBlock byteBlock, IRequestInfo requestInfo) { ResponsedData responsedData; if (byteBlock != null) { responsedData = new ResponsedData(byteBlock.ToArray(), requestInfo); } else { responsedData = new ResponsedData(null, requestInfo); } return !m_waitData.Set(responsedData); } } }