Fixing issue with timeout when reading socket.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8233 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
renan.saddam 2009-07-15 19:17:41 +00:00
parent 3b872a218e
commit 7fd6cc52c2
2 changed files with 24 additions and 2 deletions

View file

@ -120,7 +120,11 @@ class CakeSocket extends Object {
$this->setLastError($errStr, $errNum);
}
return $this->connected = is_resource($this->connection);
$this->connected = is_resource($this->connection);
if ($this->connected) {
stream_set_timeout($this->connection, $this->config['timeout']);
}
return $this->connected;
}
/**
@ -218,7 +222,13 @@ class CakeSocket extends Object {
}
if (!feof($this->connection)) {
return fread($this->connection, $length);
$buffer = fread($this->connection, $length);
$info = stream_get_meta_data($this->connection);
if ($info['timed_out']) {
$this->setLastError(E_WARNING, __('Connection timed out', true));
return false;
}
return $buffer;
} else {
return false;
}

View file

@ -144,6 +144,18 @@ class SocketTest extends CakeTestCase {
$this->Socket = new CakeSocket(array('timeout' => 5));
$this->Socket->connect();
$this->assertEqual($this->Socket->read(26), null);
$config = array('host' => 'www.cakephp.org', 'timeout' => 1);
$this->Socket = new CakeSocket($config);
$this->assertTrue($this->Socket->connect());
$this->assertFalse($this->Socket->read(1024 * 1024));
$this->assertEqual($this->Socket->lastError(), '2: ' . __('Connection timed out', true));
$config = array('host' => 'www.cakephp.org', 'timeout' => 30);
$this->Socket = new CakeSocket($config);
$this->assertTrue($this->Socket->connect());
$this->assertEqual($this->Socket->read(26), null);
$this->assertEqual($this->Socket->lastError(), null);
}
/**
* testLastError method