php-ipc/src/Interop/Channel/MultiStreamChannel.php

67 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace NoccyLabs\Ipc\Interop\Channel;
/**
* The MultiChannel is used to create a single channel and allow other threads
* to each grab an end of it. Think for example a web server that could write
* log output to a channel (as it´the MultiStreamChannel is always open); one
* thread could grab a channel to dump it to the console, and another thread
* could write to a logfile.
*/
class MultiStreamChannel implements ChannelInterface
{
protected $clients = [];
/**
* Create a new client to receive data sent to the channel
*
* @return StreamChannel
*/
public function createClient(): StreamChannel
{
[ $a, $b ] = StreamChannel::createPair();
$this->clients[] = $a;
return $b;
}
/**
* {@inheritDoc}
*
* @return boolean
*/
public function isOpen(): bool
{
return true;
}
/**
* {@inheritDoc}
*
* @param mixed $data
* @return void
*/
public function send($data)
{
foreach ($this->clients as $client) {
$client->send($data);
}
}
/**
* {@inheritDoc}
*
* @return mixed
*/
public function receive()
{
foreach ($this->clients as $client) {
if ($read = $client->receive()) {
return $read;
}
}
return false;
}
}