Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify usage by supporting new default loop #260

Merged
merged 2 commits into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 120 additions & 80 deletions README.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
"require": {
"php": ">=5.3.0",
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
"react/dns": "^1.7",
"react/event-loop": "^1.0 || ^0.5",
"react/dns": "^1.8",
"react/event-loop": "^1.2",
"react/promise": "^2.6.0 || ^1.2.1",
"react/promise-timer": "^1.4.0",
"react/stream": "^1.1"
"react/stream": "^1.2"
},
"require-dev": {
"clue/block-react": "^1.2",
Expand Down
7 changes: 1 addition & 6 deletions examples/01-echo-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
// $ php examples/01-echo-server.php unix:///tmp/server.sock
// $ nc -U /tmp/server.sock

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\ConnectionInterface;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();

$server = new Server(isset($argv[1]) ? $argv[1] : 0, $loop, array(
$server = new Server(isset($argv[1]) ? $argv[1] : 0, null, array(
'tls' => array(
'local_cert' => isset($argv[2]) ? $argv[2] : (__DIR__ . '/localhost.pem')
)
Expand All @@ -38,5 +35,3 @@
$server->on('error', 'printf');

echo 'Listening on ' . $server->getAddress() . PHP_EOL;

$loop->run();
7 changes: 1 addition & 6 deletions examples/02-chat-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@
// $ php examples/02-chat-server.php unix:///tmp/server.sock
// $ nc -U /tmp/server.sock

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\ConnectionInterface;
use React\Socket\LimitingServer;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();

$server = new Server(isset($argv[1]) ? $argv[1] : 0, $loop, array(
$server = new Server(isset($argv[1]) ? $argv[1] : 0, null, array(
'tls' => array(
'local_cert' => isset($argv[2]) ? $argv[2] : (__DIR__ . '/localhost.pem')
)
Expand Down Expand Up @@ -55,5 +52,3 @@
$server->on('error', 'printf');

echo 'Listening on ' . $server->getAddress() . PHP_EOL;

$loop->run();
7 changes: 1 addition & 6 deletions examples/03-http-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@
// $ php examples/03-http-server.php unix:///tmp/server.sock
// $ nc -U /tmp/server.sock

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\ConnectionInterface;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();

$server = new Server(isset($argv[1]) ? $argv[1] : 0, $loop, array(
$server = new Server(isset($argv[1]) ? $argv[1] : 0, null, array(
'tls' => array(
'local_cert' => isset($argv[2]) ? $argv[2] : (__DIR__ . '/localhost.pem')
)
Expand All @@ -53,5 +50,3 @@
$server->on('error', 'printf');

echo 'Listening on ' . strtr($server->getAddress(), array('tcp:' => 'http:', 'tls:' => 'https:')) . PHP_EOL;

$loop->run();
6 changes: 1 addition & 5 deletions examples/11-http-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
// $ php examples/11-http-client.php
// $ php examples/11-http-client.php reactphp.org

use React\EventLoop\Factory;
use React\Socket\Connector;
use React\Socket\ConnectionInterface;

$host = isset($argv[1]) ? $argv[1] : 'www.google.com';

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();
$connector = new Connector($loop);
$connector = new Connector();

$connector->connect($host. ':80')->then(function (ConnectionInterface $connection) use ($host) {
$connection->on('data', function ($data) {
Expand All @@ -32,5 +30,3 @@

$connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
}, 'printf');

$loop->run();
6 changes: 1 addition & 5 deletions examples/12-https-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
// $ php examples/12-https-client.php
// $ php examples/12-https-client.php reactphp.org

use React\EventLoop\Factory;
use React\Socket\Connector;
use React\Socket\ConnectionInterface;

$host = isset($argv[1]) ? $argv[1] : 'www.google.com';

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();
$connector = new Connector($loop);
$connector = new Connector();

$connector->connect('tls://' . $host . ':443')->then(function (ConnectionInterface $connection) use ($host) {
$connection->on('data', function ($data) {
Expand All @@ -32,5 +30,3 @@

$connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
}, 'printf');

$loop->run();
12 changes: 4 additions & 8 deletions examples/21-netcat-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// $ php examples/21-netcat-client.php www.google.com:80
// $ php examples/21-netcat-client.php tls://www.google.com:443

use React\EventLoop\Factory;
use React\Socket\Connector;
use React\Socket\ConnectionInterface;
use React\Stream\ReadableResourceStream;
Expand All @@ -31,13 +30,12 @@
exit(1);
}

$loop = Factory::create();
$connector = new Connector($loop);
$connector = new Connector();

$stdin = new ReadableResourceStream(STDIN, $loop);
$stdin = new ReadableResourceStream(STDIN);
$stdin->pause();
$stdout = new WritableResourceStream(STDOUT, $loop);
$stderr = new WritableResourceStream(STDERR, $loop);
$stdout = new WritableResourceStream(STDOUT);
$stderr = new WritableResourceStream(STDERR);

$stderr->write('Connecting' . PHP_EOL);

Expand All @@ -64,5 +62,3 @@
}, function ($error) use ($stderr) {
$stderr->write('Connection ERROR: ' . $error . PHP_EOL);
});

$loop->run();
8 changes: 2 additions & 6 deletions examples/22-http-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// $ php examples/22-http-client.php
// $ php examples/22-http-client.php https://reactphp.org/

use React\EventLoop\Factory;
use React\Socket\ConnectionInterface;
use React\Socket\Connector;
use React\Stream\WritableResourceStream;
Expand All @@ -32,8 +31,7 @@
exit(1);
}

$loop = Factory::create();
$connector = new Connector($loop);
$connector = new Connector();

if (!isset($parts['port'])) {
$parts['port'] = $parts['scheme'] === 'https' ? 443 : 80;
Expand All @@ -49,12 +47,10 @@
$resource .= '?' . $parts['query'];
}

$stdout = new WritableResourceStream(STDOUT, $loop);
$stdout = new WritableResourceStream(STDOUT);

$connector->connect($target)->then(function (ConnectionInterface $connection) use ($resource, $host, $stdout) {
$connection->pipe($stdout);

$connection->write("GET $resource HTTP/1.0\r\nHost: $host\r\n\r\n");
}, 'printf');

$loop->run();
9 changes: 2 additions & 7 deletions examples/91-benchmark-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,18 @@
// $ nc -N -U /tmp/server.sock
// $ dd if=/dev/zero bs=1M count=1000 | nc -N -U /tmp/server.sock

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\ConnectionInterface;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();

$server = new Server(isset($argv[1]) ? $argv[1] : 0, $loop, array(
$server = new Server(isset($argv[1]) ? $argv[1] : 0, null, array(
'tls' => array(
'local_cert' => isset($argv[2]) ? $argv[2] : (__DIR__ . '/localhost.pem')
)
));

$server->on('connection', function (ConnectionInterface $connection) use ($loop) {
$server->on('connection', function (ConnectionInterface $connection) {
echo '[connected]' . PHP_EOL;

// count the number of bytes received from this connection
Expand All @@ -56,5 +53,3 @@
$server->on('error', 'printf');

echo 'Listening on ' . $server->getAddress() . PHP_EOL;

$loop->run();
4 changes: 3 additions & 1 deletion src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use React\Dns\Config\Config as DnsConfig;
use React\Dns\Resolver\Factory as DnsFactory;
use React\Dns\Resolver\ResolverInterface;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;

/**
Expand All @@ -26,8 +27,9 @@ final class Connector implements ConnectorInterface
{
private $connectors = array();

public function __construct(LoopInterface $loop, array $options = array())
public function __construct(LoopInterface $loop = null, array $options = array())
{
$loop = $loop ?: Loop::get();
// apply default options if not explicitly given
$options += array(
'tcp' => true,
Expand Down
2 changes: 1 addition & 1 deletion src/FixedUriConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* ```php
* $connector = new React\Socket\FixedUriConnector(
* 'unix:///var/run/docker.sock',
* new React\Socket\UnixConnector($loop)
* new React\Socket\UnixConnector()
* );
*
* // destination will be ignored, actually connects to Unix domain socket
Expand Down
16 changes: 13 additions & 3 deletions src/HappyEyeBallsConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace React\Socket;

use React\Dns\Resolver\ResolverInterface;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise;

Expand All @@ -12,9 +13,18 @@ final class HappyEyeBallsConnector implements ConnectorInterface
private $connector;
private $resolver;

public function __construct(LoopInterface $loop, ConnectorInterface $connector, ResolverInterface $resolver)
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, ResolverInterface $resolver = null)
{
$this->loop = $loop;
// $connector and $resolver arguments are actually required, marked
// optional for technical reasons only. Nullable $loop without default
// requires PHP 7.1, null default is also supported in legacy PHP
// versions, but required parameters are not allowed after arguments
// with null default. Mark all parameters optional and check accordingly.
if ($connector === null || $resolver === null) {
throw new \InvalidArgumentException('Missing required $connector or $resolver argument');
}

$this->loop = $loop ?: Loop::get();
$this->connector = $connector;
$this->resolver = $resolver;
}
Expand All @@ -34,7 +44,7 @@ public function connect($uri)
}

$host = \trim($parts['host'], '[]');

// skip DNS lookup / URI manipulation if this URI already contains an IP
if (false !== \filter_var($host, \FILTER_VALIDATE_IP)) {
return $this->connector->connect($uri);
Expand Down
5 changes: 3 additions & 2 deletions src/SecureConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace React\Socket;

use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise;
use BadMethodCallException;
Expand All @@ -14,10 +15,10 @@ final class SecureConnector implements ConnectorInterface
private $streamEncryption;
private $context;

public function __construct(ConnectorInterface $connector, LoopInterface $loop, array $context = array())
public function __construct(ConnectorInterface $connector, LoopInterface $loop = null, array $context = array())
{
$this->connector = $connector;
$this->streamEncryption = new StreamEncryption($loop, false);
$this->streamEncryption = new StreamEncryption($loop ?: Loop::get(), false);
$this->context = $context;
}

Expand Down
25 changes: 16 additions & 9 deletions src/SecureServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace React\Socket;

use Evenement\EventEmitter;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use BadMethodCallException;
use UnexpectedValueException;
Expand All @@ -15,8 +16,8 @@
* TCP/IP connections and then performs a TLS handshake for each connection.
*
* ```php
* $server = new React\Socket\TcpServer(8000, $loop);
* $server = new React\Socket\SecureServer($server, $loop, array(
* $server = new React\Socket\TcpServer(8000);
* $server = new React\Socket\SecureServer($server, null, array(
* // tls context options here…
* ));
* ```
Expand Down Expand Up @@ -67,8 +68,8 @@ final class SecureServer extends EventEmitter implements ServerInterface
* PEM encoded certificate file:
*
* ```php
* $server = new React\Socket\TcpServer(8000, $loop);
* $server = new React\Socket\SecureServer($server, $loop, array(
* $server = new React\Socket\TcpServer(8000);
* $server = new React\Socket\SecureServer($server, null, array(
* 'local_cert' => 'server.pem'
* ));
* ```
Expand All @@ -82,8 +83,8 @@ final class SecureServer extends EventEmitter implements ServerInterface
* like this:
*
* ```php
* $server = new React\Socket\TcpServer(8000, $loop);
* $server = new React\Socket\SecureServer($server, $loop, array(
* $server = new React\Socket\TcpServer(8000);
* $server = new React\Socket\SecureServer($server, null, array(
* 'local_cert' => 'server.pem',
* 'passphrase' => 'secret'
* ));
Expand All @@ -94,6 +95,12 @@ final class SecureServer extends EventEmitter implements ServerInterface
* and/or PHP version.
* Passing unknown context options has no effect.
*
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
* pass the event loop instance to use for this object. You can use a `null` value
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
* given event loop instance.
*
* Advanced usage: Despite allowing any `ServerInterface` as first parameter,
* you SHOULD pass a `TcpServer` instance as first parameter, unless you
* know what you're doing.
Expand All @@ -109,13 +116,13 @@ final class SecureServer extends EventEmitter implements ServerInterface
* then close the underlying connection.
*
* @param ServerInterface|TcpServer $tcp
* @param LoopInterface $loop
* @param ?LoopInterface $loop
* @param array $context
* @throws BadMethodCallException for legacy HHVM < 3.8 due to lack of support
* @see TcpServer
* @link https://www.php.net/manual/en/context.ssl.php for TLS context options
*/
public function __construct(ServerInterface $tcp, LoopInterface $loop, array $context)
public function __construct(ServerInterface $tcp, LoopInterface $loop = null, array $context = array())
{
if (!\function_exists('stream_socket_enable_crypto')) {
throw new \BadMethodCallException('Encryption not supported on your platform (HHVM < 3.8?)'); // @codeCoverageIgnore
Expand All @@ -127,7 +134,7 @@ public function __construct(ServerInterface $tcp, LoopInterface $loop, array $co
);

$this->tcp = $tcp;
$this->encryption = new StreamEncryption($loop);
$this->encryption = new StreamEncryption($loop ?: Loop::get());
$this->context = $context;

$that = $this;
Expand Down
Loading