protected function sendPacketOfCurrentProcess(Session $session, Packet $packet) { $previousReachable = $this->isFdReachable($session->getFd()); $session->setReachable($previousReachable); if (false !== $this->dispatchSend($session, $packet)) { // if send buffer is full or other reason, send will failed, so second dispatch packet and tell them session not reachable $data = $packet->convertToArray(); $data[$this->sessionKey] = $session->getSessionId(); $packetStr = json_encode($data); $encodedPacketStr = $this->wsBuffProcessor->encode($packetStr); if (!$this->instance->send($session->getFd(), $encodedPacketStr) && $previousReachable) { $session->setReachable(false); Logger::addInfo('send directly failed, dispatch again (fd:' . $session->getFd() . ') '); $this->dispatchSend($session, $packet); } else { Logger::addInfo('send directly success (fd:' . $session->getFd() . ') |' . $encodedPacketStr . '|' . $packetStr . '|'); } } else { Logger::addInfo('send dispatch failed (fd:' . $session->getFd() . ') '); } }
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); } }