room_srv.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/search/srv_base.php');
  11. require_once(BASE_ROOT_PATH . '/helper/room/room_server.php');
  12. require_once(BASE_ROOT_PATH . '/helper/room/room_processor.php');
  13. require_once(BASE_ROOT_PATH . '/helper/room/room_handler.php');
  14. require_once(BASE_ROOT_PATH . '/helper/room/proto_type.php');
  15. require_once(BASE_ROOT_PATH . '/helper/room/base_room.php');
  16. require_once(BASE_ROOT_PATH . '/helper/room/chatwo_room.php');
  17. require_once(BASE_ROOT_PATH . '/helper/room/group_room.php');
  18. require_once(BASE_ROOT_PATH . '/helper/room/bargain_room.php');
  19. require_once(BASE_ROOT_PATH . '/helper/room/factory.php');
  20. require_once(BASE_ROOT_PATH . '/helper/room/room_client.php');
  21. require_once(BASE_ROOT_PATH . '/helper/room/bargain_manager.php');
  22. function search_work($sockfd)
  23. {
  24. Base::run_util();
  25. room\room_server::instance()->init(new room\room_processor());
  26. room\room_server::instance()->run_loop($sockfd);
  27. }
  28. function fork_subprocess($count,$listen_fd)
  29. {
  30. if (($pid = pcntl_fork()) === 0)
  31. {
  32. //ob_end_clean(); // Discard the output buffer and close
  33. fclose(STDIN); // Close all of the standard
  34. fclose(STDOUT); // file descriptors as we
  35. fclose(STDERR); // are running as a daemon.
  36. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  37. search_work($listen_fd);
  38. exit();
  39. }
  40. elseif($pid === -1)
  41. {
  42. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  43. die('could not fork');
  44. }
  45. else
  46. {
  47. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  48. $ret = pcntl_waitpid($pid,$status,WNOHANG);
  49. if($ret == 0) {
  50. Log::record("spawn-fcgi: successful ret == 0 PID: {$pid}",Log::DEBUG);
  51. }
  52. elseif($ret == -1) {
  53. Log::record("spawn-fcgi: ret == 0 PID: {$pid}",Log::DEBUG);
  54. }
  55. else {
  56. Log::record("spawn-fcgi: ret == 0 child exited PID: {$pid}.",Log::DEBUG);
  57. }
  58. }
  59. }
  60. function remote_addr()
  61. {
  62. global $config;
  63. $host = $config['room_srv']['host'];
  64. $port = $config['room_srv']['port'];
  65. return "{$host}:{$port}";
  66. }
  67. $listen_fd = stream_socket_server (remote_addr(), $errno, $errstr);
  68. if($listen_fd == false) {
  69. echo "无法创建socket,请退出之前进程.\n";
  70. }
  71. $count = 1;
  72. while ($count-- > 0) {
  73. fork_subprocess($count,$listen_fd);
  74. }
  75. //search_work($listen_fd);