예제 #1
0
        public virtual void RunAsync()
        {
            if (!session.Connected)
            {
                throw new SessionOpenException(session.RemoteEndPoint as IPEndPoint);
            }

            if (hasRunCount <= MAX_RUN_COUNT)
            {
                var commandMessage = BuildMessage();
                if (producer.Produce(ID, this, BLOCK_UNTIL_TIMEOUT_AFTER_SECONDS))
                {
                    hasRunCount++;
                    try
                    {
                        session.Write(commandMessage);
                    }
                    catch (Exception ex)
                    {
                        AbstractAsyncCommand command = null;
                        producer.TryRemoveThoseWaitToTimeout(commandMessage.Header.MessageID, out command);
                        Log.Error(string.Format(ExceptionMessage.COMMAND_RUN_ERROR, session.GetAttribute <string>(KeyName.SESSION_IDENTIFIER)), ex);
                        this.RaiseCallback(this, new CommandEventArgs <DuplexMessage>(DuplexMessage.CreateAckMessage(commandMessage, ErrorCode.FireCommandError)));
                    }
                }
            }
            else
            {
                Log.Warn(
                    string.Format(ExceptionMessage.TOO_MUCH_RETRY,
                                  session.GetAttribute <string>(KeyName.SESSION_IDENTIFIER)));
            }
        }
예제 #2
0
        public override void MessageReceived(IoSession session, object message)
        {
            var duplexMessage = message as DuplexMessage;

            if (duplexMessage != null)
            {
                switch (duplexMessage.Header.MessageType)
                {
                case MessageType.Command:
                {
                    using (var scope = ObjectHost.Host.BeginLifetimeScope())
                    {
                        scope.ResolveKeyed <ICommandExecutor <DuplexMessage> >(
                            duplexMessage.Header.CommandCode).Execute(session, duplexMessage);
                    }
                }
                break;

                default:
                {
                    var messageId = duplexMessage.Header.MessageID;
                    AbstractAsyncCommand command = null;
                    //var callbackMap = session
                    //    .GetAttribute<LRUMap<string, AbstractAsyncCommand>>(KeyName.SESSION_COMMAND_CALLBACK);
                    var tryToRemoveOk = producer.TryRemoveThoseWaitToTimeout(messageId, out command);
                    if (duplexMessage.Header.MessageType == MessageType.CommandAck)
                    {
                        if (tryToRemoveOk)
                        {
                            command.Parameter = null;
                            if (duplexMessage.Header.State == MessageState.Success)
                            {
                                producer.Produce(messageId, command, command.Timeout);
                            }
                            else
                            {
                                command.RaiseCallback(command, new CommandEventArgs <DuplexMessage>(duplexMessage));
                            }
                        }
                    }
                    else
                    {
                        //if (!tryToRemoveOk)
                        //    if (callbackMap.TryGet(messageId, out command))
                        //        callbackMap.Remove(messageId);

                        if (command != null)
                        {
                            command.RaiseCallback(command, new CommandEventArgs <DuplexMessage>(duplexMessage));
                        }
                    }
                }
                break;
                }
            }
        }