dedicated server api update

This commit is contained in:
kremsy
2014-05-08 19:28:45 +02:00
parent 55850c5ca9
commit 77a53b282c
49 changed files with 2050 additions and 2411 deletions

View File

@ -19,6 +19,12 @@ class Base64
{
$this->scalar = $data;
}
}
?>
/**
* @return string
*/
function __toString()
{
return $this->scalar;
}
}

View File

@ -8,5 +8,3 @@
namespace Maniaplanet\DedicatedServer\Xmlrpc;
class Exception extends \Exception {}
?>

View File

@ -57,5 +57,3 @@ class MapNotCompatibleOrCompleteException extends FaultException{}
class LadderModeUnknownException extends FaultException{}
class PlayerAlreadyIgnoredException extends FaultException{}
class PlayerNotIgnoredException extends FaultException{}
?>

View File

@ -17,7 +17,6 @@ class GbxRemote
private $socket;
private $timeouts = array(
'open' => 5,
'read' => 5000,
'write' => 5000
);
@ -29,13 +28,12 @@ class GbxRemote
/**
* @param string $host
* @param int $port
* @param int[string] $timeouts Override default timeouts for 'open' (in s), 'read' (in ms) and 'write' (in ms) socket operations
* @param int $timeout Timeout when opening connection
*/
function __construct($host, $port, $timeouts = array())
function __construct($host, $port, $timeout = 5)
{
$this->requestHandle = (int) 0x80000000;
$this->timeouts = array_merge($this->timeouts, $timeouts);
$this->connect($host, $port);
$this->connect($host, $port, $timeout);
}
function __destruct()
@ -45,10 +43,10 @@ class GbxRemote
/**
* Change timeouts
* @param int $read read timeout (in ms), null or 0 to leave unchanged
* @param int $write write timeout (in ms), null or 0 to leave unchanged
* @param int $read read timeout (in ms), 0 to leave unchanged
* @param int $write write timeout (in ms), 0 to leave unchanged
*/
function setTimeouts($read=null, $write=null)
function setTimeouts($read=0, $write=0)
{
if($read)
$this->timeouts['read'] = $read;
@ -68,12 +66,12 @@ class GbxRemote
/**
* @param string $host
* @param int $port
* @param int $timeout
* @throws TransportException
*/
private function connect($host, $port)
private function connect($host, $port, $timeout)
{
$this->socket = @fsockopen($host, $port, $errno, $errstr, $this->timeouts['open']);
$this->socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
if(!$this->socket)
throw new TransportException('Cannot open socket', TransportException::NOT_INITIALIZED);
@ -114,12 +112,13 @@ class GbxRemote
if(strlen($xml) > self::MAX_REQUEST_SIZE-8)
{
if($method != 'system.multicall' || count($args) < 2)
if($method != 'system.multicall' || count($args[0]) < 2)
throw new MessageException('Request too large', MessageException::REQUEST_TOO_LARGE);
$mid = count($args) >> 1;
$this->query('system.multicall', array_slice($args, 0, $mid));
$this->query('system.multicall', array_slice($args, $mid));
$mid = count($args[0]) >> 1;
$res1 = $this->query('system.multicall', array(array_slice($args[0], 0, $mid)));
$res2 = $this->query('system.multicall', array(array_slice($args[0], $mid)));
return array_merge($res1, $res2);
}
$this->writeMessage($xml);
@ -151,7 +150,7 @@ class GbxRemote
$call = array_shift($this->multicallBuffer);
return $this->query($call['methodName'], $call['params']);
default:
$result = $this->query('system.multicall', $this->multicallBuffer);
$result = $this->query('system.multicall', array($this->multicallBuffer));
$this->multicallBuffer = array();
return $result;
}
@ -185,9 +184,9 @@ class GbxRemote
*/
private function flush($waitResponse=false)
{
$r=array($this->socket);
$w=null;
$e=null;
$r = array($this->socket);
$w = null;
$e = null;
$n = @stream_select($r, $w, $e, 0);
while($waitResponse || $n > 0)
{
@ -205,12 +204,8 @@ class GbxRemote
$this->callbacksBuffer[] = $value;
}
if(!$waitResponse){
$r=array($this->socket);
$w=null;
$e=null;
if(!$waitResponse)
$n = @stream_select($r, $w, $e, 0);
}
};
}
@ -310,5 +305,3 @@ class MessageException extends Exception
const REQUEST_TOO_LARGE = 1;
const RESPONSE_TOO_LARGE = 2;
}
?>

