Beispiel #1
0
 public function dispatchReceivePacket(Packet $packet, Session $session)
 {
     if ($session->isAuth()) {
         return true;
     }
     /* @var \jegarn\session\SwooleSession $session
      * @var AuthPacket $pkt
      */
     if ($pkt = AuthPacket::getPacketFromPacket($packet)) {
         if ($pkt->auth()) {
             $session->setUserId($pkt->getUid());
             $session->setAuth(true);
             $session->setSessionId(TextUtil::generateGUID());
             SessionManager::getInstance()->addSession($session);
             $pkt->setStatus(AuthPacket::STATUS_AUTH_SUCCESS);
         } else {
             $pkt->setStatus(AuthPacket::STATUS_AUTH_FAILED);
             Logger::addInfo('auth failed(fd:' . $session->getFd() . ')');
         }
     } else {
         $pkt = new AuthPacket();
         $pkt->setPacket($packet);
         $pkt->setStatus(AuthPacket::STATUS_NEED_AUTH);
         Logger::addInfo('auth needed(fd:' . $session->getFd() . ')');
     }
     ServerManager::getInstance()->send($session, $pkt->getReversedPacket());
     return false;
 }
Beispiel #2
0
    // 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;
                    case AuthPacket::STATUS_AUTH_FAILED:
                        echo "error: auth failed\n";
                        break;
                    case AuthPacket::STATUS_AUTH_SUCCESS:
                        echo "auth success\n";
Beispiel #3
0
 public function sendAuthPacket()
 {
     $packet = new AuthPacket();
     $packet->setFrom(0);
     $packet->setTo('system');
     $packet->setAccount($this->account);
     $packet->setPassword($this->password);
     $this->send($this->serializePacket($packet));
 }