public function getUser($account) { if ($str = Cache::getInstance()->get($this->getCacheKey($account))) { return ConvertUtil::unpack($str); } 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); } }
/** * @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; }
public function addPacket($uid, Packet $packet) { Cache::getInstance()->rPush($this->getCacheKey($uid), ConvertUtil::pack($packet)); }
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; }
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; }