public function addSession(Session $session) { if ($this->isValidSession($session)) { $cache = Cache::getInstance(); $sessionStr = ConvertUtil::pack($session); // check is there has last dirty session if ($dirty = $this->getSessionByUserId($session->getUserId())) { $cache->del($this->getSessionIdCacheKey($dirty->getSessionId()), $this->getUserIdCacheKey($dirty->getUserId())); } $cache->set($this->getUserIdCacheKey($session->getUserId()), $sessionStr); $cache->set($this->getSessionIdCacheKey($session->getSessionId()), $sessionStr); // cache session $this->fdSessionInfoMap[$session->getFd()] = $session->getUserId() . ' ' . $session->getSessionId(); return $this->triggerEvent('addSession', $session); } return null; }
public function sendToRemoteServer(Session $session, Packet $packet) { $data = $packet->convertToArray(); $data[$this->serverKey] = $this->serverId; $packetStr = ConvertUtil::pack($data); $ret = SocketManager::getInstance()->sendClientMessage($session->getServerAddress(), $session->getServerPort(), pack('N', strlen($packetStr)) . $packetStr, $this->config); if (false === $ret) { Logger::addInfo('send remote server failed, dispatch again (fd:' . $session->getFd() . ') '); $session->setReachable(false); $this->dispatchSend($session, $packet); } }
/** * account and password for user auth * when your application has no uid, you can set uid same as account * notice: uid and account must be unique * @param integer|string $uid * @param string $account * @param string $password * @throws \Exception */ public function addUser($uid, $account, $password) { Cache::getInstance()->set($this->getCacheKey($account), ConvertUtil::pack(['uid' => $uid, 'account' => $account, 'password' => $this->encryptPassword($password)])); }
public function addPacket($uid, Packet $packet) { Cache::getInstance()->rPush($this->getCacheKey($uid), ConvertUtil::pack($packet)); }
function serializePacket(Packet $packet) { $data = ['from' => $packet->getFrom(), 'to' => $packet->getTo(), 'type' => $packet->getType(), 'content' => $packet->getContent()]; $data['session_id'] = G::$sessionId; echo "-----------SEND--------------------------------\n"; echo 'send: ', json_encode($data, JSON_UNESCAPED_UNICODE), "\n"; $data = ConvertUtil::pack($data); $data = pack('N', strlen($data)) . $data; echo "packet length: ", strlen($data) . "\n\n"; return $data; }