Skip to content

Commit

Permalink
feat(network): introduce network component
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Nov 8, 2021
1 parent 0c7dfe6 commit 5ec9d37
Show file tree
Hide file tree
Showing 45 changed files with 1,357 additions and 30 deletions.
8 changes: 4 additions & 4 deletions composer.json
Expand Up @@ -19,10 +19,10 @@
"revolt/event-loop": "1.x-dev"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.6",
"friendsofphp/php-cs-fixer": "^3.0",
"phpbench/phpbench": "^1.1",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.6.1",
"friendsofphp/php-cs-fixer": "^3.2.1",
"phpbench/phpbench": "^1.2.0",
"phpunit/phpunit": "^9.5.10",
"vimeo/psalm": "^4.11.2",
"php-standard-library/psalm-plugin": "^1.1.1"
},
Expand Down
36 changes: 18 additions & 18 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/.phpcs.xml
Expand Up @@ -8,6 +8,7 @@
<file>../tests</file>

<exclude-pattern>*/src/Psl/File/(WriteMode|LockType).php</exclude-pattern>
<exclude-pattern>*/src/Psl/Network/SocketScheme.php</exclude-pattern>
<exclude-pattern>*/src/Psl/Str/Encoding.php</exclude-pattern>

<arg name="basepath" value="."/>
Expand Down
3 changes: 3 additions & 0 deletions docs/README.md
Expand Up @@ -26,6 +26,7 @@
- [Psl\Iter](./component/iter.md)
- [Psl\Json](./component/json.md)
- [Psl\Math](./component/math.md)
- [Psl\Network](./component/network.md)
- [Psl\Observer](./component/observer.md)
- [Psl\Password](./component/password.md)
- [Psl\PseudoRandom](./component/pseudo-random.md)
Expand All @@ -37,7 +38,9 @@
- [Psl\Str](./component/str.md)
- [Psl\Str\Byte](./component/str-byte.md)
- [Psl\Str\Grapheme](./component/str-grapheme.md)
- [Psl\TCP](./component/tcp.md)
- [Psl\Trait](./component/trait.md)
- [Psl\Type](./component/type.md)
- [Psl\Unix](./component/unix.md)
- [Psl\Vec](./component/vec.md)

27 changes: 27 additions & 0 deletions docs/component/network-tcp.md
@@ -0,0 +1,27 @@
<!--
This markdown file was generated using `docs/documenter.php`.
Any edits to it will likely be lost.
-->

[*index](./../README.md)

---

### `Psl\Network\TCP` Component

#### `Functions`

