BaseServer.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace fcgisrv;
  3. require_once(BASE_ROOT_PATH . '/helper/http_header.php');
  4. require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
  5. require_once(BASE_ROOT_PATH . '/helper/request_helper.php');
  6. use http_header;
  7. use Log;
  8. use request_helper;
  9. abstract class BaseServer
  10. {
  11. protected $mSubPath;
  12. public function __construct($subPath) {
  13. $this->mSubPath = $subPath;
  14. }
  15. protected $mExFiles;
  16. protected function setExFiles(array $files)
  17. {
  18. $this->mExFiles = $files;
  19. }
  20. protected function is_exclude($file)
  21. {
  22. $path = BASE_ROOT_PATH . "/" . $this->mSubPath . "/";
  23. $file = str_replace($path,'',$file);
  24. return in_array($file,$this->mExFiles);
  25. }
  26. private function clear_global()
  27. {
  28. $_SESSION = [];
  29. $_COOKIE = [];
  30. $_POST = [];
  31. $_GET = [];
  32. // $_SERVER = [];
  33. }
  34. public function handle_error($level, $message, $file, $line)
  35. {
  36. if($level == E_NOTICE) return;
  37. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  38. $backtrace = debug_backtrace();
  39. foreach ($backtrace as $item) {
  40. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  41. }
  42. Log::record($trace,Log::ERR);
  43. }
  44. protected function preLooper()
  45. {
  46. set_error_handler([$this, 'handle_error']);
  47. }
  48. abstract function handle_req($file);
  49. public function run_looper()
  50. {
  51. Log::record(__FUNCTION__,Log::DEBUG);
  52. $this->preLooper();
  53. Log::record('Waiting......',Log::DEBUG);
  54. $i = 0;
  55. fcgi_init();
  56. while(($ret = fcgi_accept()) >= 0)
  57. {
  58. $mem = memory_get_usage();
  59. Log::record("index = {$i} memory={$mem} ",Log::DEBUG);
  60. ++$i;
  61. $start = microtime(true);
  62. ob_start();
  63. $this->clear_global();
  64. perfor_clear();
  65. perfor_start();
  66. Log::start_sql_log();
  67. init_request();
  68. init_cookie($_SERVER['HTTP_COOKIE']);
  69. http_header::instance()->start();
  70. $file = request_helper::script_file();
  71. $this->handle_req($file);
  72. fcgi_headers_sent();
  73. $contents = ob_get_clean();
  74. fcgi_echo($contents);
  75. Log::end_sql_log();
  76. $perlog = perfor_log();
  77. $mem = memory_get_usage();
  78. Log::record("memory={$mem} perlog = {$perlog}" ,Log::DEBUG);
  79. $ret = fcgi_fini();
  80. $mem = memory_get_usage();
  81. $msg = sprintf("memory={$mem} isCgi = {$ret} request time=%.6f\r\n\r\n",microtime(true) - $start);
  82. Log::record($msg ,Log::DEBUG);
  83. }
  84. fcgi_fini();
  85. Log::record('Waiting quit......',Log::DEBUG);
  86. }
  87. }