本文整理汇总了PHP中event_base_new函数的典型用法代码示例。如果您正苦于以下问题:PHP event_base_new函数的具体用法?PHP event_base_new怎么用?PHP event_base_new使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了event_base_new函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
public function __construct()
{
$this->base = event_base_new();
$this->callback = $this->createLibeventCallback();
$this->timersGc = new \SplQueue();
$this->timersGc->setIteratorMode(\SplQueue::IT_MODE_DELETE);
}
示例2: __construct
public function __construct()
{
// @codeCoverageIgnoreStart
if (!\extension_loaded("libevent")) {
throw new \RuntimeException("The pecl libevent extension is required to use " . __CLASS__);
}
// @codeCoverageIgnoreEnd
$this->base = \event_base_new();
$this->keepAliveBase = \event_base_new();
/**
* Prior to PHP7 we can't cancel closure watchers inside their own callbacks
* because PHP will fatal. In legacy versions we schedule manual GC workarounds.
*
* @link https://bugs.php.net/bug.php?id=62452
*/
if (PHP_MAJOR_VERSION < 7) {
$this->gcEvent = event_new();
\event_timer_set($this->gcEvent, function () {
$this->garbage = [];
$this->isGcScheduled = false;
\event_del($this->gcEvent);
});
\event_base_set($this->gcEvent, $this->keepAliveBase);
}
$this->onCoroutineResolution = function ($e = null, $r = null) {
if ($e) {
$this->onCallbackError($e);
}
};
}
示例3: __construct
/**
* Construct new event base
*
* @see event_base_new
* @link http://www.php.net/manual/function.event-base-new.php
* @param int $priority
* @throws EventException
*/
public function __construct($priority = self::DEFAULT_PRIORITY)
{
if (false === ($this->resource = event_base_new())) {
throw $this->exception('Could not create event base resourse (event_base_new).');
}
$this->setPriority($priority);
}
示例4: EpollSocketServer
function EpollSocketServer($port)
{
global $errno, $errstr;
if (!extension_loaded('libevent')) {
die("Please install libevent extension firstly/n");
}
if ($port < 1024) {
die("Port must be a number which bigger than 1024/n");
}
$socket_server = stream_socket_server("tcp://0.0.0.0:{$port}", $errno, $errstr);
if (!$socket_server) {
die("{$errstr} ({$errno})");
}
stream_set_blocking($socket_server, 0);
// 非阻塞
$base = event_base_new();
$event = event_new();
event_set($event, $socket_server, EV_READ | EV_PERSIST, array(__CLASS__, 'ev_accept'), $base);
event_base_set($event, $base);
event_add($event);
event_base_loop($base);
self::$connections = array();
self::$buffers = array();
self::$imei = array();
}
示例5: __construct
/**
* Creates the main event loop
*
* @param int $priorities The number of different priorities
*/
public function __construct($priorities = null)
{
$this->resource = event_base_new();
if (!is_null($priorities)) {
event_base_priority_init($this->resource, $priorities);
}
}
示例6: getBase
public static function getBase()
{
if (!self::$globalBase) {
self::$globalBase = event_base_new();
}
return self::$globalBase;
}
示例7: __construct
public function __construct($addr = '127.0.0.1', $port = 8305, $listenQueue = 512)
{
if (is_null(self::$_logger)) {
self::$_logger = \Zopt\Base\Logger::getLogger(__CLASS__);
}
if (!extension_loaded('libevent')) {
self::$_logger->crit('FATAL: Please firstly install libevent extension.');
die;
}
$this->_addr = $addr;
$this->_port = $port;
$this->_listenQueue = $listenQueue;
$this->_started = FALSE;
$this->_sock = NULL;
// Init the event machine using libevent c extension
$this->_listenEvent = NULL;
$this->_eventBase = event_base_new();
$this->_streams = array();
// Register signal handlers
$this->_signalEvents = array();
foreach (array(SIGTERM, SIGHUP, SIGINT, SIGQUIT) as $signo) {
$event = event_new();
event_set($event, $signo, EV_SIGNAL | EV_PERSIST, array($this, 'handleSignalEvent'), $signo);
event_base_set($event, $this->_eventBase);
event_add($event);
$this->_signalEvents[$signo] = $event;
}
}
示例8: __construct
public function __construct($port)
{
global $errno, $errstr;
if (!extension_loaded('libevent')) {
die("Please install libevent extension firstly\n");
}
if ($port < 1024) {
die("Port must be a number which bigger than 1024\n");
}
$socket_server = stream_socket_server("tcp://0.0.0.0:{$port}", $errno, $errstr);
if (!$socket_server) {
die("{$errstr} ({$errno})");
}
stream_set_blocking($socket_server, 0);
// 非阻塞
// event_base_new — Create and initialize new event base
$base = event_base_new();
// event_new — Create new event
$event = event_new();
// event_set — Prepare an event
event_set($event, $socket_server, EV_READ | EV_PERSIST, [__CLASS__, 'ev_accept'], $base);
// event_base_set — Associate event base with an event
event_base_set($event, $base);
// event_add — Add an event to the set of monitored events
event_add($event);
// event_base_loop — Handle events
event_base_loop($base);
self::$connections = [];
self::$buffers = [];
}
示例9: start
public function start()
{
$this->onStart();
$this->base = event_base_new();
if ($this->_server) {
$this->event = event_new();
event_set($this->event, $this->_server, EV_READ | EV_PERSIST, array($this, 'accept'), $this->base);
event_base_set($this->event, $this->base);
event_add($this->event);
}
if ($this->_service) {
$this->service_event = event_new();
event_set($this->service_event, $this->_service, EV_READ | EV_PERSIST, array($this, 'service'), $this->base);
event_base_set($this->service_event, $this->base);
event_add($this->service_event);
}
if ($this->_master) {
$this->master_event = event_new();
event_set($this->master_event, $this->_master, EV_READ | EV_PERSIST | EV_WRITE, array($this, 'master'), $this->base);
event_base_set($this->master_event, $this->base);
event_add($this->master_event);
}
if ($this->timer) {
$timer = event_timer_new();
event_timer_set($timer, array($this, '_onTimer'), $timer);
event_base_set($timer, $this->base);
//event_timer_pending($timer, $this->timer * 1000000);
event_timer_add($timer, $this->timer * 1000000);
}
event_base_loop($this->base);
}
示例10: start
public function start()
{
$this->base = event_base_new();
if ($this->_server) {
$this->event = event_new();
event_set($this->event, $this->_server, EV_READ | EV_PERSIST, array($this, 'accept'), $this->base);
event_base_set($this->event, $this->base);
event_add($this->event);
}
foreach ($this->_services as $serviceId => $service) {
$event = event_new();
event_set($event, $service, EV_READ | EV_PERSIST | EV_WRITE, array($this, 'service'), $this->base);
event_base_set($event, $this->base);
event_add($event);
$this->events[$serviceId] = $event;
}
if ($this->timer) {
$timer = event_timer_new();
event_timer_set($timer, array($this, '_onTimer'), $timer);
event_base_set($timer, $this->base);
//event_timer_pending($timer, $this->timer * 1000000);
event_timer_add($timer, $this->timer * 1000000);
}
event_base_loop($this->base);
}
示例11: __construct
/**
* LeBase constructor.
*/
public function __construct()
{
$this->handle = event_base_new();
if ($this->handle === false) {
throw new \RuntimeException('Can not initialize libevent.');
}
}
示例12: initialize
/**
* Normally this would go into the __construct() function but it's split out into its own
* method because we also have to initialize() when calling afterFork().
*/
private function initialize()
{
$this->base = event_base_new();
$this->gcEvent = event_new();
event_timer_set($this->gcEvent, [$this, 'collectGarbage']);
event_base_set($this->gcEvent, $this->base);
}
示例13: run
/**
* Runtime of Master process
* @return void
*/
public function run()
{
Daemon::$process = $this;
$this->prepareSystemEnv();
class_exists('Timer');
// ensure loading this class
gc_enable();
$this->eventBase = event_base_new();
$this->registerEventSignals();
FS::initEvent();
$this->fileWatcher = new FileWatcher();
$this->workers = new ThreadCollection();
$this->collections['workers'] = $this->workers;
Daemon::$appResolver = (require Daemon::$config->path->value);
$this->IPCManager = Daemon::$appResolver->getInstanceByAppName('IPCManager');
Daemon::$appResolver->preload(true);
$this->callbacks = new SplStack();
$this->spawnWorkers(min(Daemon::$config->startworkers->value, Daemon::$config->maxworkers->value));
Timer::add(function ($event) use(&$cbs) {
$self = Daemon::$process;
static $c = 0;
++$c;
if ($c > 0xfffff) {
$c = 1;
}
if ($c % 10 == 0) {
$self->workers->removeTerminated(true);
gc_collect_cycles();
} else {
$self->workers->removeTerminated();
}
if (isset(Daemon::$config->mpm->value) && is_callable(Daemon::$config->mpm->value)) {
call_user_func(Daemon::$config->mpm->value);
} else {
// default MPM
$state = Daemon::getStateOfWorkers($self);
if ($state) {
$n = max(min(Daemon::$config->minspareworkers->value - $state['idle'], Daemon::$config->maxworkers->value - $state['alive']), Daemon::$config->minworkers->value - $state['alive']);
if ($n > 0) {
Daemon::log('Spawning ' . $n . ' worker(s).');
$self->spawnWorkers($n);
event_base_loopbreak($self->eventBase);
}
$n = min($state['idle'] - Daemon::$config->maxspareworkers->value, $state['alive'] - Daemon::$config->minworkers->value);
if ($n > 0) {
Daemon::log('Stopping ' . $n . ' worker(s).');
$self->stopWorkers($n);
}
}
}
$event->timeout();
}, 1000000.0 * Daemon::$config->mpmdelay->value, 'MPM');
while (!$this->breakMainLoop) {
while (!$this->callbacks->isEmpty()) {
call_user_func($this->callbacks->shift(), $this);
}
event_base_loop($this->eventBase);
}
}
示例14: loop
public function loop()
{
if (!($this->event_base = event_base_new())) {
throw new RuntimeException("Can't create event base");
}
$this->addSocketEvent();
event_base_loop($this->event_base);
}
示例15: __construct
public function __construct()
{
$this->eventBase = event_base_new();
$this->nextTickQueue = new NextTickQueue($this);
$this->timerEvents = new SplObjectStorage();
$this->createTimerCallback();
$this->createStreamCallback();
}