public function bindSocket()
{
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if (!$sock) {
$errno = socket_last_error();
Daemon::$process->log(get_class($this) . ': Couldn\'t create UDP-socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
return false;
}
if (!isset($this->port)) {
if (isset($this->defaultPort)) {
$this->port = $this->defaultPort;
} else {
Daemon::log(get_class($this) . ' (' . get_class($this->pool) . '): no port defined for \'' . $this->uri['uri'] . '\'');
}
}
if ($this->reuse) {
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
$errno = socket_last_error();
Daemon::$process->log(get_class($this) . ': Couldn\'t set option REUSEADDR to socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
return false;
}
if (defined('SO_REUSEPORT') && !@socket_set_option($sock, SOL_SOCKET, SO_REUSEPORT, 1)) {
$errno = socket_last_error();
Daemon::$process->log(get_class($this) . ': Couldn\'t set option REUSEPORT to socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
return false;
}
}
if (!@socket_bind($sock, $this->host, $this->port)) {
$errno = socket_last_error();
$addr = $this->host . ':' . $this->port;
Daemon::$process->log(get_class($this) . ': Couldn\'t bind UDP-socket \'' . $addr . '\' (' . $errno . ' - ' . socket_strerror($errno) . ').');
return false;
}
socket_getsockname($sock, $this->host, $this->port);
$addr = $this->host . ':' . $this->port;
socket_set_nonblock($sock);
$this->setFd($sock);
return true;
}