Skip to content

Commit ff46ec4

Browse files
committed
Improve documentation and add API docs (docblocks)
1 parent 86d93cc commit ff46ec4

File tree

4 files changed

+94
-15
lines changed

4 files changed

+94
-15
lines changed

README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Among others, multicast networking is the basis for:
1919
* [Factory](#factory)
2020
* [createSender()](#createsender)
2121
* [createReceiver()](#createreceiver)
22-
* [Socket](#socket)
22+
* [SocketInterface](#socketinterface)
2323
* [Install](#install)
2424
* [Tests](#tests)
2525
* [License](#license)
@@ -48,7 +48,7 @@ See also the [examples](examples).
4848

4949
### Factory
5050

51-
The `Factory` is responsible for creating your [`Socket`](#socket) instances.
51+
The `Factory` is responsible for creating your [`SocketInterface`](#socketinterface) instances.
5252
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
5353

5454
```php
@@ -58,7 +58,9 @@ $factory = new Factory($loop);
5858

5959
#### createSender()
6060

61-
The `createSender()` method can be used to create a socket capable of sending outgoing multicast datagrams and receiving incoming unicast responses. It returns a [`Socket`](#socket) instance.
61+
The `createSender(): SocketInterface` method can be used to
62+
create a socket capable of sending outgoing multicast datagrams and receiving
63+
incoming unicast responses. It returns a [`SocketInterface`](#socketinterface) instance.
6264

6365
```php
6466
$socket = $factory->createSender();
@@ -73,11 +75,13 @@ $socket->on('message', function ($data, $address) {
7375
```
7476

7577
This method works on PHP versions as old as PHP 5.3 (and up), as its socket API has always been
76-
[level 1 multicast conformant](http://www.tldp.org/HOWTO/Multicast-HOWTO-2.html#ss2.2).
78+
[level 1 multicast conformant](https://www.tldp.org/HOWTO/Multicast-HOWTO-2.html#ss2.2).
7779

7880
#### createReceiver()
7981

80-
The `createReceiver($address)` method can be used to create a socket capable of receiving incoming multicast datagrams and sending outgoing unicast or multicast datagrams. It returns a [`Socket`](#socket) instance.
82+
The `createReceiver(string $address): SocketInterface` method can be used to
83+
create a socket capable of receiving incoming multicast datagrams and sending
84+
outgoing unicast or multicast datagrams. It returns a [`SocketInterface`](#socketinterface) instance.
8185

8286
```php
8387
$socket = $factory->createReceiver('224.10.20.30:4050');
@@ -91,10 +95,10 @@ $socket->on('message', function ($data, $remote) use ($socket) {
9195
});
9296
```
9397

94-
This method requires PHP 5.4 (or up) and ext-sockets.
98+
This method requires PHP 5.4 (or up) and `ext-sockets`.
9599
Otherwise, it will throw a `BadMethodCallException`.
96100
This is a requirement because receiving multicast datagrams requires a
97-
[level 2 multicast conformant](http://www.tldp.org/HOWTO/Multicast-HOWTO-2.html#ss2.2)
101+
[level 2 multicast conformant](https://www.tldp.org/HOWTO/Multicast-HOWTO-2.html#ss2.2)
98102
socket API.
99103
The required multicast socket options and constants have been added with
100104
[PHP 5.4](http://php.net/manual/en/migration54.global-constants.php) (and up).
@@ -104,12 +108,14 @@ to the newer stream based networking API.
104108
Internally, this library uses a workaround to create stream based sockets
105109
and then sets the required socket options on its underlying low level socket
106110
resource.
107-
This is done because React PHP is built around the general purpose stream based API
111+
This is done because ReactPHP is built around the general purpose stream based API
108112
and has only somewhat limited support for the low level socket API.
109113

110-
### Socket
114+
### SocketInterface
111115

112-
The [`Factory`](#factory) creates instances of the `React\Datagram\Socket` class from the [react/datagram](https://github.com/reactphp/datagram) package.
116+
The [`Factory`](#factory) creates instances of the `React\Datagram\SocketInterface`
117+
from the [react/datagram](https://github.com/reactphp/datagram) package.
118+
This means that you can use all its normal methods like so:
113119

114120
```php
115121
$socket->send($message, $address);
@@ -140,6 +146,10 @@ extensions and supports running on legacy PHP 5.3 through current PHP 7+ and
140146
HHVM.
141147
It's *highly recommended to use PHP 7+* for this project.
142148

149+
The [`createSender()`](#createsender) method works on all supported platforms
150+
without any additional requirements. However, the [`createReceiver()`](#createreceiver)
151+
method requires PHP 5.4 (or up) and `ext-sockets`. See above for more details.
152+
143153
## Tests
144154

145155
To run the test suite, you first need to clone this repo and then install all

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"phpunit/phpunit": "^6.0 || ^5.7 || ^4.8.35"
2424
},
2525
"suggest": {
26-
"php": "PHP 5.4+ is required for listening on multicast addresses (socket options to send IGMP announcements)",
27-
"ext-sockets": "Low level socket API required for listening on multicast addresses (socket options to send IGMP announcements)"
26+
"ext-sockets": "Requires PHP 5.4+ and the low level socket API for listening on multicast addresses (socket options to send IGMP announcements)"
2827
}
2928
}

examples/ssdp.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* UPNP simple service discovery protocol (SSDP)
3+
* UPnP simple service discovery protocol (SSDP)
44
*/
55

66
use Clue\React\Multicast\Factory;
@@ -14,7 +14,7 @@
1414
$sender = $factory->createSender();
1515

1616
// dump all incoming messages
17-
$sender->on('message', function ($data, $remote) use ($hex) {
17+
$sender->on('message', function ($data, $remote) {
1818
echo 'Received from ' . $remote . PHP_EOL;
1919
echo $data . PHP_EOL;
2020
});
@@ -24,7 +24,7 @@
2424
$sender->pause();
2525
});
2626

27-
// send a discovery message that all upnp/ssdp aware devices will respond to
27+
// send a discovery message that all UPnP/SSDP aware devices will respond to
2828
$data = "M-SEARCH * HTTP/1.1\r\n";
2929
$data .= "HOST: " . $address . "\r\n";
3030
$data .= "MAN: \"ssdp:discover\"\r\n";

src/Factory.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,44 @@ class Factory
1111
{
1212
private $loop;
1313

14+
/**
15+
* The `Factory` is responsible for creating your [`SocketInterface`](#socketinterface) instances.
16+
* It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
17+
*
18+
* ```php
19+
* $loop = React\EventLoop\Factory::create();
20+
* $factory = new Factory($loop);
21+
* ```
22+
*
23+
* @param LoopInterface $loop
24+
*/
1425
public function __construct(LoopInterface $loop)
1526
{
1627
$this->loop = $loop;
1728
}
1829

30+
/**
31+
* Creates a socket capable of sending outgoing multicast datagrams and receiving
32+
* incoming unicast responses. It returns a [`SocketInterface`](#socketinterface) instance.
33+
*
34+
* ```php
35+
* $socket = $factory->createSender();
36+
*
37+
* // send a multicast message to everybody listening on the given address
38+
* $socket->send('hello?', '224.10.20.30:4050');
39+
*
40+
* // report incoming unicast replies
41+
* $socket->on('message', function ($data, $address) {
42+
* echo 'received ' . strlen($data) . ' bytes from ' . $address . PHP_EOL;
43+
* });
44+
* ```
45+
*
46+
* This method works on PHP versions as old as PHP 5.3 (and up), as its socket API has always been
47+
* [level 1 multicast conformant](https://www.tldp.org/HOWTO/Multicast-HOWTO-2.html#ss2.2).
48+
*
49+
* @return \React\Datagram\SocketInterface
50+
* @throws RuntimeException
51+
*/
1952
public function createSender()
2053
{
2154
$stream = @stream_socket_server('udp://0.0.0.0:0', $errno, $errstr, STREAM_SERVER_BIND);
@@ -26,6 +59,43 @@ public function createSender()
2659
return new DatagramSocket($this->loop, $stream);
2760
}
2861

62+
/**
63+
* Creates a socket capable of receiving incoming multicast datagrams and sending
64+
* outgoing unicast or multicast datagrams. It returns a [`SocketInterface`](#socketinterface) instance.
65+
*
66+
* ```php
67+
* $socket = $factory->createReceiver('224.10.20.30:4050');
68+
*
69+
* // report incoming multicast messages
70+
* $socket->on('message', function ($data, $remote) use ($socket) {
71+
* echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL;
72+
*
73+
* // send a unicast reply to the remote
74+
* $socket->send($data, $remote);
75+
* });
76+
* ```
77+
*
78+
* This method requires PHP 5.4 (or up) and `ext-sockets`.
79+
* Otherwise, it will throw a `BadMethodCallException`.
80+
* This is a requirement because receiving multicast datagrams requires a
81+
* [level 2 multicast conformant](https://www.tldp.org/HOWTO/Multicast-HOWTO-2.html#ss2.2)
82+
* socket API.
83+
* The required multicast socket options and constants have been added with
84+
* [PHP 5.4](http://php.net/manual/en/migration54.global-constants.php) (and up).
85+
* These options are only available to the low level socket API (ext-sockets), not
86+
* to the newer stream based networking API.
87+
*
88+
* Internally, this library uses a workaround to create stream based sockets
89+
* and then sets the required socket options on its underlying low level socket
90+
* resource.
91+
* This is done because ReactPHP is built around the general purpose stream based API
92+
* and has only somewhat limited support for the low level socket API.
93+
*
94+
* @param string $address
95+
* @return \React\Datagram\SocketInterface
96+
* @throws BadMethodCallException
97+
* @throws RuntimeException
98+
*/
2999
public function createReceiver($address)
30100
{
31101
if (!defined('MCAST_JOIN_GROUP')) {

0 commit comments

Comments
 (0)