Unverified Commit fe5100bb authored by Nathan Van Overloop's avatar Nathan Van Overloop
Browse files

centralnic/issues#3769: add better timeout in read

parent f5cf3dce
......@@ -44,6 +44,11 @@
*/
var $debug;
/**
* @var integer timeout to wait on commands
*/
var $timeout;
/**
* constructor set initialize various objects
* @param boolean set debugging on
......@@ -94,6 +99,8 @@
if (!stream_set_timeout($this->socket,$timeout)) {
throw new Exception("Failed to set timeout on socket: $errstr (code $errno)");
}
$this->timeout=$timeout;
$GLOBALS['timeout']=$timeout;
// Set blocking
if (!stream_set_blocking($this->socket,0)) {
throw new Exception("Failed to set blocking on socket: $errstr (code $errno)");
......
......@@ -30,16 +30,22 @@
*/
class Net_EPP_Protocol {
static function _fread_nb($socket,$length) {
$result = '';
// Loop reading and checking info to see if we hit timeout
$info = stream_get_meta_data($socket);
$time_start = microtime(true);
$timeout_time=time()+$GLOBALS['timeout'];
while (!$info['timed_out'] && !feof($socket)) {
//make sure we don't wait to long
if($timeout_time>time()){
throw new exception('Timeout reading from EPP server');
}
// Try read remaining data from socket
$buffer = @fread($socket,$length - strlen($result));
$buffer = fread($socket,$length - strlen($result));
// If the buffer actually contains something then add it to the result
if ($buffer !== false) {
$result .= $buffer;
......@@ -54,16 +60,18 @@ class Net_EPP_Protocol {
// Update metadata
$info = stream_get_meta_data($socket);
$time_end = microtime(true);
if (($time_end - $time_start) > 10000000) {
throw new exception('Timeout while reading from EPP Server');
}
}
// Check for timeout
if ($info['timed_out']) {
throw new Exception('Timeout while reading data from socket');
}
if($GLOBALS['debug']){
$time_diff=microtime(true)-$time_start;
debug_log("returning after {$time_diff}");
}
return $result;
}
......@@ -106,6 +114,9 @@ class Net_EPP_Protocol {
return $pos;
}
/**
* get an EPP frame from the remote peer
* @param resource $socket a socket connected to the remote peer
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment