util.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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},ret={$ret}",Log::DEBUG);
  59. if($ret == 0) {
  60. Log::record("fork_child: successful ret == 0 PID: {$pid}",Log::DEBUG);
  61. return $pid;
  62. }
  63. elseif($ret == -1) {
  64. Log::record("fork_child: ret == -1 PID: {$pid}",Log::DEBUG);
  65. }
  66. else {
  67. Log::record("fork_child: ret == 0 child exited PID: {$pid}.",Log::DEBUG);
  68. }
  69. }
  70. }
  71. static function fork_listen($fd, $callback, $count)
  72. {
  73. $params = [$fd];
  74. $count = intval($count);
  75. if($count <= 0) {
  76. return call_user_func_array($callback,$params);
  77. }
  78. else
  79. {
  80. $childs = [];
  81. while ($count--) {
  82. $childs[] = util::fork_child($callback,$params);
  83. }
  84. }
  85. return $childs;
  86. }
  87. static function fork_listenex($fd, $callback, $count)
  88. {
  89. self::fork_listen($fd,$callback,$count);
  90. while ($count) {
  91. Log::record("waitting child quit......",Log::DEBUG);
  92. $ret = pcntl_wait($status);
  93. Log::record("pcntl_waitpid ret={$ret}",Log::DEBUG);
  94. if($ret > 0)
  95. {
  96. if(pcntl_wifexited($status)) {
  97. $ret = pcntl_wexitstatus($status);
  98. Log::record("child normal quit ret={$ret} status = {$status}",Log::DEBUG);
  99. } else {
  100. $ret = pcntl_wexitstatus($status);
  101. Log::record("child abnormal quit ret={$ret} status = {$status}",Log::DEBUG);
  102. }
  103. self::fork_listen($fd,$callback,1);
  104. }
  105. else {
  106. //子进程已经退出干净了.
  107. break;
  108. }
  109. }
  110. }
  111. static function join()
  112. {
  113. do {
  114. Log::record("waitting child quit......",Log::DEBUG);
  115. $ret = pcntl_wait($status);
  116. Log::record("pcntl_waitpid ret={$ret}",Log::DEBUG);
  117. if($ret > 0)
  118. {
  119. if(pcntl_wifexited($status)) {
  120. $ret = pcntl_wexitstatus($status);
  121. Log::record("child normal quit ret={$ret} status = {$status}",Log::DEBUG);
  122. } else {
  123. $ret = pcntl_wexitstatus($status);
  124. Log::record("child abnormal quit ret={$ret} status = {$status}",Log::DEBUG);
  125. }
  126. }
  127. else {
  128. //子进程已经退出干净了.
  129. break;
  130. }
  131. }
  132. while(true);
  133. }
  134. static function fork_worker($callback, $count)
  135. {
  136. $count = intval($count);
  137. if($count <= 0) {
  138. return call_user_func($callback);
  139. }
  140. else
  141. {
  142. while ($count--) {
  143. util::fork_child($callback,NULL);
  144. }
  145. }
  146. }
  147. static function fork_workerex($callback, $count)
  148. {
  149. self::fork_worker($callback,$count);
  150. while ($count) {
  151. Log::record("waitting child quit......",Log::DEBUG);
  152. $ret = pcntl_wait($status);
  153. Log::record("pcntl_waitpid ret={$ret}",Log::DEBUG);
  154. if($ret > 0)
  155. {
  156. if(pcntl_wifexited($status)) {
  157. $ret = pcntl_wexitstatus($status);
  158. Log::record("child normal quit ret={$ret} status = {$status}",Log::DEBUG);
  159. } else {
  160. $ret = pcntl_wexitstatus($status);
  161. Log::record("child abnormal quit ret={$ret} status = {$status}",Log::DEBUG);
  162. }
  163. self::fork_worker($callback,1);
  164. }
  165. else {
  166. //子进程已经退出干净了.
  167. break;
  168. }
  169. }
  170. }
  171. }