private static List <RequestMessage> TranslateBytesToRequests(byte[] bytes) { var requests = new List <RequestMessage>(); using (var buffer = new ByteArrayBuffer(bytes)) using (var stream = new ByteBufferStream(buffer)) { int bytesRead = 0; while (stream.Length > bytesRead) { int startPosition = bytesRead; var length = stream.ReadInt32(); stream.ReadInt32(); // requestId stream.ReadInt32(); // responseTo var opCode = (Opcode)stream.ReadInt32(); bytesRead += length; stream.Position = startPosition; var encoderFactory = new BinaryMessageEncoderFactory(stream, null); switch (opCode) { case Opcode.Query: requests.Add((RequestMessage)encoderFactory.GetQueryMessageEncoder().ReadMessage()); break; default: throw new InvalidOperationException("Unsupported request type."); } } } return(requests); }
private static List <RequestMessage> TranslateBytesToRequests(byte[] bytes) { var requests = new List <RequestMessage>(); using (var stream = new MemoryStream(bytes)) { int bytesRead = 0; while (stream.Length > bytesRead) { int startPosition = bytesRead; using (var reader = new BsonBinaryReader(stream)) { var length = reader.StreamReader.ReadInt32(); var requestId = reader.StreamReader.ReadInt32(); var responseTo = reader.StreamReader.ReadInt32(); var opCode = (Opcode)reader.StreamReader.ReadInt32(); bytesRead += length; stream.Position = startPosition; var encoderFactory = new BinaryMessageEncoderFactory(reader); switch (opCode) { case Opcode.Query: requests.Add(encoderFactory.GetQueryMessageEncoder().ReadMessage()); break; default: throw new InvalidOperationException("Unsupported request type."); } } } } return(requests); }
public void GetQueryMessageEncoder_should_return_a_QueryMessageBinaryEncoder() { using (var stream = new MemoryStream()) { var encoderFactory = new BinaryMessageEncoderFactory(stream, null); var encoder = encoderFactory.GetQueryMessageEncoder(); encoder.Should().BeOfType<QueryMessageBinaryEncoder>(); } }