2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2017-11-10 18:35:47 +01:00
|
|
|
|
2016-08-31 01:27:14 +02:00
|
|
|
namespace Amp\Parallel\Sync;
|
|
|
|
|
2017-06-08 06:33:13 +02:00
|
|
|
use Amp\ByteStream\ResourceInputStream;
|
|
|
|
use Amp\ByteStream\ResourceOutputStream;
|
2018-10-21 17:54:46 +02:00
|
|
|
use Amp\Promise;
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2018-10-21 17:54:46 +02:00
|
|
|
final class ChannelledSocket implements Channel
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2018-10-21 17:54:46 +02:00
|
|
|
/** @var ChannelledStream */
|
|
|
|
private $channel;
|
|
|
|
|
|
|
|
/** @var ResourceInputStream */
|
2017-06-08 06:33:13 +02:00
|
|
|
private $read;
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2018-10-21 17:54:46 +02:00
|
|
|
/** @var ResourceOutputStream */
|
2017-06-08 06:33:13 +02:00
|
|
|
private $write;
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2016-08-31 01:27:14 +02:00
|
|
|
/**
|
2016-09-02 01:10:52 +02:00
|
|
|
* @param resource $read Readable stream resource.
|
|
|
|
* @param resource $write Writable stream resource.
|
2016-08-31 01:27:14 +02:00
|
|
|
*
|
|
|
|
* @throws \Error If a stream resource is not given for $resource.
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function __construct($read, $write)
|
|
|
|
{
|
2018-10-21 17:54:46 +02:00
|
|
|
$this->channel = new ChannelledStream(
|
2017-06-08 06:33:13 +02:00
|
|
|
$this->read = new ResourceInputStream($read),
|
|
|
|
$this->write = new ResourceOutputStream($write)
|
|
|
|
);
|
2016-08-31 01:27:14 +02:00
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2018-10-21 17:54:46 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function receive(): Promise
|
|
|
|
{
|
|
|
|
return $this->channel->receive();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function send($data): Promise
|
|
|
|
{
|
|
|
|
return $this->channel->send($data);
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:09:13 +01:00
|
|
|
public function unreference()
|
|
|
|
{
|
|
|
|
$this->read->unreference();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function reference()
|
|
|
|
{
|
|
|
|
$this->read->reference();
|
|
|
|
}
|
|
|
|
|
2016-08-31 01:27:14 +02:00
|
|
|
/**
|
2017-06-08 06:33:13 +02:00
|
|
|
* Closes the read and write resource streams.
|
2016-08-31 01:27:14 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function close()
|
|
|
|
{
|
2017-06-08 06:33:13 +02:00
|
|
|
$this->read->close();
|
|
|
|
$this->write->close();
|
2016-08-31 01:27:14 +02:00
|
|
|
}
|
2017-06-17 18:31:35 +02:00
|
|
|
}
|