LibEventLoop.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 LibEventLoop
  17. * @package Workerman\Events\React
  18. */
  19. class LibEventLoop extends \React\EventLoop\LibEventLoop
  20. {
  21. /**
  22. * Event base.
  23. *
  24. * @var event_base resource
  25. */
  26. protected $_eventBase = null;
  27. /**
  28. * All signal Event instances.
  29. *
  30. * @var array
  31. */
  32. protected $_signalEvents = array();
  33. /**
  34. * Construct.
  35. */
  36. public function __construct()
  37. {
  38. parent::__construct();
  39. $class = new \ReflectionClass('\React\EventLoop\LibEventLoop');
  40. $property = $class->getProperty('eventBase');
  41. $property->setAccessible(true);
  42. $this->_eventBase = $property->getValue($this);
  43. }
  44. /**
  45. * Add signal handler.
  46. *
  47. * @param $signal
  48. * @param $callback
  49. * @return bool
  50. */
  51. public function addSignal($signal, $callback)
  52. {
  53. $event = event_new();
  54. $this->_signalEvents[$signal] = $event;
  55. event_set($event, $signal, EV_SIGNAL | EV_PERSIST, $callback);
  56. event_base_set($event, $this->_eventBase);
  57. event_add($event);
  58. }
  59. /**
  60. * Remove signal handler.
  61. *
  62. * @param $signal
  63. */
  64. public function removeSignal($signal)
  65. {
  66. if (isset($this->_signalEvents[$signal])) {
  67. $event = $this->_signalEvents[$signal];
  68. event_del($event);
  69. unset($this->_signalEvents[$signal]);
  70. }
  71. }
  72. }