diff --git a/README.md b/README.md index 555bc68..d5f4153 100644 --- a/README.md +++ b/README.md @@ -21,11 +21,11 @@ Server: require 'vendor/autoload.php'; -use Amp\Ipc\IpcServer; use Amp\Ipc\Sync\ChannelledSocket; use Amp\Loop; use function Amp\asyncCall; +use function Amp\Ipc\listen; Loop::run(static function () { $clientHandler = function (ChannelledSocket $socket) { @@ -41,7 +41,7 @@ Loop::run(static function () { echo "Closed connection".PHP_EOL."==========".PHP_EOL; }; - $server = new IpcServer(\sys_get_temp_dir().'/test'); + $server = listen(\sys_get_temp_dir().'/test'); while ($socket = yield $server->accept()) { asyncCall($clientHandler, $socket); } diff --git a/examples/server.php b/examples/server.php index 8fd6df9..6d537da 100644 --- a/examples/server.php +++ b/examples/server.php @@ -2,11 +2,11 @@ require 'vendor/autoload.php'; -use Amp\Ipc\IpcServer; use Amp\Ipc\Sync\ChannelledSocket; use Amp\Loop; use function Amp\asyncCall; +use function Amp\Ipc\listen; Loop::run(static function () { $clientHandler = function (ChannelledSocket $socket) { @@ -22,7 +22,7 @@ Loop::run(static function () { echo "Closed connection".PHP_EOL."==========".PHP_EOL; }; - $server = new IpcServer(\sys_get_temp_dir().'/test'); + $server = listen(\sys_get_temp_dir().'/test'); while ($socket = yield $server->accept()) { asyncCall($clientHandler, $socket); } diff --git a/lib/functions.php b/lib/functions.php index 8b899ec..97a234a 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -7,6 +7,17 @@ use Amp\Promise; use function Amp\call; +/** + * Create IPC server. + * + * @param string $uri Local endpoint on which to listen for requests + * + * @return IpcServer + */ +function listen(string $uri): IpcServer +{ + return new IpcServer($uri); +} /** * Connect to IPC server. * diff --git a/test/Fixtures/echoServer.php b/test/Fixtures/echoServer.php new file mode 100644 index 0000000..f807865 --- /dev/null +++ b/test/Fixtures/echoServer.php @@ -0,0 +1,30 @@ +send($server->getUri()); + + $socket = yield $server->accept(); + + if (!$socket instanceof ChannelledSocket) { + throw new \RuntimeException('Socket is not instance of ChanneledSocket'); + } + + while (yield $socket->receive()); + yield $socket->disconnect(); + + $server->close(); + + return $server->accept(); +};