dedi api update

This commit is contained in:
kremsy 2014-02-27 13:29:14 +01:00 committed by Steffen Schröder
parent 093b68c491
commit cdc1ab7037

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)
{ {