private BsonDocument CreateInitialHelloCommand(IReadOnlyList <IAuthenticator> authenticators, bool loadBalanced = false)
        {
            var command = HelloHelper.CreateCommand(_serverApi, loadBalanced: loadBalanced);

            HelloHelper.AddClientDocumentToCommand(command, _clientDocument);
            HelloHelper.AddCompressorsToCommand(command, _compressors);
            return(HelloHelper.CustomizeCommand(command, authenticators));
        }
        private BsonDocument CreateInitialHelloCommand(IReadOnlyList <IAuthenticator> authenticators)
        {
            var command = HelloHelper.CreateCommand();

            HelloHelper.AddClientDocumentToCommand(command, _clientDocument);
            HelloHelper.AddCompressorsToCommand(command, _compressors);
            return(HelloHelper.CustomizeCommand(command, authenticators));
        }
        public void AddClientDocumentToCommand_with_custom_document_should_return_expected_result(
            [Values("{ client : { driver : 'dotnet', version : '2.4.0' }, os : { type : 'Windows' } }")]
            string clientDocumentString)
        {
            var clientDocument = BsonDocument.Parse(clientDocumentString);
            var command        = HelloHelper.CreateCommand();
            var result         = HelloHelper.AddClientDocumentToCommand(command, clientDocument);

            result.Should().Be($"{{ isMaster : 1, client : {clientDocumentString} }}");
        }
        public void AddClientDocumentToCommand_with_custom_document_should_return_expected_result(
            [Values("{ client : { driver : 'dotnet', version : '3.6.0' }, os : { type : 'Windows' } }")]
            string clientDocumentString)
        {
            var clientDocument = BsonDocument.Parse(clientDocumentString);
            var command        = HelloHelper.CreateCommand(null);
            var result         = HelloHelper.AddClientDocumentToCommand(command, clientDocument);

            result.Should().Be($"{{ {OppressiveLanguageConstants.LegacyHelloCommandName} : 1, helloOk : true, client : {clientDocumentString} }}");
        }
        public async Task <ConnectionDescription> SendHelloAsync(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            var authenticators = GetAuthenticators(connection.Settings);
            var helloCommand   = CreateInitialHelloCommand(authenticators, connection.Settings.LoadBalanced);
            var helloProtocol  = HelloHelper.CreateProtocol(helloCommand, _serverApi);
            var helloResult    = await HelloHelper.GetResultAsync(connection, helloProtocol, cancellationToken).ConfigureAwait(false);

            if (connection.Settings.LoadBalanced && !helloResult.ServiceId.HasValue)
            {
                throw new InvalidOperationException("Driver attempted to initialize in load balancing mode, but the server does not support this mode.");
            }

            return(new ConnectionDescription(connection.ConnectionId, helloResult));
        }
示例#6
0
        public ConnectionDescription SendHello(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            var authenticators = GetAuthenticators(connection.Settings);
            var helloCommand   = CreateInitialHelloCommand(authenticators, connection.Settings.LoadBalanced);
            var helloProtocol  = HelloHelper.CreateProtocol(helloCommand, _serverApi);
            var helloResult    = HelloHelper.GetResult(connection, helloProtocol, cancellationToken);

            if (connection.Settings.LoadBalanced && !helloResult.ServiceId.HasValue)
            {
                throw new InvalidOperationException("Driver attempted to initialize in load balancing mode, but the server does not support this mode.");
            }

            var buildInfoProtocol = CreateBuildInfoProtocol(_serverApi);
            var buildInfoResult   = new BuildInfoResult(buildInfoProtocol.Execute(connection, cancellationToken));

            return(new ConnectionDescription(connection.ConnectionId, helloResult, buildInfoResult));
        }
        public void AddCompressorsToCommand_with_compressors_should_return_expected_result(
            [Values(
                 new CompressorType[] { },
                 new [] { CompressorType.Zlib },
                 new [] { CompressorType.Snappy },
                 new [] { CompressorType.Zlib, CompressorType.Snappy },
                 new [] { CompressorType.ZStandard, CompressorType.Snappy })]
            CompressorType[] compressorsParameters)
        {
            var command     = HelloHelper.CreateCommand();
            var compressors =
                compressorsParameters
                .Select(c => new CompressorConfiguration(c))
                .ToArray();
            var result = HelloHelper.AddCompressorsToCommand(command, compressors);

            var expectedCompressions = string.Join(",", compressorsParameters.Select(c => $"'{CompressorTypeMapper.ToServerName(c)}'"));

            result.Should().Be(BsonDocument.Parse($"{{ isMaster : 1, compression: [{expectedCompressions}] }}"));
        }
        public async Task <ConnectionDescription> InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            var authenticators = connection.Settings.AuthenticatorFactories.Select(f => f.Create()).ToList();
            var helloCommand   = CreateInitialHelloCommand(authenticators);
            var helloProtocol  = HelloHelper.CreateProtocol(helloCommand, _serverApi);
            var helloResult    = await HelloHelper.GetResultAsync(connection, helloProtocol, cancellationToken).ConfigureAwait(false);

            var buildInfoProtocol = CreateBuildInfoProtocol(_serverApi);
            var buildInfoResult   = new BuildInfoResult(await buildInfoProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false));

            var description = new ConnectionDescription(connection.ConnectionId, helloResult, buildInfoResult);

            await AuthenticationHelper.AuthenticateAsync(connection, description, authenticators, cancellationToken).ConfigureAwait(false);

            var connectionIdServerValue = helloResult.ConnectionIdServerValue;

            if (connectionIdServerValue.HasValue)
            {
                description = UpdateConnectionIdWithServerValue(description, connectionIdServerValue.Value);
            }
            else
            {
                try
                {
                    var getLastErrorProtocol = CreateGetLastErrorProtocol(_serverApi);
                    var getLastErrorResult   = await getLastErrorProtocol
                                               .ExecuteAsync(connection, cancellationToken)
                                               .ConfigureAwait(false);

                    description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
                }
                catch
                {
                    // if we couldn't get the server's connection id, so be it.
                }
            }

            return(description);
        }
        public void CreateCommand_without_ServerApi_should_return_legacy_hello_command()
        {
            var command = HelloHelper.CreateCommand(null);

            command.Should().Be($"{{ isMaster : 1 }}");
        }
        public void CreateCommand_with_ServerApi_should_return_hello_command()
        {
            var command = HelloHelper.CreateCommand(new ServerApi(ServerApiVersion.V1));

            command.Should().Be($"{{ hello : 1 }}");
        }