Skip to content

Commit 4ddda7b

Browse files
authored
Merge pull request #77 from clue-labs/docs
Documentation for ConnectorInterface
2 parents 624ef62 + aafd20d commit 4ddda7b

File tree

3 files changed

+109
-18
lines changed

3 files changed

+109
-18
lines changed

README.md

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,57 @@ to initialize the main loop.
2828
$loop = React\EventLoop\Factory::create();
2929
```
3030

31+
### ConnectorInterface
32+
33+
The `ConnectorInterface` is responsible for providing an interface for
34+
establishing streaming connections, such as a normal TCP/IP connection.
35+
36+
This is the main interface defined in this package and it is used throughout
37+
React's vast ecosystem.
38+
39+
Most higher-level components (such as HTTP, database or other networking
40+
service clients) accept an instance implementing this interface to create their
41+
TCP/IP connection to the underlying networking service.
42+
This is usually done via dependency injection, so it's fairly simple to actually
43+
swap this implementation against any other implementation of this interface.
44+
45+
The interface only offers a single method:
46+
47+
#### create()
48+
49+
The `create(string $host, int $port): PromiseInterface<Stream, Exception>` method
50+
can be used to establish a streaming connection.
51+
It returns a [Promise](https://github.com/reactphp/promise) which either
52+
fulfills with a [Stream](https://github.com/reactphp/stream) or
53+
rejects with an `Exception`:
54+
55+
```php
56+
$connector->create('google.com', 443)->then(
57+
function (Stream $stream) {
58+
// connection successfully established
59+
},
60+
function (Exception $error) {
61+
// failed to connect due to $error
62+
}
63+
);
64+
```
65+
66+
The returned Promise SHOULD be implemented in such a way that it can be
67+
cancelled when it is still pending. Cancelling a pending promise SHOULD
68+
reject its value with an `Exception`. It SHOULD clean up any underlying
69+
resources and references as applicable:
70+
71+
```php
72+
$promise = $connector->create($host, $port);
73+
74+
$promise->cancel();
75+
```
76+
3177
### Async TCP/IP connections
3278

33-
The `React\SocketClient\TcpConnector` provides a single promise-based
34-
`create($ip, $port)` method which resolves as soon as the connection
35-
succeeds or fails.
79+
The `React\SocketClient\TcpConnector` class implements the
80+
[`ConnectorInterface`](#connectorinterface) and allows you to create plaintext
81+
TCP/IP connections to any IP-port-combination:
3682

3783
```php
3884
$tcpConnector = new React\SocketClient\TcpConnector($loop);
@@ -69,17 +115,18 @@ $tcpConnector = new React\SocketClient\TcpConnector($loop, array(
69115
));
70116
```
71117

72-
Note that this class only allows you to connect to IP/port combinations.
73-
If you want to connect to hostname/port combinations, see also the following chapter.
118+
Note that this class only allows you to connect to IP-port-combinations.
119+
If you want to connect to hostname-port-combinations, see also the following chapter.
74120

75121
### DNS resolution
76122

77-
The `DnsConnector` class decorates a given `TcpConnector` instance by first
78-
looking up the given domain name and then establishing the underlying TCP/IP
79-
connection to the resolved IP address.
123+
The `DnsConnector` class implements the
124+
[`ConnectorInterface`](#connectorinterface) and allows you to create plaintext
125+
TCP/IP connections to any hostname-port-combination.
80126

81-
It provides the same promise-based `create($host, $port)` method which resolves with
82-
a `Stream` instance that can be used just like above.
127+
It does so by decorating a given `TcpConnector` instance so that it first
128+
looks up the given domain name via DNS (if applicable) and then establishes the
129+
underlying TCP/IP connection to the resolved target IP address.
83130

84131
Make sure to set up your DNS resolver and underlying TCP connector like this:
85132

@@ -122,11 +169,13 @@ $connector->create('www.google.com', 80)->then($callback);
122169

123170
### Async SSL/TLS connections
124171

125-
The `SecureConnector` class decorates a given `Connector` instance by enabling
126-
SSL/TLS encryption as soon as the raw TCP/IP connection succeeds.
172+
The `SecureConnector` class implements the
173+
[`ConnectorInterface`](#connectorinterface) and allows you to create secure
174+
TLS (formerly known as SSL) connections to any hostname-port-combination.
127175

128-
It provides the same promise- based `create($host, $port)` method which resolves with
129-
a `Stream` instance that can be used just like any non-encrypted stream:
176+
It does so by decorating a given `DnsConnector` instance so that it first
177+
creates a plaintext TCP/IP connection and then enables TLS encryption on this
178+
stream.
130179

131180
```php
132181
$secureConnector = new React\SocketClient\SecureConnector($dnsConnector, $loop);
@@ -173,8 +222,12 @@ stream resources will use a single, shared *default context* resource otherwise.
173222

