Ejemplo n.º 1
0
 /**
  * 事件添加
  * @see BaseEvent::add()
  */
 public function add($fd, $flag, $func, $args = null, $read_timeout = 1000, $write_timeout = 1000)
 {
     $event_key = (int) $fd;
     switch ($flag) {
         case self::EV_ACCEPT:
             $this->allEvents[$event_key][self::EV_ACCEPT] = array('poll' => uv_poll_init($this->eventBase, $fd), 'args' => $args, 'func' => $func);
             uv_poll_start($this->allEvents[$event_key][self::EV_ACCEPT]['poll'], UV::READABLE, array($this, 'accept'));
             return true;
             break;
         case self::EV_READ:
             $this->allEvents[$event_key][self::EV_READ] = array('poll' => uv_poll_init($this->eventBase, $fd), 'args' => $args, 'func' => $func);
             uv_poll_start($this->allEvents[$event_key][self::EV_READ]['poll'], UV::READABLE, array($this, 'bufferCallBack'));
             return true;
         case self::EV_WRITE:
             break;
         case self::EV_SIGNAL:
             $this->allEvents[$event_key][$flag] = array('args' => $args, 'func' => $func, 'fd' => $fd);
             pcntl_signal($fd, array($this, 'signalHandler'));
             return true;
         case self::EV_NOINOTIFY:
             $this->allEvents[$event_key][self::EV_NOINOTIFY] = array('poll' => uv_poll_init($this->eventBase, $fd), 'args' => $args, 'func' => $func);
             uv_poll_start($this->allEvents[$event_key][self::EV_NOINOTIFY]['poll'], UV::READABLE, array($this, 'notify'));
             return true;
             break;
     }
     return true;
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 protected function disableWriteWatcher(StreamWatcher $watcher, bool $dispose = false)
 {
     if ($watcher->event === null) {
         return;
     }
     $id = (int) $watcher->event;
     unset($this->writeWatchers[$id][$watcher->id]);
     if (empty($this->writeWatchers[$id])) {
         unset($this->writeWatchers[$id]);
         if (\uv_is_active($watcher->event)) {
             \uv_poll_stop($watcher->event);
         }
         if (isset($this->readWatchers[$id])) {
             \uv_poll_start($watcher->event, \UV::READABLE, $this->streamCallback);
         }
     }
     if ($dispose) {
         try {
             if (empty($this->readWatchers[$id]) && empty($this->writeWatchers[$id])) {
                 unset($this->streamEvents[$watcher->streamId]);
                 if ($this->dispatching) {
                     $this->dispose->enqueue($watcher->event);
                 } else {
                     \uv_close($watcher->event);
                 }
             }
         } finally {
             $watcher->event = null;
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function listen(Io $io, float $timeout = 0)
 {
     $resource = $io->getResource();
     $id = (int) $resource;
     if (!isset($this->sockets[$id]) || $io !== $this->sockets[$id]) {
         throw new FreedError();
     }
     // If no poll handle exists for the socket, create one now.
     if (!isset($this->polls[$id])) {
         $this->polls[$id] = \uv_poll_init_socket($this->loopHandle, $resource);
     }
     // Begin polling for events.
     \uv_poll_start($this->polls[$id], $this->type, $this->pollCallback);
     // If a timeout is given, set up a separate timer for cancelling the poll in the future.
     if ($timeout) {
         if (self::MIN_TIMEOUT > $timeout) {
             $timeout = self::MIN_TIMEOUT;
         }
         if (!isset($this->timers[$id])) {
             $timer = \uv_timer_init($this->loopHandle);
             $this->handles[(int) $timer] = $id;
             $this->timers[$id] = $timer;
         }
         $timeout *= self::MILLISEC_PER_SEC;
         \uv_timer_start($this->timers[$id], $timeout, $timeout, $this->timerCallback);
     } elseif (isset($this->timers[$id]) && \uv_is_active($this->timers[$id])) {
         \uv_timer_stop($this->timers[$id]);
     }
 }
Ejemplo n.º 4
0
<?php

$socket = stream_socket_server("tcp://0.0.0.0:9999", $errno, $errstr);
stream_set_blocking($socket, 0);
$poll = uv_poll_init(uv_default_loop(), $socket);
uv_poll_start($poll, UV::READABLE, function ($poll, $stat, $ev, $socket) {
    echo "parent poll:\n";
    var_dump($stat);
    $conn = stream_socket_accept($socket);
    $pp = uv_poll_init(uv_default_loop(), $conn);
    uv_poll_start($pp, UV::WRITABLE, function ($poll, $stat, $ev, $conn) {
        echo "  cb";
        var_dump($stat);
        var_dump($conn);
        uv_poll_stop($poll);
        uv_fs_write(uv_default_loop(), $conn, "Hello World", -1, function ($conn, $nwrite) {
            var_dump($conn);
            fclose($conn);
        });
    });
});
uv_run();
Ejemplo n.º 5
0
 private function enablePollFromWatcher($watcher)
 {
     $poll = $watcher->poll;
     $watcherId = $watcher->id;
     unset($poll->disable[$watcherId]);
     if ($watcher->type === Watcher::IO_READER) {
         $poll->flags |= \UV::READABLE;
         $poll->readers[$watcherId] = $watcher;
     } else {
         $poll->flags |= \UV::WRITABLE;
         $poll->writers[$watcherId] = $watcher;
     }
     if ($watcher->keepAlive) {
         \uv_ref($poll->handle);
     }
     @\uv_poll_start($poll->handle, $poll->flags, $poll->callback);
 }