///--------------------------------------------------------------------------------------------------------
        ///
        protected override async Task <bool> OnStart(long requesterId, string requesterName, DateTime requestTime, params object[] args)
        {
            await SendMessage(requesterId, strings.Format("{0} 채굴을 중지합니다.", requesterName));

            bool invalidPassword = false;

            var taskResponse = Task <string> .Run(() => MakeStopStakingResponse(requestTime, out invalidPassword));

            string response = await taskResponse;

            await SendMessage(requesterId, response);

            if (invalidPassword)
            {
                System.IO.File.Delete(PasswordManager.passwordFile);
                await SendMessage(requesterId, strings.Format("잘못된 암호로 실패했습니다. 봇 프로그램에서 퀀텀 월렛 암호 설정을 확인 해주세요."));
            }

            Logger.Log("채굴 중지 응답 완료.\n");

            if (invalidPassword == false)
            {
                Command.ICommand command = Command.CommandFactory.CreateCommand(Command.eCommand.CheckState);

                if (command != null)
                {
                    await command.Process(-1, "", DateTimeHandler.GetTimeZoneNow());
                }
            }

            IsCompleted = true;

            return(invalidPassword == false);
        }
Exemple #2
0
        /// <summary>
        /// 当接收到客户端新消息时,会调用此方法.
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="cmdInfo"></param>
        public override void OnReceived(SocketBase.IConnection connection, TCommandInfo cmdInfo)
        {
            if (string.IsNullOrEmpty(cmdInfo.CmdName))
            {
                return;
            }

            ThreadPool.QueueUserWorkItem(_ =>
            {
                Command.ICommand <TCommandInfo> cmd = null;
                this._dicCommand.TryGetValue(cmdInfo.CmdName, out cmd);
                try
                {
                    if (cmd == null)
                    {
                        this.HandleUnKnowCommand(connection, cmdInfo);
                    }
                    else
                    {
                        cmd.ExecuteCommand(connection, cmdInfo);
                    }
                }
                catch (Exception ex)
                {
                    this.OnCommandExecException(connection, cmdInfo, ex);
                }
            });
        }
Exemple #3
0
        private void ProcessCommand(string commandText)
        {
            if (commandText.Length > 0)
            {
                string[] tmp = commandText.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                string commandName = tmp[0];

                Command.ICommand command = SearchCommand(commandName);

                if (command != null)
                {
                    CommandContext context = new CommandContext();
                    context.StdError = writer;
                    context.StdOut   = writer;

                    if (tmp.Length >= 2)
                    {
                        string[] args = new string[tmp.Length - 1];


                        for (int i = 1, index = 0; i < tmp.Length; i++, index++)
                        {
                            args[index] = tmp[i];
                        }

                        command.Run(args, context);
                    }
                    else
                    {
                        command.Run(new string[] { }, context);
                    }
                }
            }
        }
Exemple #4
0
 /// <summary>
 /// 向命令处理服务中添加命令处理器
 /// </summary>
 /// <param name="cmd"></param>
 /// <exception cref="ArgumentNullException">cmd is null</exception>
 /// <exception cref="ArgumentNullException">cmd.Name is null</exception>
 protected virtual void AddCommand(Command.ICommand <TCommandInfo> cmd)
 {
     if (cmd == null)
     {
         throw new ArgumentNullException("cmd");
     }
     if (string.IsNullOrEmpty(cmd.Name))
     {
         throw new ArgumentNullException("cmd.name");
     }
     this._dicCommand[cmd.Name] = cmd;
 }
Exemple #5
0
        private static void Execute(Command.ICommand cmd)
        {
            cmd.Execute();

            // Adding empty line separator.
            Logger.PrintLine();

            // Exiting as -q (--quite) option is set.
            if (((Command.IQuite)cmd).Quite)
            {
                Environment.Exit(0);
            }
        }
Exemple #6
0
        public void UpdateMenuEnable()
        {
            Func <string, bool> check = name => {
                string[] names = name.Split('_');
                if (names.Length > 1)
                {
                    System.Reflection.Assembly assem = typeof(MainForm).Assembly;
                    Type t = assem.GetType(string.Format("AssetPacker.Command.{0}", names[1]));
                    if (t != null)
                    {
                        Command.ICommand command = (Command.ICommand)System.Activator.CreateInstance(t, this);
                        return(command.Check());
                    }
                }
                return(true);
            };

            foreach (var obj in menuStrip.Items)
            {
                if (obj is ToolStripMenuItem item)
                {
                    foreach (var obj2 in item.DropDownItems)
                    {
                        if (obj2 is ToolStripItem item2)
                        {
                            item2.Enabled = check(item2.Name);
                        }
                    }
                }
            }
            foreach (var i in toolStripMenu.Items)
            {
                if (i is ToolStripButton item)
                {
                    item.Enabled = check(item.Name);
                }
            }
            foreach (var i in toolStripAssets.Items)
            {
                if (i is ToolStripButton item)
                {
                    item.Enabled = check(item.Name);
                }
            }
            panelSetting.Enabled = project != null;
        }
