Skip to content

Commit ba697a2

Browse files
committed
Prototype for using stream based API
1 parent cca2a84 commit ba697a2

2 files changed

Lines changed: 14 additions & 22 deletions

File tree

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
"require": {
1717
"php": ">=5.3",
1818
"react/event-loop": "~0.3.0|~0.4.0",
19-
"react/promise": "~1.0|~2.0",
20-
"clue/socket-react": "~0.3.0"
19+
"react/datagram": "~1.0"
2120
},
2221
"require-dev": {
2322
"clue/hexdump": "0.2.*"

src/Factory.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
namespace Clue\React\Multicast;
44

55
use React\EventLoop\LoopInterface;
6-
use Socket\React\Datagram\Factory as DatagramFactory;
7-
use Socket\Raw\Factory as RawFactory;
6+
use React\Datagram\Socket as DatagramSocket;
87
use BadMethodCallException;
98

109
class Factory
@@ -13,24 +12,16 @@ class Factory
1312
private $rawFactory;
1413
private $datagramFactory;
1514

16-
public function __construct(LoopInterface $loop, RawFactory $rawFactory = null, DatagramFactory $datagramFactory = null)
15+
public function __construct(LoopInterface $loop)
1716
{
18-
if ($rawFactory === null) {
19-
$rawFactory = new RawFactory();
20-
}
21-
22-
if ($datagramFactory === null) {
23-
$datagramFactory = new DatagramFactory($loop);
24-
}
25-
26-
$this->rawFactory = $rawFactory;
27-
$this->datagramFactory = $datagramFactory;
17+
$this->loop = $loop;
2818
}
2919

3020
public function createSender()
3121
{
32-
$socket = $this->rawFactory->createUdp4();
33-
return $this->datagramFactory->createFromRaw($socket);
22+
$stream = stream_socket_server('udp://0.0.0.0:0', $errno, $errstr, STREAM_SERVER_BIND);
23+
24+
return new DatagramSocket($this->loop, $stream);
3425
}
3526

3627
public function createReceiver($address)
@@ -41,19 +32,21 @@ public function createReceiver($address)
4132

4233
$parts = parse_url('udp://' . $address);
4334

44-
$socket = $this->rawFactory->createUdp4();
35+
$stream = stream_socket_server('udp://0.0.0.0:' . $parts['port'], $errno, $errstr, STREAM_SERVER_BIND);
36+
37+
$socket = socket_import_stream($stream);
4538

4639
// allow multiple processes to bind to the same address
47-
$socket->setOption(SOL_SOCKET, SO_REUSEADDR, 1);
40+
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
4841

4942
// join multicast group and bind to port
50-
$socket->setOption(
43+
socket_set_option(
44+
$socket,
5145
IPPROTO_IP,
5246
MCAST_JOIN_GROUP,
5347
array('group' => $parts['host'], 'interface' => 0)
5448
);
55-
$socket->bind('0.0.0.0:' . $parts['port']);
5649

57-
return $this->datagramFactory->createFromRaw($socket);
50+
return new DatagramSocket($this->loop, $stream);
5851
}
5952
}

0 commit comments

Comments
 (0)