Example #1
0
 /**
  * Runtime of Master process
  * @return void
  */
 protected function run()
 {
     Daemon::$process = $this;
     $this->prepareSystemEnv();
     class_exists('Timer');
     // ensure loading this class
     gc_enable();
     /* This line must be commented according to current libevent binding implementation. May be uncommented in future. */
     //$this->eventBase = new \EventBase;
     if ($this->eventBase) {
         $this->registerEventSignals();
     } else {
         $this->registerSignals();
     }
     $this->workers = new Collection();
     $this->collections['workers'] = $this->workers;
     $this->ipcthreads = new Collection();
     $this->collections['ipcthreads'] = $this->ipcthreads;
     Daemon::$appResolver->preload(true);
     $this->callbacks = new StackCallbacks();
     $this->spawnIPCThread();
     $this->spawnWorkers(min(Daemon::$config->startworkers->value, Daemon::$config->maxworkers->value));
     $this->timerCb = function ($event) use(&$cbs) {
         static $c = 0;
         ++$c;
         if ($c > 0xfffff) {
             $c = 1;
         }
         if ($c % 10 == 0) {
             gc_collect_cycles();
         }
         if (!$this->lastMpmActionTs || microtime(true) - $this->lastMpmActionTs > $this->minMpmActionInterval) {
             $this->callMPM();
         }
         if ($event) {
             $event->timeout();
         }
     };
     if ($this->eventBase) {
         // we are using libevent in Master
         Timer::add($this->timerCb, 1000000.0 * Daemon::$config->mpmdelay->value, 'MPM');
         while (!$this->breakMainLoop) {
             $this->callbacks->executeAll($this);
             if (!$this->eventBase->dispatch()) {
                 break;
             }
         }
     } else {
         // we are NOT using libevent in Master
         $lastTimerCall = microtime(true);
         while (!$this->breakMainLoop) {
             $this->callbacks->executeAll($this);
             if (microtime(true) > $lastTimerCall + Daemon::$config->mpmdelay->value) {
                 call_user_func($this->timerCb, null);
                 $lastTimerCall = microtime(true);
             }
             $this->sigwait();
         }
     }
 }
Example #2
0
 /**
  * Runtime of Worker process.
  * @return void
  */
 protected function run()
 {
     if (Daemon::$process instanceof Master) {
         Daemon::$process->unregisterSignals();
     }
     if (Daemon::$process->eventBase) {
         Daemon::$process->eventBase->reinit();
         $this->eventBase = Daemon::$process->eventBase;
     } else {
         $this->eventBase = new \EventBase();
     }
     Daemon::$process = $this;
     if (Daemon::$logpointerAsync) {
         Daemon::$logpointerAsync->fd = null;
         Daemon::$logpointerAsync = null;
     }
     class_exists('Timer');
     if (Daemon::$config->autogc->value > 0) {
         gc_enable();
     } else {
         gc_disable();
     }
     $this->prepareSystemEnv();
     $this->registerEventSignals();
     FileSystem::init();
     // re-init
     FileSystem::initEvent();
     Daemon::openLogs();
     $this->fileWatcher = new FileWatcher();
     $this->IPCManager = Daemon::$appResolver->getInstanceByAppName('\\PHPDaemon\\IPCManager\\IPCManager');
     if (!$this->IPCManager) {
         $this->log('cannot instantiate IPCManager');
     }
     while (!$this->breakMainLoop) {
         if (!$this->eventBase->dispatch()) {
             break;
         }
     }
 }
 /**
  * Service
  *
  * @return void
  */
 public function service()
 {
     if (!$this->socket->isConnected()) {
         return false;
     }
     $base = new \EventBase();
     $listener = new \EventListener($base, function ($listener, $fd, $address, $base) {
         $event = new \EventBufferEvent($base, $fd, \EventBufferEvent::OPT_CLOSE_ON_FREE);
         $event->setCallbacks(function ($event) {
             $this->protocol->handleData(new EventSocket($event));
         }, function ($event) {
             if (0 === $event->output->length) {
                 $event->free();
             }
         }, function ($event, $events) {
             if ($events & (\EventBufferEvent::EOF | \EventBufferEvent::ERROR)) {
                 $event->free();
             }
         }, null);
         $event->enable(\Event::READ);
     }, $base, \EventListener::OPT_CLOSE_ON_FREE | \EventListener::OPT_REUSEABLE, -1, $this->socket->getHandle());
     $base->dispatch();
 }