Exemple #7
0
        /// <summary>
        /// 当接收到客户端新消息时,会调用此方法.
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="cmdInfo"></param>
        public virtual void OnReceived(SocketBase.IConnection connection, TCommandInfo cmdInfo)
        {
            if (connection == null || cmdInfo == null || string.IsNullOrEmpty(cmdInfo.CmdName))
            {
                return;
            }

            Command.ICommand <TCommandInfo> cmd = null;
            if (this._dicCommand.TryGetValue(cmdInfo.CmdName, out cmd))
            {
                cmd.ExecuteCommand(connection, cmdInfo);
            }
            else
            {
                this.HandleUnKnowCommand(connection, cmdInfo);
            }
        }
Exemple #8
0
 private void CommandHandler(object sender, System.EventArgs e)
 {
     string[] names = new string[0];
     if (sender is ToolStripItem item)
     {
         names = item.Name.Split('_');
     }
     else if (sender is Control control)
     {
         names = control.Name.Split('_');
     }
     if (names.Length > 1)
     {
         System.Reflection.Assembly assem = typeof(MainForm).Assembly;
         System.Type t = assem.GetType(string.Format("AssetPacker.Command.{0}", names[1]));
         if (t != null)
         {
             Command.ICommand command = (Command.ICommand)System.Activator.CreateInstance(t, this);
             command.Execute();
         }
     }
 }
Exemple #9
0
        public object Execute(Command.ICommand command)
        {
            var addGood = (Command.AddGood)command;

            return(db.AddGood(addGood.Good, addGood.UserId));
        }
Exemple #10
0
 public object Execute(Command.ICommand command)
 {
     throw new NotSupportedException("No such command.");
 }
Exemple #11
0
        public object Execute(Command.ICommand command)
        {
            var select = (Command.SelectByCategoryId)command;

            return(db.SelectByCategoryId(select.CategoryId, select.Dependency));
        }
        public object Execute(Command.ICommand command)
        {
            var addUser = (Command.AddUser)command;

            return(db.AddUser(addUser.User));
        }
        public object Execute(Command.ICommand command)
        {
            var removeGood = (Command.RemoveGood)command;

            return(db.RemoveGood(removeGood.GoodId));
        }
        public object Execute(Command.ICommand command)
        {
            var removeCategory = (Command.RemoveCategory)command;

            return(db.RemoveCategory(removeCategory.CatId));
        }
        public object Execute(Command.ICommand command)
        {
            var removeUser = (Command.RemoveUser)command;

            return(db.RemoveUser(removeUser.UserId));
        }
