本文整理汇总了PHP中event_base_loop函数的典型用法代码示例。如果您正苦于以下问题:PHP event_base_loop函数的具体用法?PHP event_base_loop怎么用?PHP event_base_loop使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了event_base_loop函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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);
}
示例2: 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();
}
示例3: __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 = [];
}
示例4: 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);
}
示例5: 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);
}
}
示例6: 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);
}
示例7: dispatch
/**
* {@inheritdoc}
*/
protected function dispatch(bool $blocking)
{
$flags = EVLOOP_ONCE;
if (!$blocking) {
$flags |= EVLOOP_NONBLOCK;
}
event_base_loop($this->base, $flags);
// Dispatch I/O, timer, and signal callbacks.
}
示例8: loop
public function loop()
{
if (!($this->event_base = event_base_new())) {
throw new RuntimeException("Can't create event base");
}
foreach ($this->addrs as $addr) {
$this->addAddr($addr);
}
event_base_loop($this->event_base);
}
示例9: worker_callback
public function worker_callback(swoole_process $worker)
{
self::$worker_event_base[$worker->pipe] = event_base_new();
$event = event_new();
//$GLOBALS['worker'] = $worker;
event_set($event, $worker->pipe, EV_READ | EV_PERSIST, array($this, 'worker_accept'), $worker);
event_base_set($event, self::$worker_event_base[$worker->pipe]);
event_add($event);
event_base_loop(self::$worker_event_base[$worker->pipe]);
}
示例10: doRun
private function doRun($flags = 0)
{
$this->isRunning = TRUE;
event_base_loop($this->base, $flags);
$this->isRunning = FALSE;
if ($this->stopException) {
$e = $this->stopException;
throw $e;
}
}
示例11: listen
public function listen()
{
echo "Start listening port " . $this->opts['port'] . "\n";
$socket = stream_socket_server('tcp://0.0.0.0:' . $this->opts['port'], $errno, $errstr);
stream_set_blocking($socket, 0);
$this->base = event_base_new();
$listenerEvent = event_new();
event_set($listenerEvent, $socket, EV_READ | EV_PERSIST, array($this, 'onAccept'), null);
event_base_set($listenerEvent, $this->base);
event_add($listenerEvent, -1);
event_base_loop($this->base);
}
示例12: run
public function run()
{
$this->init();
//建立服务器端Socket
$this->server_sock = $this->create("tcp://{$this->config['host']}:{$this->config['port']}");
//设置事件监听,监听到服务器端socket可读,则有连接请求
\event_set($this->server_event, $this->server_sock, EV_READ | EV_PERSIST, __CLASS__ . '::server_handle_connect', $this);
\event_base_set($this->server_event, $this->base_event);
\event_add($this->server_event);
$this->client->onStart();
\event_base_loop($this->base_event);
}
示例13: listen
/**
* Start listening
*/
public function listen()
{
$this->handle_ = stream_socket_server("tcp://{$this->host_}:{$this->port_}", $this->errno_, $this->errstr_);
stream_set_blocking($this->handle_, 0);
// no blocking
$this->base_ = \event_base_new();
$this->serverEvent_ = \event_new();
\event_set($this->serverEvent_, $this->handle_, EV_READ | EV_PERSIST, array($this, 'onConnect'));
\event_base_set($this->serverEvent_, $this->base_);
\event_add($this->serverEvent_);
\event_base_loop($this->base_);
}
示例14: run
/**
* 运行服务器程序
* @return unknown_type
*/
function run($setting)
{
$this->init();
//建立服务器端Socket
$this->server_sock = $this->create("tcp://{$this->host}:{$this->port}");
//设置事件监听,监听到服务器端socket可读,则有连接请求
event_set($this->server_event, $this->server_sock, EV_READ | EV_PERSIST, array($this, "event_connect"));
event_base_set($this->server_event, $this->base_event);
event_add($this->server_event);
$this->spawn($setting);
$this->protocol->onStart($this);
event_base_loop($this->base_event);
}
示例15: run
/**
* 运行服务器程序
* @return unknown_type
*/
function run()
{
//初始化事件系统
$this->init();
//建立服务器端Socket
$this->server_sock = $this->create("udp://{$this->host}:{$this->port}");
//设置事件监听,监听到服务器端socket可读,则有连接请求
event_set($this->server_event, $this->server_sock, EV_READ | EV_PERSIST, "sw_server_handle_recvfrom", $this);
event_base_set($this->server_event, $this->base_event);
event_add($this->server_event);
$this->protocol->onStart();
event_base_loop($this->base_event);
}