From 83696f19a046b0eee294acc87587423b374eda82 Mon Sep 17 00:00:00 2001 From: Robin van der Vleuten Date: Thu, 18 Feb 2016 15:15:03 +0100 Subject: [PATCH] Cleaned up socket and connection class --- src/Connection/Connection.php | 9 +------- src/Socket/Socket.php | 34 ++++++----------------------- src/Socket/SocketInterface.php | 17 +-------------- tests/Connection/ConnectionTest.php | 4 ++-- tests/Socket/SocketTest.php | 12 ++++++++-- 5 files changed, 21 insertions(+), 55 deletions(-) diff --git a/src/Connection/Connection.php b/src/Connection/Connection.php index 75ca53c..c50dd79 100644 --- a/src/Connection/Connection.php +++ b/src/Connection/Connection.php @@ -46,11 +46,6 @@ class Connection implements ConnectionInterface */ private $socket; - /** - * @var int - */ - private $timeout; - /** * Constructor. * @@ -72,9 +67,7 @@ public function __construct($host, $port, $secure = false, SocketInterface $sock */ public function connect() { - $this->socket - ->connect(sprintf('tcp://%s:%d', $this->host, $this->port), 1.0) - ->setBlocking(true); + $this->socket->connect(sprintf('tcp://%s:%d', $this->host, $this->port)); if ($this->secure) { $this->socket->enableCrypto(true); diff --git a/src/Socket/Socket.php b/src/Socket/Socket.php index 898be50..0edbd38 100644 --- a/src/Socket/Socket.php +++ b/src/Socket/Socket.php @@ -14,30 +14,6 @@ class Socket implements SocketInterface */ private $stream; - /** - * {@inheritdoc} - */ - public function setBlocking($blocking) - { - if (!stream_set_blocking($this->stream, $blocking ? 1 : 0)) { - throw new SocketException(); - } - - return $this; - } - - /** - * {@inheritdoc} - */ - public function setReadBuffer($buffer) - { - if (stream_set_read_buffer($this->stream, $buffer) !== 0) { - throw new SocketException(); - } - - return $this; - } - /** * {@inheritdoc} */ @@ -53,19 +29,23 @@ public function enableCrypto($enable, $cryptoType = STREAM_CRYPTO_METHOD_TLS_CLI /** * {@inheritdoc} */ - public function connect($address, $timeout = null) + public function connect($address) { - if (!$this->stream = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT)) { + if (!$this->stream = @stream_socket_client($address, $errno, $errstr, 1.0, STREAM_CLIENT_CONNECT)) { throw new SocketException(sprintf('Connection to %s failed: %s', $address, $errstr)); } + stream_set_blocking($this->stream, 1); + // Use unbuffered read operations on the underlying stream resource. // Reading chunks from the stream may otherwise leave unread bytes in // PHP's stream buffers which some event loop implementations do not // trigger events on (edge triggered). // This does not affect the default event loop implementation (level // triggered), so we can ignore platforms not supporting this (HHVM). - $this->setReadBuffer(0); + if (function_exists('stream_set_read_buffer')) { + stream_set_read_buffer($this->stream, 0); + } return $this; } diff --git a/src/Socket/SocketInterface.php b/src/Socket/SocketInterface.php index 585e8a7..c1e1b5c 100644 --- a/src/Socket/SocketInterface.php +++ b/src/Socket/SocketInterface.php @@ -7,20 +7,6 @@ */ interface SocketInterface { - /** - * @param bool $toggle - * - * @return self - */ - public function setBlocking($blocking); - - /** - * @param int $buffer - * - * @return self - */ - public function setReadBuffer($buffer); - /** * @param bool $enable * @param int $cryptoType @@ -31,11 +17,10 @@ public function enableCrypto($enable, $cryptoType = STREAM_CRYPTO_METHOD_TLS_CLI /** * @param string $address - * @param float $timeout * * @return self */ - public function connect($address, $timeout = null); + public function connect($address); /** * @return self diff --git a/tests/Connection/ConnectionTest.php b/tests/Connection/ConnectionTest.php index 84cd7fd..8f11260 100644 --- a/tests/Connection/ConnectionTest.php +++ b/tests/Connection/ConnectionTest.php @@ -42,7 +42,7 @@ public function setup() $this->socket->expects($this->once()) ->method('connect') - ->with('tcp://localhost:5000', 1.0) + ->with('tcp://localhost:5000') ->willReturnSelf(); $this->connection = new Connection('localhost', 5000, false, $this->socket); @@ -73,7 +73,7 @@ public function testErrorIsThrownWhenConnectionCannotBeEstablished() { $this->socket->expects($this->once()) ->method('connect') - ->with('tcp://localhost:5000', 1.0) + ->with('tcp://localhost:5000') ->willThrowException(new SocketException()); $this->connection->connect(); diff --git a/tests/Socket/SocketTest.php b/tests/Socket/SocketTest.php index 6b682a6..cf499b5 100644 --- a/tests/Socket/SocketTest.php +++ b/tests/Socket/SocketTest.php @@ -13,8 +13,7 @@ public function testConnectGoogle() { $socket = new Socket(); - $this->assertSame($socket, $socket->connect('www.google.nl:80', 1.0)); - $this->assertSame($socket, $socket->setBlocking(true)); + $this->assertSame($socket, $socket->connect('www.google.nl:80')); // Send HTTP request to remote server. $data = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n"; @@ -30,4 +29,13 @@ public function testConnectGoogle() $this->assertSame($socket, $socket->disconnect()); } + + /** + * @expectedException \Rvdv\Nntp\Exception\SocketException + */ + public function testConnectFail() + { + $socket = new Socket(); + $socket->connect('localhost:2'); + } }