Ejemplo n.º 1
0
        private async ValueTask ProcessStartupMessageResponse <TSender, TReceiver>(TSender sender, Memory <byte> sendBuffer, TReceiver receiver, Memory <byte> receiveBuffer,
                                                                                   string userName, string password, CancellationToken cancellationToken) where TSender : ISender where  TReceiver : IReceiver
        {
            var messageReader    = new BackendMessageReader <TReceiver>(receiver, receiveBuffer);
            var messageRef       = MessageRef.Empty;
            var receiveAsyncTask = messageReader.MoveNextAsync(messageRef, cancellationToken);

            messageRef = receiveAsyncTask.IsCompletedSuccessfully
                ? receiveAsyncTask.Result
                : await receiveAsyncTask.ConfigureAwait(false);

            if (messageRef.HasData)
            {
                switch (messageRef.MessageType)
                {
                case BackendMessageCode.AuthenticationRequest:
                    var authResponse = messageReader.ReadAuthentication(messageRef);
                    await ProcessAuthentication(authResponse, userName, password, sender, sendBuffer, messageReader, cancellationToken)
                    .ConfigureAwait(false);

                    break;

                case BackendMessageCode.ErrorResponse:
                    // TODO: process error response
                    messageReader.ReadErrorResponse(messageRef);
                    break;

                default:
                    ThrowHelper.ThrowUnexpectedBackendMessageException(messageRef.MessageType);
                    break;
                }
            }
            else
            {
                // TODO:
                ThrowHelper.ThrowNotImplementedException();
            }
        }
Ejemplo n.º 2
0
        public async ValueTask <int> SendQueryAsync(ReadOnlyMemory <char> query, CancellationToken cancellationToken)
        {
            var w               = new Query(query);
            var socket          = m_socket !;
            var sender          = new SocketSender(socket);
            var sendRequestTask = WriteAndSendMessage(sender, w, cancellationToken);

            if (!sendRequestTask.IsCompletedSuccessfully)
            {
                await sendRequestTask.ConfigureAwait(false);
            }

            var receiveBufferArray = m_arrayPool.Rent(512);

            try
            {
                var reader     = new BackendMessageReader <SocketReceiver>(new SocketReceiver(socket), receiveBufferArray);
                var messageRef = MessageRef.Empty;
                while (true)
                {
                    var moveNextTask = reader.MoveNextAsync(messageRef, cancellationToken);
                    messageRef = !moveNextTask.IsCompletedSuccessfully
                        ? await moveNextTask.ConfigureAwait(false)
                        : moveNextTask.Result;

                    if (!messageRef.HasData)
                    {
                        break;
                    }

                    switch (messageRef.MessageType)
                    {
                    case BackendMessageCode.CompletedResponse:
                        reader.ReadCommandComplete(messageRef);
                        break;

                    case BackendMessageCode.CopyInResponse:
                        ThrowHelper.ThrowNotImplementedException();
                        break;

                    case BackendMessageCode.CopyOutResponse:
                        ThrowHelper.ThrowNotImplementedException();
                        break;

                    case BackendMessageCode.RowDescription:
                        reader.ReadRowDescription(messageRef);
                        break;

                    case BackendMessageCode.DataRow:
                        reader.ReadDataRow(messageRef);
                        break;

                    case BackendMessageCode.EmptyQueryResponse:
                        ThrowHelper.ThrowNotImplementedException();
                        break;

                    case BackendMessageCode.ErrorResponse:
                        reader.ReadErrorResponse(messageRef);
                        ThrowHelper.ThrowNotImplementedException();
                        break;

                    case BackendMessageCode.ReadyForQuery:
                        reader.ReadReadyForQuery(messageRef);
                        break;

                    case BackendMessageCode.NoticeResponse:
                        reader.ReadNoticeResponse(messageRef);
                        break;

                    case BackendMessageCode.NotificationResponse:
                        ThrowHelper.ThrowNotImplementedException();
                        break;

                    default:
                        ThrowHelper.ThrowUnexpectedBackendMessageException(messageRef.MessageType);
                        break;
                    }
                }
            }
            finally
            {
                m_arrayPool.Return(receiveBufferArray);
            }

            //ThrowHelper.ThrowNotImplementedException();
            return(0);
        }
Ejemplo n.º 3
0
        private async ValueTask ProcessAuthentication <TSender, TReceiver>(Authentication authResponse, string userName, string password,
                                                                           TSender sender, Memory <byte> sendBuffer, BackendMessageReader <TReceiver> messageReader, CancellationToken cancellationToken)
            where TReceiver : IReceiver where TSender : ISender
        {
            switch (authResponse.AuthenticationRequestType)
            {
            case AuthenticationRequestType.Ok:
                ThrowHelper.ThrowNotImplementedException();
                break;

            case AuthenticationRequestType.KerberosV5:
                ThrowHelper.ThrowNotImplementedException();
                break;

            case AuthenticationRequestType.CleartextPassword:
                await AuthenticateClearText(password, sender, sendBuffer, cancellationToken).ConfigureAwait(false);

                break;

            case AuthenticationRequestType.MD5Password:
                await AuthenticateMD5(authResponse.AdditionalInfo, userName, password, sender, sendBuffer, cancellationToken).ConfigureAwait(false);

                break;

            case AuthenticationRequestType.SCMCredential:
                ThrowHelper.ThrowNotImplementedException();
                break;

            case AuthenticationRequestType.GSS:
                ThrowHelper.ThrowNotImplementedException();
                break;

            case AuthenticationRequestType.GSSContinue:
                ThrowHelper.ThrowNotImplementedException();
                break;

            case AuthenticationRequestType.SSPI:
                ThrowHelper.ThrowNotImplementedException();
                break;

            case AuthenticationRequestType.SASL:
                ThrowHelper.ThrowNotImplementedException();
                break;

            case AuthenticationRequestType.SASLContinue:
                ThrowHelper.ThrowNotImplementedException();
                break;

            case AuthenticationRequestType.SASLFinal:
                ThrowHelper.ThrowNotImplementedException();
                break;

            default:
                ThrowHelper.ThrowUnexpectedAuthenticationRequestType(authResponse.AuthenticationRequestType);
                break;
            }

            var message = MessageRef.Empty;

            while (true)
            {
                var moveNextTask = messageReader.MoveNextAsync(message, cancellationToken);
                message = !moveNextTask.IsCompletedSuccessfully
                    ? await moveNextTask.ConfigureAwait(false)
                    : moveNextTask.Result;

                if (!message.HasData)
                {
                    break;
                }
                switch (message.MessageType)
                {
                case BackendMessageCode.AuthenticationRequest:
                    if (!messageReader.IsAuthenticationOK(message))
                    {
                        ThrowHelper.ThrowNotImplementedException();
                    }
                    break;

                case BackendMessageCode.ParameterStatus:
                    // TODO:
                    break;

                case BackendMessageCode.BackendKeyData:
                    var backendKeyData = messageReader.ReadBackendKeyData(message);
                    m_processId = backendKeyData.ProcessId;
                    m_secretKey = backendKeyData.SecretKey;
                    break;

                case BackendMessageCode.ReadyForQuery:
                    var readyForQuery = messageReader.ReadReadyForQuery(message);
                    m_transactionStatus = readyForQuery.TransactionStatus;
                    break;

                case BackendMessageCode.ErrorResponse:
                    //TODO:
                    var error = messageReader.ReadErrorResponse(message);
                    ThrowHelper.ThrowNotImplementedException();
                    break;

                default:
                    ThrowHelper.ThrowUnexpectedBackendMessageException(message.MessageType);
                    break;
                }
            }
        }