diff --git a/README.md b/README.md index 8915e9d..8f39c51 100644 --- a/README.md +++ b/README.md @@ -346,8 +346,6 @@ Calling this method more than once on the same instance is a NO-OP. ### SocketServer - - The `SocketServer` class is the main class in this package that implements the [`ServerInterface`](#serverinterface) and allows you to accept incoming streaming connections, such as plaintext TCP/IP or secure TLS connection streams. @@ -519,10 +517,6 @@ given event loop instance. If you want to typehint in your higher-level protocol implementation, you SHOULD use the generic [`ServerInterface`](#serverinterface) instead. -> Changelog v1.9.0: This class has been added with an improved constructor signature - as a replacement for the previous `Server` class in order to avoid any ambiguities. - The previous name has been deprecated and should not be used anymore. - ### Advanced server usage #### TcpServer diff --git a/src/Server.php b/src/Server.php deleted file mode 100644 index 7d4111e..0000000 --- a/src/Server.php +++ /dev/null @@ -1,114 +0,0 @@ - $context); - } - - // apply default options if not explicitly given - $context += array( - 'tcp' => array(), - 'tls' => array(), - 'unix' => array() - ); - - $scheme = 'tcp'; - $pos = \strpos($uri, '://'); - if ($pos !== false) { - $scheme = \substr($uri, 0, $pos); - } - - if ($scheme === 'unix') { - $server = new UnixServer($uri, $loop, $context['unix']); - } else { - $server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']); - - if ($scheme === 'tls') { - $server = new SecureServer($server, $loop, $context['tls']); - } - } - - $this->server = $server; - - $that = $this; - $server->on('connection', function (ConnectionInterface $conn) use ($that) { - $that->emit('connection', array($conn)); - }); - $server->on('error', function (Exception $error) use ($that) { - $that->emit('error', array($error)); - }); - } - - public function getAddress() - { - return $this->server->getAddress(); - } - - public function pause() - { - $this->server->pause(); - } - - public function resume() - { - $this->server->resume(); - } - - public function close() - { - $this->server->close(); - } -} diff --git a/tests/ServerTest.php b/tests/ServerTest.php deleted file mode 100644 index f69e6cb..0000000 --- a/tests/ServerTest.php +++ /dev/null @@ -1,235 +0,0 @@ -setAccessible(true); - $tcp = $ref->getValue($server); - - $ref = new \ReflectionProperty($tcp, 'loop'); - $ref->setAccessible(true); - $loop = $ref->getValue($tcp); - - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); - - $server->close(); - } - - public function testCreateServerWithZeroPortAssignsRandomPort() - { - $server = new Server(0); - $this->assertNotEquals(0, $server->getAddress()); - $server->close(); - } - - public function testConstructorThrowsForInvalidUri() - { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); - - $this->setExpectedException('InvalidArgumentException'); - $server = new Server('invalid URI', $loop); - } - - public function testConstructorCreatesExpectedTcpServer() - { - $server = new Server(0); - - $connector = new TcpConnector(); - $promise = $connector->connect($server->getAddress()); - $promise->then($this->expectCallableOnce(), $this->expectCallableNever()); - - $connection = \React\Async\await(\React\Promise\Timer\timeout($connector->connect($server->getAddress()), self::TIMEOUT)); - - $server->close(); - $promise->then(function (ConnectionInterface $connection) { - $connection->close(); - }); - } - - public function testConstructorCreatesExpectedUnixServer() - { - if (defined('HHVM_VERSION')) { - $this->markTestSkipped('Not supported on legacy HHVM'); - } - if (!in_array('unix', stream_get_transports())) { - $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); - } - - $server = new Server($this->getRandomSocketUri()); - - $connector = new UnixConnector(); - $connector->connect($server->getAddress()) - ->then($this->expectCallableOnce(), $this->expectCallableNever()); - - $connection = \React\Async\await(\React\Promise\Timer\timeout($connector->connect($server->getAddress()), self::TIMEOUT)); - assert($connection instanceof ConnectionInterface); - - unlink(str_replace('unix://', '', $connection->getRemoteAddress())); - - $connection->close(); - $server->close(); - } - - public function testConstructorThrowsForExistingUnixPath() - { - if (!in_array('unix', stream_get_transports())) { - $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); - } - - try { - $server = new Server('unix://' . __FILE__); - $this->fail(); - } catch (\RuntimeException $e) { - if ($e->getCode() === 0) { - // Zend PHP does not currently report a sane error - $this->assertStringEndsWith('Unknown error', $e->getMessage()); - } else { - $this->assertEquals(SOCKET_EADDRINUSE, $e->getCode()); - $this->assertStringEndsWith('Address already in use (EADDRINUSE)', $e->getMessage()); - } - } - } - - public function testEmitsErrorWhenUnderlyingTcpServerEmitsError() - { - $server = new Server(0); - - $ref = new \ReflectionProperty($server, 'server'); - $ref->setAccessible(true); - $tcp = $ref->getvalue($server); - - $error = new \RuntimeException(); - $server->on('error', $this->expectCallableOnceWith($error)); - $tcp->emit('error', array($error)); - - $server->close(); - } - - public function testEmitsConnectionForNewConnection() - { - $server = new Server(0); - $server->on('connection', $this->expectCallableOnce()); - - $peer = new Promise(function ($resolve, $reject) use ($server) { - $server->on('connection', function () use ($resolve) { - $resolve(null); - }); - }); - - $client = stream_socket_client($server->getAddress()); - - \React\Async\await(\React\Promise\Timer\timeout($peer, self::TIMEOUT)); - - $server->close(); - } - - public function testDoesNotEmitConnectionForNewConnectionToPausedServer() - { - $server = new Server(0); - $server->pause(); - $server->on('connection', $this->expectCallableNever()); - - $client = stream_socket_client($server->getAddress()); - - \React\Async\await(\React\Promise\Timer\sleep(0.1)); - } - - public function testDoesEmitConnectionForNewConnectionToResumedServer() - { - $server = new Server(0); - $server->pause(); - $server->on('connection', $this->expectCallableOnce()); - - $peer = new Promise(function ($resolve, $reject) use ($server) { - $server->on('connection', function () use ($resolve) { - $resolve(null); - }); - }); - - $client = stream_socket_client($server->getAddress()); - - $server->resume(); - - \React\Async\await(\React\Promise\Timer\timeout($peer, self::TIMEOUT)); - - $server->close(); - } - - public function testDoesNotAllowConnectionToClosedServer() - { - $server = new Server(0); - $server->on('connection', $this->expectCallableNever()); - $address = $server->getAddress(); - $server->close(); - - $client = @stream_socket_client($address); - - $this->assertFalse($client); - } - - public function testEmitsConnectionWithInheritedContextOptions() - { - if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.13', '<')) { - // https://3v4l.org/hB4Tc - $this->markTestSkipped('Not supported on legacy HHVM < 3.13'); - } - - $server = new Server(0, null, array( - 'backlog' => 4 - )); - - $peer = new Promise(function ($resolve, $reject) use ($server) { - $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { - $resolve(stream_context_get_options($connection->stream)); - }); - }); - - - $client = stream_socket_client($server->getAddress()); - - $all = \React\Async\await(\React\Promise\Timer\timeout($peer, self::TIMEOUT)); - - $this->assertEquals(array('socket' => array('backlog' => 4)), $all); - - $server->close(); - } - - public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsIdle() - { - if (defined('HHVM_VERSION')) { - $this->markTestSkipped('Not supported on legacy HHVM'); - } - - $server = new Server('tls://127.0.0.1:0', null, array( - 'tls' => array( - 'local_cert' => __DIR__ . '/../examples/localhost.pem' - ) - )); - $server->on('connection', $this->expectCallableNever()); - - $client = stream_socket_client(str_replace('tls://', '', $server->getAddress())); - - \React\Async\await(\React\Promise\Timer\sleep(0.1)); - - $server->close(); - } - - private function getRandomSocketUri() - { - return "unix://" . sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(rand(), true) . '.sock'; - } -}