@@ -18,42 +18,58 @@ class LibEvLoop implements LoopInterface
18
18
{
19
19
private $ loop ;
20
20
private $ nextTickQueue ;
21
- private $ timers ;
22
- private $ readEvents = array () ;
23
- private $ writeEvents = array () ;
21
+ private $ timerEvents ;
22
+ private $ readEvents = [] ;
23
+ private $ writeEvents = [] ;
24
24
private $ running ;
25
25
26
26
public function __construct ()
27
27
{
28
28
$ this ->loop = new EventLoop ();
29
29
$ this ->nextTickQueue = new NextTickQueue ($ this );
30
- $ this ->timers = new SplObjectStorage ();
30
+ $ this ->timerEvents = new SplObjectStorage ();
31
31
}
32
32
33
33
/**
34
34
* {@inheritdoc}
35
35
*/
36
36
public function addReadStream ($ stream , callable $ listener )
37
37
{
38
- $ this ->addStream ($ stream , $ listener , IOEvent::READ );
38
+ $ callback = function () use ($ stream , $ listener ) {
39
+ call_user_func ($ listener , $ stream , $ this );
40
+ };
41
+
42
+ $ event = new IOEvent ($ callback , $ stream , IOEvent::READ );
43
+ $ this ->loop ->add ($ event );
44
+
45
+ $ this ->readEvents [(int ) $ stream ] = $ event ;
39
46
}
40
47
41
48
/**
42
49
* {@inheritdoc}
43
50
*/
44
51
public function addWriteStream ($ stream , callable $ listener )
45
52
{
46
- $ this ->addStream ($ stream , $ listener , IOEvent::WRITE );
53
+ $ callback = function () use ($ stream , $ listener ) {
54
+ call_user_func ($ listener , $ stream , $ this );
55
+ };
56
+
57
+ $ event = new IOEvent ($ callback , $ stream , IOEvent::WRITE );
58
+ $ this ->loop ->add ($ event );
59
+
60
+ $ this ->writeEvents [(int ) $ stream ] = $ event ;
47
61
}
48
62
49
63
/**
50
64
* {@inheritdoc}
51
65
*/
52
66
public function removeReadStream ($ stream )
53
67
{
54
- if (isset ($ this ->readEvents [(int ) $ stream ])) {
55
- $ this ->readEvents [(int ) $ stream ]->stop ();
56
- unset($ this ->readEvents [(int ) $ stream ]);
68
+ $ key = (int ) $ stream ;
69
+
70
+ if (isset ($ this ->readEvents [$ key ])) {
71
+ $ this ->readEvents [$ key ]->stop ();
72
+ unset($ this ->readEvents [$ key ]);
57
73
}
58
74
}
59
75
@@ -62,9 +78,11 @@ public function removeReadStream($stream)
62
78
*/
63
79
public function removeWriteStream ($ stream )
64
80
{
65
- if (isset ($ this ->writeEvents [(int ) $ stream ])) {
66
- $ this ->writeEvents [(int ) $ stream ]->stop ();
67
- unset($ this ->writeEvents [(int ) $ stream ]);
81
+ $ key = (int ) $ stream ;
82
+
83
+ if (isset ($ this ->writeEvents [$ key ])) {
84
+ $ this ->writeEvents [$ key ]->stop ();
85
+ unset($ this ->writeEvents [$ key ]);
68
86
}
69
87
}
70
88
@@ -83,7 +101,18 @@ public function removeStream($stream)
83
101
public function addTimer ($ interval , callable $ callback )
84
102
{
85
103
$ timer = new Timer ($ this , $ interval , $ callback , false );
86
- $ this ->setupTimer ($ timer );
104
+
105
+ $ callback = function () use ($ timer ) {
106
+ call_user_func ($ timer ->getCallback (), $ timer );
107
+
108
+ if ($ this ->isTimerActive ($ timer )) {
109
+ $ this ->cancelTimer ($ timer );
110
+ }
111
+ };
112
+
113
+ $ event = new TimerEvent ($ callback , $ timer ->getInterval ());
114
+ $ this ->timerEvents ->attach ($ timer , $ event );
115
+ $ this ->loop ->add ($ event );
87
116
88
117
return $ timer ;
89
118
}
@@ -94,7 +123,14 @@ public function addTimer($interval, callable $callback)
94
123
public function addPeriodicTimer ($ interval , callable $ callback )
95
124
{
96
125
$ timer = new Timer ($ this , $ interval , $ callback , true );
97
- $ this ->setupTimer ($ timer );
126
+
127
+ $ callback = function () use ($ timer ) {
128
+ call_user_func ($ timer ->getCallback (), $ timer );
129
+ };
130
+
131
+ $ event = new TimerEvent ($ callback , $ interval , $ interval );
132
+ $ this ->timerEvents ->attach ($ timer , $ event );
133
+ $ this ->loop ->add ($ event );
98
134
99
135
return $ timer ;
100
136
}
@@ -104,9 +140,9 @@ public function addPeriodicTimer($interval, callable $callback)
104
140
*/
105
141
public function cancelTimer (TimerInterface $ timer )
106
142
{
107
- if (isset ($ this ->timers [$ timer ])) {
108
- $ this ->loop ->remove ($ this ->timers [$ timer ]);
109
- $ this ->timers ->detach ($ timer );
143
+ if (isset ($ this ->timerEvents [$ timer ])) {
144
+ $ this ->loop ->remove ($ this ->timerEvents [$ timer ]);
145
+ $ this ->timerEvents ->detach ($ timer );
110
146
}
111
147
}
112
148
@@ -115,7 +151,7 @@ public function cancelTimer(TimerInterface $timer)
115
151
*/
116
152
public function isTimerActive (TimerInterface $ timer )
117
153
{
118
- return $ this ->timers ->contains ($ timer );
154
+ return $ this ->timerEvents ->contains ($ timer );
119
155
}
120
156
121
157
/**
@@ -147,11 +183,7 @@ public function run()
147
183
148
184
$ this ->nextTickQueue ->tick ();
149
185
150
- if (
151
- !$ this ->readEvents
152
- && !$ this ->writeEvents
153
- && !$ this ->timers ->count ()
154
- ) {
186
+ if (!$ this ->readEvents && !$ this ->writeEvents && !$ this ->timerEvents ->count ()) {
155
187
break ;
156
188
}
157
189
@@ -166,55 +198,4 @@ public function stop()
166
198
{
167
199
$ this ->running = false ;
168
200
}
169
-
170
- private function addStream ($ stream , $ listener , $ flags )
171
- {
172
- $ listener = $ this ->wrapStreamListener ($ stream , $ listener , $ flags );
173
- $ event = new IOEvent ($ listener , $ stream , $ flags );
174
- $ this ->loop ->add ($ event );
175
-
176
- if (($ flags & IOEvent::READ ) === $ flags ) {
177
- $ this ->readEvents [(int ) $ stream ] = $ event ;
178
- } elseif (($ flags & IOEvent::WRITE ) === $ flags ) {
179
- $ this ->writeEvents [(int ) $ stream ] = $ event ;
180
- }
181
- }
182
-
183
- private function wrapStreamListener ($ stream , $ listener , $ flags )
184
- {
185
- if (($ flags & IOEvent::READ ) === $ flags ) {
186
- $ removeCallback = array ($ this , 'removeReadStream ' );
187
- } elseif (($ flags & IOEvent::WRITE ) === $ flags ) {
188
- $ removeCallback = array ($ this , 'removeWriteStream ' );
189
- }
190
-
191
- return function ($ event ) use ($ stream , $ listener , $ removeCallback ) {
192
- call_user_func ($ listener , $ stream );
193
- };
194
- }
195
-
196
- private function setupTimer (TimerInterface $ timer )
197
- {
198
- $ dummyCallback = function () {};
199
- $ interval = $ timer ->getInterval ();
200
-
201
- if ($ timer ->isPeriodic ()) {
202
- $ libevTimer = new TimerEvent ($ dummyCallback , $ interval , $ interval );
203
- } else {
204
- $ libevTimer = new TimerEvent ($ dummyCallback , $ interval );
205
- }
206
-
207
- $ libevTimer ->setCallback (function () use ($ timer ) {
208
- call_user_func ($ timer ->getCallback (), $ timer );
209
-
210
- if (!$ timer ->isPeriodic ()) {
211
- $ timer ->cancel ();
212
- }
213
- });
214
-
215
- $ this ->timers ->attach ($ timer , $ libevTimer );
216
- $ this ->loop ->add ($ libevTimer );
217
-
218
- return $ timer ;
219
- }
220
201
}
0 commit comments