Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(network,tcp,udp): implement socket server, and socket #313

Merged
merged 1 commit into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/component/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

#### `Interfaces`

- [ServerInterface](./../../src/Psl/Network/ServerInterface.php#L10)
- [ServerInterface](./../../src/Psl/Network/ServerInterface.php#L12)
- [SocketInterface](./../../src/Psl/Network/SocketInterface.php#L15)
- [StreamServerInterface](./../../src/Psl/Network/StreamServerInterface.php#L14)
- [StreamSocketInterface](./../../src/Psl/Network/StreamSocketInterface.php#L17)

#### `Classes`

Expand Down
4 changes: 0 additions & 4 deletions docs/component/tcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@

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

#### `Interfaces`

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

#### `Classes`

- [ConnectOptions](./../../src/Psl/TCP/ConnectOptions.php#L7)
Expand Down
4 changes: 0 additions & 4 deletions docs/component/unix.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@

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

#### `Interfaces`

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

#### `Classes`

- [Server](./../../src/Psl/Unix/Server.php#L16)
Expand Down
2 changes: 1 addition & 1 deletion examples/tcp/basic-http-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

IO\write_error_line('Server is listening on http://localhost:3030');

$watcher = Async\Scheduler::onSignal(SIGINT, $server->stopListening(...));
$watcher = Async\Scheduler::onSignal(SIGINT, $server->close(...));
Async\Scheduler::unreference($watcher);

try {
Expand Down
2 changes: 1 addition & 1 deletion examples/tcp/concurrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

IO\write_error_line('< connection closed.');

$server->stopListening();
$server->close();

IO\write_error_line('< server stopped.');
},
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/concurrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

IO\write_error_line('< connection closed.');

$server->stopListening();
$server->close();

IO\write_error_line("< server stopped\n");
},
Expand Down
7 changes: 3 additions & 4 deletions src/Psl/Internal/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,9 @@ final class Loader
'Psl\File\ReadWriteHandleInterface',
'Psl\Network\Exception\ExceptionInterface',
'Psl\Network\SocketInterface',
'Psl\Network\StreamSocketInterface',
'Psl\Network\ServerInterface',
'Psl\TCP\SocketInterface',
'Psl\Unix\SocketInterface',
'Psl\Network\StreamServerInterface',
'Psl\Channel\SenderInterface',
'Psl\Channel\ReceiverInterface',
'Psl\Channel\Exception\ExceptionInterface',
Expand Down Expand Up @@ -669,12 +669,11 @@ final class Loader
'Psl\Network\Exception\InvalidArgumentException',
'Psl\Network\Address',
'Psl\Network\SocketOptions',
'Psl\Network\Internal\Socket',
'Psl\TCP\ConnectOptions',
'Psl\TCP\ServerOptions',
'Psl\TCP\Server',
'Psl\TCP\Internal\Socket',
'Psl\Unix\Server',
'Psl\Unix\Internal\Socket',
'Psl\Channel\Internal\ChannelState',
'Psl\Channel\Internal\Sender',
'Psl\Channel\Internal\Receiver',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

declare(strict_types=1);

namespace Psl\TCP\Internal;
namespace Psl\Network\Internal;

use Psl\IO;
use Psl\IO\Exception;
use Psl\IO\Internal;
use Psl\Network;
use Psl\Network\Address;
use Psl\TCP;

use function is_resource;

Expand All @@ -18,7 +17,7 @@
*
* @codeCoverageIgnore
*/
final class Socket implements IO\Stream\CloseReadWriteHandleInterface, TCP\SocketInterface
final class Socket implements Network\StreamSocketInterface
{
use IO\WriteHandleConvenienceMethodsTrait;
use IO\ReadHandleConvenienceMethodsTrait;
Expand Down
6 changes: 4 additions & 2 deletions src/Psl/Network/ServerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Psl\Network;

use Psl\IO;

/**
* Generic interface for a class able to accept socket connections.
*/
interface ServerInterface
interface ServerInterface extends IO\CloseHandleInterface
{
/**
* Retrieve the next pending connection.
Expand All @@ -30,5 +32,5 @@ public function getLocalAddress(): Address;
/**
* Stop listening; open connection are not closed.
*/
public function stopListening(): void;
public function close(): void;
}
20 changes: 20 additions & 0 deletions src/Psl/Network/StreamServerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Psl\Network;

use Psl\IO;

/**
* Generic interface for a class able to accept socket connections.
*
* Unlike {@see ServerInterface}, {@see StreamServerInterface} provides access to the underlying server stream.
*/
interface StreamServerInterface extends IO\Stream\CloseHandleInterface, ServerInterface
{
/**
* {@inheritDoc}
*/
public function nextConnection(): StreamSocketInterface;
}
19 changes: 19 additions & 0 deletions src/Psl/Network/StreamSocketInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Psl\Network;

use Psl\IO;

/**
* A handle representing a connection between processes.
*
* It is possible for both ends to be connected to the same process,
* and to either be local or across a network.
*
* Unlike {@see SocketInterface}, {@see StreamSocketInterface} provides access to the underlying socket stream.
*/
interface StreamSocketInterface extends IO\Stream\CloseReadWriteHandleInterface, SocketInterface
{
}
19 changes: 14 additions & 5 deletions src/Psl/TCP/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use const PHP_OS_FAMILY;

final class Server implements Network\ServerInterface
final class Server implements Network\StreamServerInterface
{
/**
* @var resource|null $impl
Expand Down Expand Up @@ -97,7 +97,7 @@ public static function create(
/**
* {@inheritDoc}
*/
public function nextConnection(): SocketInterface
public function nextConnection(): Network\StreamSocketInterface
{
$this->deferred?->getAwaitable()->then(static fn() => null, static fn() => null)->await();

Expand All @@ -113,7 +113,7 @@ public function nextConnection(): SocketInterface

try {
/** @psalm-suppress PossiblyNullReference */
return new Internal\Socket($this->deferred->getAwaitable()->await());
return new Network\Internal\Socket($this->deferred->getAwaitable()->await());
} finally {
Async\Scheduler::disable($this->watcher);
$this->deferred = null;
Expand All @@ -134,13 +134,14 @@ public function getLocalAddress(): Network\Address

public function __destruct()
{
$this->stopListening();
/** @psalm-suppress MissingThrowsDocblock */
$this->close();
}

/**
* {@inheritDoc}
*/
public function stopListening(): void
public function close(): void
{
Async\Scheduler::cancel($this->watcher);

Expand All @@ -156,4 +157,12 @@ public function stopListening(): void
$this->deferred = null;
$deferred?->error(new Network\Exception\AlreadyStoppedException('Server socket has already been stopped.'));
}

/**
* {@inheritDoc}
*/
public function getStream(): mixed
{
return $this->impl;
}
}
11 changes: 0 additions & 11 deletions src/Psl/TCP/SocketInterface.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Psl/TCP/connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function connect(
int $port = 0,
?ConnectOptions $options = null,
?float $timeout = null,
): SocketInterface {
): Network\StreamSocketInterface {
$options ??= ConnectOptions::create();

$context = [
Expand All @@ -32,5 +32,5 @@ function connect(
$socket = Network\Internal\socket_connect("tcp://{$host}:{$port}", $context, $timeout);

/** @psalm-suppress MissingThrowsDocblock */
return new Internal\Socket($socket);
return new Network\Internal\Socket($socket);
}
109 changes: 0 additions & 109 deletions src/Psl/Unix/Internal/Socket.php

This file was deleted.

Loading