/** * Called when timer is triggered * @return void */ public function eventCall() { try { //Daemon::log('cb - '.Debug::zdump($this->cb)); call_user_func($this->cb, $this); } catch (\Exception $e) { Daemon::uncaughtExceptionHandler($e); } }
public function updateServer($server) { if (!isset($server['address'])) { return; } $server['address'] = trim($server['address']); $app = $this; if (isset($app->jobMap[$server['address']])) { //\PHPDaemon\Daemon::log('already doing: '.$server['address']); return; } $job = new \PHPDaemon\Core\ComplexJob(function ($job) use($app, $server) { unset($app->jobMap[$server['address']]); //\PHPDaemon\Daemon::log('Removed job for '.$server['address']. ' ('.sizeof($app->jobMap).')'); $set = $job->results['info']; $set['address'] = $server['address']; $set['players'] = $job->results['players']; $set['latency'] = $job->results['latency']; $set['atime'] = time(); if (0) { \PHPDaemon\Core\Daemon::log('Updated server (' . round(memory_get_usage(true) / 1024 / 1024, 5) . '): ' . $server['address'] . ' latency = ' . round($set['latency'] * 1000, 2) . ' ==== ' . (isset($server['atime']) ? round($set['atime'] - $server['atime']) . ' secs. from last update.' : ' =---= ' . json_encode($server))); } try { $app->servers->upsert(['_id' => $server['_id']], ['$set' => $set]); } catch (\MongoException $e) { \PHPDaemon\Core\Daemon::uncaughtExceptionHandler($e); $app->servers->upsert(['_id' => $server['_id']], ['$set' => ['atime' => time()]]); } }); $app->jobMap[$server['address']] = $job; //\PHPDaemon\Daemon::log('Added job for '.$server['address']); $job('info', function ($jobname, $job) use($app, $server) { $app->client->requestInfo($server['address'], function ($conn, $result) use($app, $server, $jobname, $job) { $job('players', function ($jobname, $job) use($app, $server, $conn) { $conn->requestPlayers(function ($conn, $result) use($app, $jobname, $job) { $job->setResult($jobname, $result); $conn->finish(); }); }); $job->setResult($jobname, $result); }); }); $job('latency', function ($jobname, $job) use($app, $server) { $app->client->ping($server['address'], function ($conn, $result) use($app, $jobname, $job) { $job->setResult($jobname, $result); $conn->finish(); }); }); $job(); }
/** * onFrame * @param string $msg [@todo description] * @param integer $type [@todo description] * @return void */ public function onFrame($msg, $type) { $frames = json_decode($msg, true); if (!is_array($frames)) { return; } $this->route->onWakeup(); foreach ($frames as $frame) { try { $this->route->onFrame($frame, \PHPDaemon\Servers\WebSocket\Pool::STRING); } catch (\Exception $e) { Daemon::uncaughtExceptionHandler($e); } } $this->route->onSleep(); }
/** * Called when new frame received. * @param string $data Frame's data. * @param string $type Frame's type ("STRING" OR "BINARY"). * @return boolean Success. */ public function onFrame($data, $type) { if (!isset($this->route)) { return false; } try { $this->route->onWakeup(); $this->route->onFrame($data, $type); } catch (\Exception $e) { Daemon::uncaughtExceptionHandler($e); } $this->route->onSleep(); return true; }
/** * Called when the connection state changed * @param object $bev EventBufferEvent * @param integer $events Events * @return void */ public function onStateEv($bev, $events) { if ($events & \EventBufferEvent::CONNECTED) { $this->onWriteEv($bev); } elseif ($events & (\EventBufferEvent::ERROR | \EventBufferEvent::EOF | \EventBufferEvent::TIMEOUT)) { try { if ($this->finished) { return; } if ($events & \EventBufferEvent::ERROR) { $errno = \EventUtil::getLastSocketErrno(); if ($errno !== 0) { $this->log('Socket error #' . $errno . ':' . \EventUtil::getLastSocketError()); } if ($this->ssl && $this->bev) { while ($err = $this->bev->sslError()) { $this->log('EventBufferEvent SSL error: ' . $err); } } } $this->finished = true; $this->onFinish(); $this->close(); } catch (\Exception $e) { Daemon::uncaughtExceptionHandler($e); } } }
/** * Finish the request * @param integer Optional. Status. 0 - normal, -1 - abort, -2 - termination * @param boolean Optional. Zombie. Default is false * @return void */ public function finish($status = 0, $zombie = false) { if ($this->state === Generic::STATE_FINISHED) { return; } if (!$zombie) { $this->state = Generic::STATE_FINISHED; } if (!($r = $this->running)) { $this->onWakeup(); } while (($cb = array_shift($this->shutdownFuncs)) !== null) { try { $cb($this); } catch (\Exception $e) { Daemon::uncaughtExceptionHandler($e); // @TODO: break? } } if (!$r) { $this->onSleep(); } $this->event('finish'); $this->onFinish(); $this->cleanupEventHandlers(); if (Daemon::$compatMode) { return; } ++Daemon::$process->counterGC; if (Daemon::$compatMode) { return; } if (!Daemon::$obInStack) { // preventing recursion ob_flush(); } if ($status !== -1) { $appStatus = 0; $this->postFinishHandler(function () use($appStatus, $status) { $this->upstream->endRequest($this, $appStatus, $status); $this->free(); }); } else { $this->free(); } }
/** * Called when the connection failed * @param EventBufferEvent $bev * @return void */ public function onFailureEv($bev = null) { try { if (!$this->connected && !$this->failed) { $this->failed = true; $this->onFailure(); } $this->connected = false; } catch (\Exception $e) { Daemon::uncaughtExceptionHandler($e); } }