-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move resource-based pipes to be internal
Added ClientSocketReceivePipe and ClientSocketSendPipe, which use our stream and socket abstractions instead of resources.
- Loading branch information
Showing
9 changed files
with
184 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Amp\Cluster; | ||
|
||
use Amp\ByteStream\ResourceStream; | ||
use Amp\Cancellation; | ||
use Amp\Closable; | ||
use Amp\Cluster\Internal\StreamResourceReceivePipe; | ||
use Amp\ForbidCloning; | ||
use Amp\ForbidSerialization; | ||
use Amp\Serialization\NativeSerializer; | ||
use Amp\Serialization\SerializationException; | ||
use Amp\Serialization\Serializer; | ||
use Amp\Socket\ResourceSocket; | ||
use Amp\Socket\SocketException; | ||
|
||
/** | ||
* @template-covariant T | ||
*/ | ||
final class ClientSocketReceivePipe implements Closable | ||
{ | ||
use ForbidCloning; | ||
use ForbidSerialization; | ||
|
||
/** @var StreamResourceReceivePipe<T> */ | ||
private readonly StreamResourceReceivePipe $receive; | ||
|
||
public function __construct( | ||
ResourceStream $resourceStream, | ||
Serializer $serializer = new NativeSerializer(), | ||
) { | ||
$this->receive = new StreamResourceReceivePipe($resourceStream, $serializer); | ||
} | ||
|
||
/** | ||
* @param positive-int $chunkSize | ||
* | ||
* @return TransferredSocket<T> | ||
* | ||
* @throws SerializationException | ||
* @throws SocketException | ||
*/ | ||
public function receive( | ||
?Cancellation $cancellation = null, | ||
int $chunkSize = ResourceSocket::DEFAULT_CHUNK_SIZE, | ||
): TransferredSocket { | ||
$received = $this->receive->receive($cancellation); | ||
|
||
return new TransferredSocket( | ||
ResourceSocket::fromServerSocket($received->getResource(), $chunkSize), | ||
$received->getData(), | ||
); | ||
} | ||
|
||
public function close(): void | ||
{ | ||
$this->receive->close(); | ||
} | ||
|
||
public function isClosed(): bool | ||
{ | ||
return $this->receive->isClosed(); | ||
} | ||
|
||
public function onClose(\Closure $onClose): void | ||
{ | ||
$this->receive->onClose($onClose); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Amp\Cluster; | ||
|
||
use Amp\ByteStream\ResourceStream; | ||
use Amp\Closable; | ||
use Amp\Cluster\Internal\StreamResourceSendPipe; | ||
use Amp\ForbidCloning; | ||
use Amp\ForbidSerialization; | ||
use Amp\Serialization\NativeSerializer; | ||
use Amp\Serialization\SerializationException; | ||
use Amp\Serialization\Serializer; | ||
use Amp\Socket\SocketException; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
final class ClientSocketSendPipe implements Closable | ||
{ | ||
use ForbidCloning; | ||
use ForbidSerialization; | ||
|
||
/** @var StreamResourceSendPipe<T> */ | ||
private readonly StreamResourceSendPipe $send; | ||
|
||
public function __construct( | ||
ResourceStream $resourceStream, | ||
Serializer $serializer = new NativeSerializer(), | ||
) { | ||
$this->send = new StreamResourceSendPipe($resourceStream, $serializer); | ||
} | ||
|
||
/** | ||
* @param T $data | ||
* | ||
* @throws SerializationException | ||
* @throws SocketException | ||
*/ | ||
public function send(ResourceStream $resourceStream, mixed $data = null): void | ||
{ | ||
$resource = $resourceStream->getResource(); | ||
if (!\is_resource($resource)) { | ||
throw new SocketException('The provided socket has already been closed'); | ||
} | ||
|
||
$this->send->send($resource, $data); | ||
} | ||
|
||
public function close(): void | ||
{ | ||
$this->send->close(); | ||
} | ||
|
||
public function isClosed(): bool | ||
{ | ||
return $this->send->isClosed(); | ||
} | ||
|
||
public function onClose(\Closure $onClose): void | ||
{ | ||
$this->send->onClose($onClose); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/TransferredResource.php → src/Internal/TransferredResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Amp\Cluster; | ||
|
||
use Amp\ForbidCloning; | ||
use Amp\ForbidSerialization; | ||
use Amp\Socket\ResourceSocket; | ||
|
||
/** | ||
* @template-covariant T | ||
*/ | ||
final class TransferredSocket | ||
{ | ||
use ForbidCloning; | ||
use ForbidSerialization; | ||
|
||
/** | ||
* @param T $data | ||
*/ | ||
public function __construct( | ||
private readonly ResourceSocket $socket, | ||
private readonly mixed $data, | ||
) { | ||
} | ||
|
||
public function getSocket(): ResourceSocket | ||
{ | ||
return $this->socket; | ||
} | ||
|
||
/** | ||
* @return T | ||
*/ | ||
public function getData(): mixed | ||
{ | ||
return $this->data; | ||
} | ||
} |