/** * Some helper methods */ private function getTestUserClient($sessionId) { $config = Config::get('pushup')->server['default']; list($host, $port) = explode(':', array_shift($config['cluster'])); $tcpClient = new SC_TcpClient(); $tcpClient->setTimeout($config['connectTimeout'] * 50); $tcpClient->connect($host, $port); $tcpClient->write($sessionId); return $tcpClient; }
/** * send data and process response * * @param array $data data to send * @return mixed response */ protected function command($data) { $this->connect(); // enter multibyte workaround mode if (ini_get('mbstring.func_overload') & 2) { if ($this->mbenc === null) { $this->mbenc = mb_internal_encoding(); } mb_internal_encoding('latin1'); } // build request $request = $this->serialize($data); // send request try { $this->TcpClient->write($request); } catch (SC_TcpClient_Exception $e) { throw new SC_Pushup_Exception($e->getMessage()); } // read response try { $response = $this->TcpClient->read(2048); if (empty($response)) { throw new SC_Pushup_Exception('empty response'); } // get response length $header = unpack('Nsize', substr($response, 0, 4)); // invalid response if ($header === false) { throw new SC_Pushup_Exception('invalid response, cannot unpack header data'); } // read remaining response data $size = $header['size'] + 4; while (strlen($response) < $size) { $response .= $this->TcpClient->read($size - strlen($response)); } } catch (SC_TcpClient_Exception $e) { $this->disconnect(); SC_Log::get('pushup')->error('network error: ' . $e->getMessage()); throw new SC_Pushup_Exception('network error'); } // process response $response = $this->deserialize($response); // leave multibyte workaround mode if ($this->mbenc !== null) { mb_internal_encoding($this->mbenc); $this->mbenc = null; } // check for errors if (!is_array($response) || empty($response)) { throw new SC_Pushup_Exception('command response error'); } if (($responseState = array_shift($response)) == self::COMMAND_ERROR) { throw new SC_Pushup_Exception(array_shift($response)); } // return result if (empty($response)) { switch ($responseState) { case self::COMMAND_OK: return true; case self::LOGIN_OK: return true; case self::LOGIN_FAILED: throw new SC_Pushup_Exception('login failed'); } } return $response; }