Exemple #16
0
 public object Execute(Command.ICommand command)
 {
     return(db.GetData());
 }
        public object Execute(Command.ICommand command)
        {
            var addCategory = (Command.AddCategory)command;

            return(db.AddCategory(addCategory.Category, addCategory.UserId));
        }
        /** 异步工作任务函数
         */
        private void __WorkFunc(object param)
        {
            CancellationTokenSource cts = param as CancellationTokenSource;

            while (!cts.IsCancellationRequested)
            {
                #region 检查是否有可读的要处理
                List <TSession> session_to_deal = new List <TSession>();
                lock (_dic_sessions_lock)
                {
                    if (_dic_sessions_idel.Count > 0)
                    {
                        string[] keys = _dic_sessions_idel.Keys.ToArray();
                        foreach (var key in keys)
                        {
                            TSession sessin = _dic_sessions_idel[key];
                            if (!sessin.GetIsConnected() || !sessin.GetIsReadable())
                            {
                                continue;
                            }
                            _dic_sessions_idel.Remove(key);
                            _dic_sessions_dealing.Add(key, sessin);
                            session_to_deal.Add(sessin);
                        }
                    }
                }
                #endregion

                #region 处理可读的
                if (session_to_deal.Count > 0)
                {
                    foreach (var session in session_to_deal)
                    {
                        //接收完整一帧
                        Frame frame = session.Recv();
                        if (frame == null)
                        {
                            session.SetOffline();
                        }
                        else
                        {
                            //根据T找到帧处理器
                            Command.ICommand cmd = null;
                            if (!_dic_commands.ContainsKey(frame.GetFrameType()))
                            {
                                #region 未找到帧处理器,则调用Session的OnSessionUnkownT
                                try
                                {
                                    session.OnSessionUnkownT(frame);
                                }
                                catch (System.Exception ex)
                                {
                                    session.SetOffline();
                                    Console.WriteLine("Session.OnSessionUnkownT抛出异常," + ex.Message);
                                }
                                #endregion
                            }
                            else
                            {
                                cmd = _dic_commands[frame.GetFrameType()];
                                #region  //使用帧处理器处理帧
                                try
                                {
                                    cmd.Execute(session, frame);
                                }
                                catch (System.Exception ex) //帧处理器处理,发生异常则调用Session的OnSessionExcept
                                {
                                    try
                                    {
                                        session.OnSessionExcept(frame, ex);
                                    }
                                    catch (System.Exception ex2)
                                    {
                                        session.SetOffline();
                                        Console.WriteLine("Session.OnSessionExcept抛出异常," + ex2.Message);
                                    }
                                }
                                #endregion
                            }
                            #region 帧处理完后,释放帧所占用的流资源
                            if (frame.IsBodyHasDataInStream())
                            {
                                try
                                {
                                    var br = frame.GetBodyStream();
                                    br.Close();
                                    br.Dispose();
                                    br = null;
                                }
                                catch (System.Exception ex)
                                {
                                    Console.WriteLine("释放帧的流帧体发生异常," + ex.Message);
                                }
                            }
                            #endregion
                            frame = null;
                        }
                    }
                    //将Session归还到数组
                    lock (_dic_sessions_lock)
                    {
                        foreach (var session in session_to_deal)
                        {
                            _dic_sessions_dealing.Remove(session.GetUUID());
                            _dic_sessions_idel.Add(session.GetUUID(), session);
                        }
                        session_to_deal.Clear();
                    }
                }
                #endregion

                #region 检查是否有要主动推送的要处理
                Dictionary <string, List <dynamic> > dic_push    = new Dictionary <string, List <dynamic> >();
                Dictionary <string, TSession>        dic_session = new Dictionary <string, TSession>();
                lock (_dic_sessions_lock)
                {
                    if (_dic_wait_to_push.Count > 0)
                    {
                        var keys = _dic_wait_to_push.Keys.ToArray();
                        foreach (var key in keys)
                        {
                            string session_uuid = key;
                            if (_dic_sessions_idel.ContainsKey(session_uuid))
                            {
                                _dic_sessions_dealing.Add(session_uuid, _dic_sessions_idel[session_uuid]);

                                dic_push.Add(session_uuid, _dic_wait_to_push[session_uuid]);
                                dic_session.Add(session_uuid, _dic_sessions_idel[session_uuid]);

                                _dic_sessions_idel.Remove(session_uuid);

                                _dic_wait_to_push.Remove(session_uuid);
                            }
                        }
                    }
                }
                #endregion

                #region 处理主动推送
                if (dic_push.Count > 0)
                {
                    foreach (var push_session in dic_push)
                    {
                        string         session_uuid = push_session.Key;
                        TSession       session      = dic_session[session_uuid];
                        List <dynamic> lst          = push_session.Value;
                        foreach (var push in lst)
                        {
                            Frame frame = push.Frame;
                            Action <string, Frame, bool> on_push_result = push.OnPushResult;
                            on_push_result(session_uuid, frame, session.Send(frame));
                        }
                    }
                    //将Session归还到数组
                    lock (_dic_sessions_lock)
                    {
                        foreach (var dic in dic_session)
                        {
                            string session_uuid = dic.Key;
                            _dic_sessions_dealing.Remove(session_uuid);
                            _dic_sessions_idel.Add(session_uuid, dic.Value);
                        }
                        dic_session.Clear();
                        dic_push.Clear();
                    }
                }
                #endregion

                #region 检查并设置超时未发送或接收数据的Session
                lock (_dic_sessions_lock)
                {
                    if (_dic_sessions_idel.Count > 0)
                    {
                        foreach (var dic in _dic_sessions_idel)
                        {
                            TSession sessioin                = dic.Value;
                            DateTime accept_time             = sessioin.GetAcceptTime();
                            DateTime?last_communication_time = sessioin.GetLastCommunicationTime();
                            DateTime now = DateTime.Now;
                            TimeSpan ts;
                            if (last_communication_time == null)
                            {
                                ts = now - accept_time;
                            }
                            else
                            {
                                ts = last_communication_time.Value - accept_time;
                            }
                            if (ts.TotalSeconds > _config.MaxKeepLiveSecond)
                            {
                                sessioin.SetOffline();
                            }
                        }
                    }
                }
                #endregion

                #region 检查是否有连接断开的session要处理
                List <TSession> lst_offline = new List <TSession>();
                lock (_dic_sessions_lock)
                {
                    if (_dic_sessions_idel.Count > 0)
                    {
                        string[] keys = _dic_sessions_idel.Keys.ToArray();
                        foreach (var key in keys)
                        {
                            TSession sessin = _dic_sessions_idel[key];
                            if (sessin.GetIsConnected())
                            {
                                continue;
                            }
                            _dic_sessions_idel.Remove(key);
                            lst_offline.Add(sessin);
                        }
                    }
                }
                #endregion

                #region 处理断开的Session
                if (lst_offline.Count > 0)
                {
                    foreach (var session in lst_offline)
                    {
                        session.OnSessionStop();
                    }
                    lst_offline.Clear();
                }
                #endregion

                Thread.Sleep(15);
            }
        }