diff --git a/README.md b/README.md index 6f474c99..3f65b176 100644 --- a/README.md +++ b/README.md @@ -296,13 +296,13 @@ If you want to typehint in your higher-level protocol implementation, you SHOULD use the generic [`ServerInterface`](#serverinterface) instead. > Advanced usage: Despite allowing any `ServerInterface` as first parameter, -you SHOULD pass an unmodified `Server` instance as first parameter, unless you +you SHOULD pass a `Server` instance as first parameter, unless you know what you're doing. Internally, the `SecureServer` has to set the required TLS context options on the underlying stream resources. These resources are not exposed through any of the interfaces defined in this package, but only through the `React\Stream\Stream` class. -The unmodified `Server` class is guaranteed to emit connections that implement +The `Server` class is guaranteed to emit connections that implement the `ConnectionInterface` and also extend the `Stream` class in order to expose these underlying resources. If you use a custom `ServerInterface` and its `connection` event does not diff --git a/src/SecureServer.php b/src/SecureServer.php index 1e372db0..abfb9032 100644 --- a/src/SecureServer.php +++ b/src/SecureServer.php @@ -52,7 +52,7 @@ * @see ServerInterface * @see ConnectionInterface */ -class SecureServer extends EventEmitter implements ServerInterface +final class SecureServer extends EventEmitter implements ServerInterface { private $tcp; private $encryption; @@ -96,13 +96,13 @@ class SecureServer extends EventEmitter implements ServerInterface * Passing unknown context options has no effect. * * Advanced usage: Despite allowing any `ServerInterface` as first parameter, - * you SHOULD pass an unmodified `Server` instance as first parameter, unless you + * you SHOULD pass a `Server` instance as first parameter, unless you * know what you're doing. * Internally, the `SecureServer` has to set the required TLS context options on * the underlying stream resources. * These resources are not exposed through any of the interfaces defined in this * package, but only through the `React\Stream\Stream` class. - * The unmodified `Server` class is guaranteed to emit connections that implement + * The `Server` class is guaranteed to emit connections that implement * the `ConnectionInterface` and also extend the `Stream` class in order to * expose these underlying resources. * If you use a custom `ServerInterface` and its `connection` event does not diff --git a/src/Server.php b/src/Server.php index 8bb32cbe..4e297ea6 100644 --- a/src/Server.php +++ b/src/Server.php @@ -34,7 +34,7 @@ * @see ServerInterface * @see ConnectionInterface */ -class Server extends EventEmitter implements ServerInterface +final class Server extends EventEmitter implements ServerInterface { private $master; private $loop; @@ -100,7 +100,7 @@ class Server extends EventEmitter implements ServerInterface * and/or PHP version. * Passing unknown context options has no effect. * - * @param string $uri + * @param string|int $uri * @param LoopInterface $loop * @param array $context * @throws InvalidArgumentException if the listening address is invalid @@ -165,15 +165,6 @@ public function __construct($uri, LoopInterface $loop, array $context = array()) }); } - public function handleConnection($socket) - { - stream_set_blocking($socket, 0); - - $client = $this->createConnection($socket); - - $this->emit('connection', array($client)); - } - public function getAddress() { if (!is_resource($this->master)) { @@ -203,8 +194,11 @@ public function close() $this->removeAllListeners(); } - public function createConnection($socket) + /** @internal */ + public function handleConnection($socket) { - return new Connection($socket, $this->loop); + $this->emit('connection', array( + new Connection($socket, $this->loop) + )); } } diff --git a/tests/SecureServerTest.php b/tests/SecureServerTest.php index 854ef852..02e926b8 100644 --- a/tests/SecureServerTest.php +++ b/tests/SecureServerTest.php @@ -3,6 +3,7 @@ namespace React\Tests\Socket; use React\Socket\SecureServer; +use React\Socket\Server; class SecureServerTest extends TestCase { @@ -39,10 +40,10 @@ public function testCloseWillBePassedThroughToTcpServer() public function testConnectionWillBeEndedWithErrorIfItIsNotAStream() { - $tcp = $this->getMockBuilder('React\Socket\Server')->disableOriginalConstructor()->setMethods(null)->getMock(); - $loop = $this->getMock('React\EventLoop\LoopInterface'); + $tcp = new Server(0, $loop); + $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); $connection->expects($this->once())->method('end'); @@ -55,10 +56,10 @@ public function testConnectionWillBeEndedWithErrorIfItIsNotAStream() public function testSocketErrorWillBeForwarded() { - $tcp = $this->getMockBuilder('React\Socket\Server')->disableOriginalConstructor()->setMethods(null)->getMock(); - $loop = $this->getMock('React\EventLoop\LoopInterface'); + $tcp = new Server(0, $loop); + $server = new SecureServer($tcp, $loop, array()); $server->on('error', $this->expectCallableOnce()); diff --git a/tests/ServerTest.php b/tests/ServerTest.php index cbe5e141..727ed66e 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -30,9 +30,7 @@ public function setUp() } /** - * @covers React\EventLoop\StreamSelectLoop::tick * @covers React\Socket\Server::handleConnection - * @covers React\Socket\Server::createConnection */ public function testConnection() { @@ -43,9 +41,7 @@ public function testConnection() } /** - * @covers React\EventLoop\StreamSelectLoop::tick * @covers React\Socket\Server::handleConnection - * @covers React\Socket\Server::createConnection */ public function testConnectionWithManyClients() {