本文整理汇总了PHP中PHPDaemon\Core\Daemon类的典型用法代码示例。如果您正苦于以下问题:PHP Daemon类的具体用法?PHP Daemon怎么用?PHP Daemon使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Daemon类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onReady
/**
* 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);
}
}));
}
示例2: prepareAsync
protected function prepareAsync()
{
EventLoop::init();
Daemon::initSettings();
FileSystem::init();
FileSystem::initEvent();
}
示例3: onBound
/**
* Called when socket is bound
* @return boolean Success
*/
protected function onBound()
{
touch($this->path);
chmod($this->path, 0770);
if ($this->group === null && !empty($this->uri['pass'])) {
$this->group = $this->uri['pass'];
}
if ($this->group === null && isset(Daemon::$config->group->value)) {
$this->group = Daemon::$config->group->value;
}
if ($this->group !== null) {
if (!@chgrp($this->path, $this->group)) {
unlink($this->path);
Daemon::log('Couldn\'t change group of the socket \'' . $this->path . '\' to \'' . $this->group . '\'.');
return false;
}
}
if ($this->user === null && !empty($this->uri['user'])) {
$this->user = $this->uri['user'];
}
if ($this->user === null && isset(Daemon::$config->user->value)) {
$this->user = Daemon::$config->user->value;
}
if ($this->user !== null) {
if (!@chown($this->path, $this->user)) {
unlink($this->path);
Daemon::log('Couldn\'t change owner of the socket \'' . $this->path . '\' to \'' . $this->user . '\'.');
return false;
}
}
return true;
}
示例4: run
public function run()
{
$this->proxy = $this->orm->appInstance->proxy;
$this->params = $this['args'][0];
try {
$this->orm->appInstance->httpclient->post('https://account.fineproxy.org/proxy/download/http_auth/txt/', ['log' => $this->params['username'], 'pass' => $this->params['password'], 'logsub' => 'Войти'], function ($conn) {
$proxies = [];
foreach (explode("\n", $conn->body) as $addr) {
$addr = trim($addr);
if (!preg_match('~^\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+$~', $addr)) {
continue;
}
$proxies[] = ['type' => 'http', 'addr' => $addr, 'auth' => ['username' => $this->params['username'], 'password' => $this->params['password']]];
}
$source = 'Fineproxy-' . $this->params['username'];
$itime = time();
$j = (new ComplexJob(function ($j) use($source, $itime) {
$this->sendResult(true);
$this->proxy->removeProxyServer(['source' => $source, 'itime' => ['$lt' => $itime]]);
Daemon::log('complete');
}))->maxConcurrency(5)->more(function () use(&$proxies, $source, $itime) {
foreach ($proxies as $k => $proxy) {
(yield 'proxy_' . $k => function ($jobname, $j) use($proxy, $itime) {
$this->proxy->newProxyServer($proxy)->setOnInsertMode(false)->attr(['itime' => $itime])->save(function ($o) use($jobname, $j) {
$j->setResult($jobname, $o->lastError());
});
});
}
});
$j();
});
} catch (Exception $e) {
$this->sendResult(false);
}
}
示例5: perform
public function perform()
{
$hash = Request::getString($_REQUEST['x']);
if (!strlen($hash) || base64_decode($hash, true) === false) {
$this->req->setResult(['success' => false, 'error' => 'Wrong format of extTokenHash']);
return;
}
$this->appInstance->externalAuthTokens->findByExtTokenHash($hash, function ($result) use($hash) {
if ($result) {
$this->req->setResult(['success' => false, 'error' => 'This token was already used.']);
return;
}
$ip = $this->req->getIp();
$intToken = Crypt::hash(Daemon::uniqid() . "" . $ip . "" . Crypt::randomString());
$this->appInstance->externalAuthTokens->save(['extTokenHash' => $hash, 'intToken' => $intToken, 'ip' => $ip, 'useragent' => Request::getString($_SERVER['HTTP_USER_AGENT']), 'ctime' => microtime(true), 'status' => 'new'], function ($lastError) use($intToken) {
if (!isset($lastError['n']) || $lastError['n'] === 0) {
$this->req->setResult(['success' => false, 'errors' => ['code' => 'Sorry, internal error.']]);
return;
}
$type = Request::getString($_REQUEST['type']);
if ($type === 'email') {
// send email....
} elseif ($type === 'redirect') {
$this->req->redirectTo(HTTPClient::buildUrl(['/' . $this->req->locale . '/account/extauth', 'i' => $intToken]), false);
}
$this->req->setResult(['success' => true, 'intToken' => $intToken]);
});
});
}
示例6: onSleep
/**
* Called when the request starts sleep
* @return void
*/
public function onSleep()
{
Daemon::$context = null;
$this->running = false;
unset($_SESSION, $_GET, $_POST, $_COOKIE);
Daemon::$process->setState(Daemon::WSTATE_IDLE);
}
示例7: onReady
/**
* @TODO DESCR
* @return void
*/
public function onReady()
{
if ($this->user === null) {
$this->connected = true;
}
if ($this->connected) {
parent::onReady();
return;
}
$this->dbname = $this->path;
$this->pool->getNonce(['dbname' => $this->dbname], function ($result) {
if (isset($result['$err'])) {
Daemon::log('MongoClient: getNonce() error with ' . $this->url . ': ' . $result['$err']);
$this->finish();
}
$this->pool->auth(['user' => $this->user, 'password' => $this->password, 'nonce' => $result['nonce'], 'dbname' => $this->dbname], function ($result) {
if (!isset($result['ok']) || !$result['ok']) {
Daemon::log('MongoClient: authentication error with ' . $this->url . ': ' . $result['errmsg']);
$this->finish();
return;
}
$this->connected = true;
$this->onReady();
}, $this);
}, $this);
}
示例8: bindSocket
/**
* Bind given socket
* @param string $uri Address to bind
* @return boolean Success
*/
public function bindSocket($uri)
{
$u = \PHPDaemon\Config\Object::parseCfgUri($uri);
$scheme = $u['scheme'];
if ($scheme === 'unix') {
$socket = new \PHPDaemon\BoundSocket\UNIX($u);
} elseif ($scheme === 'udp') {
$socket = new \PHPDaemon\BoundSocket\UDP($u);
if (isset($this->config->port->value)) {
$socket->setDefaultPort($this->config->port->value);
}
} elseif ($scheme === 'tcp') {
$socket = new \PHPDaemon\BoundSocket\TCP($u);
if (isset($this->config->port->value)) {
$socket->setDefaultPort($this->config->port->value);
}
} else {
Daemon::log(get_class($this) . ': enable to bind \'' . $uri . '\': scheme \'' . $scheme . '\' is not supported');
return false;
}
$socket->attachTo($this);
if ($socket->bindSocket()) {
if ($this->enabled) {
$socket->enable();
}
return true;
}
return false;
}
示例9: init
public function init()
{
if ($this->config->enable->value) {
Daemon::log(__CLASS__ . ' up.');
$this->db = \PHPDaemon\Clients\Mongo\Pool::getInstance();
$this->tags = array();
$this->minMsgInterval = 1;
}
}
示例10: __get
/**
* @param string $name
* @return bool
*/
public function __get($name)
{
$class = '\\WakePHP\\Components\\' . $name;
if (!class_exists($class)) {
Daemon::log(get_class($this) . ': undefined class: ' . $class);
return false;
}
return $this->{$name} = new $class($this->req);
}
示例11: setPassword
public function setPassword($value)
{
if (($r = static::checkPasswordFormat($value)) !== true) {
throw new \Exception($r);
}
$this->setProperty('salt', $salt = $this->appInstance->config->cryptsalt->value . Crypt::hash(Daemon::uniqid() . "" . $this['email']));
$this->setProperty('password', Crypt::hash($value, $salt . $this->appInstance->config->cryptsaltextra->value));
return $this;
}
示例12: getObject
public function getObject($type, $cond = null, $objOrCb = null)
{
$class = ClassFinder::find($type, $this->name, $this->ns);
if (!class_exists($class)) {
Daemon::log(get_class($this) . ': undefined class: ' . $class);
return false;
}
return new $class($cond, $objOrCb, $this);
}
示例13: eventCall
/**
* 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);
}
}
示例14: onFrame
/**
* Called when new frame received.
* @param string Frame's contents.
* @param integer Frame's type.
* @return void
*/
public function onFrame($data, $type)
{
if ($data === 'ping') {
$this->client->sendFrame('pong', 'STRING', function ($client) {
// optional. called when the frame is transmitted to the client
\PHPDaemon\Core\Daemon::log('ExampleWebSocket: \'pong\' received by client.');
});
}
}
示例15: onReady
/**
* Called when the worker is ready to go
*
* @return void
*/
public function onReady()
{
// Adding listener
// ComplexJob - STATE_WAITING
$job = new \PHPDaemon\Core\ComplexJob(function ($job) {
// ComplexJob - STATE_DONE
/*array (
'bar' =>
array (
'job' => 'bar',
'success' => false,
'line' => 63,
),
'foo' =>
array (
'job' => 'foo',
'success' => true,
'line' => 84,
'arg' =>
array (
'param' => 'value',
),
),
'baz' =>
array (
'job' => 'baz',
'success' => false,
'line' => 94,
),
)*/
\PHPDaemon\Core\Daemon::log($job->results);
});
// Adding listener
// ComplexJob - STATE_WAITING
$job->addListener(function ($job) {
// ComplexJob - STATE_DONE
});
// Adding async job foo
$job('foo', $this->foo(['param' => 'value']));
// Adding with 1 sec delay
\PHPDaemon\Core\Timer::add(function ($event) use($job) {
// Adding async job bar
$job('bar', function ($jobname, $job) {
\PHPDaemon\Core\Timer::add(function ($event) use($jobname, $job) {
// Job done
$job->setResult($jobname, ['job' => 'bar', 'success' => false, 'line' => __LINE__]);
$event->finish();
}, 1000.0 * 50);
});
// Adding async job baz. Equal $job('baz', $this->baz());
$job->addJob('baz', $this->baz());
// Run jobs. All listeners will be called when the jobs done
// ComplexJob - STATE_RUNNING
$job();
$event->finish();
}, 1000000.0 * 1);
}