protected function createServer($endpoint, $config = [])
{
if (stripos($endpoint, 'unix://') !== false) {
if ($endpoint[7] === DIRECTORY_SEPARATOR) {
$path = substr($endpoint, 7);
} else {
$path = getcwd() . DIRECTORY_SEPARATOR . substr($endpoint, 7);
$endpoint = 'unix://' . $path;
}
if (file_exists($path)) {
unlink($path);
}
}
$backlog = (int) (isset($config['backlog']) ? $config['backlog'] : self::DEFAULT_BACKLOG);
$reuseaddr = (bool) (isset($config['reuseaddr']) ? $config['reuseaddr'] : false);
$reuseport = (bool) (isset($config['reuseport']) ? $config['reuseport'] : false);
$context = [];
$context['socket'] = ['bindto' => $endpoint, 'backlog' => $backlog, 'ipv6_v6only' => true, 'so_reuseaddr' => $reuseaddr, 'so_reuseport' => $reuseport];
$context = stream_context_create($context);
// Error reporting suppressed since stream_socket_server() emits an E_WARNING on failure.
$socket = @stream_socket_server($endpoint, $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context);
if (!$socket || $errno) {
throw new LogicException(sprintf('Could not bind socket [%s] because of error [%d; %s]', $endpoint, $errno, $errstr));
}
return $socket;
}