/// <summary>
 /// pre process remoting context, initial some useful infos and pass to biz
 /// </summary>
 /// <param name="ctx"> remoting context </param>
 /// <param name="cmd"> rpc request command </param>
 /// <param name="currentTimestamp"> current timestamp </param>
 private void preProcessRemotingContext(RemotingContext ctx, RpcRequestCommand cmd, long currentTimestamp)
 {
     ctx.ArriveTimestamp = cmd.ArriveTime;
     ctx.Timeout         = cmd.Timeout;
     ctx.RpcCommandType  = cmd.Type;
     ctx.InvokeContext.putIfAbsent(InvokeContext.BOLT_PROCESS_WAIT_TIME, currentTimestamp - cmd.ArriveTime);
 }
        private RpcRequestCommand createRequestCommand(short cmdCode)
        {
            RpcRequestCommand command = new RpcRequestCommand();

            command.CmdCode    = RpcCommandCode.valueOf(cmdCode);
            command.ArriveTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
            return(command);
        }
 /// <summary>
 /// print some debug log when receive request
 /// </summary>
 private void debugLog(RemotingContext ctx, RpcRequestCommand cmd, long currentTimestamp)
 {
     if (logger.IsEnabled(LogLevel.Debug))
     {
         logger.LogDebug("Rpc request received! requestId={}, from {}", cmd.Id, ctx.ChannelContext.Channel.RemoteAddress.ToString());
         logger.LogDebug("request id {} currenTimestamp {} - arriveTime {} = server cost {} < timeout {}.", cmd.Id, currentTimestamp, cmd.ArriveTime, currentTimestamp - cmd.ArriveTime, cmd.Timeout);
     }
 }
        /// <summary>
        /// dispatch request command to user processor
        /// </summary>
        /// <param name="ctx"> remoting context </param>
        /// <param name="cmd"> rpc request command </param>
        private void dispatchToUserProcessor(RemotingContext ctx, RpcRequestCommand cmd)
        {
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int id = cmd.getId();
            int id = cmd.Id;
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final byte type = cmd.getType();
            byte type = cmd.Type;
            // processor here must not be null, for it have been checked before
            UserProcessor processor = ctx.getUserProcessor(cmd.RequestClass);

            if (processor is AsyncUserProcessor)
            {
                try
                {
                    processor.handleRequest(processor.preHandleRequest(ctx, cmd.RequestObject), new RpcAsyncContext(ctx, cmd, this), cmd.RequestObject);
                }
                catch (RejectedExecutionException)
                {
                    logger.LogWarning("RejectedExecutionException occurred when do ASYNC process in RpcRequestProcessor");
                    sendResponseIfNecessary(ctx, type, CommandFactory.createExceptionResponse(id, ResponseStatus.SERVER_THREADPOOL_BUSY));
                }
                catch (System.Exception t)
                {
                    string errMsg = "AYSNC process rpc request failed in RpcRequestProcessor, id=" + id;
                    logger.LogError(errMsg, t);
                    sendResponseIfNecessary(ctx, type, CommandFactory.createExceptionResponse(id, t, errMsg));
                }
            }
            else
            {
                try
                {
                    object responseObject = processor.handleRequest(processor.preHandleRequest(ctx, cmd.RequestObject), cmd.RequestObject);

                    sendResponseIfNecessary(ctx, type, CommandFactory.createResponse(responseObject, cmd));
                }
                catch (RejectedExecutionException)
                {
                    logger.LogWarning("RejectedExecutionException occurred when do SYNC process in RpcRequestProcessor");
                    sendResponseIfNecessary(ctx, type, CommandFactory.createExceptionResponse(id, ResponseStatus.SERVER_THREADPOOL_BUSY));
                }
                catch (System.Exception t)
                {
                    string errMsg = "SYNC process rpc request failed in RpcRequestProcessor, id=" + id;
                    logger.LogError(errMsg, t);
                    sendResponseIfNecessary(ctx, type, CommandFactory.createExceptionResponse(id, t, errMsg));
                }
            }
        }
        /// <seealso cref= AbstractRemotingProcessor#doProcess(RemotingContext, RemotingCommand) </seealso>
        //JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
        //ORIGINAL LINE: @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void doProcess(final RemotingContext ctx, RpcRequestCommand cmd) throws Exception
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
        public virtual void doProcess(RemotingContext ctx, RpcRequestCommand cmd)
        {
            long currentTimestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();

            preProcessRemotingContext(ctx, cmd, currentTimestamp);
            if (ctx.TimeoutDiscard && ctx.RequestTimeout)
            {
                timeoutLog(cmd, currentTimestamp, ctx); // do some log
                return;                                 // then, discard this request
            }
            debugLog(ctx, cmd, currentTimestamp);
            // decode request all
            if (!deserializeRequestCommand(ctx, cmd, RpcDeserializeLevel.DESERIALIZE_ALL))
            {
                return;
            }
            dispatchToUserProcessor(ctx, cmd);
        }
        /// <summary>
        /// print some log when request timeout and discarded in io thread.
        /// </summary>
        //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
        //ORIGINAL LINE: private void timeoutLog(final RpcRequestCommand cmd, long currentTimestamp, RemotingContext ctx)
        private void timeoutLog(RpcRequestCommand cmd, long currentTimestamp, RemotingContext ctx)
        {
            if (logger.IsEnabled(LogLevel.Debug))
            {
                logger.LogDebug("request id [{}] currenTimestamp [{}] - arriveTime [{}] = server cost [{}] >= timeout value [{}].", cmd.Id, currentTimestamp, cmd.ArriveTime, currentTimestamp - cmd.ArriveTime, cmd.Timeout);
            }

            string remoteAddr = "UNKNOWN";

            if (null != ctx)
            {
                IChannelHandlerContext channelCtx = ctx.ChannelContext;
                IChannel channel = channelCtx.Channel;
                if (null != channel)
                {
                    remoteAddr = ((IPEndPoint)channel.RemoteAddress).ToString();
                }
            }
            logger.LogWarning("Rpc request id[{}], from remoteAddr[{}] stop process, total wait time in queue is [{}], client timeout setting is [{}].", cmd.Id, remoteAddr, currentTimestamp - cmd.ArriveTime, cmd.Timeout);
        }
        /// <summary>
        /// deserialize request command
        /// </summary>
        /// <returns> true if deserialize success; false if exception catched </returns>
        private bool deserializeRequestCommand(RemotingContext ctx, RpcRequestCommand cmd, int level)
        {
            bool result;

            try
            {
                cmd.deserialize(level);
                result = true;
            }
            catch (DeserializationException e)
            {
                logger.LogError("DeserializationException occurred when process in RpcRequestProcessor, id={}, deserializeLevel={}", cmd.Id, RpcDeserializeLevel.valueOf(level), e);
                sendResponseIfNecessary(ctx, cmd.Type, CommandFactory.createExceptionResponse(cmd.Id, ResponseStatus.SERVER_DESERIAL_EXCEPTION, e));
                result = false;
            }
            catch (System.Exception t)
            {
                string errMsg = "Deserialize RpcRequestCommand failed in RpcRequestProcessor, id=" + cmd.Id + ", deserializeLevel=" + level;
                logger.LogError(errMsg, t);
                sendResponseIfNecessary(ctx, cmd.Type, CommandFactory.createExceptionResponse(cmd.Id, t, errMsg));
                result = false;
            }
            return(result);
        }
 public ProcessTask(RpcRequestProcessor outerInstance, RemotingContext ctx, RpcRequestCommand msg)
 {
     this.outerInstance = outerInstance;
     this.ctx           = ctx;
     this.msg           = msg;
 }
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="ctx"> remoting context </param>
        /// <param name="cmd"> rpc request command </param>
        /// <param name="processor"> rpc request processor </param>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: public RpcAsyncContext(final RemotingContext ctx, final RpcRequestCommand cmd, final RpcRequestProcessor processor)
        public RpcAsyncContext(RemotingContext ctx, RpcRequestCommand cmd, RpcRequestProcessor processor)
        {
            this.ctx       = ctx;
            this.cmd       = cmd;
            this.processor = processor;
        }