public function getPacket($uid)
 {
     if ($message = Cache::getInstance()->lPop($this->getCacheKey($uid))) {
         return ConvertUtil::unpack($message);
     }
     return null;
 }
Example #2
0
 public function getUser($account)
 {
     if ($str = Cache::getInstance()->get($this->getCacheKey($account))) {
         return ConvertUtil::unpack($str);
     }
     return null;
 }
Example #3
0
 public function onSwoolePipeMessage(swoole_server $server, $from_worker_id, $message)
 {
     /**
      * @var SwooleSession $session
      * @var Packet $packet
      */
     Logger::addInfo('got pipe message');
     $sessionInfo = unpack('N', substr($message, 0, 4));
     $packetInfo = unpack('N', substr($message, 4, 4));
     if (isset($sessionInfo[1], $packetInfo[1]) && strlen($message) == $sessionInfo[1] + $packetInfo[1] + 8) {
         if ($session = ConvertUtil::unpack(substr($message, 8, $sessionInfo[1]))) {
             if ($packet = ConvertUtil::unpack(substr($message, 8 + $sessionInfo[1]))) {
                 $this->sendPacketOfCurrentProcess($session, $packet);
             } else {
                 Logger::addError('pipe crashed packet info');
             }
         } else {
             Logger::addError('pipe crashed session info');
         }
     } else {
         Logger::addError('pipe crashed length info');
     }
 }
Example #4
0
 /**
  * @param $sessionId
  * @return Session|null
  * @throws \Exception
  */
 public function getSessionBySessionId($sessionId)
 {
     if ($sessionId && ($sessionStr = Cache::getInstance()->get($this->getSessionIdCacheKey($sessionId)))) {
         if (($foundSession = ConvertUtil::unpack($sessionStr)) && $foundSession instanceof Session) {
             return $foundSession;
         }
     }
     return null;
 }
Example #5
0
 protected function parsePacket($str, &$sessionId, &$serverInfo)
 {
     $sessionId = $serverInfo = null;
     if ($arr = ConvertUtil::unpack($str)) {
         if (isset($arr[$this->serverKey])) {
             if (!($serverInfo = $this->getRegister($arr[$this->serverKey]))) {
                 return false;
             }
         }
         if (isset($arr[$this->sessionKey]) && $arr[$this->sessionKey]) {
             $sessionId = $arr[$this->sessionKey];
         }
         if ($packet = Packet::getPacketFromArray($arr)) {
             return $packet;
         }
     }
     return false;
 }
Example #6
0
/** @noinspection PhpUnusedParameterInspection */
$client->on("close", function (swoole_client $cli) {
    echo "Connection close\n";
});
$client->on("connect", function (swoole_client $cli) {
    // send a auth packet
    $packet = new AuthPacket();
    $packet->setFrom(0);
    $packet->setTo('system');
    $packet->setAccount(G::$account);
    $packet->setPassword(G::$password);
    $cli->send(serializePacket($packet));
});
$client->on("receive", function (swoole_client $cli, $message) {
    //echo 'receive: ',$message,"\n";
    if (($data = substr($message, 4)) && ($result = ConvertUtil::unpack($data)) && isset($result['from']) && isset($result['to']) && isset($result['type']) && isset($result['content'])) {
        G::$sessionId = isset($result['session_id']) ? $result['session_id'] : null;
        /* @var Packet $packet */
        $packet = Packet::getPacketFromArray($result);
        if (!G::$auth) {
            $authPacket = new AuthPacket();
            $authPacket->setFrom(G::$account);
            $authPacket->setTo('system');
            if ($packet->getType() == $authPacket->getType()) {
                $authPacket->setContent($packet->getContent());
                switch ($authPacket->getStatus()) {
                    case AuthPacket::STATUS_NEED_AUTH:
                        $authPacket->setAccount(G::$account);
                        $authPacket->setPassword(G::$password);
                        $cli->send(serializePacket($authPacket));
                        break;
Example #7
0
 protected function dealMessage($data)
 {
     echo "begin to deal message\n";
     if ($data && ($result = ConvertUtil::unpack($data)) && isset($result['from']) && isset($result['to']) && isset($result['type']) && isset($result['content'])) {
         $this->sessionId = isset($result['session_id']) ? $result['session_id'] : null;
         /* @var Packet $packet
          * @var AuthPacket $authPacket
          */
         $packet = Packet::getPacketFromArray($result);
         if (!$this->auth) {
             if ($authPacket = AuthPacket::getPacketFromPacket($packet)) {
                 switch ($authPacket->getStatus()) {
                     case AuthPacket::STATUS_NEED_AUTH:
                         echo "[ERROR] need auth\n";
                         break;
                     case AuthPacket::STATUS_AUTH_FAILED:
                         echo "[ERROR] auth failed\n";
                         break;
                     case AuthPacket::STATUS_AUTH_SUCCESS:
                         echo "[SUCCESS] auth success\n";
                         $this->auth = true;
                         $this->userId = $authPacket->getUid();
                         break;
                     default:
                         echo "[ERROR] auth status undefined!\n";
                 }
             } else {
                 echo "[ERROR] first packet is not auth packet\n";
             }
         }
         echo "---------RECEIVE----------------------------\n";
         echo "from: ", $packet->getFrom(), "\n";
         echo "to: ", $packet->getTo(), "\n";
         echo "type: ", $packet->getType(), "\n";
         echo "session: ", $this->sessionId, "\n";
         echo "content: ", json_encode($packet->getContent(), JSON_UNESCAPED_UNICODE), "\n\n";
     } else {
         echo "[ERROR] receive error\n";
     }
 }