ExtEventLoop.php
4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Workerman\Events\React;
use Workerman\Events\EventInterface;
/**
* Class ExtEventLoop
* @package Workerman\Events\React
*/
class ExtEventLoop extends \React\EventLoop\ExtEventLoop
{
/**
* Event base.
*
* @var EventBase
*/
protected $_eventBase = null;
/**
* All signal Event instances.
*
* @var array
*/
protected $_signalEvents = array();
/**
* @var array
*/
protected $_timerIdMap = array();
/**
* @var int
*/
protected $_timerIdIndex = 0;
/**
* Add event listener to event loop.
*
* @param $fd
* @param $flag
* @param $func
* @param array $args
* @return bool
*/
public function add($fd, $flag, $func, $args = array())
{
$args = (array)$args;
switch ($flag) {
case EventInterface::EV_READ:
return $this->addReadStream($fd, $func);
case EventInterface::EV_WRITE:
return $this->addWriteStream($fd, $func);
case EventInterface::EV_SIGNAL:
return $this->addSignal($fd, $func);
case EventInterface::EV_TIMER:
$timer_id = ++$this->_timerIdIndex;
$timer_obj = $this->addPeriodicTimer($fd, function() use ($func, $args) {
call_user_func_array($func, $args);
});
$this->_timerIdMap[$timer_id] = $timer_obj;
return $timer_id;
case EventInterface::EV_TIMER_ONCE:
$timer_id = ++$this->_timerIdIndex;
$timer_obj = $this->addTimer($fd, function() use ($func, $args, $timer_id) {
unset($this->_timerIdMap[$timer_id]);
call_user_func_array($func, $args);
});
$this->_timerIdMap[$timer_id] = $timer_obj;
return $timer_id;
}
return false;
}
/**
* Remove event listener from event loop.
*
* @param mixed $fd
* @param int $flag
* @return bool
*/
public function del($fd, $flag)
{
switch ($flag) {
case EventInterface::EV_READ:
return $this->removeReadStream($fd);
case EventInterface::EV_WRITE:
return $this->removeWriteStream($fd);
case EventInterface::EV_SIGNAL:
return $this->removeSignal($fd);
case EventInterface::EV_TIMER:
case EventInterface::EV_TIMER_ONCE;
if (isset($this->_timerIdMap[$fd])){
$timer_obj = $this->_timerIdMap[$fd];
unset($this->_timerIdMap[$fd]);
$this->cancelTimer($timer_obj);
return true;
}
}
return false;
}
/**
* Main loop.
*
* @return void
*/
public function loop()
{
$this->run();
}
/**
* Construct
*/
public function __construct()
{
parent::__construct();
$class = new \ReflectionClass('\React\EventLoop\ExtEventLoop');
$property = $class->getProperty('eventBase');
$property->setAccessible(true);
$this->_eventBase = $property->getValue($this);
}
/**
* Add signal handler.
*
* @param $signal
* @param $callback
* @return bool
*/
public function addSignal($signal, $callback)
{
$event = \Event::signal($this->_eventBase, $signal, $callback);
if (!$event||!$event->add()) {
return false;
}
$this->_signalEvents[$signal] = $event;
}
/**
* Remove signal handler.
*
* @param $signal
*/
public function removeSignal($signal)
{
if (isset($this->_signalEvents[$signal])) {
$this->_signalEvents[$signal]->del();
unset($this->_signalEvents[$signal]);
}
}
/**
* Destroy loop.
*
* @return void
*/
public function destroy()
{
foreach ($this->_signalEvents as $event) {
$event->del();
}
}
/**
* Get timer count.
*
* @return integer
*/
public function getTimerCount()
{
return count($this->_timerIdMap);
}
}