123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace fcgisrv;
- require_once(BASE_ROOT_PATH . '/helper/http_header.php');
- require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
- require_once(BASE_ROOT_PATH . '/helper/request_helper.php');
- use http_header;
- use Log;
- use request_helper;
- abstract class BaseServer
- {
- protected $mSubPath;
- public function __construct($subPath) {
- $this->mSubPath = $subPath;
- }
- protected $mExFiles;
- protected function setExFiles(array $files)
- {
- $this->mExFiles = $files;
- }
- protected function is_exclude($file)
- {
- $path = BASE_ROOT_PATH . "/" . $this->mSubPath . "/";
- $file = str_replace($path,'',$file);
- return in_array($file,$this->mExFiles);
- }
- private function clear_global()
- {
- $_SESSION = [];
- $_COOKIE = [];
- $_POST = [];
- $_GET = [];
- }
- public function handle_error($level, $message, $file, $line)
- {
- if($level == E_NOTICE) return;
- $trace = "handle_error: level=$level,msg=$message file=$file,line=$line\n";
- $backtrace = debug_backtrace();
- foreach ($backtrace as $item) {
- $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
- }
- Log::record($trace, Log::ERR);
- }
- protected function preLooper()
- {
- set_error_handler([$this, 'handle_error']);
- }
- abstract function handle_req($file);
- public function run_looper()
- {
- Log::record(__FUNCTION__,Log::DEBUG);
- $this->preLooper();
- Log::record('Waiting......',Log::DEBUG);
- $i = 0;
- fcgi_init();
- while(fcgi_accept() >= 0)
- {
- $mem = memory_get_usage();
- Log::record("index = $i memory=$mem ",Log::DEBUG);
- ++$i;
- $start = microtime(true);
- ob_start();
- $this->clear_global();
- perfor_clear();
- perfor_start();
- Log::start_sql_log();
- init_request();
- init_cookie($_SERVER['HTTP_COOKIE']);
- http_header::instance()->start();
- $file = request_helper::script_file();
- $this->handle_req($file);
- fcgi_headers_sent();
- $contents = ob_get_clean();
- fcgi_echo($contents);
- Log::end_sql_log();
- $perlog = perfor_log();
- $mem = memory_get_usage();
- Log::record("memory=$mem perlog = $perlog" ,Log::DEBUG);
- $ret = fcgi_fini();
- $mem = memory_get_usage();
- $msg = sprintf("memory=$mem isCgi=$ret request time=%.6f\r\n\r\n", microtime(true) - $start);
- Log::record($msg ,Log::DEBUG);
- }
- fcgi_fini();
- Log::record('Waiting quit......',Log::DEBUG);
- }
- }
|