Exemple #1
0
        public static async Task ConnectToClientNodeAsync(RavenServer server, TcpConnectionInfo tcpConnectionInfo, TimeSpan timeout, Logger log, string database, NodeConnectionTestResult result, CancellationToken token = default)
        {
            TcpClient tcpClient;
            string    url;

            (tcpClient, url) = await TcpUtils.ConnectSocketAsync(tcpConnectionInfo, timeout, log, token);

            var connection = await TcpUtils.WrapStreamWithSslAsync(tcpClient, tcpConnectionInfo, server.Certificate.Certificate, server.CipherSuitesPolicy, timeout, token);

            using (tcpClient)
            {
                using (server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext ctx))
                    await using (var writer = new AsyncBlittableJsonTextWriter(ctx, connection))
                    {
                        await WriteOperationHeaderToRemote(writer, TcpConnectionHeaderMessage.OperationTypes.TestConnection, database);

                        using (var responseJson = await ctx.ReadForMemoryAsync(connection, $"TestConnectionHandler/{url}/Read-Handshake-Response"))
                        {
                            var headerResponse = JsonDeserializationServer.TcpConnectionHeaderResponse(responseJson);
                            switch (headerResponse.Status)
                            {
                            case TcpConnectionStatus.Ok:
                                result.Success = true;
                                break;

                            case TcpConnectionStatus.AuthorizationFailed:
                                result.Success = false;
                                result.Error   = $"Connection to {url} failed because of authorization failure: {headerResponse.Message}";
                                break;

                            case TcpConnectionStatus.TcpVersionMismatch:
                                result.Success = false;
                                result.Error   = $"Connection to {url} failed because of mismatching tcp version: {headerResponse.Message}";
                                await WriteOperationHeaderToRemote(writer, TcpConnectionHeaderMessage.OperationTypes.Drop, database);

                                break;
                            }
                        }
                    }
            }
        }