/** * 将协议封包 * @param mixed $proto 协议 * @param bool $autoSession 自动填充session * @return CatLib\Constraint\Network\IPacket */ public function packet($proto, $autoSession = false) { $packet = $this->parser->serialize($proto); if ($autoSession) { $packet->setSessionID($this->request->sessionID()); } return $packet; }
/** * 获取控制器 * @param \CatLib\Constraint\Network\IRequest $request 请求数据 * @return array() */ protected function getController(IRequest $request) { $match = $this->router->match($request->packet()->protoID()); $namespace = Server::isTask() ? $this->namespaceTask : $this->namespace; if ($match != null) { $cls = $namespace . $match["controller"]; $func = $match["func"]; } else { $cls = $namespace . $request->protoName(); $func = $this->func; } $controller = $this->app[$cls]; if ($controller instanceof Controller) { return [$controller, $func]; } return [null, $func]; }
/** * 快速设定回应协议 * @param string $field 字段名 * @param string $data 数据 * @return void */ public function set($field, $data) { if (Server::isTask()) { return; } if ($this->simpleResponseProto == null) { $this->simpleResponseProto = $this->parser->makeResponseProto($this->request->proto()); if ($this->simpleResponseProto == null) { return; } } $this->parser->set($this->simpleResponseProto, $field, $data); }
/** * 收到来自客户端的请求 * @param string $data 二进制数据 * @param string $fd 客户端唯一标识符 * @param string $fromID 来自于哪个响应器ID */ public function handle($data, $fd, $fromID) { $this->response->clear(); $this->request->init($data, $fd, $fromID); return $this->sendRequestThroughRoute(); }