PHPDaemon\Network\IOStream::setFd PHP Method

setFd() public method

Sets fd
public setFd ( resource $fd, object $bev = null ) : void
$fd resource File descriptor
$bev object EventBufferEvent
return void
    public function setFd($fd, $bev = null)
    {
        if ($this->eventLoop === null) {
            $this->eventLoop = EventLoop::$instance;
        }
        $this->fd = $fd;
        if ($this->fd === false) {
            $this->finish();
            return;
        }
        if ($bev !== null) {
            $this->bev = $bev;
            $this->bev->setCallbacks([$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']);
            if (!$this->bev) {
                return;
            }
            $this->ready = true;
            $this->alive = true;
        } else {
            $flags = !is_resource($this->fd) ? \EventBufferEvent::OPT_CLOSE_ON_FREE : 0;
            $flags |= \EventBufferEvent::OPT_DEFER_CALLBACKS;
            /* buggy option */
            if ($this->ctx) {
                if ($this->ctx instanceof \EventSslContext) {
                    $this->bev = $this->eventLoop->bufferEventSsl($this->fd, $this->ctx, $this->ctxMode, $flags);
                    if ($this->bev) {
                        $this->bev->setCallbacks([$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']);
                    }
                    $this->ssl = true;
                } else {
                    $this->log('unsupported type of context: ' . ($this->ctx ? get_class($this->ctx) : 'undefined'));
                    return;
                }
            } else {
                $this->bev = $this->eventLoop->bufferEvent($this->fd, $flags, [$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']);
            }
            if (!$this->bev) {
                return;
            }
        }
        if ($this->priority !== null) {
            $this->bev->priority = $this->priority;
        }
        $this->setTimeouts($this->timeoutRead !== null ? $this->timeoutRead : $this->timeout, $this->timeoutWrite !== null ? $this->timeoutWrite : $this->timeout);
        if ($this->bevConnect && $this->fd === null) {
            //$this->bev->connectHost(Daemon::$process->dnsBase, $this->hostReal, $this->port);
            $this->bev->connect($this->addr);
        }
        if (!$this->bev) {
            $this->finish();
            return;
        }
        if (!$this->bev->enable(\Event::READ | \Event::WRITE | \Event::TIMEOUT | \Event::PERSIST)) {
            $this->finish();
            return;
        }
        $this->bev->setWatermark(\Event::READ, $this->lowMark, $this->highMark);
        init:
        if ($this->keepalive && $this->fd != null) {
            $this->setKeepalive(true);
        }
        if (!$this->inited) {
            $this->inited = true;
            $this->init();
        }
    }