Skip to content

Commit

Permalink
Cleaned up socket and connection class
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvdvleuten committed Feb 18, 2016
1 parent 68cdb67 commit 83696f1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 55 deletions.
9 changes: 1 addition & 8 deletions src/Connection/Connection.php
Expand Up @@ -46,11 +46,6 @@ class Connection implements ConnectionInterface
*/
private $socket;

/**
* @var int
*/
private $timeout;

/**
* Constructor.
*
Expand All @@ -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);
Expand Down
34 changes: 7 additions & 27 deletions src/Socket/Socket.php
Expand Up @@ -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}
*/
Expand All @@ -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;
}
Expand Down
17 changes: 1 addition & 16 deletions src/Socket/SocketInterface.php
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/Connection/ConnectionTest.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 10 additions & 2 deletions tests/Socket/SocketTest.php
Expand Up @@ -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";
Expand All @@ -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');
}
}

0 comments on commit 83696f1

Please sign in to comment.