/** * 接收到数据时回调此函数,发生在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)); }