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

Replace listen() call with URIs passed to constructor #61

Merged
merged 4 commits into from
Jan 27, 2017

Conversation

clue
Copy link
Member

@clue clue commented Jan 17, 2017

The current socket API exhibits a temporal dependence, i.e. it's not safe to call listen() or shutdown() multiple times on the same instance and call order is actually significant. (see #19)

This PR replaces the listen($port, $host) API with URIs passed to the constructor.
This means the Server now always represents a listening Socket (which may
possibly be in a closed state).

  • URIs are literally the only way to provide a consistent addressing scheme throughout React's components, see Consistent addressing scheme reactphp#199
  • Not all protocols know the concept of a "port", for example the future UnixServer currently would have an unused $port parameter (Add support for Unix domain sockets (UDS) #17)
  • The Server class accepts a single port parameter instead of a full URI and assumes a localhost socket in this case for better compatibility with previous releases.

This is obviously a BC break from a consumer perspective, but for the most part this is easily patched like this:

- $server = new Server($loop);
- $server->listen($port);
+ $server = new Server($port, $loop);

Also, empirical evidence suggests many places simply consume a ServerInterface in order to react to the connection event - this very common API is not affected.

This means that the Server class now starts listening immediately and a consumer can no longer defer the listen() call. I personally consider this a good thing (poka yoke) as I know many people have stumbled over this. Also, this API is now somewhat similar to Node's net.createServer() which also starts up a listening server (vs. the older explicit server.listen() call).

If you want to review this PR and don't want to mess with all these details, I'd suggest taking a look at the examples (and their minimal diff). The previous CLI accepted merely an optional port, the new CLI also accepts full listening addresses, for example like this:

$ php examples/02-chat.php 0.0.0.0:8080

Closes #19
Supersedes / closes #6, refs reactphp-legacy/socket-client#74, reactphp/reactphp#199 and #34

This fixes the temporal dependency design flaw for the most part,
because a Server now always represents a listening Socket (which may
possibly be in a closed state)
$this->loop->removeStream($this->master);
fclose($this->master);
$this->removeAllListeners();
if (is_resource($this->master)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpicking (and personal preference): I'd like to see an early return here.

if (!is_resource($this->master)) {
    return;
}

$this->loop->removeStream($this->master);
fclose($this->master);
$this->removeAllListeners();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clean up temporal dependence
3 participants