Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Unix domain sockets (UDS) #41

Merged
merged 1 commit into from
Sep 22, 2015
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,18 @@ $secureConnector->create('www.google.com', 443)->then(function (React\Stream\Str

$loop->run();
```

### Unix domain sockets

Similarly, the `UnixConnector` class can be used to connect to Unix domain socket (UDS)
paths like this:

```php
$connector = new React\SocketClient\UnixConnector($loop);

$connector->create('/tmp/demo.sock')->then(function (React\Stream\Stream $stream) {
$stream->write("HELLO\n");
});

$loop->run();
```
36 changes: 36 additions & 0 deletions src/UnixConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace React\SocketClient;

use React\SocketClient\ConnectorInterface;
use React\Stream\Stream;
use React\EventLoop\LoopInterface;
use React\Promise;
use RuntimeException;

/**
* Unix domain socket connector
*
* Unix domain sockets use atomic operations, so we can as well emulate
* async behavior.
*/
class UnixConnector implements ConnectorInterface
{
private $loop;

public function __construct(LoopInterface $loop)
{
$this->loop = $loop;
}

public function create($path, $unusedPort = 0)
{
$resource = @stream_socket_client('unix://' . $path, $errno, $errstr, 1.0);

if (!$resource) {
return Promise\reject(new RuntimeException('Unable to connect to unix domain socket "' . $path . '": ' . $errstr, $errno));
}

return Promise\resolve(new Stream($resource, $this->loop));
}
}
46 changes: 46 additions & 0 deletions tests/UnixConnectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace React\Tests\SocketClient;

use React\SocketClient\UnixConnector;

class UnixConnectorTest extends TestCase
{
private $loop;
private $connector;

public function setUp()
{
$this->loop = $this->getMock('React\EventLoop\LoopInterface');
$this->connector = new UnixConnector($this->loop);
}

public function testInvalid()
{
$promise = $this->connector->create('google.com', 80);
$promise->then(null, $this->expectCallableOnce());
}

public function testValid()
{
// random unix domain socket path
$path = sys_get_temp_dir() . '/test' . uniqid() . '.sock';

// temporarily create unix domain socket server to connect to
$server = stream_socket_server('unix://' . $path, $errno, $errstr);

// skip test if we can not create a test server (Windows etc.)
if (!$server) {
$this->markTestSkipped('Unable to create socket "' . $path . '": ' . $errstr . '(' . $errno .')');
return;
}

// tests succeeds if we get notified of successful connection
$promise = $this->connector->create($path, 0);
$promise->then($this->expectCallableOnce());

// clean up server
fclose($server);
unlink($path);
}
}