Skip to content

Commit 371095b

Browse files
committed
Improve error reporting
1 parent e86e8a0 commit 371095b

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/Factory.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use React\EventLoop\LoopInterface;
66
use React\Datagram\Socket as DatagramSocket;
77
use BadMethodCallException;
8+
use RuntimeException;
89

910
class Factory
1011
{
@@ -19,7 +20,10 @@ public function __construct(LoopInterface $loop)
1920

2021
public function createSender()
2122
{
22-
$stream = stream_socket_server('udp://0.0.0.0:0', $errno, $errstr, STREAM_SERVER_BIND);
23+
$stream = @stream_socket_server('udp://0.0.0.0:0', $errno, $errstr, STREAM_SERVER_BIND);
24+
if ($stream === false) {
25+
throw new RuntimeException('Unable to create sending socket: ' . $errstr, $errno);
26+
}
2327

2428
return new DatagramSocket($this->loop, $stream);
2529
}
@@ -32,20 +36,32 @@ public function createReceiver($address)
3236

3337
$parts = parse_url('udp://' . $address);
3438

35-
$stream = stream_socket_server('udp://0.0.0.0:' . $parts['port'], $errno, $errstr, STREAM_SERVER_BIND);
39+
$stream = @stream_socket_server('udp://0.0.0.0:' . $parts['port'], $errno, $errstr, STREAM_SERVER_BIND);
40+
if ($stream === false) {
41+
throw new RuntimeException('Unable to create receiving socket: ' . $errstr, $errno);
42+
}
3643

3744
$socket = socket_import_stream($stream);
45+
if ($stream === false) {
46+
throw new RuntimeException('Unable to access underlying socket resource');
47+
}
3848

3949
// allow multiple processes to bind to the same address
40-
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
50+
$ret = socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
51+
if ($ret === false) {
52+
throw new RuntimeException('Unable to enable SO_REUSEADDR');
53+
}
4154

4255
// join multicast group and bind to port
43-
socket_set_option(
56+
$ret = socket_set_option(
4457
$socket,
4558
IPPROTO_IP,
4659
MCAST_JOIN_GROUP,
4760
array('group' => $parts['host'], 'interface' => 0)
4861
);
62+
if ($ret === false) {
63+
throw new RuntimeException('Unable to join multicast group');
64+
}
4965

5066
return new DatagramSocket($this->loop, $stream);
5167
}

0 commit comments

Comments
 (0)