room_srv.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/12/14
  6. * Time: 上午11:53
  7. */
  8. define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
  9. require_once (BASE_ROOT_PATH . '/fooder.php');
  10. require_once(BASE_ROOT_PATH . '/helper/room/room_server.php');
  11. function search_work($sockfd)
  12. {
  13. Base::run_util();
  14. room\server::instance()->init(new search\processor());
  15. room\server::instance()->run_loop($sockfd);
  16. }
  17. function fork_subprocess($count,$listen_fd)
  18. {
  19. if (($pid = pcntl_fork()) === 0)
  20. {
  21. //ob_end_clean(); // Discard the output buffer and close
  22. fclose(STDIN); // Close all of the standard
  23. fclose(STDOUT); // file descriptors as we
  24. fclose(STDERR); // are running as a daemon.
  25. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  26. search_work($listen_fd);
  27. exit();
  28. }
  29. elseif($pid === -1)
  30. {
  31. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  32. die('could not fork');
  33. }
  34. else
  35. {
  36. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  37. $ret = pcntl_waitpid($pid,$status,WNOHANG);
  38. if($ret == 0) {
  39. Log::record("spawn-fcgi: successful ret == 0 PID: {$pid}",Log::DEBUG);
  40. }
  41. elseif($ret == -1) {
  42. Log::record("spawn-fcgi: ret == 0 PID: {$pid}",Log::DEBUG);
  43. }
  44. else {
  45. Log::record("spawn-fcgi: ret == 0 child exited PID: {$pid}.",Log::DEBUG);
  46. }
  47. }
  48. }
  49. function remote_addr()
  50. {
  51. global $config;
  52. $host = $config['room']['host'];
  53. $port = $config['room']['port'];
  54. return "{$host}:{$port}";
  55. }
  56. $listen_fd = stream_socket_server (remote_addr(), $errno, $errstr);
  57. if($listen_fd == false) {
  58. echo "无法创建socket,请退出之前进程.\n";
  59. }
  60. //$count = 1;
  61. //while ($count-- > 0) {
  62. // fork_subprocess($count,$listen_fd);
  63. //}
  64. search_work($listen_fd);