1
0
mirror of https://github.com/danog/ipc.git synced 2024-11-27 04:24:48 +01:00
ipc/lib/Sync/ChannelledSocket.php

69 lines
1.4 KiB
PHP
Raw Normal View History

2020-02-14 20:31:11 +01:00
<?php
namespace Amp\Ipc\Sync;
use Amp\ByteStream\ResourceInputStream;
use Amp\ByteStream\ResourceOutputStream;
use Amp\Promise;
final class ChannelledSocket implements Channel
{
/** @var ChannelledStream */
private $channel;
/** @var ResourceInputStream */
private $read;
/** @var ResourceOutputStream */
private $write;
/**
* @param resource $read Readable stream resource.
* @param resource $write Writable stream resource.
*
* @throws \Error If a stream resource is not given for $resource.
*/
public function __construct($read, $write)
{
$this->channel = new ChannelledStream(
$this->read = new ResourceInputStream($read),
$this->write = new ResourceOutputStream($write)
);
}
/**
* {@inheritdoc}
*/
public function receive(): Promise
{
return $this->channel->receive();
}
/**
* {@inheritdoc}
*/
public function send($data): Promise
{
return $this->channel->send($data);
}
public function unreference()
{
$this->read->unreference();
}
public function reference()
{
$this->read->reference();
}
/**
* Closes the read and write resource streams.
*/
public function close()
{
$this->read->close();
$this->write->close();
}
}