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 $reqhandle;
public $protocol = 0; public $protocol = 0;
/**
* @var int Timeout in milli-seconds
*/
public $timeout;
static $received; static $received;
static $sent; 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->socket = false;
$this->reqhandle = 0x80000000; $this->reqhandle = 0x80000000;
$this->init($hostname, $port, $timeout); $this->timeout = $timeout;
$this->init($hostname, $port);
} }
function __destruct() function __destruct()
@ -95,13 +107,13 @@ class Client
$this->terminate(); $this->terminate();
} }
protected function init($hostname, $port, $timeout) protected function init($hostname, $port)
{ {
$this->bigEndianTest(); $this->bigEndianTest();
// open connection // open connection
$this->socket = @fsockopen($hostname, $port, $errno, $errstr, $timeout); $this->socket = @fsockopen($hostname, $port, $errno, $errstr, $this->timeout/1000);
if (!$this->socket) if (!$this->socket)
{ {
throw new FatalException("transport error - could not open socket (error: $errno, $errstr)", FatalException::NOT_INITIALIZED); throw new FatalException("transport error - could not open socket (error: $errno, $errstr)", FatalException::NOT_INITIALIZED);
@ -141,7 +153,7 @@ class Client
{ {
$xml = $request->getXml(); $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 // send request
$this->reqhandle++; $this->reqhandle++;
if ($this->protocol == 1) if ($this->protocol == 1)
@ -184,12 +196,12 @@ class Client
{ {
$size = 0; $size = 0;
$recvhandle = 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 // Get result
if ($this->protocol == 1) if ($this->protocol == 1)
{ {
$contents = fread($this->socket, 4); $contents = fread($this->socket, 4);
if (strlen($contents) == 0) if (strlen($contents) == 0 || $contents === false)
{ {
throw new FatalException('transport error - connection interrupted!', FatalException::INTERRUPTED); throw new FatalException('transport error - connection interrupted!', FatalException::INTERRUPTED);
} }
@ -200,7 +212,7 @@ class Client
else else
{ {
$contents = fread($this->socket, 8); $contents = fread($this->socket, 8);
if (strlen($contents) == 0) if (strlen($contents) == 0 || $contents === false)
{ {
throw new FatalException('transport error - connection interrupted!', FatalException::INTERRUPTED); throw new FatalException('transport error - connection interrupted!', FatalException::INTERRUPTED);
} }
@ -229,7 +241,7 @@ class Client
$contents = ''; $contents = '';
$contents_length = 0; $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) while ($contents_length < $size)
{ {
$contents .= fread($this->socket, $size-$contents_length); $contents .= fread($this->socket, $size-$contents_length);
@ -337,7 +349,7 @@ class Client
return $this->message->params[0]; return $this->message->params[0];
} }
function readCallbacks($timeout = 2000) function readCallbacks()
{ {
if (!$this->socket || $this->protocol == 0) if (!$this->socket || $this->protocol == 0)
throw new FatalException('transport error - Client not initialized', FatalException::NOT_INITIALIZED); throw new FatalException('transport error - Client not initialized', FatalException::NOT_INITIALIZED);
@ -349,7 +361,7 @@ class Client
$contents = ''; $contents = '';
$contents_length = 0; $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) // (assignment in arguments is forbidden since php 5.1.1)
$read = array($this->socket); $read = array($this->socket);
$write = NULL; $write = NULL;
@ -358,7 +370,7 @@ class Client
try try
{ {
$nb = @stream_select($read, $write, $except, 0, $timeout); $nb = @stream_select($read, $write, $except, 0, $this->timeout * 1000);
} }
catch (\Exception $e) catch (\Exception $e)
{ {
@ -384,13 +396,11 @@ class Client
while ($nb !== false && $nb > 0) 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; $size = 0;
$recvhandle = 0; $recvhandle = 0;
// Get result // Get result
$contents = fread($this->socket, 8); $contents = fread($this->socket, 8);
if (strlen($contents) == 0) if (strlen($contents) == 0 || $contents === false)
{ {
throw new FatalException('transport error - connection interrupted!', FatalException::INTERRUPTED); throw new FatalException('transport error - connection interrupted!', FatalException::INTERRUPTED);
} }
@ -437,7 +447,7 @@ class Client
try 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) catch (\Exception $e)
{ {

View File

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