/** * Called when the worker is ready to go. * @return void */ public function onReady() { $appInstance = $this; // a reference to this application instance for ExampleWebSocketRoute \PHPDaemon\Servers\WebSocket\Pool::getInstance()->addRoute('ExamplePubSub', function ($client) use($appInstance) { return new ExamplePubSubWebSocketRoute($client, $appInstance); }); $this->sql = \PHPDaemon\Clients\MySQL\Pool::getInstance(); $this->pubsub = new \PHPDaemon\PubSub\PubSub(); $this->pubsub->addEvent('usersNum', \PHPDaemon\PubSub\PubSubEvent::init()->onActivation(function ($pubsub) use($appInstance) { \PHPDaemon\Core\Daemon::log('onActivation'); if (isset($pubsub->event)) { \PHPDaemon\Core\Timer::setTimeout($pubsub->event, 0); return; } $pubsub->event = setTimeout(function ($timer) use($pubsub, $appInstance) { $appInstance->sql->getConnection(function ($sql) use($pubsub) { if (!$sql->connected) { return; } $sql->query('SELECT COUNT(*) `num` FROM `dle_users`', function ($sql, $success) use($pubsub) { $pubsub->pub(sizeof($sql->resultRows) ? $sql->resultRows[0]['num'] : 'null'); }); }); $timer->timeout(5000000.0); // 5 seconds }, 0); })->onDeactivation(function ($pubsub) { if (isset($pubsub->event)) { \PHPDaemon\Core\Timer::cancelTimeout($pubsub->event); } })); }
/** * Adds your subscription on object in FS * @param string $path Path * @param mixed $cb Callback * @param integer $flags Look inotify_add_watch() * @return true */ public function addWatch($path, $cb, $flags = null) { $path = realpath($path); if (!isset($this->files[$path])) { $this->files[$path] = []; if ($this->inotify) { $this->descriptors[inotify_add_watch($this->inotify, $path, $flags ?: IN_MODIFY)] = $path; } } $this->files[$path][] = $cb; Timer::setTimeout('fileWatcher'); return true; }
/** * @TODO DESCR * @return void */ public function onWrite() { $this->onWrite->executeAll($this->route); if (method_exists($this->route, 'onWrite')) { $this->route->onWrite(); } if ($this->finished) { if (!sizeof($this->buffer) && !sizeof($this->framesBuffer)) { $this->onFinish(); } } Timer::setTimeout($this->finishTimer); }
/** * Called when we got UDP packet * @param resource $stream Descriptor * @param integer $events Events * @param mixed $arg Attached variable * @return boolean Success. */ public function onReadUdp($stream = null, $events = 0, $arg = null) { if (Daemon::$config->logevents->value) { Daemon::$process->log(get_class($this) . '::' . __METHOD__ . ' invoked.'); } if (Daemon::$process->reload) { return false; } if ($this->pool->maxConcurrency) { if ($this->pool->count() >= $this->pool->maxConcurrency) { $this->overload = true; return false; } } $host = null; do { $l = @socket_recvfrom($this->fd, $buf, 10240, MSG_DONTWAIT, $host, $port); if (!$l) { break; } $key = '[' . $host . ']:' . $port; if (!isset($this->portsMap[$key])) { if ($this->pool->allowedClients !== null) { if (!self::netMatch($conn->pool->allowedClients, $host)) { Daemon::log('Connection is not allowed (' . $host . ')'); } continue; } $class = $this->pool->connectionClass; $conn = new $class(null, $this->pool); $conn->setDgram(true); $conn->onWriteEv(null); $conn->setPeername($host, $port); $conn->setParentSocket($this); $this->portsMap[$key] = $conn; $conn->timeoutRef = setTimeout(function ($timer) use($conn) { $conn->finish(); $timer->finish(); }, $conn->timeout * 1000000.0); $conn->onUdpPacket($buf); } else { $conn = $this->portsMap[$key]; $conn->onUdpPacket($buf); Timer::setTimeout($conn->timeoutRef); } } while (true); return $host !== null; }
/** * Called when new data received * @return void */ public function onRead() { Timer::setTimeout($this->keepaliveTimer); while (($line = $this->readline()) !== null) { if ($line === '') { continue; } if (strlen($line) > 512) { Daemon::$process->log('IRCBouncerConnection error: buffer overflow.'); $this->finish(); return; } $line = binarySubstr($line, 0, -strlen($this->EOL)); $p = strpos($line, ':', 1); $max = $p ? substr_count($line, " ", 0, $p) + 1 : 18; $e = explode(" ", $line, $max); $i = 0; $cmd = $e[$i++]; $args = []; for ($s = min(sizeof($e), 14); $i < $s; ++$i) { if ($e[$i][0] === ':') { $args[] = binarySubstr($e[$i], 1); break; } $args[] = $e[$i]; } if (ctype_digit($cmd)) { $code = (int) $cmd; $cmd = isset(IRC::$codes[$code]) ? IRC::$codes[$code] : 'UNKNOWN-' . $code; } $this->onCommand($cmd, $args); } if (strlen($this->buf) > 512) { Daemon::$process->log('IRCClientConnection error: buffer overflow.'); $this->finish(); } }
/** * Called when new data received * @return void */ public function onRead() { Timer::setTimeout($this->keepaliveTimer); if (isset($this->xml)) { $this->xml->feed($this->readUnlimited()); } }
/** * Output some data * @param string $s String to out * @param boolean $flush * @return boolean Success */ public function out($s, $flush = true) { if ($this->heartbeatTimer !== null) { Timer::setTimeout($this->heartbeatTimer); } return parent::out($s, $flush); }