dubbo-php-framework的TcpServer的解析(六)

版权声明:转载请注明来源 https://blog.csdn.net/u013702678/article/details/82726141

之前第五篇时,提到了一些回调函数的设置,这篇我们开始分析这些回调函数的实现。

//master进程启动时的回调函数
public function onMasterStart($server)
{
     //重命名master进程的进程名称
     Console::setProcessName($this->processName.'_master_process');
   //记录master进程的进程ID信息
     file_put_contents($this->masterPidFile, $server->master_pid);
     //记录manager进程的进程ID信息
     file_put_contents($this->managerPidFile, $server->manager_pid);
     //修改进程的拥有者信息
     if ($this->user)
     {
        Console::changeUser($this->user);
     }
}
//manager进程启动时的回调函数
public function onManagerStart($server)
{
    
    set_exception_handler(array($this, 'exceptionHandler'));
	set_error_handler(array($this, 'errorHandler'));

    //重命名进程名信息
    Console::setProcessName($this->processName.'_manager_process');
    if ($this->user)
    {   
        //修改进程拥有者信息
        Console::changeUser($this->user);
    }
        
    //监控app启动时间
    $this->sw->appMonitor->onAppStart();
                
    //app启动后向zookeeper注册服务信息
	FSOFRegistry::instance()->setParams($this->processName, $this->config, $this->port,
    $this->serverProviders, $this->start_without_registry);
	FSOFRegistry::instance()->registerZk();
 }
//manager进程停止时的回调函数
public function onManagerStop($server)
    {   
        //取消zk注册
		FSOFRegistry::instance()->unRegisterZk();
    }
//worker进程启动时的回调函数,其中server为swoole_server对象,workerId为swoole内部的worker进程的进程ID
public function onWorkerStart($server, $workerId)
    {
        
       if($server->taskworker)//server是否为task模式,目前dubbo-php-framework没有用task模式
        {
        	// rename task process
            Console::setProcessName($this->processName.'_task_worker_process');
        }
        else //server为worker进程模式
        {
        	//修改worker进程的进程名信息
            Console::setProcessName($this->processName.'_event_worker_process');
        }

        if ($this->user)
        {
            //修改worker进程拥有者信息
            Console::changeUser($this->user);
        }

        //启动app
        //log系统初始化需要用到processName,暂时以常量方式处理
        if(!defined('FSOF_PROVIDER_APP_NAME')) define('FSOF_PROVIDER_APP_NAME', $this->processName);

        //定义appLauncherPath路径信息,位于dubbo-php-framework-master/provider/core/app/AppLauncher.php处
        $AppLauncherPath = dirname(__DIR__).DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'AppLauncher.php';
        //引入相应的文件信息
        require_once $AppLauncherPath;
        //创建Provider应用信息,这里已经开始加载用户自定义的代码,开始处理业务逻辑。
        $protocol = AppLauncher::createApplication($this->requireFile);
        if (!$protocol)
        {
            $this->logger->error('the protocol class  is empty or undefined');
            throw new \Exception('[error] the protocol class  is empty or undefined');
        }

        //设置BaseServer对象的Protocol属性信息
        $this->setProtocol($protocol);

		//设置appContext
        $this->appContext = new AppContext();
    	//设置服务是否是无状态的属性信息
		$service_properties = isset($this->config['service_properties']) ? $this->config['service_properties'] : array();
		if (isset($service_properties['stateless']))
		{
			$this->appContext->setStateless($service_properties['stateless'], $this);
        }
        else
        {
        	$this->appContext->setStateless(FALSE, $this);
        }

        $this->logger->debug("workerId [{$workerId}]  start ok");
        //调用protocol的onStart方法,后续具体再分析Protocol的相关信息
        $this->protocol->onStart($server, $workerId);
        
        //设置全局exception,错误处理函数,放在最后,则相关异常错误信息都会被框架捕捉处理
        set_exception_handler(array($this, 'exceptionHandler'));
        set_error_handler(array($this, 'errorHandler'));
        register_shutdown_function(array($this, 'fatalHandler'));

    }
public static function  createApplication($appBootLoader)
    {
    	/**
    	 * 执行app的初始化操作,所有初始化逻辑都在$appBootLoader中定义
    	 * 先加载app的初始化文件,这样app的自定义autoload机制优先级高于AppLauncher
      	**/
    	require_once $appBootLoader;
    	
    	//autoload 用户的所有代码
    	$appRoot = AppAutoLoader::getFatherPath($appBootLoader, 1);
        AppAutoLoader::addRoot($appRoot);
        return new FSOFProtocol();
    }
//接收到数据时的回调函数实现
public function onReceive($server, $clientId, $fromId, $data)
    {
		$this->cur_fd = $clientId;
		$this->cur_from_id = $fromId;
		$time = 0;
		if (method_exists('swoole_server','getReceivedTime'))
		{
			$time = $this->sw->getReceivedTime()*1000000;
		}

		$reqInfo = array("inqueue_time" => $time);
        //调用protocol的onReceiver方法,后续展开分析。
		$this->protocol->onReceive($server, $clientId, $fromId, $data, $reqInfo);
    }

猜你喜欢

转载自blog.csdn.net/u013702678/article/details/82726141
今日推荐