Example #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;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 protected function enableWriteWatcher(StreamWatcher $watcher)
 {
     if (isset($this->streamEvents[$watcher->streamId])) {
         $watcher->event = $this->streamEvents[$watcher->streamId];
     } else {
         if (!\is_resource($watcher->stream)) {
             throw new \Exception('Invalid resource detected');
         }
         switch (\stream_get_meta_data($watcher->stream)['stream_type'] ?? '') {
             case 'STDIO':
             case 'tcp_socket/ssl':
                 $watcher->event = \uv_poll_init($this->loop, $watcher->stream);
                 break;
             default:
                 $watcher->event = \uv_poll_init_socket($this->loop, $watcher->stream);
         }
         $this->streamEvents[$watcher->streamId] = $watcher->event;
     }
     $id = (int) $watcher->event;
     if (empty($this->writeWatchers[$id])) {
         if (\uv_is_active($watcher->event)) {
             \uv_poll_stop($watcher->event);
         }
         \uv_poll_start($watcher->event, \UV::WRITABLE | (empty($this->readWatchers[$id]) ? 0 : \UV::READABLE), $this->streamCallback);
     }
     $this->writeWatchers[$id][$watcher->id] = $watcher;
 }
Example #3
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();