private void WriteType1Section(BsonBinaryWriter writer, Type1CommandMessageSection section, long messageStartPosition)
        {
            var stream     = writer.BsonStream;
            var serializer = section.DocumentSerializer;
            var context    = BsonSerializationContext.CreateRoot(writer);

            int?maxMessageSize;

            if (IsEncryptionConfigured)
            {
                var maxBatchSize          = 2 * 1024 * 1024; // 2 MiB
                var maxMessageEndPosition = stream.Position + maxBatchSize;
                maxMessageSize = (int)(maxMessageEndPosition - messageStartPosition);
            }
            else
            {
                maxMessageSize = MaxMessageSize;
            }

            var payloadStartPosition = stream.Position;

            stream.WriteInt32(0); // size
            stream.WriteCString(section.Identifier);

            var batch           = section.Documents;
            var maxDocumentSize = section.MaxDocumentSize ?? writer.Settings.MaxDocumentSize;

            writer.PushSettings(s => ((BsonBinaryWriterSettings)s).MaxDocumentSize = maxDocumentSize);
            writer.PushElementNameValidator(section.ElementNameValidator);
            try
            {
                var maxBatchCount  = Math.Min(batch.Count, section.MaxBatchCount ?? int.MaxValue);
                var processedCount = maxBatchCount;
                for (var i = 0; i < maxBatchCount; i++)
                {
                    var documentStartPosition = stream.Position;
                    var document = batch.Items[batch.Offset + i];
                    serializer.Serialize(context, document);

                    var messageSize = stream.Position - messageStartPosition;
                    if (messageSize > maxMessageSize && batch.CanBeSplit && i > 0)
                    {
                        stream.Position = documentStartPosition;
                        stream.SetLength(documentStartPosition);
                        processedCount = i;
                        break;
                    }
                }
                batch.SetProcessedCount(processedCount);
            }
            finally
            {
                writer.PopElementNameValidator();
                writer.PopSettings();
            }
            stream.BackpatchSize(payloadStartPosition);
        }
        private void WriteQuery(BsonBinaryWriter binaryWriter, BsonDocument query, IElementNameValidator queryValidator)
        {
            var maxWireDocumentSize = MaxWireDocumentSize ?? MaxDocumentSize ?? binaryWriter.Settings.MaxDocumentSize;

            binaryWriter.PushSettings(s => ((BsonBinaryWriterSettings)s).MaxDocumentSize = maxWireDocumentSize);
            binaryWriter.PushElementNameValidator(queryValidator);
            try
            {
                var context = BsonSerializationContext.CreateRoot(binaryWriter);
                BsonDocumentSerializer.Instance.Serialize(context, query ?? new BsonDocument());
            }
            finally
            {
                binaryWriter.PopElementNameValidator();
                binaryWriter.PopSettings();
            }
        }