Esempio n. 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;
 }
Esempio n. 2
0
 public function dispatchReceivePacket(Packet $packet, Session $session)
 {
     if (!$session->isAuth()) {
         return;
     }
     /* @var \jegarn\session\SwooleSession $session
      * @var GroupChatPacket $pkt
      */
     if ($pkt = ChatroomPacket::getPacketFromPacket($packet)) {
         // check send user is a member of this group
         $cm = ChatroomManager::getInstance();
         if ($packet->isFromSystemUser() || $cm->isGroupUser($pkt->getGroupId(), $pkt->getFrom())) {
             $fromUid = $pkt->getFrom();
             if ($uidList = $cm->getGroupUsers($pkt->getGroupId())) {
                 $sm = ServerManager::getInstance();
                 foreach ($uidList as $uid) {
                     if ($uid != $fromUid) {
                         $pktCopy = clone $pkt;
                         $pktCopy->setTo($uid);
                         if ($toSession = SessionManager::getInstance()->getSessionByUserId($uid)) {
                             Logger::addInfo('user online,chatroom(uid:' . $uid . ')');
                             $sm->send($toSession, $pktCopy);
                         } else {
                             Logger::addInfo('user not online,chatroom,abandon message(uid:' . $uid . ')');
                         }
                     }
                 }
             } else {
                 Logger::addInfo('chatroom ' . $pkt->getGroupId() . ' users lost, from uid ' . $pkt->getFrom());
             }
         } else {
             Logger::addInfo('chatroom ' . $pkt->getGroupId() . ' has no user ' . $pkt->getFrom() . ' and it is not a system user');
         }
     }
 }
Esempio n. 3
0
 public function onSwooleReceive(swoole_server $server, $fd, $from_id, $message)
 {
     $this->rBuffProcessor->append($fd, $message);
     $session = $this->getSwooleSession($server, $fd, $from_id);
     $sessionManager = SessionManager::getInstance();
     for (;;) {
         $packetStr = $this->rBuffProcessor->consumePacket($fd);
         if (is_string($packetStr)) {
             if ($packet = self::parsePacket($packetStr, $sessionId, $serverInfo)) {
                 Logger::addInfo('parsed packet info: (fd: ' . $fd . ')' . str_replace("\n", ' ', var_export($packet, true)) . 'session: ' . $sessionId . '|serverInfo: ' . $serverInfo);
                 if ($serverInfo) {
                     // internal server can send to any user and any where
                     if ($toUserSession = $sessionManager->getSessionByUserId($packet->getTo())) {
                         Logger::addInfo('internal server toUser online: (fd: ' . $fd . ')');
                         $this->send($toUserSession, $packet);
                     } else {
                         Logger::addInfo('internal server toUser not online: (fd: ' . $fd . ')');
                         $toUserSession = new SwooleSession();
                         $toUserSession->setAuth(true);
                         $toUserSession->setUserId($packet->getTo());
                         $this->send($toUserSession, $packet);
                     }
                 } else {
                     if ($packet->isFromSystemUser()) {
                         Logger::addInfo('fake user deny: (fd: ' . $fd . ')');
                         break;
                     } else {
                         // come from socket, and it must be the same user, so if is a valid session. don't need to get from cache
                         if (!$sessionManager->isValidSession($session) && ($fs = $sessionManager->getSessionBySessionId($sessionId))) {
                             $session = $fs;
                         }
                         if (false === $this->dispatchReceive($session, $packet)) {
                             Logger::addInfo('dispatch receive failed: (fd: ' . $fd . ')');
                             break;
                         }
                     }
                 }
             } else {
                 $this->increaseError();
                 Logger::addError('str can not convert packet[SwooleServer](fd: ' . $fd . ')[BEGIN]' . $packetStr . '[END]');
                 continue;
             }
         } else {
             if (false === $packetStr) {
                 $this->increaseError();
                 Logger::addError('packet parse failed(fd: ' . $fd . ')');
                 break;
             } else {
                 break;
             }
         }
     }
 }
Esempio n. 4
0
 protected function sendOrSavePacket(GroupChatPacket $packet, $uid)
 {
     $pkt = clone $packet;
     $pkt->setTo($uid);
     if ($toSession = SessionManager::getInstance()->getSessionByUserId($uid)) {
         Logger::addInfo('user online,groupchat(uid:' . $uid . ')');
         ServerManager::getInstance()->send($toSession, $pkt);
     } else {
         Logger::addInfo('user not online,groupchat,storage message(uid:' . $uid . ')');
         OfflineGroupMessageManager::getInstance()->addPacket($uid, $pkt);
     }
 }
 public function dispatchReceivePacket(Packet $packet, Session $session)
 {
     /* @var \jegarn\session\SwooleSession $toUserSession
      * @var ChatPacket $pkt
      */
     if ($session->isAuth()) {
         if ($pkt = NotificationPacket::getPacketFromPacket($packet)) {
             if ($toUserSession = SessionManager::getInstance()->getSessionByUserId($packet->getTo())) {
                 Logger::addInfo('user online,notification(uid:' . $packet->getTo() . ')');
                 ServerManager::getInstance()->send($toUserSession, $pkt);
             } else {
                 Logger::addInfo('user offline,storage notification(uid:' . $packet->getTo() . ')');
                 OfflineNotificationMessageManager::getInstance()->addPacket($packet->getTo(), $pkt);
             }
         }
     }
 }
Esempio n. 6
0
 protected function dealWebsocketPacket($server, $fd, $from_id)
 {
     $session = $this->getSwooleSession($server, $fd, $from_id);
     $sessionManager = SessionManager::getInstance();
     for (;;) {
         $packetStr = $this->wsBuffProcessor->consumePacket($fd, $response);
         if ($response == WebsocketBufferProcessor::$PONG) {
             Logger::addInfo('send pong packet : (fd: ' . $fd . ')');
             $this->instance->send($fd, WebsocketBufferProcessor::$PONG);
         } else {
             if ($response == WebsocketBufferProcessor::$CLOSE) {
                 $this->close($session);
                 // browser not close the connection but send a close packet, so close it manual.
             }
         }
         if (is_string($packetStr)) {
             if ($packet = $this->parsePacket($packetStr, $sessionId, $serverInfo)) {
                 Logger::addInfo('parsed packet info: (fd: ' . $fd . ')' . str_replace("\n", ' ', var_export($packet, true)) . 'session: ' . $sessionId . '|serverInfo: ' . $serverInfo);
                 if (!$sessionManager->isValidSession($session) && $sessionId && ($fs = $sessionManager->getSessionBySessionId($sessionId))) {
                     $session = $fs;
                 }
                 if (false === $this->dispatchReceive($session, $packet)) {
                     Logger::addInfo('dispatch receive failed: (fd: ' . $fd . ')');
                 }
             } else {
                 $this->increaseError();
                 Logger::addError('str can not convert packet(fd: ' . $fd . ')[BEGIN]' . $packetStr . '[END]');
             }
         } else {
             if (false === $packetStr) {
                 $this->increaseError();
                 Logger::addError('packet parse failed(fd: ' . $fd . ')');
                 break;
             } else {
                 break;
             }
         }
     }
 }
Esempio n. 7
0
 public function dispatchClose(Session $session)
 {
     $this->increaseClose();
     return SessionManager::getInstance()->removeSession($session);
 }