BaseServer.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }
  33. public function handle_error($level, $message, $file, $line)
  34. {
  35. if($level == E_NOTICE) return;
  36. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  37. $backtrace = debug_backtrace();
  38. foreach ($backtrace as $item) {
  39. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  40. }
  41. Log::record($trace,Log::ERR);
  42. }
  43. protected function preLooper()
  44. {
  45. set_error_handler([$this, 'handle_error']);
  46. }
  47. abstract function handle_req($file);
  48. public function run_looper()
  49. {
  50. Log::record(__FUNCTION__,Log::DEBUG);
  51. $this->preLooper();
  52. Log::record('Waiting......',Log::DEBUG);
  53. $i = 0;
  54. fcgi_init();
  55. while(($ret = fcgi_accept()) >= 0)
  56. {
  57. $mem = memory_get_usage();
  58. Log::record("index = {$i} memory={$mem} ",Log::DEBUG);
  59. ++$i;
  60. $start = microtime(true);
  61. ob_start();
  62. $this->clear_global();
  63. perfor_clear();
  64. perfor_start();
  65. Log::start_sql_log();
  66. init_request();
  67. init_cookie($_SERVER['HTTP_COOKIE']);
  68. http_header::instance()->start();
  69. $file = request_helper::script_file();
  70. $this->handle_req($file);
  71. fcgi_headers_sent();
  72. $contents = ob_get_clean();
  73. fcgi_echo($contents);
  74. Log::end_sql_log();
  75. $perlog = perfor_log();
  76. $mem = memory_get_usage();
  77. Log::record("memory={$mem} perlog = {$perlog}" ,Log::DEBUG);
  78. $ret = fcgi_fini();
  79. $mem = memory_get_usage();
  80. $msg = sprintf("memory={$mem} isCgi = {$ret} request time=%.6f\r\n\r\n",microtime(true) - $start);
  81. Log::record($msg ,Log::DEBUG);
  82. }
  83. fcgi_fini();
  84. Log::record('Waiting quit......',Log::DEBUG);
  85. }
  86. }