8
8
9
9
class Buffer extends EventEmitter
10
10
{
11
- private $ loop ;
12
- private $ socket ;
11
+ protected $ loop ;
12
+ protected $ socket ;
13
+
13
14
private $ listening = false ;
14
15
private $ outgoing = array ();
15
16
private $ writable = true ;
@@ -29,32 +30,27 @@ public function send($data, $remoteAddress = null)
29
30
$ this ->outgoing []= array ($ data , $ remoteAddress );
30
31
31
32
if (!$ this ->listening ) {
32
- $ this ->loop -> addWriteStream ( $ this -> socket , array ( $ this , ' handleWrite ' ) );
33
+ $ this ->handleResume ( );
33
34
$ this ->listening = true ;
34
35
}
35
36
}
36
37
37
- public function handleWrite ()
38
+ public function onWritable ()
38
39
{
39
40
list ($ data , $ remoteAddress ) = array_shift ($ this ->outgoing );
40
41
41
- if ($ remoteAddress === null ) {
42
- // do not use fwrite() as it obeys the stream buffer size and
43
- // packets are not to be split at 8kb
44
- $ ret = @stream_socket_sendto ($ this ->socket , $ data );
45
- } else {
46
- $ ret = @stream_socket_sendto ($ this ->socket , $ data , 0 , $ remoteAddress );
42
+ try {
43
+ $ this ->handleWrite ($ data , $ remoteAddress );
47
44
}
48
-
49
- if ($ ret < 0 ) {
50
- $ error = error_get_last ();
51
- $ message = 'Unable to send packet: ' . trim ($ error ['message ' ]);
52
- $ this ->emit ('error ' , array (new Exception ($ message )));
45
+ catch (Exception $ e ) {
46
+ $ this ->emit ('error ' , array ($ e , $ this ));
53
47
}
54
48
55
49
if (!$ this ->outgoing ) {
56
- $ this ->loop ->removeWriteStream ($ this ->socket );
57
- $ this ->listening = false ;
50
+ if ($ this ->listening ) {
51
+ $ this ->handlePause ();
52
+ $ this ->listening = false ;
53
+ }
58
54
59
55
if (!$ this ->writable ) {
60
56
$ this ->close ();
@@ -71,7 +67,7 @@ public function close()
71
67
$ this ->emit ('close ' , array ($ this ));
72
68
73
69
if ($ this ->listening ) {
74
- $ this ->loop -> removeWriteStream ( $ this -> socket );
70
+ $ this ->handlePause ( );
75
71
$ this ->listening = false ;
76
72
}
77
73
@@ -89,8 +85,34 @@ public function end()
89
85
90
86
$ this ->writable = false ;
91
87
92
- if (!$ this ->listening ) {
88
+ if (!$ this ->outgoing ) {
93
89
$ this ->close ();
94
90
}
95
91
}
92
+
93
+ protected function handlePause ()
94
+ {
95
+ $ this ->loop ->removeWriteStream ($ this ->socket );
96
+ }
97
+
98
+ protected function handleResume ()
99
+ {
100
+ $ this ->loop ->addWriteStream ($ this ->socket , array ($ this , 'onWritable ' ));
101
+ }
102
+
103
+ protected function handleWrite ($ data , $ remoteAddress )
104
+ {
105
+ if ($ remoteAddress === null ) {
106
+ // do not use fwrite() as it obeys the stream buffer size and
107
+ // packets are not to be split at 8kb
108
+ $ ret = @stream_socket_sendto ($ this ->socket , $ data );
109
+ } else {
110
+ $ ret = @stream_socket_sendto ($ this ->socket , $ data , 0 , $ remoteAddress );
111
+ }
112
+
113
+ if ($ ret < 0 ) {
114
+ $ error = error_get_last ();
115
+ throw new Exception ('Unable to send packet: ' . trim ($ error ['message ' ]));
116
+ }
117
+ }
96
118
}
0 commit comments