util.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/7/28
  6. * Time: 下午12:49
  7. */
  8. namespace event;
  9. use Log;
  10. class util
  11. {
  12. static public function listen($host,$port)
  13. {
  14. $listen_fd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  15. if(!socket_set_option($listen_fd, SOL_SOCKET, SO_REUSEADDR, 1)) {
  16. Log::record("socket_set_option 地址重用失败.",Log::DEBUG);
  17. return false;
  18. }
  19. if(!socket_set_nonblock($listen_fd)) {
  20. $err = socket_last_error();
  21. Log::record("socket_set_blocking error : {$err}",Log::DEBUG);
  22. return false;
  23. }
  24. if(!socket_bind($listen_fd, $host, $port)) {
  25. echo "无法绑定socket {$host} : {$port},请退出之前进程.\n";
  26. return false;
  27. }
  28. if(!socket_listen($listen_fd)) {
  29. echo "无法监听socket,请退出之前进程.\n";
  30. return false;
  31. }
  32. return $listen_fd;
  33. }
  34. static function fork_child($callback, $params)
  35. {
  36. if (($pid = pcntl_fork()) === 0)
  37. {
  38. //ob_end_clean(); // Discard the output buffer and close
  39. fclose(STDIN); // Close all of the standard
  40. fclose(STDOUT); // file descriptors as we
  41. fclose(STDERR); // are running as a daemon.
  42. Log::record("pcntl_fork pid = {$pid}",Log::DEBUG);
  43. if(!empty($params)) {
  44. call_user_func_array($callback,$params);
  45. } else {
  46. call_user_func($callback);
  47. }
  48. exit();
  49. }
  50. elseif($pid === -1)
  51. {
  52. Log::record("pid = {$pid},could not fork process",Log::DEBUG);
  53. die('could not fork process');
  54. }
  55. else
  56. {
  57. $ret = pcntl_waitpid($pid,$status,WNOHANG);
  58. Log::record("pcntl_waitpid pid = {$pid}",Log::DEBUG);
  59. if($ret == 0) {
  60. Log::record("spawn-fcgi: successful ret == 0 PID: {$pid}",Log::DEBUG);
  61. }
  62. elseif($ret == -1) {
  63. Log::record("spawn-fcgi: ret == -1 PID: {$pid}",Log::DEBUG);
  64. }
  65. else {
  66. Log::record("spawn-fcgi: ret == 0 child exited PID: {$pid}.",Log::DEBUG);
  67. }
  68. }
  69. }
  70. static function fork_listen($host, $port, $callback, $count)
  71. {
  72. $fd = util::listen($host,$port);
  73. if($fd === false) {
  74. Log::record("cannot open listen socket = {$host}:{$port}",Log::DEBUG);
  75. return false;
  76. }
  77. $params = [$fd];
  78. $count = intval($count);
  79. if($count <= 0) {
  80. return call_user_func_array($callback,$params);
  81. }
  82. else
  83. {
  84. while ($count--) {
  85. util::fork_child($callback,$params);
  86. }
  87. socket_close($fd);
  88. }
  89. }
  90. static function fork_worker($callback, $count)
  91. {
  92. $count = intval($count);
  93. if($count <= 0) {
  94. return call_user_func($callback);
  95. }
  96. else
  97. {
  98. while ($count--) {
  99. util::fork_child($callback,NULL);
  100. }
  101. }
  102. }
  103. }