public void CanReadMultipleLines() { // Arrange var buffer = new ChunkBuffer(); byte[] data = Encoding.UTF8.GetBytes("hel\nlo world\noy"); // Act buffer.Add(data, data.Length); // Assert Assert.Equal("hel", buffer.ReadLine()); Assert.Equal("lo world", buffer.ReadLine()); Assert.Null(buffer.ReadLine()); }
public void WillCompleteNewLine() { // Arrange var buffer = new ChunkBuffer(); byte[] data = Encoding.UTF8.GetBytes("hello"); buffer.Add(data, data.Length); Assert.Null(buffer.ReadLine()); data = Encoding.UTF8.GetBytes("\n"); buffer.Add(data, data.Length); Assert.Equal("hello", buffer.ReadLine()); data = Encoding.UTF8.GetBytes("Another line"); buffer.Add(data, data.Length); Assert.Null(buffer.ReadLine()); data = Encoding.UTF8.GetBytes("\nnext"); buffer.Add(data, data.Length); Assert.Equal("Another line", buffer.ReadLine()); }
public void ReturnsTextUpToNewLine() { // Arrange var buffer = new ChunkBuffer(); byte[] data = Encoding.UTF8.GetBytes("hello world\noy"); // Act buffer.Add(data, data.Length); // Assert Assert.Equal("hello world", buffer.ReadLine()); }
public void ReturnsNullIfNoNewLineIfBuffer() { // Arrange var buffer = new ChunkBuffer(); byte[] data = Encoding.UTF8.GetBytes("hello world"); // Act buffer.Add(data, data.Length); // Assert Assert.Null(buffer.ReadLine()); }
private void ProcessChunks() { Debug.WriteLine("ProcessChunks"); while (Reading && _buffer.HasChunks) { var line = _buffer.ReadLine(); // No new lines in the buffer so stop processing if (line == null) { break; } if (!Reading) { return; } // Try parsing the sseEvent SseEvent sseEvent; if (!SseEvent.TryParse(line, out sseEvent)) { continue; } if (!Reading) { return; } Debug.WriteLine("SSE READ: " + sseEvent); switch (sseEvent.Type) { case EventType.Id: _connection.MessageId = sseEvent.Data; break; case EventType.Data: if (!sseEvent.Data.Equals("initialized", StringComparison.OrdinalIgnoreCase)) { if (Reading) { // We don't care about timeout messages here since it will just reconnect // as part of being a long running request bool timedOutReceived; bool disconnectReceived; HttpBasedTransport.ProcessResponse( _connection, sseEvent.Data, out timedOutReceived, out disconnectReceived); if (disconnectReceived) { _connection.Stop(); } if (timedOutReceived) { return; } } } break; } } }
private void ProcessChunks() { //Debug.WriteLine("AsyncStreamReader: ProcessChunks"); while (Reading && m_buffer.HasChunks) { string _line = m_buffer.ReadLine(); // No new lines in the buffer so stop processing if (_line == null) { break; } if (!Reading) { return; } // Try parsing the sseEvent SseEvent _sseEvent; if (!SseEvent.TryParse(_line, out _sseEvent)) { continue; } if (!Reading) { return; } //Debug.WriteLine("AsyncStreamReader: SSE READ [{0}]", _sseEvent.ToString()); switch (_sseEvent.Type) { case EventType.Id: m_connection.MessageId = _sseEvent.Data; break; case EventType.Data: if (_sseEvent.Data.Equals("initialized", StringComparison.OrdinalIgnoreCase)) { if (m_initializeCallback != null) { // Mark the connection as started m_initializeCallback(); } } else { if (Reading) { // We don't care about timeout messages here since it will just reconnect // as part of being a long running request bool _timedOutReceived; bool _disconnectReceived; HttpBasedTransport.ProcessResponse( m_connection, _sseEvent.Data, out _timedOutReceived, out _disconnectReceived); if (_disconnectReceived) { m_connection.Stop(); } if (_timedOutReceived) { return; } } } break; } } }