PHPDaemon\BoundSocket\UNIX::bindSocket PHP Method

bindSocket() public method

Bind socket
public bindSocket ( ) : boolean
return boolean Success.
    public function bindSocket()
    {
        if ($this->erroneous) {
            return false;
        }
        if ($this->path === null && isset($this->uri['path'])) {
            $this->path = $this->uri['path'];
        }
        if (pathinfo($this->path, PATHINFO_EXTENSION) !== 'sock') {
            Daemon::$process->log('Unix-socket \'' . $this->path . '\' must has \'.sock\' extension.');
            return false;
        }
        if (file_exists($this->path)) {
            unlink($this->path);
        }
        if ($this->listenerMode) {
            $this->setFd('unix:' . $this->path);
            return true;
        }
        $sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
        if (!$sock) {
            $errno = socket_last_error();
            Daemon::$process->log(get_class($this) . ': Couldn\'t create UNIX-socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
            return false;
        }
        // SO_REUSEADDR is meaningless in AF_UNIX context
        if (!@socket_bind($sock, $this->path)) {
            if (isset($this->config->maxboundsockets->value)) {
                // no error-messages when maxboundsockets defined
                return false;
            }
            $errno = socket_last_error();
            Daemon::$process->log(get_class($this) . ': Couldn\'t bind Unix-socket \'' . $this->path . '\' (' . $errno . ' - ' . socket_strerror($errno) . ').');
            return false;
        }
        socket_set_nonblock($sock);
        $this->onBound();
        if (!socket_listen($sock, SOMAXCONN)) {
            $errno = socket_last_error();
            Daemon::$process->log(get_class($this) . ': Couldn\'t listen UNIX-socket \'' . $this->path . '\' (' . $errno . ' - ' . socket_strerror($errno) . ')');
        }
        $this->setFd($sock);
        return true;
    }