- [connect](./../../src/Psl/Network/TCP/connect.php#L15)

#### `Interfaces`

- [SocketInterface](./../../src/Psl/Network/TCP/SocketInterface.php#L9)

#### `Classes`

- [ConnectOptions](./../../src/Psl/Network/TCP/ConnectOptions.php#L7)
- [Server](./../../src/Psl/Network/TCP/Server.php#L14)
- [ServerOptions](./../../src/Psl/Network/TCP/ServerOptions.php#L9)


27 changes: 27 additions & 0 deletions docs/component/network.md
@@ -0,0 +1,27 @@
<!--
This markdown file was generated using `docs/documenter.php`.
Any edits to it will likely be lost.
-->

[*index](./../README.md)

---

### `Psl\Network` Component

#### `Interfaces`

- [ServerInterface](./../../src/Psl/Network/ServerInterface.php#L10)
- [SocketInterface](./../../src/Psl/Network/SocketInterface.php#L15)

#### `Classes`

- [Address](./../../src/Psl/Network/Address.php#L7)
- [SocketOptions](./../../src/Psl/Network/SocketOptions.php#L7)

#### `Enums`

- [SocketScheme](./../../src/Psl/Network/SocketScheme.php#L10)


27 changes: 27 additions & 0 deletions docs/component/tcp.md
@@ -0,0 +1,27 @@
<!--
This markdown file was generated using `docs/documenter.php`.
Any edits to it will likely be lost.
-->

[*index](./../README.md)

---

### `Psl\TCP` Component

#### `Functions`

- [connect](./../../src/Psl/TCP/connect.php#L18)

#### `Interfaces`

- [SocketInterface](./../../src/Psl/TCP/SocketInterface.php#L9)

#### `Classes`

- [ConnectOptions](./../../src/Psl/TCP/ConnectOptions.php#L7)
- [Server](./../../src/Psl/TCP/Server.php#L14)
- [ServerOptions](./../../src/Psl/TCP/ServerOptions.php#L9)


25 changes: 25 additions & 0 deletions docs/component/unix.md
@@ -0,0 +1,25 @@
<!--
This markdown file was generated using `docs/documenter.php`.
Any edits to it will likely be lost.
-->

[*index](./../README.md)

---

### `Psl\Unix` Component

#### `Functions`

- [connect](./../../src/Psl/Unix/connect.php#L17)

#### `Interfaces`

- [SocketInterface](./../../src/Psl/Unix/SocketInterface.php#L9)

#### `Classes`

- [Server](./../../src/Psl/Unix/Server.php#L12)


3 changes: 3 additions & 0 deletions docs/documenter.php
Expand Up @@ -202,6 +202,7 @@ function get_all_components(): array
'Psl\\Iter',
'Psl\\Json',
'Psl\\Math',
'Psl\\Network',
'Psl\\Observer',
'Psl\\Password',
'Psl\\PseudoRandom',
Expand All @@ -213,8 +214,10 @@ function get_all_components(): array
'Psl\\Str',
'Psl\\Str\\Byte',
'Psl\\Str\\Grapheme',
'Psl\\TCP',
'Psl\\Trait',
'Psl\\Type',
'Psl\\Unix',
'Psl\\Vec',
];

Expand Down
5 changes: 5 additions & 0 deletions examples/run.php
Expand Up @@ -28,6 +28,11 @@
foreach ($files as $file) {
$script = Filesystem\get_filename($file);

if ($script === 'basic-http-server') {
// long running process.
continue;
}

$output->writeAll("- $component/$script -> started\n");

$awaitables[] = Async\run(static function() use($component, $script, $file): array {
Expand Down
29 changes: 29 additions & 0 deletions examples/tcp/basic-http-client.php
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Psl\Example\TCP;

use Psl\Async;
use Psl\IO;
use Psl\TCP;

require __DIR__ . '/../../vendor/autoload.php';

function fetch(string $host, string $path): string
{
$client = TCP\connect($host, 80);
$client->writeAll("GET {$path} HTTP/1.1\r\nHost: $host\r\nConnection: close\r\n\r\n");
$response = $client->readAll();
$client->close();

return $response;
}

Async\main(static function (): int {
$response = fetch('example.com', '/');

IO\output_handle()->writeAll($response);

return 0;
});
34 changes: 34 additions & 0 deletions examples/tcp/basic-http-server.php
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Psl\Example\TCP;

use Psl\Async;
use Psl\IO;
use Psl\Str;
use Psl\TCP;

require __DIR__ . '/../../vendor/autoload.php';

Async\main(static function (): never {
$server = TCP\Server::create('localhost');

IO\output_handle()->writeAll("Server is listening on http://localhost:" . $server->getLocalAddress()->port . "\n");

while (true) {
$connection = $server->nextConnection();
$request = $connection->read();

IO\output_handle()->writeAll(Str\split($request, "\n")[0] . "\n");

$connection->writeAll("HTTP/1.1 200 OK\n");
$connection->writeAll("Server: PSL\n");
$connection->writeAll("Connection: close\n");
$connection->writeAll("Content-Type: text/plain\n\n");

$connection->writeAll("Hello, World!");

$connection->close();
}
});

0 comments on commit 5ec9d37

Please sign in to comment.