//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在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 TouchSocket.Core; namespace TouchSocket.Sockets { /// /// 数据处理适配器 /// public abstract class DataHandlingAdapter : DisposableObject { /// /// 最后缓存的时间 /// protected DateTime LastCacheTime { get; set; } /// /// 是否在收到数据时,即刷新缓存时间。默认true。 /// /// 当设为true时,将弱化的作用,只要一直有数据,则缓存不会过期。 /// 当设为false时,则在的时效内。必须完成单个缓存的数据。 /// /// public bool UpdateCacheTimeWhenRev { get; set; } = true; /// /// 缓存超时时间。默认1秒。 /// public TimeSpan CacheTimeout { get; set; } = TimeSpan.FromSeconds(1); /// /// 是否启用缓存超时。默认true。 /// public bool CacheTimeoutEnable { get; set; } = true; /// /// 当插件在被第一次加载时调用。 /// /// /// 此适配器已被其他终端使用,请重新创建对象。 public virtual void OnLoaded(ITcpClientBase client) { if (this.Client != null) { throw new Exception("此适配器已被其他终端使用,请重新创建对象。"); } this.Client = client; } /// /// 拼接发送 /// public abstract bool CanSplicingSend { get; } /// /// 是否允许发送对象。 /// public abstract bool CanSendRequestInfo { get; } /// /// 获取或设置适配器能接收的最大数据包长度。 /// public int MaxPackageSize { get; set; } = 1024 * 1024 * 10; /// /// 适配器拥有者。 /// public ITcpClientBase Client { get; private set; } /// /// 当接收数据处理完成后,回调该函数执行接收 /// public Action ReceivedCallBack { get; set; } /// /// 当接收数据处理完成后,回调该函数执行发送 /// public Action SendCallBack { get; set; } /// /// 收到数据的切入点,该方法由框架自动调用。 /// /// public void ReceivedInput(ByteBlock byteBlock) { try { this.PreviewReceived(byteBlock); } catch (Exception ex) { this.OnError(ex.Message); } } /// /// 发送数据的切入点,该方法由框架自动调用。 /// /// /// /// public void SendInput(byte[] buffer, int offset, int length) { this.PreviewSend(buffer, offset, length); } /// /// 发送数据的切入点,该方法由框架自动调用。 /// /// public void SendInput(IList> transferBytes) { this.PreviewSend(transferBytes); } /// /// 发送数据的切入点,该方法由框架自动调用。 /// /// public void SendInput(IRequestInfo requestInfo) { this.PreviewSend(requestInfo); } /// /// 处理已经经过预先处理后的数据 /// /// 以二进制形式传递 /// 以解析实例传递 protected void GoReceived(ByteBlock byteBlock, IRequestInfo requestInfo) { this.ReceivedCallBack.Invoke(byteBlock, requestInfo); } /// /// 发送已经经过预先处理后的数据 /// /// /// /// protected void GoSend(byte[] buffer, int offset, int length) { this.SendCallBack.Invoke(buffer, offset, length); } /// /// 在解析时发生错误。 /// /// 错误异常 /// 是否调用 /// 是否记录日志 protected virtual void OnError(string error, bool reset = true, bool log = true) { if (reset) { this.Reset(); } if (log && this.Client != null && this.Client.Logger != null) { this.Client.Logger.Error(error); } } /// /// 当接收到数据后预先处理数据,然后调用处理数据 /// /// protected abstract void PreviewReceived(ByteBlock byteBlock); /// /// 当发送数据前预先处理数据 /// /// 数据 /// 偏移 /// 长度 protected abstract void PreviewSend(byte[] buffer, int offset, int length); /// /// 当发送数据前预先处理数据 /// /// protected abstract void PreviewSend(IRequestInfo requestInfo); /// /// 组合发送预处理数据, /// 当属性SplicingSend实现为True时,系统才会调用该方法。 /// /// 代发送数据组合 protected abstract void PreviewSend(IList> transferBytes); /// /// 重置解析器到初始状态,一般在被触发时,由返回值指示是否调用。 /// protected virtual void Reset() { this.LastCacheTime = DateTime.Now; } /// /// 该方法被触发时,一般说明已经断开连接。 /// /// protected override void Dispose(bool disposing) { base.Dispose(disposing); } } }