Swoole.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Ares<aresrr#qq.com>
  10. * @link http://www.workerman.net/
  11. * @link https://github.com/ares333/Workerman
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman\Events;
  15. use Swoole\Event;
  16. use Swoole\Timer;
  17. use Swoole\Process;
  18. class Swoole implements EventInterface
  19. {
  20. protected $_timer = array();
  21. protected $_fd = array();
  22. // milisecond
  23. public static $signalDispatchInterval = 200;
  24. protected $_hasSignal = false;
  25. /**
  26. *
  27. * {@inheritdoc}
  28. *
  29. * @see \Workerman\Events\EventInterface::add()
  30. */
  31. public function add($fd, $flag, $func, $args = null)
  32. {
  33. if (! isset($args)) {
  34. $args = array();
  35. }
  36. switch ($flag) {
  37. case self::EV_SIGNAL:
  38. $res = pcntl_signal($fd, $func, false);
  39. if (! $this->_hasSignal && $res) {
  40. Timer::tick(static::$signalDispatchInterval,
  41. function () {
  42. pcntl_signal_dispatch();
  43. });
  44. $this->_hasSignal = true;
  45. }
  46. return $res;
  47. case self::EV_TIMER:
  48. case self::EV_TIMER_ONCE:
  49. $method = self::EV_TIMER == $flag ? 'tick' : 'after';
  50. $timer_id = Timer::$method($fd * 1000,
  51. function ($timer_id = null) use ($func, $args) {
  52. call_user_func_array($func, $args);
  53. });
  54. $this->_timer[] = $timer_id;
  55. return $timer_id;
  56. case self::EV_READ:
  57. case self::EV_WRITE:
  58. if ($flag == self::EV_READ) {
  59. $res = Event::add($fd, $func, null, SWOOLE_EVENT_READ);
  60. } else {
  61. $res = Event::add($fd, null, $func, SWOOLE_EVENT_WRITE);
  62. }
  63. if (! in_array((int) $fd, $this->_fd) && $res) {
  64. $this->_fd[] = (int) $fd;
  65. }
  66. return $res;
  67. }
  68. }
  69. /**
  70. *
  71. * {@inheritdoc}
  72. *
  73. * @see \Workerman\Events\EventInterface::del()
  74. */
  75. public function del($fd, $flag)
  76. {
  77. switch ($flag) {
  78. case self::EV_SIGNAL:
  79. return pcntl_signal($fd, SIG_IGN, false);
  80. case self::EV_TIMER:
  81. case self::EV_TIMER_ONCE:
  82. return Timer::clear($fd);
  83. case self::EV_READ:
  84. case self::EV_WRITE:
  85. $key = array_search((int) $fd, $this->_fd);
  86. if (false !== $key) {
  87. $res = Event::del($fd);
  88. if ($res) {
  89. unset($this->_fd[$key]);
  90. }
  91. return $res;
  92. }
  93. }
  94. }
  95. /**
  96. *
  97. * {@inheritdoc}
  98. *
  99. * @see \Workerman\Events\EventInterface::clearAllTimer()
  100. */
  101. public function clearAllTimer()
  102. {
  103. foreach ($this->_timer as $v) {
  104. Timer::clear($v);
  105. }
  106. $this->_timer = array();
  107. }
  108. /**
  109. *
  110. * {@inheritdoc}
  111. *
  112. * @see \Workerman\Events\EventInterface::loop()
  113. */
  114. public function loop()
  115. {
  116. Event::wait();
  117. }
  118. /**
  119. *
  120. * {@inheritdoc}
  121. *
  122. * @see \Workerman\Events\EventInterface::destroy()
  123. */
  124. public function destroy()
  125. {
  126. Event::exit();
  127. }
  128. /**
  129. *
  130. * {@inheritdoc}
  131. *
  132. * @see \Workerman\Events\EventInterface::getTimerCount()
  133. */
  134. public function getTimerCount()
  135. {
  136. return count($this->_timer);
  137. }
  138. }