Skip to content

Instantly share code, notes, and snippets.

@RafaelKa
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save RafaelKa/33856a11023455653af8 to your computer and use it in GitHub Desktop.

Select an option

Save RafaelKa/33856a11023455653af8 to your computer and use it in GitHub Desktop.
mode for file permissions on created uds file
<?php
namespace React\Socket;
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
/** @event connection */
class Server extends EventEmitter implements ServerInterface
{
public $master;
private $loop;
public function __construct(LoopInterface $loop)
{
$this->loop = $loop;
}
public function listen($port, $host = '127.0.0.1', $modeForUDS = NULL)
{
if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$localSocket = 'tcp://' . $host . ':' . $port;
} elseif (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
// enclose IPv6 addresses in square brackets before appending port
$localSocket = 'tcp://[' . $host . ']:' . $port;
} elseif (preg_match('#^unix://#', $host)) {
$localSocket = $host;
$udsFilePath = preg_replace('#^unix://#', '', $host);
} else {
throw new \UnexpectedValueException(
'"' . $host . '" does not match to a set of supported transports. ' .
'Supported transports are: IPv4, IPv6 and unix:// .'
, 1433253311);
}
$this->master = @stream_socket_server($localSocket, $errno, $errstr);
if (false === $this->master) {
$message = "Could not bind to $localSocket . Error: $errstr";
throw new ConnectionException($message, $errno);
}
if (isset($udsFilePath) && !empty($modeForUDS)) {
chmod($udsFilePath, $modeForUDS);
}
stream_set_blocking($this->master, 0);
$this->loop->addReadStream($this->master, function ($master) {
$newSocket = stream_socket_accept($master);
if (false === $newSocket) {
$this->emit('error', array(new \RuntimeException('Error accepting new connection')));
return;
}
$this->handleConnection($newSocket);
});
}
public function handleConnection($socket)
{
stream_set_blocking($socket, 0);
$client = $this->createConnection($socket);
$this->emit('connection', array($client));
}
public function getPort()
{
$name = stream_socket_get_name($this->master, false);
return (int) substr(strrchr($name, ':'), 1);
}
public function shutdown()
{
$this->loop->removeStream($this->master);
fclose($this->master);
$this->removeAllListeners();
}
public function createConnection($socket)
{
return new Connection($socket, $this->loop);
}
}
<?php
namespace React\Socket;
use Evenement\EventEmitterInterface;
/** @event connection */
interface ServerInterface extends EventEmitterInterface
{
public function listen($port, $host = '127.0.0.1', $modeForUDS);
public function getPort();
public function shutdown();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment