Exemplo n.º 1
0
 /**
  * 请求调度。
  * 
  * @param array $argv    指定 CLI 模式运行时的命令行参数集合。
  * @param array $def_cnf 指定系统全局配置参数。
  */
 function dispatch(&$argv, &$def_cnf)
 {
     if (0 != strcmp(PHP_SAPI, 'cli')) {
         echo '此脚本仅支持 cli 模式运行!', PHP_EOL;
         exit(0);
     }
     // 载入命令行参数
     $this->argv = $argv;
     include SYS_CONF . 'cmd.inc.php';
     $this->cmds = $g_cmd_hash;
     $this->timestamp = time();
     $this->_start_ms = microtime(true);
     $this->cfgs['g'] = $def_cnf;
     // 设置系统全局参数
     date_default_timezone_set($this->_timeZone);
     error_reporting($this->_errorLevel);
     set_error_handler(array($this, 'defErrorHandler'), $this->_errorLevel);
     set_exception_handler(array($this, 'defExceptionHandler'));
     // 初始化全局基础对象
     $this->app = new Application($this);
     Console::initialize($this, true);
     // 实例化基础对象 ...
     $this->logger = new Logger($this);
     $this->db = new DbPdo($this->_dbAutoCommit, $this->_dbPersistent);
     $this->db->addDb($this->cfgs['g']['db']);
     // 初始化 Swoole 实例 ...
     $this->_as = new AppServer($this);
     $this->_ci = new \swoole_server($this->_host, $this->_port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
     $this->_ci->set($this->cfgs['g']['swoole']);
     $this->_ci->on('start', array($this->_as, 'onStart'));
     $this->_ci->on('shutdown', array($this->_as, 'onShutdown'));
     $this->_ci->on('connect', array($this->_as, 'onConnect'));
     $this->_ci->on('close', array($this->_as, 'onClose'));
     $this->_ci->on('workerStart', array($this->_as, 'onWorkerStart'));
     $this->_ci->on('workerStop', array($this->_as, 'onWorkerStop'));
     $this->_ci->on('masterConnect', array($this->_as, 'onMasterConnect'));
     $this->_ci->on('masterClose', array($this->_as, 'onMasterClose'));
     $this->_ci->on('receive', array($this->_as, 'onReceive'));
     $this->_ci->on('timer', array($this->_as, 'onTimer'));
     $this->_ci->on('task', array($this->_as, 'onTask'));
     $this->_ci->on('finish', array($this->_as, 'onFinish'));
     if (!empty($this->_listeners)) {
         foreach ($this->_listeners as $v) {
             $this->_ci->addlistener($v[0], $v[1], SWOOLE_SOCK_TCP);
         }
     }
     $this->_ci->start();
 }