2014-04-19 00:32:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ManiaPlanet dedicated server Xml-RPC client
|
|
|
|
*
|
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Maniaplanet\DedicatedServer\Xmlrpc;
|
|
|
|
|
|
|
|
class GbxRemote
|
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
const MAX_REQUEST_SIZE = 0x400000; // 4MB
|
2014-04-19 00:32:34 +02:00
|
|
|
const MAX_RESPONSE_SIZE = 0x400000; // 4MB
|
|
|
|
|
|
|
|
public static $received;
|
|
|
|
public static $sent;
|
|
|
|
|
|
|
|
private $socket;
|
2024-08-25 22:30:06 +02:00
|
|
|
private $readTimeout = ['sec' => 5, 'usec' => 0];
|
|
|
|
private $writeTimeout = ['sec' => 5, 'usec' => 0];
|
2014-04-19 00:32:34 +02:00
|
|
|
private $requestHandle;
|
2024-08-25 22:30:06 +02:00
|
|
|
private $callbacksBuffer = [];
|
|
|
|
private $multicallBuffer = [];
|
2014-04-19 00:32:34 +02:00
|
|
|
private $lastNetworkActivity = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $host
|
|
|
|
* @param int $port
|
2014-05-08 19:38:15 +02:00
|
|
|
* @param int $timeout Timeout when opening connection
|
2014-04-19 00:32:34 +02:00
|
|
|
*/
|
2014-05-08 19:38:15 +02:00
|
|
|
function __construct($host, $port, $timeout = 5)
|
2014-04-19 00:32:34 +02:00
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
$this->requestHandle = (int)0x80000000;
|
2014-05-08 19:38:15 +02:00
|
|
|
$this->connect($host, $port, $timeout);
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $host
|
|
|
|
* @param int $port
|
2014-05-08 19:38:15 +02:00
|
|
|
* @param int $timeout
|
2014-04-19 00:32:34 +02:00
|
|
|
* @throws TransportException
|
|
|
|
*/
|
2014-05-08 19:38:15 +02:00
|
|
|
private function connect($host, $port, $timeout)
|
2014-04-19 00:32:34 +02:00
|
|
|
{
|
2014-05-08 19:38:15 +02:00
|
|
|
$this->socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
|
2024-08-25 22:30:06 +02:00
|
|
|
if (!$this->socket) {
|
2014-04-19 00:32:34 +02:00
|
|
|
throw new TransportException('Cannot open socket', TransportException::NOT_INITIALIZED);
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
|
2014-05-20 15:20:16 +02:00
|
|
|
stream_set_read_buffer($this->socket, 0);
|
2014-04-20 18:00:40 +02:00
|
|
|
stream_set_write_buffer($this->socket, 0);
|
|
|
|
|
2014-04-19 00:32:34 +02:00
|
|
|
// handshake
|
|
|
|
$header = $this->read(15);
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($header === false) {
|
|
|
|
if (!is_resource($this->socket)) {
|
|
|
|
$this->onIoFailure('socket closed during handshake');
|
|
|
|
}
|
|
|
|
$this->onIoFailure(sprintf('during handshake (%s)', socket_strerror(socket_last_error())));
|
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
|
|
|
|
extract(unpack('Vsize/a*protocol', $header));
|
|
|
|
/** @var $size int */
|
|
|
|
/** @var $protocol string */
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($size != 11 || $protocol != 'GBXRemote 2') {
|
2014-04-19 00:32:34 +02:00
|
|
|
throw new TransportException('Wrong protocol header', TransportException::WRONG_PROTOCOL);
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
$this->lastNetworkActivity = time();
|
|
|
|
}
|
|
|
|
|
2024-08-25 22:30:06 +02:00
|
|
|
/**
|
|
|
|
* @param int $size
|
|
|
|
* @return boolean|string
|
|
|
|
*/
|
|
|
|
private function read($size)
|
2014-04-19 00:32:34 +02:00
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
@stream_set_timeout($this->socket, $this->readTimeout['sec'], $this->readTimeout['usec']);
|
|
|
|
|
|
|
|
$data = '';
|
|
|
|
while (strlen($data) < $size) {
|
|
|
|
$buf = @fread($this->socket, $size - strlen($data));
|
|
|
|
if ($buf === '' || $buf === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$data .= $buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$received += $size;
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $when
|
|
|
|
* @throws TransportException
|
|
|
|
*/
|
|
|
|
private function onIoFailure($when)
|
|
|
|
{
|
|
|
|
$meta = stream_get_meta_data($this->socket);
|
|
|
|
if ($meta['timed_out']) {
|
|
|
|
throw new TransportException('Connection timed out ' . $when, TransportException::TIMED_OUT);
|
|
|
|
}
|
|
|
|
throw new TransportException('Connection interrupted ' . $when, TransportException::INTERRUPTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
function __destruct()
|
|
|
|
{
|
|
|
|
$this->terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function terminate()
|
|
|
|
{
|
|
|
|
if ($this->socket) {
|
2014-04-19 00:32:34 +02:00
|
|
|
fclose($this->socket);
|
|
|
|
$this->socket = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-08-25 22:30:06 +02:00
|
|
|
* Change timeouts
|
|
|
|
* @param int $read read timeout (in ms), 0 to leave unchanged
|
|
|
|
* @param int $write write timeout (in ms), 0 to leave unchanged
|
2014-04-19 00:32:34 +02:00
|
|
|
*/
|
2024-08-25 22:30:06 +02:00
|
|
|
public function setTimeouts($read = 0, $write = 0)
|
2014-04-19 00:32:34 +02:00
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($read) {
|
|
|
|
$this->readTimeout['sec'] = (int)($read / 1000);
|
|
|
|
$this->readTimeout['usec'] = ($read % 1000) * 1000;
|
|
|
|
}
|
|
|
|
if ($write) {
|
|
|
|
$this->writeTimeout['sec'] = (int)($write / 1000);
|
|
|
|
$this->writeTimeout['usec'] = ($write % 1000) * 1000;
|
|
|
|
}
|
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
|
2024-08-25 22:30:06 +02:00
|
|
|
/**
|
|
|
|
* @return int Network idle time in seconds
|
|
|
|
*/
|
|
|
|
function getIdleTime()
|
|
|
|
{
|
|
|
|
$this->assertConnected();
|
|
|
|
return time() - $this->lastNetworkActivity;
|
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
|
2024-08-25 22:30:06 +02:00
|
|
|
/**
|
|
|
|
* @throws TransportException
|
|
|
|
*/
|
|
|
|
private function assertConnected()
|
|
|
|
{
|
|
|
|
if (!$this->socket) {
|
|
|
|
throw new TransportException('Connection not initialized', TransportException::NOT_INITIALIZED);
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $method
|
|
|
|
* @param mixed[] $args
|
|
|
|
*/
|
|
|
|
function addCall($method, $args)
|
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
$this->multicallBuffer[] = [
|
2014-04-19 00:32:34 +02:00
|
|
|
'methodName' => $method,
|
|
|
|
'params' => $args
|
2024-08-25 22:30:06 +02:00
|
|
|
];
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function multiquery()
|
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
switch (count($this->multicallBuffer)) {
|
2014-04-19 00:32:34 +02:00
|
|
|
case 0:
|
2024-08-25 22:30:06 +02:00
|
|
|
return [];
|
2014-04-19 00:32:34 +02:00
|
|
|
case 1:
|
|
|
|
$call = array_shift($this->multicallBuffer);
|
2024-08-25 22:30:06 +02:00
|
|
|
return [$this->query($call['methodName'], $call['params'])];
|
2014-04-19 00:32:34 +02:00
|
|
|
default:
|
2024-08-25 22:30:06 +02:00
|
|
|
$result = $this->query('system.multicall', [$this->multicallBuffer]);
|
|
|
|
foreach ($result as &$value) {
|
|
|
|
if (isset($value['faultCode'])) {
|
2014-06-12 15:39:50 +02:00
|
|
|
$value = FaultException::create($value['faultString'], $value['faultCode']);
|
2024-08-25 22:30:06 +02:00
|
|
|
} else {
|
2014-06-12 15:39:50 +02:00
|
|
|
$value = $value[0];
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->multicallBuffer = [];
|
2014-04-19 00:32:34 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-08-25 22:30:06 +02:00
|
|
|
* @param string $method
|
|
|
|
* @param mixed[] $args
|
|
|
|
* @return mixed
|
|
|
|
* @throws MessageException
|
2014-04-19 00:32:34 +02:00
|
|
|
*/
|
2024-08-25 22:30:06 +02:00
|
|
|
function query($method, $args = [])
|
2014-04-19 00:32:34 +02:00
|
|
|
{
|
|
|
|
$this->assertConnected();
|
2024-08-25 22:30:06 +02:00
|
|
|
$xml = Request::encode($method, $args);
|
|
|
|
|
|
|
|
if (strlen($xml) > self::MAX_REQUEST_SIZE - 8) {
|
|
|
|
if ($method != 'system.multicall' || count($args[0]) < 2) {
|
|
|
|
throw new MessageException('Request too large', MessageException::REQUEST_TOO_LARGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
$mid = count($args[0]) >> 1;
|
|
|
|
$res1 = $this->query('system.multicall', [array_slice($args[0], 0, $mid)]);
|
|
|
|
$res2 = $this->query('system.multicall', [array_slice($args[0], $mid)]);
|
|
|
|
return array_merge($res1, $res2);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->writeMessage($xml);
|
|
|
|
return $this->flush(true);
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-08-25 22:30:06 +02:00
|
|
|
* @param string $xml
|
2014-04-19 00:32:34 +02:00
|
|
|
* @throws TransportException
|
|
|
|
*/
|
2024-08-25 22:30:06 +02:00
|
|
|
private function writeMessage($xml)
|
2014-04-19 00:32:34 +02:00
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($this->requestHandle == (int)0xffffffff) {
|
|
|
|
$this->requestHandle = (int)0x80000000;
|
|
|
|
}
|
|
|
|
$data = pack('V2', strlen($xml), ++$this->requestHandle) . $xml;
|
|
|
|
if (!$this->write($data)) {
|
|
|
|
$this->onIoFailure('while writing');
|
|
|
|
}
|
|
|
|
$this->lastNetworkActivity = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function write($data)
|
|
|
|
{
|
|
|
|
@stream_set_timeout($this->socket, $this->writeTimeout['sec'], $this->writeTimeout['usec']);
|
|
|
|
self::$sent += strlen($data);
|
|
|
|
|
|
|
|
while (strlen($data) > 0) {
|
|
|
|
$written = @fwrite($this->socket, $data);
|
|
|
|
if ($written === 0 || $written === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = substr($data, $written);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $waitResponse
|
|
|
|
* @return mixed
|
|
|
|
* @throws FaultException
|
|
|
|
*/
|
2024-08-25 22:30:06 +02:00
|
|
|
private function flush($waitResponse = false)
|
2014-04-19 00:32:34 +02:00
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
$r = [$this->socket];
|
|
|
|
while ($waitResponse || @stream_select($r, $w, $e, 0) > 0) {
|
2014-04-19 00:32:34 +02:00
|
|
|
list($handle, $xml) = $this->readMessage();
|
|
|
|
list($type, $value) = Request::decode($xml);
|
2024-08-25 22:30:06 +02:00
|
|
|
switch ($type) {
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'fault':
|
|
|
|
throw FaultException::create($value['faultString'], $value['faultCode']);
|
|
|
|
case 'response':
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($handle == $this->requestHandle) {
|
2014-04-19 00:32:34 +02:00
|
|
|
return $value;
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
break;
|
|
|
|
case 'call':
|
|
|
|
$this->callbacksBuffer[] = $value;
|
|
|
|
}
|
2014-05-20 15:20:16 +02:00
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed[]
|
|
|
|
* @throws TransportException
|
|
|
|
* @throws MessageException
|
|
|
|
*/
|
|
|
|
private function readMessage()
|
|
|
|
{
|
|
|
|
$header = $this->read(8);
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($header === false) {
|
2014-05-20 15:20:16 +02:00
|
|
|
$this->onIoFailure('while reading header');
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
|
|
|
|
extract(unpack('Vsize/Vhandle', $header));
|
|
|
|
/** @var $size int */
|
|
|
|
/** @var $handle int */
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($size == 0 || $handle == 0) {
|
2014-04-19 00:32:34 +02:00
|
|
|
throw new TransportException('Incorrect header', TransportException::PROTOCOL_ERROR);
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($size > self::MAX_RESPONSE_SIZE) {
|
2014-04-19 00:32:34 +02:00
|
|
|
throw new MessageException('Response too large', MessageException::RESPONSE_TOO_LARGE);
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
|
|
|
|
$data = $this->read($size);
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($data === false) {
|
2014-05-20 15:20:16 +02:00
|
|
|
$this->onIoFailure('while reading data');
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
|
2024-08-25 22:30:06 +02:00
|
|
|
$this->lastNetworkActivity = time();
|
|
|
|
return [$handle, $data];
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
2014-05-20 15:20:16 +02:00
|
|
|
|
|
|
|
/**
|
2024-08-25 22:30:06 +02:00
|
|
|
* @return mixed[]
|
2014-05-20 15:20:16 +02:00
|
|
|
*/
|
2024-08-25 22:30:06 +02:00
|
|
|
function getCallbacks()
|
2014-05-20 15:20:16 +02:00
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
$this->assertConnected();
|
|
|
|
$this->flush();
|
|
|
|
$cb = $this->callbacksBuffer;
|
|
|
|
$this->callbacksBuffer = [];
|
|
|
|
return $cb;
|
2014-05-20 15:20:16 +02:00
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class TransportException extends Exception
|
|
|
|
{
|
|
|
|
const NOT_INITIALIZED = 1;
|
2024-08-25 22:30:06 +02:00
|
|
|
const INTERRUPTED = 2;
|
|
|
|
const TIMED_OUT = 3;
|
|
|
|
const WRONG_PROTOCOL = 4;
|
|
|
|
const PROTOCOL_ERROR = 5;
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class MessageException extends Exception
|
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
const REQUEST_TOO_LARGE = 1;
|
2014-04-19 00:32:34 +02:00
|
|
|
const RESPONSE_TOO_LARGE = 2;
|
|
|
|
}
|