Example #1
0
 public function __construct()
 {
     $system = $this;
     //set up the event handler
     set_error_handler(function ($severity, $message, $filePath, $line) use(&$system) {
         $error = new Error($severity, $message, $filePath, $line);
         $system->emit("system.error", $error);
     });
     //set up exception handler
     set_exception_handler(function ($exception) use(&$system) {
         $system->emit("system.exception", $exception);
     });
     //on shutdown, run the event loop
     register_shutdown_function(function () {
         uv_run();
     });
     // start the event loop
     $this->eventLoop = uv_default_loop();
     //set up the core module map
     $modulePrefix = "\\Phastlight\\Module\\";
     $this->moduleMap = array('console' => $modulePrefix . 'Console\\Main', 'process' => $modulePrefix . 'Process\\Main', 'os' => $modulePrefix . 'OS\\Main', 'http' => $modulePrefix . 'HTTP\\Main', 'timer' => $modulePrefix . 'Timer\\Main', 'util' => $modulePrefix . 'Util\\Main', 'fs' => $modulePrefix . 'FileSystem\\Main', 'net' => $modulePrefix . 'NET\\Main', 'child_process' => $modulePrefix . 'ChildProcess\\Main', 'cluster' => $modulePrefix . 'Cluster\\Main');
     $this->modules = array();
 }
 public function listen($port)
 {
     printf("# server listened at {$port}\n");
     uv_tcp_nodelay($this->server, 1);
     uv_tcp_bind($this->server, uv_ip4_addr("0.0.0.0", $port));
     uv_listen($this->server, 511, array($this, "onConnect"));
     uv_run(uv_default_loop());
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 protected function dispatch(int $level)
 {
     $this->dispatching = true;
     try {
         if ($this->deferred || $this->enable || $this->running <= $level) {
             \uv_run($this->loop, \UV::RUN_NOWAIT);
         } else {
             \uv_run($this->loop, \UV::RUN_ONCE);
         }
     } finally {
         foreach ($this->dispose as $callback) {
             \uv_close($callback);
         }
         $this->dispatching = false;
     }
 }
Example #4
0
<?php

uv_fs_open(uv_default_loop(), "./tmp", UV::O_WRONLY | UV::O_CREAT | UV::O_APPEND, UV::S_IRWXU | UV::S_IRUSR, function ($r) {
    var_dump($r);
    uv_fs_write(uv_default_loop(), $r, "hello", 0, function ($a) use($r) {
        var_dump($a);
        var_dump("ok");
        uv_fs_fdatasync(uv_default_loop(), $r, function () {
            echo "fsync finished";
        });
    });
});
uv_run();
Example #5
0
 /**
  * {@inheritdoc}
  */
 protected function dispatch(bool $blocking)
 {
     \uv_run($this->loopHandle, $blocking ? \UV::RUN_ONCE : \UV::RUN_NOWAIT);
 }
Example #6
0
 /**
  * {@inheritDoc}
  */
 public function tick($noWait = false)
 {
     if ($this->state) {
         throw new \LogicException("Cannot tick() recursively; event reactor already active");
     }
     $this->state = self::TICKING;
     $noWait = (bool) $noWait;
     $immediates = $this->immediates;
     foreach ($immediates as $watcher) {
         if (!$this->tryImmediate($watcher)) {
             break;
         }
     }
     // Check the conditional again because a manual stop() could've changed the state
     if ($this->state > 0) {
         \uv_run($this->loop, $noWait || $this->immediates ? \UV::RUN_NOWAIT : \UV::RUN_ONCE);
     }
     $this->state = self::STOPPED;
     if ($this->stopException) {
         $e = $this->stopException;
         $this->stopException = null;
         throw $e;
     }
 }
Example #7
0
                });
            });
            return;
        } else {
            if ($nread == 0) {
                if (uv_last_error() == UV::EOF) {
                    uv_shutdown($client, function ($client) use(&$parsers, &$clients) {
                        uv_close($client, function ($client) use(&$parsers, &$clients) {
                            unset($parsers[(int) $client]);
                            unset($clients[(int) $client]);
                        });
                    });
                    return;
                }
            } else {
                $result = array();
                if (uv_http_parser_execute($parsers[(int) $client], $buffer, $result)) {
                    $response = "HTTP/1.1 200 OK\r\n\r\nHello World";
                    uv_write($client, $response, function ($client) use(&$parsers, &$clients) {
                        uv_close($client, function ($client) use(&$parsers, &$clients) {
                            unset($parsers[(int) $client]);
                            unset($clients[(int) $client]);
                        });
                    });
                }
            }
        }
    });
});
uv_run(uv_default_loop());
Example #8
0
 public function listen($port)
 {
     uv_tcp_nodelay($this->server, 1);
     uv_tcp_bind6($this->server, uv_ip6_addr("::1", $port));
     uv_listen($this->server, 511, array($this, "onConnect"));
     uv_run(uv_default_loop());
 }
Example #9
0
 /**
  * Runs the loop only once
  *
  * @return LoopInterface
  */
 public function runOnce()
 {
     \uv_run($this->loop, \UV::RUN_ONCE);
     return $this;
 }
Example #10
0
 public function loop()
 {
     uv_run();
 }