Skip to content

Commit

Permalink
Merge pull request #100 from hason/socket
Browse files Browse the repository at this point in the history
Improved SocketHandler
  • Loading branch information
Seldaek committed Aug 10, 2012
2 parents 4ab32e3 + 535cea5 commit ae31045
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
27 changes: 17 additions & 10 deletions src/Monolog/Handler/SocketHandler.php
Expand Up @@ -89,7 +89,7 @@ public function setPersistent($boolean)
/** /**
* Set connection timeout. Only has effect before we connect. * Set connection timeout. Only has effect before we connect.
* *
* @param integer $seconds * @param float $seconds
* *
* @see http://php.net/manual/en/function.fsockopen.php * @see http://php.net/manual/en/function.fsockopen.php
*/ */
Expand All @@ -102,14 +102,14 @@ public function setConnectionTimeout($seconds)
/** /**
* Set write timeout. Only has effect before we connect. * Set write timeout. Only has effect before we connect.
* *
* @param type $seconds * @param float $seconds
* *
* @see http://php.net/manual/en/function.stream-set-timeout.php * @see http://php.net/manual/en/function.stream-set-timeout.php
*/ */
public function setTimeout($seconds) public function setTimeout($seconds)
{ {
$this->validateTimeout($seconds); $this->validateTimeout($seconds);
$this->timeout = (int) $seconds; $this->timeout = (float) $seconds;
} }


/** /**
Expand Down Expand Up @@ -183,10 +183,15 @@ protected function fsockopen()


/** /**
* Wrapper to allow mocking * Wrapper to allow mocking
*
* @see http://php.net/manual/en/function.stream-set-timeout.php
*/ */
protected function streamSetTimeout() protected function streamSetTimeout()
{ {
return stream_set_timeout($this->resource, $this->timeout); $seconds = floor($this->timeout);
$microseconds = round(($this->timeout - $seconds)*1e6);

return stream_set_timeout($this->resource, $seconds, $microseconds);
} }


/** /**
Expand All @@ -207,11 +212,9 @@ protected function streamGetMetadata()


private function validateTimeout($value) private function validateTimeout($value)
{ {
$ok = filter_var($value, FILTER_VALIDATE_INT, array('options' => array( $ok = filter_var($value, FILTER_VALIDATE_FLOAT);
'min_range' => 0, if ($ok === false || $value < 0) {
))); throw new \InvalidArgumentException("Timeout must be 0 or a positive float (got $value)");
if ($ok === false) {
throw new \InvalidArgumentException("Timeout must be 0 or a positive integer (got $value)");
} }
} }


Expand Down Expand Up @@ -254,7 +257,11 @@ private function writeToSocket($data)
$length = strlen($data); $length = strlen($data);
$sent = 0; $sent = 0;
while ($this->isConnected() && $sent < $length) { while ($this->isConnected() && $sent < $length) {
$chunk = $this->fwrite(substr($data, $sent)); if (0 == $sent) {
$chunk = $this->fwrite($data);
} else {
$chunk = $this->fwrite(substr($data, $sent));
}
if ($chunk === false) { if ($chunk === false) {
throw new \RuntimeException("Could not write to socket"); throw new \RuntimeException("Could not write to socket");
} }
Expand Down
8 changes: 4 additions & 4 deletions tests/Monolog/Handler/SocketHandlerTest.php
Expand Up @@ -50,8 +50,8 @@ public function testBadConnectionTimeout()
public function testSetConnectionTimeout() public function testSetConnectionTimeout()
{ {
$this->createHandler('localhost:1234'); $this->createHandler('localhost:1234');
$this->handler->setConnectionTimeout(10); $this->handler->setConnectionTimeout(10.1);
$this->assertEquals(10, $this->handler->getConnectionTimeout()); $this->assertEquals(10.1, $this->handler->getConnectionTimeout());
} }


/** /**
Expand All @@ -66,8 +66,8 @@ public function testBadTimeout()
public function testSetTimeout() public function testSetTimeout()
{ {
$this->createHandler('localhost:1234'); $this->createHandler('localhost:1234');
$this->handler->setTimeout(10); $this->handler->setTimeout(10.25);
$this->assertEquals(10, $this->handler->getTimeout()); $this->assertEquals(10.25, $this->handler->getTimeout());
} }


public function testSetConnectionString() public function testSetConnectionString()
Expand Down

0 comments on commit ae31045

Please sign in to comment.