Example #4
0
// Event callback
function eventcb($bev, $events, $base)
{
    if ($events & EventBufferEvent::CONNECTED) {
        echo "Connected.\n";
    } elseif ($events & (EventBufferEvent::ERROR | EventBufferEvent::EOF)) {
        if ($events & EventBufferEvent::ERROR) {
            echo "DNS error: ", $bev->getDnsErrorString(), PHP_EOL;
        }
        echo "Closing\n";
        $base->exit();
        exit("Done\n");
    }
}
if ($argc != 3) {
    echo <<<EOS
Trivial HTTP 0.x client
Syntax: php {$argv[0]} [hostname] [resource]
Example: php {$argv[0]} www.google.com /

EOS;
    exit;
}
$base = new EventBase();
$dns_base = new EventDnsBase($base, TRUE);
$bev = new EventBufferEvent($base, null, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS, "readcb", null, "eventcb", $base);
$bev->enable(Event::READ | Event::WRITE);
$bev->getOutput()->add("GET {$argv[2]} HTTP/1.0\r\n" . "Host: {$argv[1]}\r\n" . "Connection: Close\r\n\r\n");
$bev->connectHost($dns_base, $argv[1], 80, EventUtil::AF_UNSPEC);
$base->dispatch();
Example #5
0
 /**
  * Runtime of Worker process.
  * @return void
  */
 protected function run()
 {
     $this->lambdaCache = new CappedStorageHits();
     $this->lambdaCache->setMaxCacheSize(Daemon::$config->lambdacachemaxsize->value);
     $this->lambdaCache->setCapWindow(Daemon::$config->lambdacachecapwindow->value);
     $this->callbacks = new StackCallbacks();
     if (Daemon::$process instanceof Master) {
         Daemon::$process->unregisterSignals();
     }
     if (Daemon::$process && Daemon::$process->eventBase) {
         Daemon::$process->eventBase->reinit();
         $this->eventBase = Daemon::$process->eventBase;
     } else {
         $this->eventBase = new \EventBase();
     }
     Daemon::$process = $this;
     if (Daemon::$logpointerAsync) {
         $oldfd = Daemon::$logpointerAsync->fd;
         Daemon::$logpointerAsync->fd = null;
         Daemon::$logpointerAsync = null;
     }
     class_exists('Timer');
     $this->autoReloadLast = time();
     $this->reloadDelay = Daemon::$config->mpmdelay->value + 2;
     $this->setState(Daemon::WSTATE_PREINIT);
     if (Daemon::$config->autogc->value > 0) {
         gc_enable();
         gc_collect_cycles();
     } else {
         gc_disable();
     }
     if (Daemon::$runworkerMode) {
         if (!Daemon::$config->verbosetty->value) {
             fclose(STDIN);
             fclose(STDOUT);
             fclose(STDERR);
         }
         Daemon::$appResolver->preload(true);
     }
     $this->prepareSystemEnv();
     $this->overrideNativeFuncs();
     $this->setState(Daemon::WSTATE_INIT);
     $this->dnsBase = new \EventDnsBase($this->eventBase, false);
     // @TODO: test with true
     $this->registerEventSignals();
     FileSystem::init();
     FileSystem::initEvent();
     Daemon::openLogs();
     $this->IPCManager = Daemon::$appResolver->getInstanceByAppName('\\PHPDaemon\\IPCManager\\IPCManager');
     if (!$this->IPCManager) {
         $this->log('cannot instantiate IPCManager');
     }
     Daemon::$appResolver->preload();
     foreach (Daemon::$appInstances as $app) {
         foreach ($app as $appInstance) {
             if (!$appInstance->ready) {
                 $appInstance->ready = true;
                 $appInstance->onReady();
             }
         }
     }
     $this->setState(Daemon::WSTATE_IDLE);
     Timer::add(function ($event) {
         if (!Daemon::$runworkerMode) {
             if ($this->IPCManager) {
                 $this->IPCManager->ensureConnection();
             }
         }
         $this->breakMainLoopCheck();
         if ($this->breakMainLoop) {
             $this->eventBase->exit();
             return;
         }
         if (Daemon::checkAutoGC()) {
             $this->callbacks->push(function ($thread) {
                 gc_collect_cycles();
             });
             $this->eventBase->exit();
         }
         $event->timeout();
     }, 1000000.0 * 1, 'breakMainLoopCheck');
     if (Daemon::$config->autoreload->value > 0) {
         Timer::add(function ($event) {
             static $n = 0;
             $list = get_included_files();
             $s = sizeof($list);
             if ($s > $n) {
                 $slice = array_map('realpath', array_slice($list, $n));
                 Daemon::$process->IPCManager->sendPacket(['op' => 'addIncludedFiles', 'files' => $slice]);
                 $n = $s;
             }
             $event->timeout();
         }, 1000000.0 * Daemon::$config->autoreload->value, 'watchIncludedFiles');
     }
     while (!$this->breakMainLoop) {
         $this->callbacks->executeAll($this);
         if (!$this->eventBase->dispatch()) {
             break;
         }
     }
     $this->shutdown();
 }
Example #6
0
 public function dispatch()
 {
     $this->base->dispatch();
 }