Skip to content

Commit

Permalink
Fixing issue with timeout when reading socket.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8233 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
renan committed Jul 15, 2009
1 parent 3b872a2 commit 7fd6cc5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cake/libs/socket.php
Expand Up @@ -120,7 +120,11 @@ function connect() {
$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;
}

/**
Expand Down Expand Up @@ -218,7 +222,13 @@ function read($length = 1024) {
}

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;
}
Expand Down
12 changes: 12 additions & 0 deletions cake/tests/cases/libs/socket.test.php
Expand Up @@ -144,6 +144,18 @@ function testSocketReading() {
$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
Expand Down

0 comments on commit 7fd6cc5

Please sign in to comment.