1
0
mirror of https://github.com/danog/ipc.git synced 2024-11-26 20:15:05 +01:00

Add missing files

This commit is contained in:
Daniil Gentili 2020-07-17 14:37:37 +02:00
parent 787ca479ef
commit 1a30e5492a
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 45 additions and 4 deletions

View File

@ -21,11 +21,11 @@ Server:
require 'vendor/autoload.php'; require 'vendor/autoload.php';
use Amp\Ipc\IpcServer;
use Amp\Ipc\Sync\ChannelledSocket; use Amp\Ipc\Sync\ChannelledSocket;
use Amp\Loop; use Amp\Loop;
use function Amp\asyncCall; use function Amp\asyncCall;
use function Amp\Ipc\listen;
Loop::run(static function () { Loop::run(static function () {
$clientHandler = function (ChannelledSocket $socket) { $clientHandler = function (ChannelledSocket $socket) {
@ -41,7 +41,7 @@ Loop::run(static function () {
echo "Closed connection".PHP_EOL."==========".PHP_EOL; 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()) { while ($socket = yield $server->accept()) {
asyncCall($clientHandler, $socket); asyncCall($clientHandler, $socket);
} }

View File

@ -2,11 +2,11 @@
require 'vendor/autoload.php'; require 'vendor/autoload.php';
use Amp\Ipc\IpcServer;
use Amp\Ipc\Sync\ChannelledSocket; use Amp\Ipc\Sync\ChannelledSocket;
use Amp\Loop; use Amp\Loop;
use function Amp\asyncCall; use function Amp\asyncCall;
use function Amp\Ipc\listen;
Loop::run(static function () { Loop::run(static function () {
$clientHandler = function (ChannelledSocket $socket) { $clientHandler = function (ChannelledSocket $socket) {
@ -22,7 +22,7 @@ Loop::run(static function () {
echo "Closed connection".PHP_EOL."==========".PHP_EOL; 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()) { while ($socket = yield $server->accept()) {
asyncCall($clientHandler, $socket); asyncCall($clientHandler, $socket);
} }

View File

@ -7,6 +7,17 @@ use Amp\Promise;
use function Amp\call; 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. * Connect to IPC server.
* *

View File

@ -0,0 +1,30 @@
<?php
\error_reporting(E_ALL);
\ini_set('log_errors', 1);
\ini_set('error_log', '/tmp/amphp.log');
\error_log('Inited IPC test!');
use Amp\Ipc\IpcServer;
use Amp\Ipc\Sync\ChannelledSocket;
use Amp\Parallel\Sync\Channel;
use function Amp\delay;
return function (Channel $channel) use ($argv) {
$server = new IpcServer($argv[1], $argv[2] === "1" ? true : false);
yield $channel->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();
};