@@ -28,11 +28,57 @@ to initialize the main loop.
28
28
$loop = React\EventLoop\Factory::create();
29
29
```
30
30
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
+
31
77
### Async TCP/IP connections
32
78
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:
36
82
37
83
``` php
38
84
$tcpConnector = new React\SocketClient\TcpConnector($loop);
@@ -69,17 +115,18 @@ $tcpConnector = new React\SocketClient\TcpConnector($loop, array(
69
115
));
70
116
```
71
117
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.
74
120
75
121
### DNS resolution
76
122
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 .
80
126
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.
83
130
84
131
Make sure to set up your DNS resolver and underlying TCP connector like this:
85
132
@@ -122,11 +169,13 @@ $connector->create('www.google.com', 80)->then($callback);
122
169
123
170
### Async SSL/TLS connections
124
171
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.
127
175
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.
130
179
131
180
``` php
132
181
$secureConnector = new React\SocketClient\SecureConnector($dnsConnector, $loop);
@@ -173,8 +222,12 @@ stream resources will use a single, shared *default context* resource otherwise.
173
222
174
223
### Connection timeouts
175
224
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
178
231
underlying connection attempt if it takes too long.
179
232
180
233
``` php
@@ -200,8 +253,9 @@ attempt, abort the timer and reject the resulting promise.
200
253
201
254
### Unix domain sockets
202
255
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:
205
259
206
260
``` php
207
261
$connector = new React\SocketClient\UnixConnector($loop);
0 commit comments