Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,23 @@ public function createReceiver($address)
throw new RuntimeException('Unable to enable SO_REUSEADDR');
}

set_error_handler(function () {
// ignore PHP warning, error is reported via socket_last_error() below
});

// join multicast group and bind to port
$ret = socket_set_option(
$socket,
IPPROTO_IP,
MCAST_JOIN_GROUP,
array('group' => $parts['host'], 'interface' => 0)
);

restore_error_handler();

if ($ret === false) {
throw new RuntimeException('Unable to join multicast group');
$errno = socket_last_error($socket);
throw new RuntimeException('Unable to join multicast group: ' . socket_strerror($errno), $errno);
}

return new DatagramSocket($this->loop, $stream);
Expand Down
25 changes: 25 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ public function testMultipleReceivers()
$this->loop->run();
}

public function testCreateReceiverThrowsForNonMulticastAddressWithoutCallingCustomErrorHandler()
{
if (!defined('MCAST_JOIN_GROUP') || !function_exists('socket_import_stream')) {
$this->markTestSkipped('No multicast support');
}

$error = null;
set_error_handler(function ($_, $errstr) use (&$error) {
$error = $errstr;
});

$this->setExpectedException('RuntimeException', 'Unable to join multicast group: ' . socket_strerror(SOCKET_EINVAL), SOCKET_EINVAL);

try {
$this->factory->createReceiver('127.0.0.1:2244');

restore_error_handler();
} catch (\Exception $e) {
restore_error_handler();
$this->assertNull($error);

throw $e;
}
}

public function testConstructWithoutLoopAssignsLoopAutomatically()
{
$factory = new Factory();
Expand Down