/// <summary> /// Adds the array into BufferSegments. /// </summary> /// <param name="buffer">The buffer which will be added into BufferSegments.</param> /// <param name="offset">The offset.</param> /// <param name="length">The length.</param> /// <param name="isReusableBuffer">if set to <c>true</c> [is reusable buffer].</param> protected void AddArraySegment(byte[] buffer, int offset, int length, bool isReusableBuffer) { if (isReusableBuffer) { BufferSegments.AddSegment(new ArraySegment <byte>(buffer.CloneRange(offset, length))); } else { BufferSegments.AddSegment(new ArraySegment <byte>(buffer, offset, length)); } }
protected bool FindCommandInfoDirectly(byte[] readBuffer, int offset, int length, bool isReusableBuffer, out string command, out int left) { left = 0; ArraySegment <byte> currentSegment; if (isReusableBuffer) { //Next received data also will be saved in this buffer, so we should create a new byte[] to persistent current received data currentSegment = new ArraySegment <byte>(readBuffer.CloneRange(offset, length)); } else { currentSegment = new ArraySegment <byte>(readBuffer, offset, length); } BufferSegments.AddSegment(currentSegment); int?result = BufferSegments.SearchMark(Terminator); if (!result.HasValue) { command = string.Empty; return(false); } if (result.Value < 0) { command = string.Empty; return(false); } int findLen = result.Value + Terminator.Length; int total = BufferSegments.Count; command = Encoding.GetString(BufferSegments.ToArrayData(0, result.Value)); ClearBufferSegments(); if (findLen < total) { left = total - findLen; } return(true); }
/// <summary> /// Adds the array segment into BufferSegment. /// </summary> /// <param name="buffer">The buffer.</param> /// <param name="offset">The offset.</param> /// <param name="length">The length.</param> protected void AddArraySegment(byte[] buffer, int offset, int length) { BufferSegments.AddSegment(buffer, offset, length, true); }