Example #1
0
 /**
  * @see EventInterface::add()
  */
 public function add($fd, $flag, $func, $args = array())
 {
     switch ($flag) {
         case self::EV_SIGNAL:
             $fd_key = (int) $fd;
             $event = \Event::signal($this->_eventBase, $fd, $func);
             if (!$event || !$event->add()) {
                 return false;
             }
             $this->_eventSignal[$fd_key] = $event;
             return true;
         case self::EV_TIMER:
         case self::EV_TIMER_ONCE:
             $param = array($func, (array) $args, $flag, $fd, self::$_timerId);
             $event = new \Event($this->_eventBase, -1, \Event::TIMEOUT | \Event::PERSIST, array($this, "timerCallback"), $param);
             if (!$event || !$event->addTimer($fd)) {
                 return false;
             }
             $this->_eventTimer[self::$_timerId] = $event;
             return self::$_timerId++;
         default:
             $fd_key = (int) $fd;
             $real_flag = $flag === self::EV_READ ? \Event::READ | \Event::PERSIST : \Event::WRITE | \Event::PERSIST;
             $event = new \Event($this->_eventBase, $fd, $real_flag, $func, $fd);
             if (!$event || !$event->add()) {
                 return false;
             }
             $this->_allEvents[$fd_key][$flag] = $event;
             return true;
     }
 }
Example #2
0
 /**
  * Add signal handler.
  *
  * @param $signal
  * @param $callback
  * @return bool
  */
 public function addSignal($signal, $callback)
 {
     $event = \Event::signal($this->_eventBase, $signal, $callback);
     if (!$event || !$event->add()) {
         return false;
     }
     $this->_signalEvents[$signal] = $event;
 }
Example #3
0
 public function __construct($base)
 {
     $this->base = $base;
     $this->ev = Event::signal($base, SIGTERM, array($this, 'eventSighandler'), 'libo');
     $this->ev->add();
 }
Example #4
0
foreach ($addresses as $port => $ip) {
    echo $ip, " ", $port, PHP_EOL;
    $socket[$i] = stream_socket_server("tcp://{$ip}:{$port}", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN);
    /*
    	$socket[$i] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    	if (!socket_bind($socket[$i], $ip, $port)) {
    		exit("socket_bind failed\n");
    	}
    	socket_listen($socket[$i], 0);
    	socket_set_nonblock($socket[$i]);
    */
    if (!$http->accept($socket[$i])) {
        echo "Accept failed\n";
        exit(1);
    }
    ++$i;
}
$http->setDefaultCallback(function ($req) {
    echo "URI: ", $req->getUri(), PHP_EOL;
    $req->sendReply(200, "OK");
    echo "OK\n";
});
$signal = Event::signal($base, SIGINT, function () use($base) {
    echo "Caught SIGINT. Stopping...\n";
    $base->stop();
});
$signal->add();
$base->dispatch();
echo "END\n";
// We didn't close sockets, since Libevent already sets CLOSE_ON_FREE and CLOSE_ON_EXEC flags on the file
// descriptor associated with the sockets.
Example #5
0
 /**
  * Register signals.
  * @return void
  */
 protected function registerEventSignals()
 {
     if (!$this->eventBase) {
         return;
     }
     foreach (self::$signals as $no => $name) {
         if ($name === 'SIGKILL' || $name == 'SIGSTOP') {
             continue;
         }
         $ev = \Event::signal($this->eventBase, $no, [$this, 'eventSighandler'], [$no]);
         if (!$ev) {
             $this->log('Cannot event_set for ' . $name . ' signal');
         }
         $ev->add();
         $this->sigEvents[$no] = $ev;
     }
 }
Example #6
0
 /**
  * @param array ...$args
  * @return mixed
  */
 public function signal(...$args)
 {
     return \Event::signal($this->base, ...$args);
 }