From c29d6d3bceb89532ca29acce2e099a782a9520a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Wed, 31 Jul 2024 14:25:37 +0200 Subject: [PATCH 1/5] Added simplified examples to the README --- README.adoc | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/README.adoc b/README.adoc index c822e16..61a06ec 100644 --- a/README.adoc +++ b/README.adoc @@ -12,4 +12,39 @@ The payload of the message is provided by the user. Serialisation format etc. is == Getting started -See examples in the `test/` directory. +See full working examples in the `test/` directory. + +=== Simple server + +[source,php] +---- +class MyMessageHandler implements MessageHandler { + public function handleMessage(string $msg): string { + return "Hello, client, I received: $msg"; + } +} + +$handler = new MyMessageHandler(); +$address = new InetSocketAddress('127.0.0.1', 1389, AF_INET); +$server = new SocketStreamsServer([ + new SocketData($address, $handler) +]); + +$server->listen(); +while (true) + $server->checkMessages(1); +---- + +=== Simple client + +[source,php] +---- +$address = new InetSocketAddress('127.0.0.1', 1389); +$client = new SocketStreamClient($address); + +$client->connect(); +$client->sendMessage("Hello, server!"); +$response = $client->receiveMessage(); +echo $response . PHP_EOL; +$client->disconnect(); +---- From f44caf725d7c842f5a322daf6c5c31fb3756092e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Wed, 31 Jul 2024 14:27:53 +0200 Subject: [PATCH 2/5] Improved error handling when initializing server --- src/SocketStreamsServer.php | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/SocketStreamsServer.php b/src/SocketStreamsServer.php index f527e37..9950c0a 100644 --- a/src/SocketStreamsServer.php +++ b/src/SocketStreamsServer.php @@ -2,6 +2,8 @@ namespace SimpleIPC\SyMPLib; +use InvalidArgumentException; + /** * IPC server that listens on multiple Unix sockets, * calling a respective message handler to handle data @@ -16,18 +18,32 @@ class SocketStreamsServer private $recvBufSize; private $socketsData; - private $sockets; - private $handlers; + private $sockets = []; + private $handlers = []; public $verbosity = 0; /** * @param SocketData[] $socketsData * @param int $recvBufSize + * + * @throws InvalidArgumentException */ public function __construct(array $socketsData, int $recvBufSize = self::RECV_BUF_SIZE) { + $correctTypes = array_reduce( + $socketsData, + function ($a, $x) { + return $a && $x instanceof SocketData; + }, + true + ); + if (empty($socketsData) || !$correctTypes) { + throw new InvalidArgumentException("You must provide a non-empty array of SocketData objects"); + } + $this->socketsData = $socketsData; $this->recvBufSize = $recvBufSize; + $this->checkEnv(); } @@ -101,8 +117,19 @@ private static function createAndBindSocket(SocketAddress $address) return $socket; } + /** + * @param int $timeoutSeconds + * @param int $timeoutMicroseconds + * + * @throws NoListeningSocketsException + * + * @return int Returns the number of messages handled + */ public function checkMessages(int $timeoutSeconds = 0, int $timeoutMicroseconds = 0): int { + if (empty($this->sockets)) { + throw new NoListeningSocketsException("No listening sockets, make sure you call listen() before checkMessages()"); + } $limit = 1024; $cnt = 0; $sec = $timeoutSeconds; From 0150c786bb42b18ba9c069eca76f7d9586356bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Wed, 31 Jul 2024 14:31:42 +0200 Subject: [PATCH 3/5] Improved the exception message wording --- src/SocketStreamsServer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SocketStreamsServer.php b/src/SocketStreamsServer.php index 9950c0a..f5a22af 100644 --- a/src/SocketStreamsServer.php +++ b/src/SocketStreamsServer.php @@ -38,7 +38,7 @@ function ($a, $x) { true ); if (empty($socketsData) || !$correctTypes) { - throw new InvalidArgumentException("You must provide a non-empty array of SocketData objects"); + throw new InvalidArgumentException("First argument must be a non-empty array of SocketData objects"); } $this->socketsData = $socketsData; From 37b5233affa77676b2c605b1f5c7b515642fb43c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Wed, 31 Jul 2024 14:36:07 +0200 Subject: [PATCH 4/5] Changed keywords to a minimal set --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6425a02..61dc392 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "simple-ipc/php-symplib", "description": "Synchronous Message Passing Framework for PHP", "type": "library", - "keywords": ["ipc", "sockets", "bsd", "posix"], + "keywords": ["ipc", "sockets"], "license": "BSD-3-Clause", "authors": [ { From e48573b8fc4256cfe156564142ebab1e02dba30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Wed, 31 Jul 2024 14:37:13 +0200 Subject: [PATCH 5/5] Fixed typo --- README.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.adoc b/README.adoc index 61a06ec..a8f06de 100644 --- a/README.adoc +++ b/README.adoc @@ -8,7 +8,7 @@ Stream sockets are used as message passing channels. A single server can listen Messages use a simple wire protocol, where the first 8 bytes of the message specify the length of the rest of the message (unsigned, big endian). -The payload of the message is provided by the user. Serialisation format etc. is left to user's choice. +The payload of the message is provided by the user. Serialization format etc. is left to user's choice. == Getting started