public ChannelReadResult Read() { if (!_isOpen) { throw new InvalidOperationException("Channel is closed or was not opened"); } if (_inputReader.EndOfStream) { _isOpen = false; return(new ChannelReadResult { IsIdle = false }); } var bytesRead = _inputReader.Read(_buffer, 0, _buffer.Length); if (bytesRead != 0) { _inputStreamParser.AddPartial(new string(_buffer, 0, bytesRead)); if (_inputStreamParser.HasCompletedFrames) { var frames = _inputStreamParser.GetCompletedFrames(); _inputStreamParser.ClearCompletedFrames(); var messages = frames.Select(message => _serialiser.DeserializeMessage(message)); return(new ChannelReadResult { IsIdle = false, Requests = messages.Where(m => m.IsRequest).Select(m => m.Request).ToArray(), Responses = messages.Where(m => m.IsResponse).Select(m => m.Response).ToArray() }); } else { return(new ChannelReadResult { IsIdle = false }); } } else { return(new ChannelReadResult { IsIdle = true }); } }