-
-
Notifications
You must be signed in to change notification settings - Fork 157
Support Unix domain socket (UDS) server #120
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace React\Socket; | ||
|
||
use Evenement\EventEmitter; | ||
use React\EventLoop\LoopInterface; | ||
use InvalidArgumentException; | ||
use RuntimeException; | ||
|
||
/** | ||
* The `UnixServer` class implements the `ServerInterface` and | ||
* is responsible for accepting plaintext connections on unix domain sockets. | ||
* | ||
* ```php | ||
* $server = new UnixServer('unix:///tmp/app.sock', $loop); | ||
* ``` | ||
* | ||
* See also the `ServerInterface` for more details. | ||
* | ||
* @see ServerInterface | ||
* @see ConnectionInterface | ||
*/ | ||
final class UnixServer extends EventEmitter implements ServerInterface | ||
{ | ||
private $master; | ||
private $loop; | ||
private $listening = false; | ||
|
||
/** | ||
* Creates a plaintext socket server and starts listening on the given unix socket | ||
* | ||
* This starts accepting new incoming connections on the given address. | ||
* See also the `connection event` documented in the `ServerInterface` | ||
* for more details. | ||
* | ||
* ```php | ||
* $server = new UnixServer('unix:///tmp/app.sock', $loop); | ||
* ``` | ||
* | ||
* @param string $path | ||
* @param LoopInterface $loop | ||
* @param array $context | ||
* @throws InvalidArgumentException if the listening address is invalid | ||
* @throws RuntimeException if listening on this address fails (already in use etc.) | ||
*/ | ||
public function __construct($path, LoopInterface $loop, array $context = array()) | ||
{ | ||
$this->loop = $loop; | ||
|
||
if (strpos($path, '://') === false) { | ||
$path = 'unix://' . $path; | ||
} elseif (substr($path, 0, 7) !== 'unix://') { | ||
throw new \InvalidArgumentException('Given URI "' . $path . '" is invalid'); | ||
} | ||
|
||
$this->master = @stream_socket_server( | ||
$path, | ||
$errno, | ||
$errstr, | ||
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, | ||
stream_context_create(array('socket' => $context)) | ||
); | ||
if (false === $this->master) { | ||
throw new RuntimeException('Failed to listen on unix domain socket "' . $path . '": ' . $errstr, $errno); | ||
} | ||
stream_set_blocking($this->master, 0); | ||
|
||
$this->resume(); | ||
} | ||
|
||
public function getAddress() | ||
{ | ||
if (!is_resource($this->master)) { | ||
return null; | ||
} | ||
|
||
return 'unix://' . stream_socket_get_name($this->master, false); | ||
} | ||
|
||
public function pause() | ||
{ | ||
if (!$this->listening) { | ||
return; | ||
} | ||
|
||
$this->loop->removeReadStream($this->master); | ||
$this->listening = false; | ||
} | ||
|
||
public function resume() | ||
{ | ||
if ($this->listening || !is_resource($this->master)) { | ||
return; | ||
} | ||
|
||
$that = $this; | ||
$this->loop->addReadStream($this->master, function ($master) use ($that) { | ||
$newSocket = @stream_socket_accept($master); | ||
if (false === $newSocket) { | ||
$that->emit('error', array(new \RuntimeException('Error accepting new connection'))); | ||
|
||
return; | ||
} | ||
$that->handleConnection($newSocket); | ||
}); | ||
$this->listening = true; | ||
} | ||
|
||
public function close() | ||
{ | ||
if (!is_resource($this->master)) { | ||
return; | ||
} | ||
|
||
$this->pause(); | ||
fclose($this->master); | ||
$this->removeAllListeners(); | ||
} | ||
|
||
/** @internal */ | ||
public function handleConnection($socket) | ||
{ | ||
$connection = new Connection($socket, $this->loop); | ||
$connection->unix = true; | ||
|
||
$this->emit('connection', array( | ||
$connection | ||
)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe export the Server constructing logic to a separate method like this: the-eater@2defb1b?diff=split
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functionally LGTM, however I don't currently see this covered in the test suite (plus minor CS issue above). Can you update this?
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@clue what's your expectation here? Testing the type of the create private server member? Would need a hint how to do this due to the private nature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is a facade that mostly contains some glue code to make the underlying APIs more easily accessible. Just check the other high-level functional tests and the code coverage. IMHO it's sufficient to create an Unix domain socket server with this facade and check a client connection can be established.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha. Tests added.