Example #1
0
 /**
  * 触发修饰器时
  * @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);
 }
Example #2
0
 /**
  * 将协议封包
  * @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;
 }