Skip to content

Commit

Permalink
Merge pull request #1 from vrza/server-exceptions
Browse files Browse the repository at this point in the history
Improved error handling when initializing server
  • Loading branch information
vrza authored Aug 3, 2024
2 parents 2cb578f + e48573b commit c94cdc4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
39 changes: 37 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,43 @@ 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

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();
----
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
31 changes: 29 additions & 2 deletions src/SocketStreamsServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("First argument must be a non-empty array of SocketData objects");
}

$this->socketsData = $socketsData;
$this->recvBufSize = $recvBufSize;

$this->checkEnv();
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c94cdc4

Please sign in to comment.