@@ -21,12 +21,12 @@ public function __construct()
21
21
22
22
public function addReadStream ($ stream , $ listener )
23
23
{
24
- $ this ->readEvents [( int ) $ stream ] = $ this -> addStream ($ stream , $ listener , \libev \IOEvent::READ );
24
+ $ this ->addStream ($ stream , $ listener , \libev \IOEvent::READ );
25
25
}
26
26
27
27
public function addWriteStream ($ stream , $ listener )
28
28
{
29
- $ this ->writeEvents [( int ) $ stream ] = $ this -> addStream ($ stream , $ listener , \libev \IOEvent::WRITE );
29
+ $ this ->addStream ($ stream , $ listener , \libev \IOEvent::WRITE );
30
30
}
31
31
32
32
public function removeReadStream ($ stream )
@@ -54,18 +54,29 @@ public function removeStream($stream)
54
54
55
55
private function addStream ($ stream , $ listener , $ flags )
56
56
{
57
- $ listener = $ this ->wrapStreamListener ($ stream , $ listener );
57
+ $ listener = $ this ->wrapStreamListener ($ stream , $ listener, $ flags );
58
58
$ event = new \libev \IOEvent ($ listener , $ stream , $ flags );
59
59
$ this ->loop ->add ($ event );
60
60
61
- return $ event ;
61
+ if (($ flags & \libev \IOEvent::READ ) === $ flags ) {
62
+ $ this ->readEvents [(int )$ stream ] = $ event ;
63
+ } elseif (($ flags & \libev \IOEvent::WRITE ) === $ flags ) {
64
+ $ this ->writeEvents [(int )$ stream ] = $ event ;
65
+ }
62
66
}
63
67
64
- private function wrapStreamListener ($ stream , $ listener )
68
+ private function wrapStreamListener ($ stream , $ listener, $ flags )
65
69
{
66
- return function ($ event ) use ($ stream , $ listener ) {
70
+ if (($ flags & \libev \IOEvent::READ ) === $ flags ) {
71
+ $ removeCallback = array ($ this , 'removeReadStream ' );
72
+ } elseif (($ flags & \libev \IOEvent::WRITE ) === $ flags ) {
73
+ $ removeCallback = array ($ this , 'removeWriteStream ' );
74
+ }
75
+
76
+ return function ($ event ) use ($ stream , $ listener , $ removeCallback ) {
67
77
if (feof ($ stream )) {
68
- $ event ->stop ();
78
+ call_user_func ($ removeCallback , $ stream );
79
+
69
80
return ;
70
81
}
71
82
0 commit comments