View File

@ -22,9 +22,12 @@ if(extension_loaded('xmlrpc'))
* @param mixed[] $args
* @return string
*/
static function encode($method, $args)
static function encode($method, $args, $escape=true)
{
return xmlrpc_encode_request($method, $args, self::$options);
$opts = self::$options;
if(!$escape)
$opts['escaping'] = array();
return xmlrpc_encode_request($method, $args, $opts);
}
/**
@ -40,7 +43,7 @@ if(extension_loaded('xmlrpc'))
if($method === null)
{
if(is_array($value) && @xmlrpc_is_fault($value))
if(is_array($value) && xmlrpc_is_fault($value))
return array('fault', $value);
return array('response', $value);
}
@ -59,55 +62,80 @@ else
* @param mixed[] $args
* @return string
*/
static function encode($method, $args)
static function encode($method, $args, $escape=true)
{
$xml = '<?xml version="1.0" encoding="utf-8"?><methodCall><methodName><![CDATA['.$method.']]></methodName><params>';
$xml = '<?xml version="1.0" encoding="utf-8"?><methodCall><methodName>'.self::escape($method, $escape).'</methodName>';
if(!$args)
return $xml.'<params/></methodCall>';
$xml .= '<params>';
foreach($args as $arg)
$xml .= '<param><value>'.self::encodeValue($arg).'</value></param>';
$xml .= '</params></methodCall>';
return $xml;
$xml .= '<param><value>'.self::encodeValue($arg, $escape).'</value></param>';
return $xml.'</params></methodCall>';
}
/**
* @param mixed $v
* @return string
*/
private static function encodeValue($v)
private static function encodeValue($v, $escape=true)
{
switch(gettype($v))
{
case 'boolean':
return '<boolean><![CDATA['.((int) $v).']]></boolean>';
return '<boolean>'.self::escape((int) $v, $escape).'</boolean>';
case 'integer':
return '<int><![CDATA['.$v.']]></int>';
return '<int>'.self::escape($v, $escape).'</int>';
case 'double':
return '<double><![CDATA['.$v.']]></double>';
return '<double>'.self::escape($v, $escape).'</double>';
case 'string':
return '<string><![CDATA['.$v.']]></string>';
case 'NULL':
if(!$v)
return '<string/>';
return '<string>'.self::escape($v, $escape).'</string>';
case 'object':
if($v instanceof Base64)
return '<base64><![CDATA['.base64_encode($v->scalar).']]></base64>';
{
if(!$v->scalar)
return '<base64/>';
return '<base64>'.self::escape(base64_encode($v->scalar), $escape).'</base64>';
}
if($v instanceof \DateTime)
return '<dateTime.iso8601><![CDATA['.$v->format(self::DATE_FORMAT).']]></dateTime.iso8601>';
return '<dateTime.iso8601>'.self::escape($v->format(self::DATE_FORMAT), $escape).'</dateTime.iso8601>';
$v = get_object_vars($v);
// fallthrough
// fallthrough
case 'array':
$return = '';
// empty array case
if(!$v)
return '<array><data/></array>';
// pure array case
if(array_keys($v) === range(0, count($v) - 1))
{
foreach($v as $item)
$return .= '<value>'.self::encodeValue($item).'</value>';
$return .= '<value>'.self::encodeValue($item, $escape).'</value>';
return '<array><data>'.$return.'</data></array>';
}
// else it's a struct
foreach($v as $name => $value)
$return .= '<member><name><![CDATA['.$name.']]></name><value>'.self::encodeValue($value).'</value></member>';
$return .= '<member><name>'.self::escape($name, $escape).'</name><value>'.self::encodeValue($value, $escape).'</value></member>';
return '<struct>'.$return.'</struct>';
}
return '';
}
/**
* @param string $str
* @param bool $escape
* @return string
*/
private static function escape($str, $escape=true)
{
if($escape)
return '<![CDATA['.$str.']]>';
return $str;
}
/**
* @param string $message
* @return mixed
@ -142,7 +170,7 @@ else
switch($elt->getName())
{
case 'boolean':
return (bool) $elt;
return (bool) (int) $elt;
case 'i4':
case 'int':
return (int) $elt;
@ -170,5 +198,3 @@ else
}
class ParseException extends Exception {}
?>