api update

This commit is contained in:
kremsy 2014-02-27 13:55:32 +01:00 committed by Steffen Schröder
parent b736e46861
commit a85f5b36fb
2 changed files with 31 additions and 23 deletions

View File

@ -25,6 +25,11 @@ class Client
public $reqhandle;
public $protocol = 0;
/**
* @var int Timeout in milli-seconds
*/
public $timeout;
static $received;
static $sent;
@ -83,11 +88,18 @@ class Client
}
}
function __construct($hostname = 'localhost', $port = 5000, $timeout)
/**
*
* @param string $hostname
* @param int $port
* @param int $timeout In milliseconds
*/
function __construct($hostname, $port, $timeout = 50)
{
$this->socket = false;
$this->reqhandle = 0x80000000;
$this->init($hostname, $port, $timeout);
$this->timeout = $timeout;
$this->init($hostname, $port);
}
function __destruct()
@ -95,13 +107,13 @@ class Client
$this->terminate();
}
protected function init($hostname, $port, $timeout)
protected function init($hostname, $port)
{
$this->bigEndianTest();
// open connection
$this->socket = @fsockopen($hostname, $port, $errno, $errstr, $timeout);
$this->socket = @fsockopen($hostname, $port, $errno, $errstr, $this->timeout/1000);
if (!$this->socket)
{
throw new FatalException("transport error - could not open socket (error: $errno, $errstr)", FatalException::NOT_INITIALIZED);
@ -141,7 +153,7 @@ class Client
{
$xml = $request->getXml();
@stream_set_timeout($this->socket, 20); // timeout 20 s (to write the request)
@stream_set_timeout($this->socket, 0, $this->timeout * 1000 * 100);
// send request
$this->reqhandle++;
if ($this->protocol == 1)
@ -184,12 +196,12 @@ class Client
{
$size = 0;
$recvhandle = 0;
@stream_set_timeout($this->socket, 5); // timeout 20 s (to read the reply header)
@stream_set_timeout($this->socket, 0, $this->timeout * 1000);
// Get result
if ($this->protocol == 1)
{
$contents = fread($this->socket, 4);
if (strlen($contents) == 0)
if (strlen($contents) == 0 || $contents === false)
{
throw new FatalException('transport error - connection interrupted!', FatalException::INTERRUPTED);
}
@ -200,7 +212,7 @@ class Client
else
{
$contents = fread($this->socket, 8);
if (strlen($contents) == 0)
if (strlen($contents) == 0 || $contents === false)
{
throw new FatalException('transport error - connection interrupted!', FatalException::INTERRUPTED);
}
@ -229,7 +241,7 @@ class Client
$contents = '';
$contents_length = 0;
@stream_set_timeout($this->socket, 0, 10000); // timeout 10 ms (for successive reads until end)
@stream_set_timeout($this->socket, 0, $this->timeout * 1000);
while ($contents_length < $size)
{
$contents .= fread($this->socket, $size-$contents_length);
@ -337,7 +349,7 @@ class Client
return $this->message->params[0];
}
function readCallbacks($timeout = 2000)
function readCallbacks()
{
if (!$this->socket || $this->protocol == 0)
throw new FatalException('transport error - Client not initialized', FatalException::NOT_INITIALIZED);
@ -349,7 +361,7 @@ class Client
$contents = '';
$contents_length = 0;
@stream_set_timeout($this->socket, 0, 100000); // timeout 10 ms (to read available data)
@stream_set_timeout($this->socket, 0, $this->timeout * 1000); // timeout 10 ms (to read available data)
// (assignment in arguments is forbidden since php 5.1.1)
$read = array($this->socket);
$write = NULL;
@ -358,7 +370,7 @@ class Client
try
{
$nb = @stream_select($read, $write, $except, 0, $timeout);
$nb = @stream_select($read, $write, $except, 0, $this->timeout * 1000);
}
catch (\Exception $e)
{
@ -384,13 +396,11 @@ class Client
while ($nb !== false && $nb > 0)
{
$timeout = 0; // we don't want to wait for the full time again, just flush the available data
$size = 0;
$recvhandle = 0;
// Get result
$contents = fread($this->socket, 8);
if (strlen($contents) == 0)
if (strlen($contents) == 0 || $contents === false)
{
throw new FatalException('transport error - connection interrupted!', FatalException::INTERRUPTED);
}
@ -437,7 +447,7 @@ class Client
try
{
$nb = @stream_select($read, $write, $except, 0, $timeout);
$nb = @stream_select($read, $write, $except, 0, 0); // Notimeout, just flush the data
}
catch (\Exception $e)
{

View File

@ -48,7 +48,7 @@ class ManiaControl implements CommandListener, TimerListener {
const API_VERSION = '2013-04-16';
const OS_UNIX = 'Unix';
const OS_WIN = 'Windows';
const CONNECT_TIMEOUT = 20;
const CONNECT_TIMEOUT = 20000;
const SCRIPT_TIMEOUT = 20;
const URL_WEBSERVICE = 'http://ws.maniacontrol.com/';
const SETTING_PERMISSION_SHUTDOWN = 'Shutdown ManiaControl';
@ -224,15 +224,13 @@ class ManiaControl implements CommandListener, TimerListener {
// Announce quit
$this->chat->sendInformation('ManiaControl shutting down.');
// Hide manialinks
try {
if($this->client){
// Hide manialinks
$this->client->sendHideManialinkPage();
} catch(Exception $e) {
// Close the client connection
$this->client->delete($this->server->ip, $this->server->port);
}
// Close the client connection
$this->client->delete($this->server->ip, $this->server->port);
//Check and Trigger Fatal Errors
$error = error_get_last();
if ($error && ($error['type'] & E_FATAL)) {