Example #1
0
        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 IByteBuffer CompressMessages(
            IEnumerable <RequestMessage> messages,
            IByteBuffer uncompressedBuffer,
            MessageEncoderSettings messageEncoderSettings)
        {
            var outputBufferChunkSource = new OutputBufferChunkSource(BsonChunkPool.Default);
            var compressedBuffer        = new MultiChunkBuffer(outputBufferChunkSource);

            using (var uncompressedStream = new ByteBufferStream(uncompressedBuffer, ownsBuffer: false))
                using (var compressedStream = new ByteBufferStream(compressedBuffer, ownsBuffer: false))
                {
                    foreach (var message in messages)
                    {
                        var uncompressedMessageLength = uncompressedStream.ReadInt32();
                        uncompressedStream.Position -= 4;

                        using (var uncompressedMessageSlice = uncompressedBuffer.GetSlice((int)uncompressedStream.Position, uncompressedMessageLength))
                            using (var uncompressedMessageStream = new ByteBufferStream(uncompressedMessageSlice, ownsBuffer: false))
                            {
                                if (message.MayBeCompressed)
                                {
                                    CompressMessage(message, uncompressedMessageStream, compressedStream, messageEncoderSettings);
                                }
                                else
                                {
                                    uncompressedMessageStream.EfficientCopyTo(compressedStream);
                                }
                            }
                    }
                    compressedBuffer.Length = (int)compressedStream.Length;
                }

            return(compressedBuffer);
        }