public bool UpdateForNextAttempt()
        {
            _resultDto = null;

            var correlationId = Guid.NewGuid();

            Attempt += 1;
            var package = new TcpPackage(SentPackage.Command, correlationId, SentPackage.Data);

            SentPackage = package;

            return(true);
        }
        public ProcessResult Process(TcpPackage package)
        {
            try
            {
                if (package.Command != TcpCommand.CreateStreamCompleted)
                {
                    return(new ProcessResult(ProcessResultStatus.NotifyError,
                                             new Exception(string.Format("Not expected command, expected {0}, received {1}",
                                                                         TcpCommand.CreateStreamCompleted, package.Command))));
                }

                var data = package.Data;
                var dto  = data.Deserialize <ClientMessages.CreateStreamCompleted>();
                _resultDto = dto;

                switch ((OperationErrorCode)dto.ErrorCode)
                {
                case OperationErrorCode.Success:
                    return(new ProcessResult(ProcessResultStatus.Success));

                case OperationErrorCode.PrepareTimeout:
                case OperationErrorCode.CommitTimeout:
                case OperationErrorCode.ForwardTimeout:
                    if (Attempt < MaxRetriesCount)
                    {
                        return(new ProcessResult(ProcessResultStatus.Retry));
                    }
                    else
                    {
                        return(new ProcessResult(ProcessResultStatus.NotifyError,
                                                 new Exception(string.Format("Max retries count reached, last error: {0}",
                                                                             (OperationErrorCode)dto.ErrorCode))));
                    }

                case OperationErrorCode.WrongExpectedVersion:
                case OperationErrorCode.StreamDeleted:
                case OperationErrorCode.InvalidTransaction:
                    return(new ProcessResult(ProcessResultStatus.NotifyError,
                                             new Exception(string.Format("{0}", (OperationErrorCode)dto.ErrorCode))));

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (Exception ex)
            {
                return(new ProcessResult(ProcessResultStatus.NotifyError, ex));
            }
        }
예제 #3
0
        public InspectionResult InspectPackage(TcpPackage package)
        {
            try
            {
                if (package.Command != TcpCommand.CreateStreamCompleted)
                {
                    return(new InspectionResult(InspectionDecision.NotifyError,
                                                new CommandNotExpectedException(TcpCommand.CreateStreamCompleted.ToString(),
                                                                                package.Command.ToString())));
                }

                var data = package.Data;
                var dto  = data.Deserialize <ClientMessages.CreateStreamCompleted>();
                _result = dto;

                switch ((OperationErrorCode)dto.ErrorCode)
                {
                case OperationErrorCode.Success:
                    return(new InspectionResult(InspectionDecision.Succeed));

                case OperationErrorCode.PrepareTimeout:
                case OperationErrorCode.CommitTimeout:
                case OperationErrorCode.ForwardTimeout:
                    return(new InspectionResult(InspectionDecision.Retry));

                case OperationErrorCode.WrongExpectedVersion:
                    var err = string.Format("Create stream failed due to WrongExpectedVersion. Stream: {0}, CorrID: {1}.",
                                            _stream,
                                            CorrelationId);
                    return(new InspectionResult(InspectionDecision.NotifyError, new WrongExpectedVersionException(err)));

                case OperationErrorCode.StreamDeleted:
                    return(new InspectionResult(InspectionDecision.NotifyError, new StreamDeletedException(_stream)));

                case OperationErrorCode.InvalidTransaction:
                    return(new InspectionResult(InspectionDecision.NotifyError, new InvalidTransactionException()));

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (Exception e)
            {
                return(new InspectionResult(InspectionDecision.NotifyError, e));
            }
        }
        public bool UpdateForNextAttempt()
        {
            _resultDto = null;

            var correlationId = Guid.NewGuid();
            Attempt += 1;
            var package = new TcpPackage(SentPackage.Command, correlationId, SentPackage.Data);
            SentPackage = package;

            return true;
        }
        public ProcessResult Process(TcpPackage package)
        {
            try
            {
                if (package.Command != TcpCommand.CreateStreamCompleted)
                {
                    return new ProcessResult(ProcessResultStatus.NotifyError,
                                             new Exception(string.Format("Not expected command, expected {0}, received {1}",
                                                                         TcpCommand.CreateStreamCompleted, package.Command)));
                }

                var data = package.Data;
                var dto = data.Deserialize<ClientMessages.CreateStreamCompleted>();
                _resultDto = dto;

                switch ((OperationErrorCode)dto.ErrorCode)
                {
                    case OperationErrorCode.Success:
                        return new ProcessResult(ProcessResultStatus.Success);

                    case OperationErrorCode.PrepareTimeout:
                    case OperationErrorCode.CommitTimeout:
                    case OperationErrorCode.ForwardTimeout:
                        if (Attempt < MaxRetriesCount)
                        {
                            return new ProcessResult(ProcessResultStatus.Retry);
                        }
                        else
                        {
                            return new ProcessResult(ProcessResultStatus.NotifyError,
                                                     new Exception(string.Format("Max retries count reached, last error: {0}",
                                                                  (OperationErrorCode)dto.ErrorCode)));
                        }
                    case OperationErrorCode.WrongExpectedVersion:
                    case OperationErrorCode.StreamDeleted:
                    case OperationErrorCode.InvalidTransaction:
                        return new ProcessResult(ProcessResultStatus.NotifyError,
                                                 new Exception(string.Format("{0}", (OperationErrorCode)dto.ErrorCode)));
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
            catch (Exception ex)
            {
                return new ProcessResult(ProcessResultStatus.NotifyError, ex);
            }
        }