/** * 初始化Request * @param \swoole_server $server 注入当前进程的$server对象 */ public function __construct() { $className = array_pop(explode('\\', get_called_class())); //如果ID被子类设置了,则沿用子类的设置;如果没有,尝试从配置文件中获取; $this->ID = is_numeric($this->ID) ? $this->ID : C($className . 'ID'); //RequestID应该被设置好 if ($this->ID() === null) { DILog(\get_called_class() . '.ID wasn\'t set or configured.', 'w'); } elseif (!is_numeric($this->ID())) { DILog(\get_called_class() . '.ID isn\'t numeric.', 'w'); } }
/** * 初始化Handler * @param \swoole_server $server 注入当前进程的$server对象 */ public function __construct(&$server = null) { $className = array_pop(explode('\\', get_called_class())); //如果ID被子类设置了,则沿用子类的设置;如果没有,尝试从配置文件中获取; $this->ID = is_numeric($this->ID) ? $this->ID : C($className . 'ID'); //HandlerID应该被设置好 if ($this->ID() === null) { DILog(\get_called_class() . '.ID wasn\'t set or configured, this handler won\'t be loaded.', 'w'); } elseif (!is_numeric($this->ID())) { DILog(\get_called_class() . '.ID isn\'t numeric, this handler won\'t be loaded.', 'w'); } else { $this->server = $server; } }
/** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options = array()) { ini_set('default_socket_timeout', -1); //不超时 if (empty($options)) { $options = array('host' => C('KVSTORE_HOST'), 'port' => C('KVSTORE_PORT'), 'timeout' => false, 'persistent' => false, 'prefix' => C('DATA_CACHE_PREFIX'), 'user' => C('DATA_CACHE_USER'), 'pwd' => C('DATA_CACHE_PW'), 'expire' => 12000); } else { $options['host'] = isset($options['host']) ?: C('KVSTORE_HOST'); $options['port'] = isset($options['port']) ?: C('KVSTORE_PORT'); $options['timeout'] = isset($options['timeout']) ?: FALSE; $options['persistent'] = isset($options['persistent']) ?: FALSE; $options['prefix'] = isset($options['prefix']) ?: C('DATA_CACHE_PREFIX'); $options['user'] = isset($options['user']) ?: C('DATA_CACHE_USER'); $options['pwd'] = isset($options['pwd']) ?: C('DATA_CACHE_PW'); $options['expire'] = isset($options['expire']) ?: 12000; } $this->options = $options; $redis = new \Redis(); for ($i = 0; $i < 2; $i++) { if ($redis->pconnect($options['host'], $options['port']) == false) { try { $error = $redis->getLastError(); } catch (\RedisException $ex) { $error = $ex->getMessage(); DILog($error); if ($error != 'Redis server went away') { break; //跳出重试循环 } else { sleep(0.5); //暂停0.5s以后重试连接 DILog('Kvstore: Try Reconnected'); } } // E($redis->getLastError()); } } $redis->setOption(\Redis::OPT_PREFIX, $options['prefix']); /* user:password 拼接成AUTH的密码 */ if ($redis->auth($options['user'] . ':' . $options['pwd']) == false) { $error = $redis->getLastError(); DILog($error); // E($redis->getLastError()); } $this->handler = $redis; }
public function OnWorkerStart(\swoole_process $worker) { DILog('DIWorkerStart'); }
/** * 重新加载WorkerReloadHelper */ protected function InitWorkerReloadHelper() { //初始化基类 $baseReloadHelperClass = new \ReflectionClass("DIServer\\ReloadHelper"); $reloadHelperFilePath = DI_APP_SERVER_WORKER_PATH . '/ReloadHelper.php'; if (file_exists($reloadHelperFilePath)) { require_cache($reloadHelperFilePath); try { $className = DI_SERVER_NAME . '\\Worker\\ReloadHelper'; $reloadHelperClass = new \ReflectionClass($className); //生成 if ($reloadHelperClass->isSubclassOf($baseReloadHelperClass)) { $this->ReloadHelper = $reloadHelperClass->newInstance(); return; } } catch (\ReflectionException $ex) { // DILog("ReflectionException On {$reloadHelperFile}:{$ex->getMessage()}",'n'); } } DILog("Can't Create {$reloadHelperFilePath}, Use default BaseReloadHelper.", 'n'); $this->ReloadHelper = $baseReloadHelperClass->newInstance(); }
public function OnWorkerStop(\swoole_server &$server, &$worker_id) { DILog(DI_SERVER_NAME . " Worker Stop {$worker_id}" . "/Count<handler>" . count(DIHandler())); }
/** * 从指定路径加载Ticker * @param type $tickerFile Ticker的路径 * @param type $namespace Ticker的命名空间 * @return boolean Ticker是否加载成功 */ function DILoadTicker($tickerFile, $namespace = '') { if (is_array($tickerFile)) { foreach ($tickerFile as $path) { DILoadTicker($path, $namespace); } } else { if (file_exists($tickerFile)) { $fileName = array_pop(explode('/', $tickerFile)); $sortClassName = str_ireplace('.php', '', $fileName); $className = $namespace . '\\' . $sortClassName; require_cache($tickerFile); $TickerClass = new \ReflectionClass("\\DIServer\\Ticker"); try { $tickerClass = new \ReflectionClass($className); if ($tickerClass->isSubclassOf($TickerClass)) { $ticker = $tickerClass->newInstance(); DITicker($ticker); } } catch (\ReflectionException $ex) { DILog("ReflectionException On {$tickerFile}:{$ex->getMessage()}"); } return TRUE; } else { return FALSE; } } }
/** * 数据库错误信息 * 并显示当前的SQL语句 * @access public * @return string */ public function error() { if ($this->PDOStatement) { $error = $this->PDOStatement->errorInfo(); $this->error = $error[1] . ':' . $error[2]; } else { $this->error = ''; } if ('' != $this->queryStr) { $this->error .= "\n [ SQL语句 ] : " . $this->queryStr; } // 记录错误日志 if (!IS_CLI) { trace($this->error, '', 'ERR'); } else { DILog($this->error, 'e'); } if ($this->config['debug'] && !IS_CLI) { // 开启数据库调试模式 E($this->error); } else { return $this->error; } }
/** * 设置回调 */ protected function SetOnServer() { if ($this->server) { $callBack =& $this->callBack; $this->server->on("start", [$callBack, 'OnStart']); $this->server->on("connect", [$callBack, 'OnConnect']); $this->server->on("receive", [$callBack, 'OnReceive']); $this->server->on("close", [$callBack, 'OnClose']); $this->server->on("task", [$callBack, 'OnTask']); $this->server->on('finish', [$callBack, 'OnFinish']); $this->server->on('shutdown', [$callBack, 'OnShutdown']); $this->server->on('WorkerStart', [$callBack, 'OnWorkerStart']); $this->server->on('WorkerStop', [$callBack, 'OnWorkerStop']); $this->server->on('WorkerError', [$callBack, 'OnWorkerError']); $this->server->on('PipeMessage', [$callBack, 'OnPipeMessage']); DILog('Swoole_version is ' . swoole_version(), 'n'); if (version_compare(\swoole_version(), '1.7.17')) { //OnPacket方法与现有的基于Receive的业务逻辑有差异,暂时不支持。 // DILog('OnPacket is activated.'); // $this->server->on('Packet', [$callBack, 'OnPacket']); //1.7.18特性 } } }