//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在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 TouchSocket.Core; namespace TouchSocket.Sockets { /// /// 区间数据包处理适配器,支持以任意字符、字节数组起始与结尾的数据包。 /// public abstract class CustomBetweenAndDataHandlingAdapter : CustomDataHandlingAdapter where TBetweenAndRequestInfo : class, IBetweenAndRequestInfo { /// /// 起始字符,不可以为null,可以为0长度 /// public abstract byte[] StartCode { get; } /// /// 即使找到了终止因子,也不会结束,默认0 /// public int MinSize { get; set; } /// /// 结束字符,不可以为null,不可以为0长度,必须具有有效值。 /// public abstract byte[] EndCode { get; } /// /// 筛选解析数据。实例化的TRequest会一直保存,直至解析成功,或手动清除。 /// 当不满足解析条件时,请返回,此时会保存的数据 /// 当数据部分异常时,请移动到指定位置,然后返回 /// 当完全满足解析条件时,请返回最后将移至指定位置。 /// /// 字节块 /// 是否为上次遗留对象,当该参数为True时,request也将是上次实例化的对象。 /// 对象。 /// 缓存容量。当需要首次缓存时,指示申请的ByteBlock的容量。合理的值可避免ByteBlock扩容带来的性能消耗。 /// protected override FilterResult Filter(ByteBlock byteBlock, bool beCached, ref TBetweenAndRequestInfo request, ref int tempCapacity) { if (beCached) { int len; int pos = byteBlock.Pos; while (true) { int indexEnd = byteBlock.Buffer.IndexOfFirst(byteBlock.Pos, byteBlock.CanReadLen, this.EndCode); if (indexEnd == -1) { byteBlock.Pos = pos; return FilterResult.Cache; } len = indexEnd - this.EndCode.Length - pos + 1; if (len >= this.MinSize) { break; } byteBlock.Pos += len + this.EndCode.Length; } byteBlock.Pos = pos; request.OnParsingBody(byteBlock.ToArray(pos, len)); byteBlock.Pos += len; if (request.OnParsingEndCode(byteBlock.ToArray(byteBlock.Pos, this.EndCode.Length))) { byteBlock.Pos += this.EndCode.Length; return FilterResult.Success; } else { byteBlock.Pos += this.EndCode.Length; return FilterResult.GoOn; } } else { TBetweenAndRequestInfo requestInfo = this.GetInstance(); int indexStart = byteBlock.Buffer.IndexOfFirst(byteBlock.Pos, byteBlock.CanReadLen, this.StartCode); if (indexStart == -1) { return FilterResult.Cache; } if (!requestInfo.OnParsingStartCode(byteBlock.ToArray(byteBlock.Pos, this.StartCode.Length))) { byteBlock.Pos += this.StartCode.Length; return FilterResult.GoOn; } byteBlock.Pos += this.StartCode.Length; request = requestInfo; int len; int pos = byteBlock.Pos; while (true) { int indexEnd = byteBlock.Buffer.IndexOfFirst(byteBlock.Pos, byteBlock.CanReadLen, this.EndCode); if (indexEnd == -1) { byteBlock.Pos = pos; return FilterResult.Cache; } len = indexEnd - this.EndCode.Length - pos + 1; if (len >= this.MinSize) { break; } byteBlock.Pos += len + this.EndCode.Length; } byteBlock.Pos = pos; request.OnParsingBody(byteBlock.ToArray(pos, len)); byteBlock.Pos += len; if (request.OnParsingEndCode(byteBlock.ToArray(byteBlock.Pos, this.EndCode.Length))) { byteBlock.Pos += this.EndCode.Length; return FilterResult.Success; } else { byteBlock.Pos += this.EndCode.Length; return FilterResult.GoOn; } } } /// /// 获取泛型实例。 /// /// protected abstract TBetweenAndRequestInfo GetInstance(); } /// /// 区间类型的适配器数据模型接口。 /// public interface IBetweenAndRequestInfo : IRequestInfo { /// /// 当解析到起始字符时。 /// /// /// bool OnParsingStartCode(byte[] startCode); /// /// 当解析数据体。 /// 在此方法中,您必须手动保存Body内容 /// /// void OnParsingBody(byte[] body); /// /// 当解析到起始字符时。 /// /// /// bool OnParsingEndCode(byte[] endCode); } }