Example #1
0
    protected static $_appInitPath = '';
    public static function setRootPath($root_path)
    {
        self::$_appInitPath = $root_path;
    }
    /**
     * 根据命名空间加载文件
     *
     * @param string $name        	
     * @return boolean
     */
    public static function loadByNamespace($name)
    {
        // 相对路径
        $class_path = str_replace('\\', DIRECTORY_SEPARATOR, $name);
        $class_file = self::$_appInitPath . '/' . $class_path . '.php';
        // 找到文件
        if (is_file($class_file)) {
            // 加载
            require_once $class_file;
            if (class_exists($name, false)) {
                return true;
            }
        }
        return false;
    }
}
$doc_root = dirname(__DIR__);
Autoloader::setRootPath($doc_root . '/src');
// 设置类自动加载回调函数
spl_autoload_register('Autoloader::loadByNamespace');
Example #2
0
 /**
  * 运行worker实例
  */
 public function run()
 {
     //更新 Worker 状态
     self::$_status = self::STATUS_RUNNING;
     // 注册进程退出回调,用来检查是否有错误
     register_shutdown_function(array("Worker", 'checkErrors'));
     // 设置自动加载根目录
     Autoloader::setRootPath($this->_appInitPath);
     // 如果没有全局事件轮询,则创建一个
     if (!self::$globalEvent) {
         if (extension_loaded('libevent')) {
             self::$globalEvent = new Libevent();
         } else {
             self::$globalEvent = new Select();
         }
         // 监听_mainSocket上的可读事件(客户端连接事件)
         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'));
             }
         }
     }
     // 重新安装事件处理函数,使用全局事件轮询监听信号事件
     self::reinstallSignal();
     // 用全局事件轮询初始化定时器
     Timer::init(self::$globalEvent);
     // 如果有设置进程启动回调,则执行
     if ($this->onWorkerStart) {
         call_user_func($this->onWorkerStart, $this);
     }
     // 子进程主循环
     self::$globalEvent->loop();
 }