/// <summary> /// 多数据段处理 /// </summary> /// <param name="baizeSession">会话</param> /// <param name="sequence">数据</param> /// <returns></returns> private bool MultiSegmentHandler(BaizeSession baizeSession, ReadOnlySequence <byte> sequence) { bool rtn = false; int size = this._filterInfo.BasePortocalFilterInfo.Size; BaizeBufferSegment tmpHead = null, tmpTail = null; int tmpHeadIndex = 0; int tmpTailIndex = 0; if (sequence.First.Length >= size) { ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(sequence.First.Slice(0, size)); baizeSession.Data = readOnly; } else { int length = 0; foreach (var memory in sequence) { if (tmpHead == null) { tmpHead = tmpTail = new BaizeBufferSegment(); tmpTail.SetUnownedMemory(memory); } else { BaizeBufferSegment next = new BaizeBufferSegment(); next.SetUnownedMemory(memory); tmpTail.SetNext(next); tmpTail = next; } if (length + memory.Length >= size) { tmpTailIndex = size - length; rtn = true; break; } length += memory.Length; } if (rtn) { ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(tmpHead, tmpHeadIndex, tmpTail, tmpTailIndex); baizeSession.Data = readOnly; } else { if (tmpHead != null) { tmpHead.ResetMemory(); tmpHead = null; } } } return(rtn); }
/// <summary> /// 获得多段的包的长度 /// </summary> /// <param name="sequence"></param> /// <returns></returns> private int GetMultiSegmentPacketLength(ReadOnlySequence <byte> sequence) { int rtn = 0; int length = 0; BaizeBufferSegment tmpHead = null, tmpTail = null; int tmpHeadIndex = 0; int tmpTailIndex = 0; bool complete = false; foreach (var memory in sequence) { if (tmpHead == null) { tmpHead = tmpTail = new BaizeBufferSegment(); tmpTail.SetUnownedMemory(memory); } else { BaizeBufferSegment next = new BaizeBufferSegment(); next.SetUnownedMemory(memory); tmpTail.SetNext(next); tmpTail = next; } if (length + memory.Length >= this._filterInfo.BasePortocalFilterInfo.Size) { tmpTailIndex = this._filterInfo.BasePortocalFilterInfo.Size - length; complete = true; break; } length += memory.Length; } if (complete) { ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(tmpHead, tmpHeadIndex, tmpTail, tmpTailIndex); if (readOnly.First.Length <= this._filterInfo.BasePortocalFilterInfo.Size) { rtn = GetMultSegmentPacketLength(readOnly); } else { rtn = GetPacketLength(readOnly); } } else { if (tmpHead != null) { tmpHead.ResetMemory(); tmpHead = null; } } return(rtn); }
/// <summary> /// 多数据段处理 /// </summary> /// <param name="baizeSession">会话</param> /// <param name="sequence">数据</param> /// <returns></returns> private bool MultiSegmentHandler(BaizeSession baizeSession, ReadOnlySequence <byte> sequence) { bool rtn = false; BaizeBufferSegment tmpHead = null, tmpTail = null; int tmpHeadIndex = 0; int tmpTailIndex = 0; foreach (var memory in sequence) { if (tmpHead == null) { tmpHead = tmpTail = new BaizeBufferSegment(); tmpTail.SetUnownedMemory(memory); } else { BaizeBufferSegment next = new BaizeBufferSegment(); next.SetUnownedMemory(memory); tmpTail.SetNext(next); tmpTail = next; } int terminatorIndex = memory.Span.IndexOf(_terminatorMark.Span); if (terminatorIndex != -1) { tmpTailIndex = terminatorIndex; rtn = true; break; } } if (rtn) { ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(tmpHead, tmpHeadIndex, tmpTail, tmpTailIndex + _terminatorMark.Length); baizeSession.Data = readOnly; } else { if (tmpHead != null) { tmpHead.ResetMemory(); tmpHead = null; } } return(rtn); }
/// <summary> /// 填充数据 /// </summary> /// <param name="data">填充数据</param> public void FillData(ReadOnlySequence <byte> data) { _bufferDataCount += data.Length; foreach (var memory in data) { if (_head == null) { _head = _tail = new BaizeBufferSegment(); _tail.SetUnownedMemory(memory); } else { BaizeBufferSegment next = new BaizeBufferSegment(); next.SetUnownedMemory(memory); _tail.SetNext(next); _tail = next; } } }
/// <summary> /// 多数据段处理 /// </summary> /// <param name="baizeSession">会话</param> /// <param name="sequence">数据</param> /// <returns></returns> private bool MultiSegmentHandler(BaizeSession baizeSession, ReadOnlySequence <byte> sequence) { bool rtn = false; BaizeBufferSegment tmpHead = null, tmpTail = null; int tmpHeadIndex = 0; int tmpTailIndex = 0; bool foundBeginMark = false; foreach (var memory in sequence) { bool addSegment = false; if (!foundBeginMark) { int beginIndex = memory.Span.IndexOf(_beginMark.Span); if (beginIndex != -1) { foundBeginMark = true; tmpHeadIndex = beginIndex; tmpHead = tmpTail = new BaizeBufferSegment(); tmpTail.SetUnownedMemory(memory); addSegment = true; } } if (foundBeginMark && !addSegment) { BaizeBufferSegment next = new BaizeBufferSegment(); next.SetUnownedMemory(memory); tmpTail.SetNext(next); tmpTail = next; } int endIndex = -1; if (addSegment) { endIndex = memory.Span.Slice(tmpHeadIndex + _beginMark.Length).IndexOf(_endMark.Span); if (endIndex != -1) { endIndex += _beginMark.Length + tmpHeadIndex; tmpTailIndex = endIndex; rtn = true; break; } } else { endIndex = memory.Span.IndexOf(_endMark.Span); if (endIndex != -1) { tmpTailIndex = endIndex; rtn = true; break; } } } if (rtn) { ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(tmpHead, tmpHeadIndex, tmpTail, tmpTailIndex + _endMark.Length); baizeSession.Data = readOnly; } else { if (tmpHead != null) { tmpHead.ResetMemory(); tmpHead = null; } } return(rtn); }
/// <summary> /// 重置 /// </summary> public void Reset() { _head.ResetMemory(); _head = null; _bufferDataCount = 0; }