1
0
mirror of https://github.com/danog/ipc.git synced 2024-11-30 04:29:09 +01:00
ipc/lib/Sync/ChannelledStream.php

82 lines
2.2 KiB
PHP
Raw Normal View History

2020-02-14 20:31:11 +01:00
<?php
namespace Amp\Ipc\Sync;
use Amp\ByteStream\InputStream;
use Amp\ByteStream\OutputStream;
use Amp\ByteStream\StreamException;
use Amp\Promise;
use function Amp\call;
/**
* An asynchronous channel for sending data between threads and processes.
*
* Supports full duplex read and write.
*/
final class ChannelledStream implements Channel
{
/** @var \Amp\ByteStream\InputStream */
private $read;
/** @var \Amp\ByteStream\OutputStream */
private $write;
/** @var \SplQueue */
private $received;
/** @var \Amp\Parser\Parser */
private $parser;
/**
* Creates a new channel from the given stream objects. Note that $read and $write can be the same object.
*
* @param \Amp\ByteStream\InputStream $read
* @param \Amp\ByteStream\OutputStream $write
*/
public function __construct(InputStream $read, OutputStream $write)
{
$this->read = $read;
$this->write = $write;
$this->received = new \SplQueue;
$this->parser = new ChannelParser([$this->received, 'push']);
}
/**
* {@inheritdoc}
*/
public function send($data): Promise
{
return call(function () use ($data) {
try {
return yield $this->write->write($this->parser->encode($data));
} catch (StreamException $exception) {
throw new ChannelException("Sending on the channel failed. Did the context die?", 0, $exception);
}
});
}
/**
* {@inheritdoc}
*/
public function receive(): Promise
{
return call(function () {
while ($this->received->isEmpty()) {
try {
$chunk = yield $this->read->read();
} catch (StreamException $exception) {
throw new ChannelException("Reading from the channel failed. Did the context die?", 0, $exception);
}
if ($chunk === null) {
2020-03-05 21:12:35 +01:00
throw new ChannelException("The channel closed unexpectedly. Did the context die?");
2020-02-14 20:31:11 +01:00
}
$this->parser->push($chunk);
}
return $this->received->shift();
});
}
}