/** * 请求调度。 * * @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(); }
/** * 接收到数据时回调此函数,发生在worker进程中。 * * @param ISwoole $sw * @param int $fd * @param int $from_id * @param string $data */ function onReceive($sw, $fd, $from_id, $data) { // 读取客户端来源信息 ... $client_info = $sw->connection_info($fd); Console::debug('[接收数据] ', $data); // 解析客户端数据协议 ... $data_s = DataParser::decode($data); Console::debug('[OnReceive][Client IP: ', $client_info['remote_ip'], ', From: ', $client_info['from_port'], '] ', $data_s); if (!isset($this->ctx->cmds[$data_s['cmd']])) { Console::error('无效的命令编号(' . $data_s['cmd'] . ')。'); return false; } // 实例化 IController 控制器对象并执行命令方法 ... $cls_n = $this->ctx->getControllerNs() . '\\' . $this->ctx->cmds[$data_s['cmd']][0]; $cls_m = $this->ctx->cmds[$data_s['cmd']][1]; $cls_o = new $cls_n($this->ctx, $sw, $fd, $client_info, $this->ctx->cmds[$data_s['cmd']]); if ($cls_o instanceof IController) { $cls_o->initialize(); $cls_o->{$cls_m}($data_s); $cls_o->dispose(); } $cls_o = NULL; }
/** * 发送错误信息给客户端。 * * @param int $errno * @param string $errstr */ protected function error($errno, $errstr) { $this->serv->send($this->fd, DataParser::std($this->cmd_data[2], $this->cmd_data[3], NULL, $errno, $errstr)); }