Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed May 21, 2019
1 parent aaa315e commit 8b3309e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 47 deletions.
32 changes: 3 additions & 29 deletions docs/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,12 @@ function connect(

### TLS

If you want to connect via TLS, you can use `Amp\Socket\cryptoConnect()`, which connects to the specified URI and enables TLS in one step.

```php
/**
* Asynchronously establish an encrypted TCP connection (non-blocking).
*
* Note: Once resolved the socket stream will already be set to non-blocking mode.
*
* @param string $uri
* @param ClientConnectContext $socketContext
* @param ClientTlsContext $tlsContext
* @param CancellationToken $token
*
* @return Promise<ClientSocket>
*/
function cryptoConnect(
string $uri,
ClientConnectContext $socketContext = null,
ClientTlsContext $tlsContext = null,
CancellationToken $token = null
): Promise {
/* ... */
}
```

{:.note}
> If you want to connect and enable TLS at a later time, you can use `Socket::enableCrypto()` on the `Socket` instance returned from `connect()`.
If you want to connect via TLS, you need use `Amp\Socket\connect()` and then call `$socket->setupTls()` on the returned socket.

## Sending Data

`ClientSocket` implements `OutputStream`, so everything from [`amphp/byte-stream`](https://amphp.org/byte-stream/#outputstream) applies.
`EncryptableSocket` implements `OutputStream`, so everything from [`amphp/byte-stream`](https://amphp.org/byte-stream/#outputstream) applies.

## Receiving Data

`ClientSocket` implements `InputStream`, so everything from [`amphp/byte-stream`](https://amphp.org/byte-stream/#inputstream) applies.
`EncryptableSocket` implements `InputStream`, so everything from [`amphp/byte-stream`](https://amphp.org/byte-stream/#inputstream) applies.
28 changes: 15 additions & 13 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ permalink: /
```php
$uri = new Uri($argv[1]);
$host = $uri->getHost();
$port = $uri->getPort()

$connectContext = (new ClientConnectContext)
->withTlsContext(new ClientTlsContext($host));

$socket = yield connect("tcp://" . $host . ":" . $port, $connectContext);

if ($uri->getScheme() === "https") {
/** @var Socket $socket */
$socket = yield cryptoConnect("tcp://" . $host . ":" . $uri->getPort());
} else {
/** @var Socket $socket */
$socket = yield connect("tcp://" . $host . ":" . $uri->getPort());
$socket->setupTls();
}

yield $socket->write("GET {$uri} HTTP/1.1\r\nHost: $host\r\nConnection: close\r\n\r\n");
Expand All @@ -29,22 +31,22 @@ while (null !== $chunk = yield $socket->read()) {

```php
Loop::run(function () {
$clientHandler = function (ServerSocket $socket) {
list($ip, $port) = explode(":", $socket->getRemoteAddress());
$clientHandler = function (ResourceSocket $socket) {
[$ip, $port] = explode(":", $socket->getRemoteAddress());

echo "Accepted connection from {$ip}:{$port}." . PHP_EOL;

$body = "Hey, your IP is {$ip} and your local port used is {$port}.";
$bodyLength = \strlen($body);

yield $socket->end("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: {$bodyLength}\r\n\r\n{$body}");
};

$server = Amp\Socket\listen("127.0.0.1:0");

echo "Listening for new connections on " . $server->getAddress() . " ..." . PHP_EOL;
echo "Open your browser and visit http://" . $server->getAddress() . "/" . PHP_EOL;

while ($socket = yield $server->accept()) {
Amp\asyncCall($clientHandler, $socket);
}
Expand Down
10 changes: 5 additions & 5 deletions docs/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ It's best to handle clients in their own coroutine, while letting the server acc
```php
Loop::run(function () {
$clientHandler = function (ServerSocket $socket) {
list($ip, $port) = explode(":", $socket->getRemoteAddress());
[$ip, $port] = explode(":", $socket->getRemoteAddress());

echo "Accepted connection from {$ip}:{$port}." . PHP_EOL;

Expand Down Expand Up @@ -82,18 +82,18 @@ Sometimes you don't know the address the server is listening on, e.g. because yo

## Sending Data

`ServerSocket` implements `OutputStream`, so everything from [`amphp/byte-stream`](https://amphp.org/byte-stream/#outputstream) applies.
`ResourceSocket` implements `OutputStream`, so everything from [`amphp/byte-stream`](https://amphp.org/byte-stream/#outputstream) applies.

## Receiving Data

`ServerSocket` implements `InputStream`, so everything from [`amphp/byte-stream`](https://amphp.org/byte-stream/#inputstream) applies.
`ResourceSocket` implements `InputStream`, so everything from [`amphp/byte-stream`](https://amphp.org/byte-stream/#inputstream) applies.

## Server Shutdown

Once you're done with the server socket, you should close the socket. That means, the server won't listen on the specified location anymore. Use `Server::close()` to close the server socket.

## TLS

As already mentioned in the documentation for `Amp\Socket\listen()`, you need to enable TLS manually after accepting connections. For a TLS server socket, you listen on the `tcp://` protocol on a specified address. After accepting clients you call `$socket->enableCrypto()` where `$socket` is the socket returned from `Server::accept()`.
As already mentioned in the documentation for `Amp\Socket\listen()`, you need to enable TLS manually after accepting connections. For a TLS server socket, you listen on the `tcp://` protocol on a specified address. After accepting clients you call `$socket->setupTls()` where `$socket` is the socket returned from `Server::accept()`.

Any data transmitted before `Socket::enableCrypto()` resolves successfully will be transmitted in clear text. Don't attempt to read from the socket or write to it manually. Doing so will read the raw TLS handshake data that's supposed to be read by OpenSSL.
Any data transmitted before `Socket::setupTls()` resolves successfully will be transmitted in clear text. Don't attempt to read from the socket or write to it manually. Doing so will read the raw TLS handshake data that's supposed to be read by OpenSSL.

0 comments on commit 8b3309e

Please sign in to comment.