174223
### Connection timeouts
175224

176-
The `TimeoutConnector` class decorates any given `Connector` instance.
177-
It provides the same `create()` method, but will automatically reject the
225+
The `TimeoutConnector` class implements the
226+
[`ConnectorInterface`](#connectorinterface) and allows you to add timeout
227+
handling to any existing connector instance.
228+
229+
It does so by decorating any given [`ConnectorInterface`](#connectorinterface)
230+
instance and starting a timer that will automatically reject and abort any
178231
underlying connection attempt if it takes too long.
179232

180233
```php
@@ -200,8 +253,9 @@ attempt, abort the timer and reject the resulting promise.
200253

201254
### Unix domain sockets
202255

203-
Similarly, the `UnixConnector` class can be used to connect to Unix domain socket (UDS)
204-
paths like this:
256+
The `UnixConnector` class implements the
257+
[`ConnectorInterface`](#connectorinterface) and allows you to connect to
258+
Unix domain socket (UDS) paths like this:
205259

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

src/Connector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
use React\Dns\Resolver\Resolver;
77

88
/**
9+
* Legacy Connector
10+
*
11+
* This class is not to be confused with the ConnectorInterface and should not
12+
* be used as a typehint.
13+
*
914
* @deprecated Exists for BC only, consider using the newer DnsConnector instead
15+
* @see DnsConnector for the newer replacement
16+
* @see ConnectorInterface for the base interface
1017
*/
1118
class Connector implements ConnectorInterface
1219
{

src/ConnectorInterface.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,37 @@
22

33
namespace React\SocketClient;
44

5+
/**
6+
* The `ConnectorInterface` is responsible for providing an interface for
7+
* establishing streaming connections, such as a normal TCP/IP connection.
8+
*
9+
* This is the main interface defined in this package and it is used throughout
10+
* React's vast ecosystem.
11+
*
12+
* Most higher-level components (such as HTTP, database or other networking
13+
* service clients) accept an instance implementing this interface to create their
14+
* TCP/IP connection to the underlying networking service.
15+
* This is usually done via dependency injection, so it's fairly simple to actually
16+
* swap this implementation against any other implementation of this interface.
17+
*
18+
* The interface only offers a single `create()` method.
19+
*/
520
interface ConnectorInterface
621
{
22+
/**
23+
* Creates a Promise which resolves with a stream once the connection to the given remote address succeeds
24+
*
25+
* The Promise resolves with a `React\Stream\Stream` instance on success or
26+
* rejects with an `Exception` if the connection is not successful.
27+
*
28+
* The returned Promise SHOULD be implemented in such a way that it can be
29+
* cancelled when it is still pending. Cancelling a pending promise SHOULD
30+
* reject its value with an Exception. It SHOULD clean up any underlying
31+
* resources and references as applicable.
32+
*
33+
* @param string $host
34+
* @param int $port
35+
* @return React\Promise\PromiseInterface resolves with a Stream on success or rejects with an Exception on error
36+
*/
737
public function create($host, $port);
838
}

0 commit comments

Comments
 (0)