Ejemplo n.º 1
0
 /**
  * Run worker instance.
  *
  * @return void
  */
 public function run()
 {
     //Update process state.
     self::$_status = self::STATUS_RUNNING;
     // Eegister shutdown function for checking errors.
     register_shutdown_function(array("\\wsl\\workerman\\Worker", 'checkErrors'));
     // Set autoload root path.
     Autoloader::setRootPath($this->_autoloadRootPath);
     // Create a global event loop.
     if (!self::$globalEvent) {
         $eventLoopClass = "\\wsl\\workerman\\Events\\" . ucfirst(self::getEventLoopName());
         self::$globalEvent = new $eventLoopClass();
         // Register a listener to be notified when server socket is ready to read.
         if ($this->_socketName) {
             if ($this->transport !== 'udp') {
                 self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
             } else {
                 self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
             }
         }
     }
     // Reinstall signal.
     self::reinstallSignal();
     // Init Timer.
     Timer::init(self::$globalEvent);
     // Try to emit onWorkerStart callback.
     if ($this->onWorkerStart) {
         try {
             call_user_func($this->onWorkerStart, $this);
         } catch (\Exception $e) {
             echo $e;
             exit(250);
         } catch (\Error $e) {
             echo $e;
             exit(250);
         }
     }
     // Main loop.
     self::$globalEvent->loop();
 }