1 | package AnyEvent::Handle; |
1 | package AnyEvent::Handle; |
2 | |
2 | |
3 | no warnings; |
3 | no warnings; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use AnyEvent; |
6 | use AnyEvent (); |
7 | use IO::Handle; |
7 | use AnyEvent::Util (); |
|
|
8 | use Scalar::Util (); |
|
|
9 | use Carp (); |
|
|
10 | use Fcntl (); |
8 | use Errno qw/EAGAIN EINTR/; |
11 | use Errno qw/EAGAIN EINTR/; |
9 | |
12 | |
10 | =head1 NAME |
13 | =head1 NAME |
11 | |
14 | |
12 | AnyEvent::Handle - non-blocking I/O on filehandles via AnyEvent |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
13 | |
16 | |
14 | =head1 VERSION |
17 | This module is experimental. |
15 | |
18 | |
16 | Version 0.01 |
|
|
17 | |
|
|
18 | =cut |
19 | =cut |
19 | |
20 | |
20 | our $VERSION = '0.01'; |
21 | our $VERSION = '0.04'; |
21 | |
22 | |
22 | =head1 SYNOPSIS |
23 | =head1 SYNOPSIS |
23 | |
24 | |
24 | use AnyEvent; |
25 | use AnyEvent; |
25 | use AnyEvent::Handle; |
26 | use AnyEvent::Handle; |
26 | |
27 | |
27 | my $cv = AnyEvent->condvar; |
28 | my $cv = AnyEvent->condvar; |
28 | |
29 | |
29 | my $ae_fh = AnyEvent::Handle->new (fh => \*STDIN); |
30 | my $ae_fh = AnyEvent::Handle->new (fh => \*STDIN); |
30 | |
31 | |
31 | $ae_fh->on_eof (sub { $cv->broadcast }); |
32 | #TODO |
32 | |
|
|
33 | $ae_fh->readlines (sub { |
|
|
34 | my ($ae_fh, @lines) = @_; |
|
|
35 | for (@lines) { |
|
|
36 | chomp; |
|
|
37 | print "Line: $_"; |
|
|
38 | } |
|
|
39 | }); |
|
|
40 | |
33 | |
41 | # or use the constructor to pass the callback: |
34 | # or use the constructor to pass the callback: |
42 | |
35 | |
43 | my $ae_fh2 = |
36 | my $ae_fh2 = |
44 | AnyEvent::Handle->new ( |
37 | AnyEvent::Handle->new ( |
45 | fh => \*STDIN, |
38 | fh => \*STDIN, |
46 | on_eof => sub { |
39 | on_eof => sub { |
47 | $cv->broadcast; |
40 | $cv->broadcast; |
48 | }, |
41 | }, |
49 | on_readline => sub { |
42 | #TODO |
50 | my ($ae_fh, @lines) = @_; |
43 | ); |
51 | for (@lines) { |
44 | |
52 | chomp; |
45 | $cv->wait; |
53 | print "Line: $_"; |
46 | |
|
|
47 | =head1 DESCRIPTION |
|
|
48 | |
|
|
49 | This module is a helper module to make it easier to do event-based I/O on |
|
|
50 | filehandles. For utility functions for doing non-blocking connects and accepts |
|
|
51 | on sockets see L<AnyEvent::Util>. |
|
|
52 | |
|
|
53 | In the following, when the documentation refers to of "bytes" then this |
|
|
54 | means characters. As sysread and syswrite are used for all I/O, their |
|
|
55 | treatment of characters applies to this module as well. |
|
|
56 | |
|
|
57 | All callbacks will be invoked with the handle object as their first |
|
|
58 | argument. |
|
|
59 | |
|
|
60 | =head1 METHODS |
|
|
61 | |
|
|
62 | =over 4 |
|
|
63 | |
|
|
64 | =item B<new (%args)> |
|
|
65 | |
|
|
66 | The constructor supports these arguments (all as key => value pairs). |
|
|
67 | |
|
|
68 | =over 4 |
|
|
69 | |
|
|
70 | =item fh => $filehandle [MANDATORY] |
|
|
71 | |
|
|
72 | The filehandle this L<AnyEvent::Handle> object will operate on. |
|
|
73 | |
|
|
74 | NOTE: The filehandle will be set to non-blocking (using |
|
|
75 | AnyEvent::Util::fh_nonblocking). |
|
|
76 | |
|
|
77 | =item on_eof => $cb->($self) |
|
|
78 | |
|
|
79 | Set the callback to be called on EOF. |
|
|
80 | |
|
|
81 | While not mandatory, it is highly recommended to set an eof callback, |
|
|
82 | otherwise you might end up with a closed socket while you are still |
|
|
83 | waiting for data. |
|
|
84 | |
|
|
85 | =item on_error => $cb->($self) |
|
|
86 | |
|
|
87 | This is the fatal error callback, that is called when, well, a fatal error |
|
|
88 | occurs, such as not being able to resolve the hostname, failure to connect |
|
|
89 | or a read error. |
|
|
90 | |
|
|
91 | The object will not be in a usable state when this callback has been |
|
|
92 | called. |
|
|
93 | |
|
|
94 | On callback entrance, the value of C<$!> contains the operating system |
|
|
95 | error (or C<ENOSPC> or C<EPIPE>). |
|
|
96 | |
|
|
97 | While not mandatory, it is I<highly> recommended to set this callback, as |
|
|
98 | you will not be notified of errors otherwise. The default simply calls |
|
|
99 | die. |
|
|
100 | |
|
|
101 | =item on_read => $cb->($self) |
|
|
102 | |
|
|
103 | This sets the default read callback, which is called when data arrives |
|
|
104 | and no read request is in the queue. |
|
|
105 | |
|
|
106 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
|
|
107 | method or access the C<$self->{rbuf}> member directly. |
|
|
108 | |
|
|
109 | When an EOF condition is detected then AnyEvent::Handle will first try to |
|
|
110 | feed all the remaining data to the queued callbacks and C<on_read> before |
|
|
111 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
|
|
112 | error will be raised (with C<$!> set to C<EPIPE>). |
|
|
113 | |
|
|
114 | =item on_drain => $cb->() |
|
|
115 | |
|
|
116 | This sets the callback that is called when the write buffer becomes empty |
|
|
117 | (or when the callback is set and the buffer is empty already). |
|
|
118 | |
|
|
119 | To append to the write buffer, use the C<< ->push_write >> method. |
|
|
120 | |
|
|
121 | =item rbuf_max => <bytes> |
|
|
122 | |
|
|
123 | If defined, then a fatal error will be raised (with C<$!> set to C<ENOSPC>) |
|
|
124 | when the read buffer ever (strictly) exceeds this size. This is useful to |
|
|
125 | avoid denial-of-service attacks. |
|
|
126 | |
|
|
127 | For example, a server accepting connections from untrusted sources should |
|
|
128 | be configured to accept only so-and-so much data that it cannot act on |
|
|
129 | (for example, when expecting a line, an attacker could send an unlimited |
|
|
130 | amount of data without a callback ever being called as long as the line |
|
|
131 | isn't finished). |
|
|
132 | |
|
|
133 | =item read_size => <bytes> |
|
|
134 | |
|
|
135 | The default read block size (the amount of bytes this module will try to read |
|
|
136 | on each [loop iteration). Default: C<4096>. |
|
|
137 | |
|
|
138 | =item low_water_mark => <bytes> |
|
|
139 | |
|
|
140 | Sets the amount of bytes (default: C<0>) that make up an "empty" write |
|
|
141 | buffer: If the write reaches this size or gets even samller it is |
|
|
142 | considered empty. |
|
|
143 | |
|
|
144 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
|
|
145 | |
|
|
146 | When this parameter is given, it enables TLS (SSL) mode, that means it |
|
|
147 | will start making tls handshake and will transparently encrypt/decrypt |
|
|
148 | data. |
|
|
149 | |
|
|
150 | For the TLS server side, use C<accept>, and for the TLS client side of a |
|
|
151 | connection, use C<connect> mode. |
|
|
152 | |
|
|
153 | You can also provide your own TLS connection object, but you have |
|
|
154 | to make sure that you call either C<Net::SSLeay::set_connect_state> |
|
|
155 | or C<Net::SSLeay::set_accept_state> on it before you pass it to |
|
|
156 | AnyEvent::Handle. |
|
|
157 | |
|
|
158 | =item tls_ctx => $ssl_ctx |
|
|
159 | |
|
|
160 | Use the given Net::SSLeay::CTX object to create the new TLS connection |
|
|
161 | (unless a connection object was specified directly). If this parameter is |
|
|
162 | missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. |
|
|
163 | |
|
|
164 | =back |
|
|
165 | |
|
|
166 | =cut |
|
|
167 | |
|
|
168 | sub new { |
|
|
169 | my $class = shift; |
|
|
170 | |
|
|
171 | my $self = bless { @_ }, $class; |
|
|
172 | |
|
|
173 | $self->{fh} or Carp::croak "mandatory argument fh is missing"; |
|
|
174 | |
|
|
175 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
|
|
176 | |
|
|
177 | if ($self->{tls}) { |
|
|
178 | require Net::SSLeay; |
|
|
179 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); |
|
|
180 | } |
|
|
181 | |
|
|
182 | $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; |
|
|
183 | $self->on_error (delete $self->{on_error}) if $self->{on_error}; |
|
|
184 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
|
|
185 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
|
|
186 | |
|
|
187 | $self->start_read; |
|
|
188 | |
|
|
189 | $self |
|
|
190 | } |
|
|
191 | |
|
|
192 | sub _shutdown { |
|
|
193 | my ($self) = @_; |
|
|
194 | |
|
|
195 | delete $self->{rw}; |
|
|
196 | delete $self->{ww}; |
|
|
197 | delete $self->{fh}; |
|
|
198 | } |
|
|
199 | |
|
|
200 | sub error { |
|
|
201 | my ($self) = @_; |
|
|
202 | |
|
|
203 | { |
|
|
204 | local $!; |
|
|
205 | $self->_shutdown; |
|
|
206 | } |
|
|
207 | |
|
|
208 | if ($self->{on_error}) { |
|
|
209 | $self->{on_error}($self); |
|
|
210 | } else { |
|
|
211 | die "AnyEvent::Handle uncaught fatal error: $!"; |
|
|
212 | } |
|
|
213 | } |
|
|
214 | |
|
|
215 | =item $fh = $handle->fh |
|
|
216 | |
|
|
217 | This method returns the file handle of the L<AnyEvent::Handle> object. |
|
|
218 | |
|
|
219 | =cut |
|
|
220 | |
|
|
221 | sub fh { $_[0]->{fh} } |
|
|
222 | |
|
|
223 | =item $handle->on_error ($cb) |
|
|
224 | |
|
|
225 | Replace the current C<on_error> callback (see the C<on_error> constructor argument). |
|
|
226 | |
|
|
227 | =cut |
|
|
228 | |
|
|
229 | sub on_error { |
|
|
230 | $_[0]{on_error} = $_[1]; |
|
|
231 | } |
|
|
232 | |
|
|
233 | =item $handle->on_eof ($cb) |
|
|
234 | |
|
|
235 | Replace the current C<on_eof> callback (see the C<on_eof> constructor argument). |
|
|
236 | |
|
|
237 | =cut |
|
|
238 | |
|
|
239 | sub on_eof { |
|
|
240 | $_[0]{on_eof} = $_[1]; |
|
|
241 | } |
|
|
242 | |
|
|
243 | ############################################################################# |
|
|
244 | |
|
|
245 | =back |
|
|
246 | |
|
|
247 | =head2 WRITE QUEUE |
|
|
248 | |
|
|
249 | AnyEvent::Handle manages two queues per handle, one for writing and one |
|
|
250 | for reading. |
|
|
251 | |
|
|
252 | The write queue is very simple: you can add data to its end, and |
|
|
253 | AnyEvent::Handle will automatically try to get rid of it for you. |
|
|
254 | |
|
|
255 | When data could be written and the write buffer is shorter then the low |
|
|
256 | water mark, the C<on_drain> callback will be invoked. |
|
|
257 | |
|
|
258 | =over 4 |
|
|
259 | |
|
|
260 | =item $handle->on_drain ($cb) |
|
|
261 | |
|
|
262 | Sets the C<on_drain> callback or clears it (see the description of |
|
|
263 | C<on_drain> in the constructor). |
|
|
264 | |
|
|
265 | =cut |
|
|
266 | |
|
|
267 | sub on_drain { |
|
|
268 | my ($self, $cb) = @_; |
|
|
269 | |
|
|
270 | $self->{on_drain} = $cb; |
|
|
271 | |
|
|
272 | $cb->($self) |
|
|
273 | if $cb && $self->{low_water_mark} >= length $self->{wbuf}; |
|
|
274 | } |
|
|
275 | |
|
|
276 | =item $handle->push_write ($data) |
|
|
277 | |
|
|
278 | Queues the given scalar to be written. You can push as much data as you |
|
|
279 | want (only limited by the available memory), as C<AnyEvent::Handle> |
|
|
280 | buffers it independently of the kernel. |
|
|
281 | |
|
|
282 | =cut |
|
|
283 | |
|
|
284 | sub _drain_wbuf { |
|
|
285 | my ($self) = @_; |
|
|
286 | |
|
|
287 | unless ($self->{ww}) { |
|
|
288 | Scalar::Util::weaken $self; |
|
|
289 | my $cb = sub { |
|
|
290 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
|
|
291 | |
|
|
292 | if ($len > 0) { |
|
|
293 | substr $self->{wbuf}, 0, $len, ""; |
|
|
294 | |
|
|
295 | $self->{on_drain}($self) |
|
|
296 | if $self->{low_water_mark} >= length $self->{wbuf} |
|
|
297 | && $self->{on_drain}; |
|
|
298 | |
|
|
299 | delete $self->{ww} unless length $self->{wbuf}; |
|
|
300 | } elsif ($! != EAGAIN && $! != EINTR) { |
|
|
301 | $self->error; |
|
|
302 | } |
|
|
303 | }; |
|
|
304 | |
|
|
305 | $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb); |
|
|
306 | |
|
|
307 | $cb->($self); |
|
|
308 | }; |
|
|
309 | } |
|
|
310 | |
|
|
311 | sub push_write { |
|
|
312 | my $self = shift; |
|
|
313 | |
|
|
314 | if ($self->{filter_w}) { |
|
|
315 | $self->{filter_w}->($self, \$_[0]); |
|
|
316 | } else { |
|
|
317 | $self->{wbuf} .= $_[0]; |
|
|
318 | $self->_drain_wbuf; |
|
|
319 | } |
|
|
320 | } |
|
|
321 | |
|
|
322 | ############################################################################# |
|
|
323 | |
|
|
324 | =back |
|
|
325 | |
|
|
326 | =head2 READ QUEUE |
|
|
327 | |
|
|
328 | AnyEvent::Handle manages two queues per handle, one for writing and one |
|
|
329 | for reading. |
|
|
330 | |
|
|
331 | The read queue is more complex than the write queue. It can be used in two |
|
|
332 | ways, the "simple" way, using only C<on_read> and the "complex" way, using |
|
|
333 | a queue. |
|
|
334 | |
|
|
335 | In the simple case, you just install an C<on_read> callback and whenever |
|
|
336 | new data arrives, it will be called. You can then remove some data (if |
|
|
337 | enough is there) from the read buffer (C<< $handle->rbuf >>) if you want |
|
|
338 | or not. |
|
|
339 | |
|
|
340 | In the more complex case, you want to queue multiple callbacks. In this |
|
|
341 | case, AnyEvent::Handle will call the first queued callback each time new |
|
|
342 | data arrives and removes it when it has done its job (see C<push_read>, |
|
|
343 | below). |
|
|
344 | |
|
|
345 | This way you can, for example, push three line-reads, followed by reading |
|
|
346 | a chunk of data, and AnyEvent::Handle will execute them in order. |
|
|
347 | |
|
|
348 | Example 1: EPP protocol parser. EPP sends 4 byte length info, followed by |
|
|
349 | the specified number of bytes which give an XML datagram. |
|
|
350 | |
|
|
351 | # in the default state, expect some header bytes |
|
|
352 | $handle->on_read (sub { |
|
|
353 | # some data is here, now queue the length-header-read (4 octets) |
|
|
354 | shift->unshift_read_chunk (4, sub { |
|
|
355 | # header arrived, decode |
|
|
356 | my $len = unpack "N", $_[1]; |
|
|
357 | |
|
|
358 | # now read the payload |
|
|
359 | shift->unshift_read_chunk ($len, sub { |
|
|
360 | my $xml = $_[1]; |
|
|
361 | # handle xml |
|
|
362 | }); |
|
|
363 | }); |
|
|
364 | }); |
|
|
365 | |
|
|
366 | Example 2: Implement a client for a protocol that replies either with |
|
|
367 | "OK" and another line or "ERROR" for one request, and 64 bytes for the |
|
|
368 | second request. Due tot he availability of a full queue, we can just |
|
|
369 | pipeline sending both requests and manipulate the queue as necessary in |
|
|
370 | the callbacks: |
|
|
371 | |
|
|
372 | # request one |
|
|
373 | $handle->push_write ("request 1\015\012"); |
|
|
374 | |
|
|
375 | # we expect "ERROR" or "OK" as response, so push a line read |
|
|
376 | $handle->push_read_line (sub { |
|
|
377 | # if we got an "OK", we have to _prepend_ another line, |
|
|
378 | # so it will be read before the second request reads its 64 bytes |
|
|
379 | # which are already in the queue when this callback is called |
|
|
380 | # we don't do this in case we got an error |
|
|
381 | if ($_[1] eq "OK") { |
|
|
382 | $_[0]->unshift_read_line (sub { |
|
|
383 | my $response = $_[1]; |
|
|
384 | ... |
|
|
385 | }); |
|
|
386 | } |
|
|
387 | }); |
|
|
388 | |
|
|
389 | # request two |
|
|
390 | $handle->push_write ("request 2\015\012"); |
|
|
391 | |
|
|
392 | # simply read 64 bytes, always |
|
|
393 | $handle->push_read_chunk (64, sub { |
|
|
394 | my $response = $_[1]; |
|
|
395 | ... |
|
|
396 | }); |
|
|
397 | |
|
|
398 | =over 4 |
|
|
399 | |
|
|
400 | =cut |
|
|
401 | |
|
|
402 | sub _drain_rbuf { |
|
|
403 | my ($self) = @_; |
|
|
404 | |
|
|
405 | if ( |
|
|
406 | defined $self->{rbuf_max} |
|
|
407 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
408 | ) { |
|
|
409 | $! = &Errno::ENOSPC; return $self->error; |
|
|
410 | } |
|
|
411 | |
|
|
412 | return if $self->{in_drain}; |
|
|
413 | local $self->{in_drain} = 1; |
|
|
414 | |
|
|
415 | while (my $len = length $self->{rbuf}) { |
|
|
416 | no strict 'refs'; |
|
|
417 | if (my $cb = shift @{ $self->{queue} }) { |
|
|
418 | if (!$cb->($self)) { |
|
|
419 | if ($self->{eof}) { |
|
|
420 | # no progress can be made (not enough data and no data forthcoming) |
|
|
421 | $! = &Errno::EPIPE; return $self->error; |
54 | } |
422 | } |
|
|
423 | |
|
|
424 | unshift @{ $self->{queue} }, $cb; |
|
|
425 | return; |
55 | } |
426 | } |
56 | ); |
|
|
57 | |
|
|
58 | $cv->wait; |
|
|
59 | |
|
|
60 | =head1 DESCRIPTION |
|
|
61 | |
|
|
62 | This module is a helper module to make it easier to do non-blocking I/O |
|
|
63 | on filehandles (and sockets, see L<AnyEvent::Socket>). |
|
|
64 | |
|
|
65 | The event loop is provided by L<AnyEvent>. |
|
|
66 | |
|
|
67 | =head1 METHODS |
|
|
68 | |
|
|
69 | =over 4 |
|
|
70 | |
|
|
71 | =item B<new (%args)> |
|
|
72 | |
|
|
73 | The constructor has these arguments: |
|
|
74 | |
|
|
75 | =over 4 |
|
|
76 | |
|
|
77 | =item fh => $filehandle |
|
|
78 | |
|
|
79 | The filehandle this L<AnyEvent::Handle> object will operate on. |
|
|
80 | |
|
|
81 | NOTE: The filehandle will be set to non-blocking. |
|
|
82 | |
|
|
83 | =item read_block_size => $size |
|
|
84 | |
|
|
85 | The default read block size use for reads via the C<on_read> |
|
|
86 | method. |
|
|
87 | |
|
|
88 | =item on_read => $cb |
|
|
89 | |
|
|
90 | =item on_eof => $cb |
|
|
91 | |
|
|
92 | =item on_error => $cb |
|
|
93 | |
|
|
94 | These are shortcuts, that will call the corresponding method and set the callback to C<$cb>. |
|
|
95 | |
|
|
96 | =item on_readline => $cb |
|
|
97 | |
|
|
98 | The C<readlines> method is called with the default seperator and C<$cb> as callback |
|
|
99 | for you. |
|
|
100 | |
|
|
101 | =back |
|
|
102 | |
|
|
103 | =cut |
|
|
104 | |
|
|
105 | sub new { |
|
|
106 | my $this = shift; |
|
|
107 | my $class = ref($this) || $this; |
|
|
108 | my $self = { |
|
|
109 | read_block_size => 4096, |
|
|
110 | rbuf => '', |
|
|
111 | @_ |
|
|
112 | }; |
|
|
113 | bless $self, $class; |
|
|
114 | |
|
|
115 | $self->{fh}->blocking (0) if $self->{fh}; |
|
|
116 | |
|
|
117 | if ($self->{on_read}) { |
|
|
118 | $self->on_read ($self->{on_read}); |
|
|
119 | |
|
|
120 | } elsif ($self->{on_readline}) { |
427 | } elsif ($self->{on_read}) { |
121 | $self->readlines ($self->{on_readline}); |
428 | $self->{on_read}($self); |
122 | |
429 | |
|
|
430 | if ( |
|
|
431 | $self->{eof} # if no further data will arrive |
|
|
432 | && $len == length $self->{rbuf} # and no data has been consumed |
|
|
433 | && !@{ $self->{queue} } # and the queue is still empty |
|
|
434 | && $self->{on_read} # and we still want to read data |
|
|
435 | ) { |
|
|
436 | # then no progress can be made |
|
|
437 | $! = &Errno::EPIPE; return $self->error; |
|
|
438 | } |
|
|
439 | } else { |
|
|
440 | # read side becomes idle |
|
|
441 | delete $self->{rw}; |
|
|
442 | return; |
|
|
443 | } |
|
|
444 | } |
|
|
445 | |
123 | } elsif ($self->{on_eof}) { |
446 | if ($self->{eof}) { |
124 | $self->on_eof ($self->{on_eof}); |
447 | $self->_shutdown; |
125 | |
448 | $self->{on_eof}($self) |
126 | } elsif ($self->{on_error}) { |
449 | if $self->{on_eof}; |
127 | $self->on_eof ($self->{on_error}); |
|
|
128 | } |
450 | } |
129 | |
|
|
130 | return $self |
|
|
131 | } |
451 | } |
132 | |
452 | |
133 | =item B<fh> |
453 | =item $handle->on_read ($cb) |
134 | |
454 | |
135 | This method returns the filehandle of the L<AnyEvent::Handle> object. |
455 | This replaces the currently set C<on_read> callback, or clears it (when |
136 | |
456 | the new callback is C<undef>). See the description of C<on_read> in the |
137 | =cut |
457 | constructor. |
138 | |
|
|
139 | sub fh { $_[0]->{fh} } |
|
|
140 | |
|
|
141 | =item B<on_read ($callback)> |
|
|
142 | |
|
|
143 | This method installs a C<$callback> that will be called |
|
|
144 | when new data arrived. You can access the read buffer via the C<rbuf> |
|
|
145 | method (see below). |
|
|
146 | |
|
|
147 | The first argument of the C<$callback> will be the L<AnyEvent::Handle> object. |
|
|
148 | |
458 | |
149 | =cut |
459 | =cut |
150 | |
460 | |
151 | sub on_read { |
461 | sub on_read { |
152 | my ($self, $cb) = @_; |
462 | my ($self, $cb) = @_; |
|
|
463 | |
153 | $self->{on_read} = $cb; |
464 | $self->{on_read} = $cb; |
|
|
465 | } |
154 | |
466 | |
155 | unless (defined $self->{on_read}) { |
467 | =item $handle->rbuf |
|
|
468 | |
|
|
469 | Returns the read buffer (as a modifiable lvalue). |
|
|
470 | |
|
|
471 | You can access the read buffer directly as the C<< ->{rbuf} >> member, if |
|
|
472 | you want. |
|
|
473 | |
|
|
474 | NOTE: The read buffer should only be used or modified if the C<on_read>, |
|
|
475 | C<push_read> or C<unshift_read> methods are used. The other read methods |
|
|
476 | automatically manage the read buffer. |
|
|
477 | |
|
|
478 | =cut |
|
|
479 | |
|
|
480 | sub rbuf : lvalue { |
|
|
481 | $_[0]{rbuf} |
|
|
482 | } |
|
|
483 | |
|
|
484 | =item $handle->push_read ($cb) |
|
|
485 | |
|
|
486 | =item $handle->unshift_read ($cb) |
|
|
487 | |
|
|
488 | Append the given callback to the end of the queue (C<push_read>) or |
|
|
489 | prepend it (C<unshift_read>). |
|
|
490 | |
|
|
491 | The callback is called each time some additional read data arrives. |
|
|
492 | |
|
|
493 | It must check whether enough data is in the read buffer already. |
|
|
494 | |
|
|
495 | If not enough data is available, it must return the empty list or a false |
|
|
496 | value, in which case it will be called repeatedly until enough data is |
|
|
497 | available (or an error condition is detected). |
|
|
498 | |
|
|
499 | If enough data was available, then the callback must remove all data it is |
|
|
500 | interested in (which can be none at all) and return a true value. After returning |
|
|
501 | true, it will be removed from the queue. |
|
|
502 | |
|
|
503 | =cut |
|
|
504 | |
|
|
505 | sub push_read { |
|
|
506 | my ($self, $cb) = @_; |
|
|
507 | |
|
|
508 | push @{ $self->{queue} }, $cb; |
|
|
509 | $self->_drain_rbuf; |
|
|
510 | } |
|
|
511 | |
|
|
512 | sub unshift_read { |
|
|
513 | my ($self, $cb) = @_; |
|
|
514 | |
|
|
515 | push @{ $self->{queue} }, $cb; |
|
|
516 | $self->_drain_rbuf; |
|
|
517 | } |
|
|
518 | |
|
|
519 | =item $handle->push_read_chunk ($len, $cb->($self, $data)) |
|
|
520 | |
|
|
521 | =item $handle->unshift_read_chunk ($len, $cb->($self, $data)) |
|
|
522 | |
|
|
523 | Append the given callback to the end of the queue (C<push_read_chunk>) or |
|
|
524 | prepend it (C<unshift_read_chunk>). |
|
|
525 | |
|
|
526 | The callback will be called only once C<$len> bytes have been read, and |
|
|
527 | these C<$len> bytes will be passed to the callback. |
|
|
528 | |
|
|
529 | =cut |
|
|
530 | |
|
|
531 | sub _read_chunk($$) { |
|
|
532 | my ($self, $len, $cb) = @_; |
|
|
533 | |
|
|
534 | sub { |
|
|
535 | $len <= length $_[0]{rbuf} or return; |
|
|
536 | $cb->($_[0], substr $_[0]{rbuf}, 0, $len, ""); |
|
|
537 | 1 |
|
|
538 | } |
|
|
539 | } |
|
|
540 | |
|
|
541 | sub push_read_chunk { |
|
|
542 | $_[0]->push_read (&_read_chunk); |
|
|
543 | } |
|
|
544 | |
|
|
545 | |
|
|
546 | sub unshift_read_chunk { |
|
|
547 | $_[0]->unshift_read (&_read_chunk); |
|
|
548 | } |
|
|
549 | |
|
|
550 | =item $handle->push_read_line ([$eol, ]$cb->($self, $line, $eol)) |
|
|
551 | |
|
|
552 | =item $handle->unshift_read_line ([$eol, ]$cb->($self, $line, $eol)) |
|
|
553 | |
|
|
554 | Append the given callback to the end of the queue (C<push_read_line>) or |
|
|
555 | prepend it (C<unshift_read_line>). |
|
|
556 | |
|
|
557 | The callback will be called only once a full line (including the end of |
|
|
558 | line marker, C<$eol>) has been read. This line (excluding the end of line |
|
|
559 | marker) will be passed to the callback as second argument (C<$line>), and |
|
|
560 | the end of line marker as the third argument (C<$eol>). |
|
|
561 | |
|
|
562 | The end of line marker, C<$eol>, can be either a string, in which case it |
|
|
563 | will be interpreted as a fixed record end marker, or it can be a regex |
|
|
564 | object (e.g. created by C<qr>), in which case it is interpreted as a |
|
|
565 | regular expression. |
|
|
566 | |
|
|
567 | The end of line marker argument C<$eol> is optional, if it is missing (NOT |
|
|
568 | undef), then C<qr|\015?\012|> is used (which is good for most internet |
|
|
569 | protocols). |
|
|
570 | |
|
|
571 | Partial lines at the end of the stream will never be returned, as they are |
|
|
572 | not marked by the end of line marker. |
|
|
573 | |
|
|
574 | =cut |
|
|
575 | |
|
|
576 | sub _read_line($$) { |
|
|
577 | my $self = shift; |
|
|
578 | my $cb = pop; |
|
|
579 | my $eol = @_ ? shift : qr|(\015?\012)|; |
|
|
580 | my $pos; |
|
|
581 | |
|
|
582 | $eol = quotemeta $eol unless ref $eol; |
|
|
583 | $eol = qr|^(.*?)($eol)|s; |
|
|
584 | |
|
|
585 | sub { |
|
|
586 | $_[0]{rbuf} =~ s/$eol// or return; |
|
|
587 | |
|
|
588 | $cb->($_[0], $1, $2); |
|
|
589 | 1 |
|
|
590 | } |
|
|
591 | } |
|
|
592 | |
|
|
593 | sub push_read_line { |
|
|
594 | $_[0]->push_read (&_read_line); |
|
|
595 | } |
|
|
596 | |
|
|
597 | sub unshift_read_line { |
|
|
598 | $_[0]->unshift_read (&_read_line); |
|
|
599 | } |
|
|
600 | |
|
|
601 | =item $handle->stop_read |
|
|
602 | |
|
|
603 | =item $handle->start_read |
|
|
604 | |
|
|
605 | In rare cases you actually do not want to read anything from the |
|
|
606 | socket. In this case you can call C<stop_read>. Neither C<on_read> no |
|
|
607 | any queued callbacks will be executed then. To start reading again, call |
|
|
608 | C<start_read>. |
|
|
609 | |
|
|
610 | =cut |
|
|
611 | |
|
|
612 | sub stop_read { |
|
|
613 | my ($self) = @_; |
|
|
614 | |
156 | delete $self->{on_read_w}; |
615 | delete $self->{rw}; |
157 | return; |
616 | } |
158 | } |
617 | |
159 | |
618 | sub start_read { |
160 | $self->{on_read_w} = |
619 | my ($self) = @_; |
161 | AnyEvent->io (poll => 'r', fh => $self->{fh}, cb => sub { |
620 | |
162 | #d# warn "READ:[$self->{read_size}] $self->{read_block_size} : ".length ($self->{rbuf})."\n"; |
621 | unless ($self->{rw} || $self->{eof}) { |
163 | my $rbuf_len = length $self->{rbuf}; |
622 | Scalar::Util::weaken $self; |
164 | my $l; |
623 | |
165 | if (defined $self->{read_size}) { |
624 | $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
166 | $l = sysread $self->{fh}, $self->{rbuf}, |
625 | my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf}; |
167 | ($self->{read_size} - $rbuf_len), $rbuf_len; |
|
|
168 | } else { |
|
|
169 | $l = sysread $self->{fh}, $self->{rbuf}, $self->{read_block_size}, $rbuf_len; |
626 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
170 | } |
|
|
171 | #d# warn "READL $l [$self->{rbuf}]\n"; |
|
|
172 | |
627 | |
|
|
628 | if ($len > 0) { |
|
|
629 | $self->{filter_r} |
|
|
630 | ? $self->{filter_r}->($self, $rbuf) |
|
|
631 | : $self->_drain_rbuf; |
|
|
632 | |
173 | if (not defined $l) { |
633 | } elsif (defined $len) { |
174 | return if $! == EAGAIN || $! == EINTR; |
|
|
175 | $self->{on_error}->($self) if $self->{on_error}; |
|
|
176 | delete $self->{on_read_w}; |
634 | delete $self->{rw}; |
|
|
635 | $self->{eof} = 1; |
|
|
636 | $self->_drain_rbuf; |
177 | |
637 | |
178 | } elsif ($l == 0) { |
638 | } elsif ($! != EAGAIN && $! != EINTR) { |
179 | $self->{on_eof}->($self) if $self->{on_eof}; |
639 | return $self->error; |
180 | delete $self->{on_read_w}; |
|
|
181 | |
|
|
182 | } else { |
|
|
183 | $self->{on_read}->($self); |
|
|
184 | } |
640 | } |
185 | }); |
641 | }); |
|
|
642 | } |
186 | } |
643 | } |
187 | |
644 | |
188 | =item B<on_error ($callback)> |
645 | sub _dotls { |
189 | |
|
|
190 | Whenever a read or write operation resulted in an error the C<$callback> |
|
|
191 | will be called. |
|
|
192 | |
|
|
193 | The first argument of C<$callback> will be the L<AnyEvent::Handle> object itself. |
|
|
194 | The error is given as errno in C<$!>. |
|
|
195 | |
|
|
196 | =cut |
|
|
197 | |
|
|
198 | sub on_error { |
|
|
199 | $_[0]->{on_error} = $_[1]; |
|
|
200 | } |
|
|
201 | |
|
|
202 | =item B<on_eof ($callback)> |
|
|
203 | |
|
|
204 | Installs the C<$callback> that will be called when the end of file is |
|
|
205 | encountered in a read operation this C<$callback> will be called. The first |
|
|
206 | argument will be the L<AnyEvent::Handle> object itself. |
|
|
207 | |
|
|
208 | =cut |
|
|
209 | |
|
|
210 | sub on_eof { |
|
|
211 | $_[0]->{on_eof} = $_[1]; |
|
|
212 | } |
|
|
213 | |
|
|
214 | =item B<rbuf> |
|
|
215 | |
|
|
216 | Returns a reference to the read buffer. |
|
|
217 | |
|
|
218 | NOTE: The read buffer should only be used or modified if the C<on_read> |
|
|
219 | method is used directly. The C<read> and C<readlines> methods will provide |
|
|
220 | the read data to their callbacks. |
|
|
221 | |
|
|
222 | =cut |
|
|
223 | |
|
|
224 | sub rbuf : lvalue { |
|
|
225 | $_[0]->{rbuf} |
|
|
226 | } |
|
|
227 | |
|
|
228 | =item B<read ($len, $callback)> |
|
|
229 | |
|
|
230 | Will read exactly C<$len> bytes from the filehandle and call the C<$callback> |
|
|
231 | if done so. The first argument to the C<$callback> will be the L<AnyEvent::Handle> |
|
|
232 | object itself and the second argument the read data. |
|
|
233 | |
|
|
234 | NOTE: This method will override any callbacks installed via the C<on_read> method. |
|
|
235 | |
|
|
236 | =cut |
|
|
237 | |
|
|
238 | sub read { |
|
|
239 | my ($self, $len, $cb) = @_; |
646 | my ($self) = @_; |
240 | |
647 | |
241 | $self->{read_cb} = $cb; |
648 | if (length $self->{tls_wbuf}) { |
242 | my $old_blk_size = $self->{read_block_size}; |
649 | while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{tls_wbuf})) > 0) { |
243 | $self->{read_block_size} = $len; |
650 | substr $self->{tls_wbuf}, 0, $len, ""; |
244 | |
|
|
245 | $self->on_read (sub { |
|
|
246 | #d# warn "OFOFO $len || ".length($_[0]->{rbuf})."||\n"; |
|
|
247 | |
|
|
248 | if ($len == length $_[0]->{rbuf}) { |
|
|
249 | $_[0]->{read_block_size} = $old_blk_size; |
|
|
250 | $_[0]->on_read (undef); |
|
|
251 | $_[0]->{read_cb}->($_[0], (substr $self->{rbuf}, 0, $len, '')); |
|
|
252 | } |
651 | } |
253 | }); |
652 | } |
254 | } |
|
|
255 | |
653 | |
256 | =item B<readlines ($callback)> |
654 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) { |
257 | |
655 | $self->{wbuf} .= $buf; |
258 | =item B<readlines ($sep, $callback)> |
656 | $self->_drain_wbuf; |
259 | |
|
|
260 | This method will read lines from the filehandle, seperated by C<$sep> or C<"\n"> |
|
|
261 | if C<$sep> is not provided. C<$sep> will be used as "line" seperator. |
|
|
262 | |
|
|
263 | The C<$callback> will be called when at least one |
|
|
264 | line could be read. The first argument to the C<$callback> will be the L<AnyEvent::Handle> |
|
|
265 | object itself and the rest of the arguments will be the read lines. |
|
|
266 | |
|
|
267 | NOTE: This method will override any callbacks installed via the C<on_read> method. |
|
|
268 | |
|
|
269 | =cut |
|
|
270 | |
|
|
271 | sub readlines { |
|
|
272 | my ($self, $sep, $cb) = @_; |
|
|
273 | |
|
|
274 | if (ref $sep) { |
|
|
275 | $cb = $sep; |
|
|
276 | $sep = "\n"; |
|
|
277 | |
|
|
278 | } elsif (not defined $sep) { |
|
|
279 | $sep = "\n"; |
|
|
280 | } |
657 | } |
281 | |
658 | |
282 | my $sep_len = length $sep; |
659 | while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
|
|
660 | $self->{rbuf} .= $buf; |
|
|
661 | $self->_drain_rbuf; |
|
|
662 | } |
283 | |
663 | |
284 | $self->{on_readline} = $cb; |
664 | my $err = Net::SSLeay::get_error ($self->{tls}, -1); |
285 | |
665 | |
286 | $self->on_read (sub { |
666 | if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { |
287 | my @lines; |
667 | if ($err == Net::SSLeay::ERROR_SYSCALL ()) { |
288 | my $rb = \$_[0]->{rbuf}; |
668 | $self->error; |
289 | my $pos; |
669 | } elsif ($err == Net::SSLeay::ERROR_SSL ()) { |
290 | while (($pos = index ($$rb, $sep)) >= 0) { |
670 | $! = &Errno::EIO; |
291 | push @lines, substr $$rb, 0, $pos + $sep_len, ''; |
671 | $self->error; |
292 | } |
672 | } |
293 | $self->{on_readline}->($_[0], @lines); |
673 | |
|
|
674 | # all others are fine for our purposes |
|
|
675 | } |
|
|
676 | } |
|
|
677 | |
|
|
678 | # TODO: maybe document... |
|
|
679 | sub starttls { |
|
|
680 | my ($self, $ssl, $ctx) = @_; |
|
|
681 | |
|
|
682 | if ($ssl eq "accept") { |
|
|
683 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
|
|
684 | Net::SSLeay::set_accept_state ($ssl); |
|
|
685 | } elsif ($ssl eq "connect") { |
|
|
686 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
|
|
687 | Net::SSLeay::set_connect_state ($ssl); |
|
|
688 | } |
|
|
689 | |
|
|
690 | $self->{tls} = $ssl; |
|
|
691 | |
|
|
692 | # basically, this is deep magic (because SSL_read should have the same issues) |
|
|
693 | # but the openssl maintainers basically said: "trust us, it just works". |
|
|
694 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
|
|
695 | # and mismaintained ssleay-module doesn't even offer them). |
|
|
696 | Net::SSLeay::CTX_set_mode ($self->{tls}, |
|
|
697 | (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
|
|
698 | | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
|
|
699 | |
|
|
700 | $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
|
|
701 | $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
|
|
702 | |
|
|
703 | Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio}); |
|
|
704 | |
|
|
705 | $self->{filter_w} = sub { |
|
|
706 | $_[0]{tls_wbuf} .= ${$_[1]}; |
|
|
707 | &_dotls; |
294 | }); |
708 | }; |
295 | } |
709 | $self->{filter_r} = sub { |
296 | |
710 | Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); |
297 | =item B<write ($data)> |
711 | &_dotls; |
298 | |
|
|
299 | =item B<write ($callback)> |
|
|
300 | |
|
|
301 | =item B<write ($data, $callback)> |
|
|
302 | |
|
|
303 | This method will write C<$data> to the filehandle and call the C<$callback> |
|
|
304 | afterwards. If only C<$callback> is provided it will be called when the |
|
|
305 | write buffer becomes empty the next time (or immediately if it already is empty). |
|
|
306 | |
|
|
307 | =cut |
|
|
308 | |
|
|
309 | sub write { |
|
|
310 | my ($self, $data, $cb) = @_; |
|
|
311 | if (ref $data) { $cb = $data; undef $data } |
|
|
312 | push @{$self->{write_bufs}}, [$data, $cb]; |
|
|
313 | $self->_check_writer; |
|
|
314 | } |
|
|
315 | |
|
|
316 | sub _check_writer { |
|
|
317 | my ($self) = @_; |
|
|
318 | |
|
|
319 | if ($self->{write_w}) { |
|
|
320 | unless ($self->{write_cb}) { |
|
|
321 | while (@{$self->{write_bufs}} && not defined $self->{write_bufs}->[0]->[1]) { |
|
|
322 | my $wba = shift @{$self->{write_bufs}}; |
|
|
323 | $self->{wbuf} .= $wba->[0]; |
|
|
324 | } |
|
|
325 | } |
|
|
326 | return; |
|
|
327 | } |
712 | }; |
|
|
713 | } |
328 | |
714 | |
329 | my $wba = shift @{$self->{write_bufs}} |
715 | sub DESTROY { |
330 | or return; |
716 | my $self = shift; |
331 | |
717 | |
332 | unless (defined $wba->[0]) { |
718 | Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; |
333 | $wba->[1]->($self) if $wba->[1]; |
719 | } |
334 | $self->_check_writer; |
|
|
335 | return; |
|
|
336 | } |
|
|
337 | |
720 | |
338 | $self->{wbuf} = $wba->[0]; |
721 | =item AnyEvent::Handle::TLS_CTX |
339 | $self->{write_cb} = $wba->[1]; |
|
|
340 | |
722 | |
341 | $self->{write_w} = |
723 | This function creates and returns the Net::SSLeay::CTX object used by |
342 | AnyEvent->io (poll => 'w', fh => $self->{fh}, cb => sub { |
724 | default for TLS mode. |
343 | my $l = syswrite $self->{fh}, $self->{wbuf}, length $self->{wbuf}; |
|
|
344 | |
725 | |
345 | if (not defined $l) { |
726 | The context is created like this: |
346 | return if $! == EAGAIN || $! == EINTR; |
|
|
347 | delete $self->{write_w}; |
|
|
348 | $self->{on_error}->($self) if $self->{on_error}; |
|
|
349 | |
727 | |
350 | } else { |
728 | Net::SSLeay::load_error_strings; |
351 | substr $self->{wbuf}, 0, $l, ''; |
729 | Net::SSLeay::SSLeay_add_ssl_algorithms; |
|
|
730 | Net::SSLeay::randomize; |
352 | |
731 | |
353 | if (length ($self->{wbuf}) == 0) { |
732 | my $CTX = Net::SSLeay::CTX_new; |
354 | $self->{write_cb}->($self) if $self->{write_cb}; |
|
|
355 | |
733 | |
356 | delete $self->{write_w}; |
734 | Net::SSLeay::CTX_set_options $CTX, Net::SSLeay::OP_ALL |
357 | delete $self->{wbuf}; |
|
|
358 | delete $self->{write_cb}; |
|
|
359 | |
735 | |
360 | $self->_check_writer; |
736 | =cut |
361 | } |
737 | |
362 | } |
738 | our $TLS_CTX; |
363 | }); |
739 | |
|
|
740 | sub TLS_CTX() { |
|
|
741 | $TLS_CTX || do { |
|
|
742 | require Net::SSLeay; |
|
|
743 | |
|
|
744 | Net::SSLeay::load_error_strings (); |
|
|
745 | Net::SSLeay::SSLeay_add_ssl_algorithms (); |
|
|
746 | Net::SSLeay::randomize (); |
|
|
747 | |
|
|
748 | $TLS_CTX = Net::SSLeay::CTX_new (); |
|
|
749 | |
|
|
750 | Net::SSLeay::CTX_set_options ($TLS_CTX, Net::SSLeay::OP_ALL ()); |
|
|
751 | |
|
|
752 | $TLS_CTX |
|
|
753 | } |
364 | } |
754 | } |
365 | |
755 | |
366 | =back |
756 | =back |
367 | |
757 | |
368 | =head1 AUTHOR |
758 | =head1 AUTHOR |
369 | |
759 | |
370 | Robin Redeker, C<< <elmex at ta-sa.org> >> |
760 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |
371 | |
761 | |
372 | =cut |
762 | =cut |
373 | |
763 | |
374 | 1; # End of AnyEvent::Handle |
764 | 1; # End of AnyEvent::Handle |