Esempio n. 1
0
        private void BeginJob(AsyncResult <IOutput> jar, Action job, bool startup = true)
        {
            try
            {
                if (startup && !_isStreamOpened.Value)
                {
                    Evaluate(new StartupRequest(jar.StreamId, _startupOptions), jar.StreamId, (frame) =>
                    {
                        var response = FrameParser.Parse(frame);
                        if (response is ReadyResponse)
                        {
                            _isStreamOpened.Value = true;
                            job();
                        }
                        else if (response is AuthenticateResponse)
                        {
                            if (_authProvider == NoneAuthProvider.Instance) //Apache C*
                            {
                                var credentials = _authInfoProvider.GetAuthInfos(_serverAddress);

                                Evaluate(new CredentialsRequest(jar.StreamId, credentials), jar.StreamId, new Action <ResponseFrame>((frame2) =>
                                {
                                    var response2 = FrameParser.Parse(frame2);
                                    if (response2 is ReadyResponse)
                                    {
                                        _isStreamOpened.Value = true;
                                        job();
                                    }
                                    else
                                    {
                                        _protocolErrorHandlerAction(new ErrorActionParam()
                                        {
                                            AbstractResponse = response2, Jar = jar
                                        });
                                    }
                                }));
                            }
                            else //DSE
                            {
                                var authenticator = _authProvider.NewAuthenticator(this._serverAddress);

                                var initialResponse = authenticator.InitialResponse();
                                if (null == initialResponse)
                                {
                                    initialResponse = new byte[0];
                                }

                                WaitForSaslResponse(jar, initialResponse, authenticator, job);
                            }
                        }
                        else
                        {
                            _protocolErrorHandlerAction(new ErrorActionParam()
                            {
                                AbstractResponse = response, Jar = jar
                            });
                        }
                    });
                }
                else
                {
                    job();
                }
            }
            catch (Exception ex)
            {
                if (!SetupSocketException(ex))
                {
                    throw;
                }
            }
        }
Esempio n. 2
0
        private AsyncResult <IOutput> BeginJob(AsyncCallback callback, object state, object owner, string propId, Action <int> job, bool startup = true)
        {
            var streamId = AllocateStreamId();

            if (streamId == -1)
            {
                throw new StreamAllocationException();
            }

            _defaultFatalErrorAction = new Action <ResponseFrame>((frame2) =>
            {
                var response2 = FrameParser.Parse(frame2);
                _protocolErrorHandlerAction(new ErrorActionParam()
                {
                    AbstractResponse = response2, StreamId = streamId
                });
            });

            var ar = new AsyncResult <IOutput>(callback, state, owner, propId, null, null, _asyncCallAbortTimeout);

            lock (_frameGuardier)
                _frameReadAsyncResult[streamId] = ar;

            try
            {
                if (startup && !_isStreamOpened.Value)
                {
                    Evaluate(new StartupRequest(streamId, _startupOptions), streamId, (frame) =>
                    {
                        var response = FrameParser.Parse(frame);
                        if (response is ReadyResponse)
                        {
                            _isStreamOpened.Value = true;
                            job(streamId);
                        }
                        else if (response is AuthenticateResponse)
                        {
                            if (_authInfoProvider == null)
                            {
                                throw new AuthenticationException("Credentials are required.", _serverAddress);
                            }

                            var credentials = _authInfoProvider.GetAuthInfos(_serverAddress);

                            Evaluate(new CredentialsRequest(streamId, credentials), streamId, new Action <ResponseFrame>((frame2) =>
                            {
                                var response2 = FrameParser.Parse(frame2);
                                if (response2 is ReadyResponse)
                                {
                                    _isStreamOpened.Value = true;
                                    job(streamId);
                                }
                                else
                                {
                                    _protocolErrorHandlerAction(new ErrorActionParam()
                                    {
                                        AbstractResponse = response2, StreamId = streamId
                                    });
                                }
                            }));
                        }
                        else
                        {
                            _protocolErrorHandlerAction(new ErrorActionParam()
                            {
                                AbstractResponse = response, StreamId = streamId
                            });
                        }
                    });
                }
                else
                {
                    job(streamId);
                }
            }
            catch (Exception ex)
            {
                if (!SetupSocketException(ex))
                {
                    throw;
                }
            }

            return(ar);
        }
        private void BeginJob(AsyncResult <IOutput> jar, Action job, bool startup = true)
        {
            try
            {
                if (startup && !_isStreamOpened.Value)
                {
                    Evaluate(new StartupRequest(jar.StreamId, _startupOptions), jar.StreamId, (frame) =>
                    {
                        var response = FrameParser.Parse(frame);
                        if (response is ReadyResponse)
                        {
                            _isStreamOpened.Value = true;
                            job();
                        }
                        else if (response is AuthenticateResponse)
                        {
                            if (_authInfoProvider == null)
                            {
                                throw new AuthenticationException("Credentials are required.", _serverAddress);
                            }

                            var credentials = _authInfoProvider.GetAuthInfos(_serverAddress);

                            Evaluate(new CredentialsRequest(jar.StreamId, credentials), jar.StreamId, new Action <ResponseFrame>((frame2) =>
                            {
                                var response2 = FrameParser.Parse(frame2);
                                if (response2 is ReadyResponse)
                                {
                                    _isStreamOpened.Value = true;
                                    job();
                                }
                                else
                                {
                                    _protocolErrorHandlerAction(new ErrorActionParam()
                                    {
                                        AbstractResponse = response2, Jar = jar
                                    });
                                }
                            }));
                        }
                        else
                        {
                            _protocolErrorHandlerAction(new ErrorActionParam()
                            {
                                AbstractResponse = response, Jar = jar
                            });
                        }
                    });
                }
                else
                {
                    job();
                }
            }
            catch (Exception ex)
            {
                if (!SetupSocketException(ex))
                {
                    throw;
                }
            }
        }