/// <inheritdoc/> public override bool AnalyseChannel(TChannelKey key, IReadOnlyList <byte> data, AnalyseResultHandler <TChannelKey, TResultType> analyseResultHandler) { if (key == null || data == null || analyseResultHandler == null) { return(false); } Channel <TChannelKey, byte> channel = GetChannel(key); if (channel == null) { return(false); } channel.AddRange(data); // 在数据中搜索 终止数据,并返回索引集合 // 0 - search index while (true) { int index = this.BoyerMoore.Search(channel.Cache, channel.Offset); if (index < 0) { break; } var bodySize = index - channel.Offset; if (bodySize <= 0) { break; } var bodyBytes = channel.GetRange(channel.Offset, bodySize); TResultType result = ConvertResultType(bodyBytes); bool handled = analyseResultHandler.Invoke(key, result); // 清除已处理了的数据 if (handled) { channel.RemoveRange(channel.Offset, bodySize + this.BoyerMoore.PatternLength); } else { channel.Offset = index + this.BoyerMoore.PatternLength; } } channel.CheckOverflow(); return(true); }
/// <inheritdoc/> public override bool AnalyseChannel(TChannelKey key, IReadOnlyList <byte> data, AnalyseResultHandler <TChannelKey, TResultType> analyseResultHandler) { if (key == null || data == null || analyseResultHandler == null) { return(false); } Channel <TChannelKey, byte> channel = GetChannel(key); if (channel == null) { return(false); } channel.AddRange(data); // start index - end index while (true) { int start = StartBoyerMoore.Search(channel.Cache, channel.Offset); if (start < 0) { break; } start += StartBoyerMoore.PatternLength; int end = EndBoyerMoore.Search(channel.Cache, start); if (end < 0) { break; } int bodySize = end - start; if (bodySize <= 0) { break; } var bodyBytes = channel.GetRange(start, bodySize); TResultType result = ConvertResultType(bodyBytes); bool handled = analyseResultHandler.Invoke(key, result); if (handled) { channel.RemoveRange(channel.Offset, bodySize + EndBoyerMoore.PatternLength); } else { channel.Offset = end + EndBoyerMoore.PatternLength; } } channel.CheckOverflow(); return(true); }
/// <inheritdoc/> public override bool AnalyseChannel(TChannelKey key, IReadOnlyList <byte> data, AnalyseResultHandler <TChannelKey, TResultType> analyseResultHandler) { if (key == null || data == null || analyseResultHandler == null) { return(false); } Channel <TChannelKey, byte> channel = GetChannel(key); if (channel == null) { return(false); } if (channel.MaxSize < PacketSize) { Console.WriteLine("Error:设定的最大缓存大小 {0}:{1} 小于 设定的包 {2}:{3} 大小", "Cache.MaxSize", channel.MaxSize, nameof(PacketSize), PacketSize); return(false); } // 添加数据到通道缓存 channel.AddRange(data); while (true) { if (channel.Available < PacketSize) { break; } var packetBytes = channel.GetRange(channel.Offset, PacketSize); TResultType result = ConvertResultType(packetBytes); bool handled = analyseResultHandler.Invoke(key, result); if (handled) { channel.RemoveRange(channel.Offset, PacketSize); } else { channel.Offset += PacketSize; } } channel.CheckOverflow(); return(true); }
/// <inheritdoc/> public override bool AnalyseChannel(TChannelKey key, IReadOnlyList <byte> data, AnalyseResultHandler <TChannelKey, TResultType> analyseResultHandler) { if (key == null || data == null || analyseResultHandler == null) { return(false); } Channel <TChannelKey, byte> channel = GetChannel(key); if (channel == null) { return(false); } if (MaxPacketSize > channel.MaxSize) { Console.WriteLine("Error:设定的最大包大小 {0}:{1} 超出,设定的最大通道缓存 Cache.MaxSize:{2} 大小", nameof(MaxPacketSize), MaxPacketSize, channel.MaxSize); return(false); } bool isDataError = false; channel.AddRange(data); while (true) { if (channel.Available < HeadSize) { break; } var headBytes = channel.GetRange(channel.Offset, HeadSize); int bodySize = GetBodySize(headBytes); if (HeadSize + bodySize > MaxPacketSize || bodySize <= 0) { isDataError = true; Console.WriteLine("Error:当前包大小 {0}, 超出设定的最大包大小 {1}", HeadSize + bodySize, MaxPacketSize); break; } if (channel.Available - HeadSize < bodySize) { break; } var bodyBytes = channel.GetRange(channel.Offset + HeadSize, bodySize); TResultType result = ConvertResultType(headBytes, bodyBytes); bool handled = analyseResultHandler.Invoke(key, result); if (handled) { channel.RemoveRange(channel.Offset, HeadSize + bodySize); } else { channel.Offset += HeadSize + bodySize; } } channel.CheckOverflow(); return(!isDataError); }
/// <summary> /// 分析指定通道的数据 /// <para>考虑到如果在线程中运行,不建议抛出异常信息,直接返回 bool 值,参数错误 或 数据错误 返回 false</para> /// </summary> /// <param name="key">通道的唯一标识键,不能为 null 值,或无效引用</param> /// <param name="data">需要分析或缓存的数据</param> /// <param name="analyseResultHandler">分析产生结果时的代理调用</param> /// <exception cref="ArgumentNullException">参数不能为空</exception> /// <returns> 返回数据分析状态 </returns> public virtual bool AnalyseChannel(TChannelKey key, IReadOnlyList <TDataType> data, AnalyseResultHandler <TChannelKey, TResultType> analyseResultHandler) { // 1. 数据分析过程在这里实现 // 3. 转换完成后回调 analyseResultHandler return(false); }