/** * 触发修饰器时 * @param \CatLib\Constraint\Network\IResponse | false $response 响应 * @param Closure $next 下一步操作 */ public function handle($response, Closure $next) { if ($response === false) { return $next($response); } $response->end(); $sendQueue = $response->getSendQueue(); foreach ($sendQueue as $fd => $protoList) { if ($fd <= 0) { continue; } $packList = []; foreach ($protoList as $proto) { $packet = $this->parser->serialize($proto); $packet->setSessionID($response->sessionID()); $packList[] = $packet; } $sendData = ""; foreach ($packList as $packet) { $sendData = $sendData . $packet->serialize(); } $length = strlen($sendData); $sendData = pack("C*", $length >> 8 & 0xff, $length & 0xff) . $sendData; Server::send($sendData, $fd); } return $next($response); }
/** * 将协议封包 * @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 string $field 字段名 * @return string */ public function get($field) { if (!is_string($field)) { return null; } return $this->parser->get($this->proto(), $field); }
/** * 快速设定回应协议 * @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); }