StreamSelectLoop.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman\Events\React;
  15. /**
  16. * Class StreamSelectLoop
  17. * @package Workerman\Events\React
  18. */
  19. class StreamSelectLoop extends \React\EventLoop\StreamSelectLoop
  20. {
  21. /**
  22. * Add signal handler.
  23. *
  24. * @param $signal
  25. * @param $callback
  26. * @return bool
  27. */
  28. public function addSignal($signal, $callback)
  29. {
  30. pcntl_signal($signal, $callback);
  31. }
  32. /**
  33. * Remove signal handler.
  34. *
  35. * @param $signal
  36. */
  37. public function removeSignal($signal)
  38. {
  39. pcntl_signal($signal, SIG_IGN);
  40. }
  41. /**
  42. * Emulate a stream_select() implementation that does not break when passed
  43. * empty stream arrays.
  44. *
  45. * @param array &$read An array of read streams to select upon.
  46. * @param array &$write An array of write streams to select upon.
  47. * @param integer|null $timeout Activity timeout in microseconds, or null to wait forever.
  48. *
  49. * @return integer|false The total number of streams that are ready for read/write.
  50. * Can return false if stream_select() is interrupted by a signal.
  51. */
  52. protected function streamSelect(array &$read, array &$write, $timeout)
  53. {
  54. if ($read || $write) {
  55. $except = null;
  56. // Calls signal handlers for pending signals
  57. pcntl_signal_dispatch();
  58. // suppress warnings that occur, when stream_select is interrupted by a signal
  59. return @stream_select($read, $write, $except, $timeout === null ? null : 0, $timeout);
  60. }
  61. // Calls signal handlers for pending signals
  62. pcntl_signal_dispatch();
  63. $timeout && usleep($timeout);
  64. return 0;
  65. }
  66. }