Skip to content

Commit b3469a1

Browse files
committed
Documentation for Connector
1 parent 75110fc commit b3469a1

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ to google.com via a local SOCKS proxy server:
1212
```php
1313
$loop = React\EventLoop\Factory::create();
1414
$client = new Client('127.0.0.1:9050', $loop);
15+
$connector = $client->createConnector();
1516

16-
$client->getConnection('www.google.com:80')->then(function ($stream) {
17+
$connector->create('www.google.com:80')->then(function ($stream) {
1718
$stream->write("GET / HTTP/1.0\r\n\r\n");
1819
});
1920

@@ -263,6 +264,24 @@ If you do not want to use authentication anymore:
263264
$client->unsetAuth();
264265
```
265266

267+
### Connector
268+
269+
The `Connector` instance can be used to establish TCP connections to remote hosts.
270+
Each instance can be used to establish any number of TCP connections.
271+
272+
It implements React's `ConnectorInterface` which only provides a single
273+
`create()` method.
274+
275+
The `create($host, $port)` method can be used to establish a TCP
276+
connection to the given target host and port.
277+
278+
It functions as an [adapter](https://en.wikipedia.org/wiki/Adapter_pattern):
279+
Many higher-level networking protocols build on top of TCP. It you're dealing
280+
with one such client implementation, it probably uses/accepts an instance
281+
implementing React's `ConnectorInterface` (and usually its default `Connector`
282+
instance). In this case you can also pass this `Connector` instance instead
283+
to make this client implementation SOCKS-aware. That's it.
284+
266285
### Server
267286

268287
#### Server protocol

src/Client.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,46 @@ public function unsetAuth()
135135
$this->auth = null;
136136
}
137137

138+
/**
139+
* Creates a SecureConnector instance that can be used to establish encrypted TLS connections to remote hosts.
140+
*
141+
* This is actually a convenience helper method that uses React's normal
142+
* `SecureConnector` wrapper and this libraries' `Connector` for the
143+
* underlying TCP connection.
144+
*
145+
* @return SecureConnector
146+
* @uses self::createConnector()
147+
*/
138148
public function createSecureConnector()
139149
{
140150
return new SecureConnector($this->createConnector(), $this->loop);
141151
}
142152

153+
/**
154+
* Creates a Connector instance that can be used to establish TCP connections to remote hosts.
155+
*
156+
* This return a new `Connector` instance which can then be used to establish
157+
* any number of TCP connections.
158+
*
159+
* @return Connector
160+
* @see Connector
161+
*/
143162
public function createConnector()
144163
{
145164
return new Connector($this);
146165
}
147166

167+
/**
168+
* Should not be called directly, use createConnector() instead.
169+
*
170+
* This method contains the internal implementation for estasblishing a
171+
* connection to the SOCKS server.
172+
*
173+
* @param string $host
174+
* @param int $port
175+
* @return Promise Promise<Stream,Exception>
176+
* @internal use self::createConnector() instead
177+
*/
148178
public function getConnection($host, $port)
149179
{
150180
if (strlen($host) > 255 || $port > 65535 || $port < 0) {
@@ -214,6 +244,17 @@ private function resolve($host)
214244
return $this->resolver->resolve($host);
215245
}
216246

247+
/**
248+
* Internal helper used to handle the communication with the SOCKS server
249+
*
250+
* @param Stream $stream
251+
* @param string $host
252+
* @param int $port
253+
* @param float $timeout
254+
* @param string $protocolVersion
255+
* @param string|null $auth
256+
* @return Promise Promise<stream, Exception>
257+
*/
217258
public function handleConnectedSocks(Stream $stream, $host, $port, $timeout, $protocolVersion, $auth=null)
218259
{
219260
$deferred = new Deferred();

src/Connector.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55
use React\SocketClient\ConnectorInterface;
66
use Clue\React\Socks\Client;
77

8+
/**
9+
* The Connector instance can be used to establish TCP connections to remote hosts.
10+
*
11+
* Each instance can be used to establish any number of TCP connections.
12+
*
13+
* It implements React's `ConnectorInterface` which only provides a single
14+
* `create()` method.
15+
*
16+
* You can use this method directly in order to establish a TCP connection to
17+
* the given target host and port.
18+
*
19+
* It functions as an adapter:
20+
* Many higher-level networking protocols build on top of TCP. It you're dealing
21+
* with one such client implementation, it probably uses/accepts an instance
22+
* implementing React's `ConnectorInterface` (and usually its default `Connector`
23+
* instance). In this case you can also pass this `Connector` instance instead
24+
* to make this client implementation SOCKS-aware. That's it.
25+
*/
826
class Connector implements ConnectorInterface
927
{
1028
private $client;

0 commit comments

Comments
 (0)