unsafe static bool YieldMessage(SlidingBuffer buffer, char *contentPtr, int endOfBodyPosition, List <MessageInfo> messagesList, ref int currentMessageIndex) { if (buffer.CurrentMessageHeader != null) { var endOfHeaderPos = buffer.CurrentMessageHeader.EndOfHeaderPosition; var bodyLen = TrimBodyRight(contentPtr, endOfBodyPosition, endOfHeaderPos) - endOfHeaderPos; string messageBody = new string(contentPtr, buffer.CurrentMessageHeader.EndOfHeaderPosition, bodyLen ); var h = buffer.CurrentMessageHeader; messagesList.Add(new MessageInfo() { HeaderMatch = h.Match2, MessageIndex = currentMessageIndex++, StreamPosition = h.StreamPosition, MessageBoby = messageBody, Buffer = h.Buffer }); buffer.CurrentMessageHeader = null; } return(true); }
public void Add_AddsOneByte_Added() { var buffer = new SlidingBuffer(3); buffer.Add(1); Assert.AreEqual(1, buffer[1]); }
public void SetUp() { buffer = new SlidingBuffer <int>(3); buffer.Put(1); buffer.Put(2); buffer.Put(3); timeout = new TimeSpan(50); }
async Task ITextLogParser.ParseStream( Stream inputStream, IHeaderMatcher headerMatcher, Func <List <MessageInfo>, Task <bool> > messagesSink, TextLogParserOptions options ) { inputStream.Position = 0; var totalLen = inputStream.Length; var progressHandler = options.ProgressHandler; if (totalLen == 0) { progressHandler = null; } int bufferUnderflowThreshold = 1024 * 4; byte[] rawBytesBuffer = new byte[options.RawBufferSize]; char[] rawCharsBuffer = new char[options.RawBufferSize]; var messages = new List <MessageInfo>(5000); var buffer = new SlidingBuffer(); int currentMessageIndex = 0; if ((options.Flags & TextLogParserFlags.SkipDoubleBytePeamble) != 0) { await inputStream.ReadAsync(rawBytesBuffer, 0, 2); } for (; ;) { int bytesRead = await inputStream.ReadAsync(rawBytesBuffer, 0, options.RawBufferSize); if (bytesRead == 0) { break; } int charsCount = ((options.Flags & TextLogParserFlags.UCS2) == 0) ? GetChars(rawBytesBuffer, bytesRead, rawCharsBuffer) : Encoding.Unicode.GetChars(rawBytesBuffer, 0, bytesRead, rawCharsBuffer, 0); buffer.Push(rawCharsBuffer, charsCount); int endOfProcessedTextPosition = HandleBufferContent(headerMatcher, buffer, messages, bufferUnderflowThreshold, ref currentMessageIndex, options.Flags); buffer.Pop(endOfProcessedTextPosition); if (!await messagesSink(messages)) { break; } messages.Clear(); progressHandler?.Invoke((double)inputStream.Position / (double)totalLen); } buffer.Pop(HandleBufferContent(headerMatcher, buffer, messages, 0, ref currentMessageIndex, options.Flags)); YieldMessage(buffer, buffer.CurrentContent.Length, messages, ref currentMessageIndex); if (messages.Count != 0) { await messagesSink(messages); } }
public AsyncSocketHelper2(CouchbasePooledSocket socket) { this.socket = socket; this.asyncBuffer = new SlidingBuffer(ChunkSize); this.readEvent = new SocketAsyncEventArgs(); this.readEvent.Completed += new EventHandler <SocketAsyncEventArgs>(AsyncReadCompleted); this.readEvent.SetBuffer(new byte[ChunkSize], 0, ChunkSize); this.readInProgressEvent = new ManualResetEvent(false); }
public void Add_AddsFourBytes_Added() { var buffer = new SlidingBuffer(3); for (int i = 1; i <= 5; i++) { buffer.Add((byte)i); } Assert.AreEqual(5, buffer[1]); Assert.AreEqual(4, buffer[2]); Assert.AreEqual(3, buffer[3]); }
unsafe static int HandleBufferContent(IHeaderMatcher headerMatcher, SlidingBuffer buffer, List <MessageInfo> messages, int bufferUnderflowThreshold, ref int currentMessageIndex, Flags flags) { string content = buffer.CurrentContent; int contentLen = content.Length; fixed(char *contentPtr = content) { for (int currentPosition = 0; ;) { if (contentLen - currentPosition < bufferUnderflowThreshold) { return(currentPosition); } var startIdx = currentMessageIndex > 0 ? FindNewLine(contentPtr, contentLen, currentPosition) : currentPosition; var match = headerMatcher.Match(contentPtr, contentLen, startIdx, content); if (match == null) { return(currentPosition); } YieldMessage(buffer, contentPtr, match.Index, messages, ref currentMessageIndex); var h = buffer.AllocatedMatchHeader; h.StreamPosition = buffer.ContentStreamPosition + match.Index; h.EndOfHeaderPosition = match.Index + match.Length; h.Match2 = match; h.Buffer = content; buffer.CurrentMessageHeader = h; if ((flags & Flags.UCS2) != 0) { int DefaultAlignmentBlockSize = 32 * 1024; var realStreamPos = h.StreamPosition * 2; if ((flags & Flags.SkipDoubleBytePeamble) != 0) { realStreamPos += 2; } h.StreamPosition = (realStreamPos / DefaultAlignmentBlockSize) * DefaultAlignmentBlockSize + (realStreamPos % DefaultAlignmentBlockSize) / 2; } currentPosition = match.Index + match.Length; } } }
unsafe static bool YieldMessage(SlidingBuffer buffer, int endOfBodyPosition, List <MessageInfo> messagesList, ref int currentMessageIndex) { fixed(char *contentPtr = buffer.CurrentContent) return(YieldMessage(buffer, contentPtr, endOfBodyPosition, messagesList, ref currentMessageIndex)); }