… | |
… | |
12 | |
12 | |
13 | =head1 NAME |
13 | =head1 NAME |
14 | |
14 | |
15 | AnyEvent::Handle - non-blocking I/O on filehandles via AnyEvent |
15 | AnyEvent::Handle - non-blocking I/O on filehandles via AnyEvent |
16 | |
16 | |
17 | =cut |
17 | This module is experimental. |
18 | |
18 | |
|
|
19 | =cut |
|
|
20 | |
19 | our $VERSION = '0.02'; |
21 | our $VERSION = '0.04'; |
20 | |
22 | |
21 | =head1 SYNOPSIS |
23 | =head1 SYNOPSIS |
22 | |
24 | |
23 | use AnyEvent; |
25 | use AnyEvent; |
24 | use AnyEvent::Handle; |
26 | use AnyEvent::Handle; |
… | |
… | |
43 | $cv->wait; |
45 | $cv->wait; |
44 | |
46 | |
45 | =head1 DESCRIPTION |
47 | =head1 DESCRIPTION |
46 | |
48 | |
47 | This module is a helper module to make it easier to do event-based I/O on |
49 | This module is a helper module to make it easier to do event-based I/O on |
48 | filehandles (and sockets, see L<AnyEvent::Socket> for an easy way to make |
50 | filehandles. For utility functions for doing non-blocking connects and accepts |
49 | non-blocking resolves and connects). |
51 | on sockets see L<AnyEvent::Util>. |
50 | |
52 | |
51 | In the following, when the documentation refers to of "bytes" then this |
53 | In the following, when the documentation refers to of "bytes" then this |
52 | means characters. As sysread and syswrite are used for all I/O, their |
54 | means characters. As sysread and syswrite are used for all I/O, their |
53 | treatment of characters applies to this module as well. |
55 | treatment of characters applies to this module as well. |
54 | |
56 | |
… | |
… | |
70 | The filehandle this L<AnyEvent::Handle> object will operate on. |
72 | The filehandle this L<AnyEvent::Handle> object will operate on. |
71 | |
73 | |
72 | NOTE: The filehandle will be set to non-blocking (using |
74 | NOTE: The filehandle will be set to non-blocking (using |
73 | AnyEvent::Util::fh_nonblocking). |
75 | AnyEvent::Util::fh_nonblocking). |
74 | |
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 | |
75 | =item on_error => $cb->($self) [MANDATORY] |
85 | =item on_error => $cb->($self) |
76 | |
86 | |
77 | This is the fatal error callback, that is called when a fatal error ocurs, |
87 | This is the fatal error callback, that is called when, well, a fatal error |
78 | such as not being able to resolve the hostname, failure to connect or a |
88 | occurs, such as not being able to resolve the hostname, failure to connect |
79 | read error. |
89 | or a read error. |
80 | |
90 | |
81 | The object will not be in a usable state when this callback has been |
91 | The object will not be in a usable state when this callback has been |
82 | called. |
92 | called. |
83 | |
93 | |
84 | On callback entrance, the value of C<$!> contains the opertaing system |
94 | On callback entrance, the value of C<$!> contains the operating system |
85 | error (or C<ENOSPC> or C<EPIPE>). |
95 | error (or C<ENOSPC> or C<EPIPE>). |
86 | |
96 | |
87 | =item on_eof => $cb->($self) [MANDATORY] |
97 | While not mandatory, it is I<highly> recommended to set this callback, as |
88 | |
98 | you will not be notified of errors otherwise. The default simply calls |
89 | Set the callback to be called on EOF. |
99 | die. |
90 | |
100 | |
91 | =item on_read => $cb->($self) |
101 | =item on_read => $cb->($self) |
92 | |
102 | |
93 | This sets the default read callback, which is called when data arrives |
103 | This sets the default read callback, which is called when data arrives |
94 | and no read request is in the queue. If the read callback is C<undef> |
104 | and no read request is in the queue. |
95 | or has never been set, than AnyEvent::Handle will cease reading from the |
|
|
96 | filehandle. |
|
|
97 | |
105 | |
98 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
106 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
99 | method or acces sthe C<$self->{rbuf}> member directly. |
107 | method or access the C<$self->{rbuf}> member directly. |
100 | |
108 | |
101 | When an EOF condition is detected then AnyEvent::Handle will first try to |
109 | When an EOF condition is detected then AnyEvent::Handle will first try to |
102 | feed all the remaining data to the queued callbacks and C<on_read> before |
110 | feed all the remaining data to the queued callbacks and C<on_read> before |
103 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
111 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
104 | error will be raised (with C<$!> set to C<EPIPE>). |
112 | error will be raised (with C<$!> set to C<EPIPE>). |
… | |
… | |
131 | |
139 | |
132 | Sets the amount of bytes (default: C<0>) that make up an "empty" write |
140 | Sets the amount of bytes (default: C<0>) that make up an "empty" write |
133 | buffer: If the write reaches this size or gets even samller it is |
141 | buffer: If the write reaches this size or gets even samller it is |
134 | considered empty. |
142 | considered empty. |
135 | |
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 | |
136 | =back |
164 | =back |
137 | |
165 | |
138 | =cut |
166 | =cut |
139 | |
167 | |
140 | sub new { |
168 | sub new { |
… | |
… | |
144 | |
172 | |
145 | $self->{fh} or Carp::croak "mandatory argument fh is missing"; |
173 | $self->{fh} or Carp::croak "mandatory argument fh is missing"; |
146 | |
174 | |
147 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
175 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
148 | |
176 | |
149 | $self->on_error ((delete $self->{on_error}) or Carp::croak "mandatory argument on_error is missing"); |
177 | if ($self->{tls}) { |
150 | $self->on_eof ((delete $self->{on_eof} ) or Carp::croak "mandatory argument on_eof is missing"); |
178 | require Net::SSLeay; |
|
|
179 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); |
|
|
180 | } |
151 | |
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}; |
152 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
184 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
153 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
185 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
|
|
186 | |
|
|
187 | $self->start_read; |
154 | |
188 | |
155 | $self |
189 | $self |
156 | } |
190 | } |
157 | |
191 | |
158 | sub _shutdown { |
192 | sub _shutdown { |
… | |
… | |
169 | { |
203 | { |
170 | local $!; |
204 | local $!; |
171 | $self->_shutdown; |
205 | $self->_shutdown; |
172 | } |
206 | } |
173 | |
207 | |
|
|
208 | if ($self->{on_error}) { |
174 | $self->{on_error}($self); |
209 | $self->{on_error}($self); |
|
|
210 | } else { |
|
|
211 | die "AnyEvent::Handle uncaught fatal error: $!"; |
|
|
212 | } |
175 | } |
213 | } |
176 | |
214 | |
177 | =item $fh = $handle->fh |
215 | =item $fh = $handle->fh |
178 | |
216 | |
179 | This method returns the filehandle of the L<AnyEvent::Handle> object. |
217 | This method returns the filehandle of the L<AnyEvent::Handle> object. |
… | |
… | |
196 | |
234 | |
197 | Replace the current C<on_eof> callback (see the C<on_eof> constructor argument). |
235 | Replace the current C<on_eof> callback (see the C<on_eof> constructor argument). |
198 | |
236 | |
199 | =cut |
237 | =cut |
200 | |
238 | |
201 | ############################################################################# |
|
|
202 | |
|
|
203 | sub on_eof { |
239 | sub on_eof { |
204 | $_[0]{on_eof} = $_[1]; |
240 | $_[0]{on_eof} = $_[1]; |
205 | } |
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 |
206 | |
259 | |
207 | =item $handle->on_drain ($cb) |
260 | =item $handle->on_drain ($cb) |
208 | |
261 | |
209 | Sets the C<on_drain> callback or clears it (see the description of |
262 | Sets the C<on_drain> callback or clears it (see the description of |
210 | C<on_drain> in the constructor). |
263 | C<on_drain> in the constructor). |
… | |
… | |
226 | want (only limited by the available memory), as C<AnyEvent::Handle> |
279 | want (only limited by the available memory), as C<AnyEvent::Handle> |
227 | buffers it independently of the kernel. |
280 | buffers it independently of the kernel. |
228 | |
281 | |
229 | =cut |
282 | =cut |
230 | |
283 | |
231 | sub push_write { |
284 | sub _drain_wbuf { |
232 | my ($self, $data) = @_; |
285 | my ($self) = @_; |
233 | |
|
|
234 | $self->{wbuf} .= $data; |
|
|
235 | |
286 | |
236 | unless ($self->{ww}) { |
287 | unless ($self->{ww}) { |
237 | Scalar::Util::weaken $self; |
288 | Scalar::Util::weaken $self; |
238 | my $cb = sub { |
289 | my $cb = sub { |
239 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
290 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
240 | |
291 | |
241 | if ($len > 0) { |
292 | if ($len > 0) { |
242 | substr $self->{wbuf}, 0, $len, ""; |
293 | substr $self->{wbuf}, 0, $len, ""; |
243 | |
|
|
244 | |
294 | |
245 | $self->{on_drain}($self) |
295 | $self->{on_drain}($self) |
246 | if $self->{low_water_mark} >= length $self->{wbuf} |
296 | if $self->{low_water_mark} >= length $self->{wbuf} |
247 | && $self->{on_drain}; |
297 | && $self->{on_drain}; |
248 | |
298 | |
… | |
… | |
256 | |
306 | |
257 | $cb->($self); |
307 | $cb->($self); |
258 | }; |
308 | }; |
259 | } |
309 | } |
260 | |
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 | |
261 | ############################################################################# |
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 |
262 | |
401 | |
263 | sub _drain_rbuf { |
402 | sub _drain_rbuf { |
264 | my ($self) = @_; |
403 | my ($self) = @_; |
265 | |
404 | |
|
|
405 | if ( |
|
|
406 | defined $self->{rbuf_max} |
|
|
407 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
408 | ) { |
|
|
409 | $! = &Errno::ENOSPC; return $self->error; |
|
|
410 | } |
|
|
411 | |
266 | return if exists $self->{in_drain}; |
412 | return if $self->{in_drain}; |
267 | local $self->{in_drain} = 1; |
413 | local $self->{in_drain} = 1; |
268 | |
414 | |
269 | while (my $len = length $self->{rbuf}) { |
415 | while (my $len = length $self->{rbuf}) { |
270 | no strict 'refs'; |
416 | no strict 'refs'; |
271 | if (@{ $self->{queue} }) { |
417 | if (my $cb = shift @{ $self->{queue} }) { |
272 | if ($self->{queue}[0]($self)) { |
418 | if (!$cb->($self)) { |
273 | shift @{ $self->{queue} }; |
|
|
274 | } elsif ($self->{eof}) { |
419 | if ($self->{eof}) { |
275 | # no progress can be made (not enough data and no data forthcoming) |
420 | # no progress can be made (not enough data and no data forthcoming) |
276 | $! = &Errno::EPIPE; return $self->error; |
421 | $! = &Errno::EPIPE; return $self->error; |
277 | } else { |
422 | } |
|
|
423 | |
|
|
424 | unshift @{ $self->{queue} }, $cb; |
278 | return; |
425 | return; |
279 | } |
426 | } |
280 | } elsif ($self->{on_read}) { |
427 | } elsif ($self->{on_read}) { |
281 | $self->{on_read}($self); |
428 | $self->{on_read}($self); |
282 | |
429 | |
… | |
… | |
296 | } |
443 | } |
297 | } |
444 | } |
298 | |
445 | |
299 | if ($self->{eof}) { |
446 | if ($self->{eof}) { |
300 | $self->_shutdown; |
447 | $self->_shutdown; |
301 | $self->{on_eof}($self); |
448 | $self->{on_eof}($self) |
|
|
449 | if $self->{on_eof}; |
302 | } |
450 | } |
303 | } |
451 | } |
304 | |
452 | |
305 | =item $handle->on_read ($cb) |
453 | =item $handle->on_read ($cb) |
306 | |
454 | |
… | |
… | |
312 | |
460 | |
313 | sub on_read { |
461 | sub on_read { |
314 | my ($self, $cb) = @_; |
462 | my ($self, $cb) = @_; |
315 | |
463 | |
316 | $self->{on_read} = $cb; |
464 | $self->{on_read} = $cb; |
317 | |
|
|
318 | unless ($self->{rw} || $self->{eof}) { |
|
|
319 | Scalar::Util::weaken $self; |
|
|
320 | |
|
|
321 | $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
|
|
322 | my $len = sysread $self->{fh}, $self->{rbuf}, $self->{read_size} || 8192, length $self->{rbuf}; |
|
|
323 | |
|
|
324 | if ($len > 0) { |
|
|
325 | if (exists $self->{rbuf_max}) { |
|
|
326 | if ($self->{rbuf_max} < length $self->{rbuf}) { |
|
|
327 | $! = &Errno::ENOSPC; return $self->error; |
|
|
328 | } |
|
|
329 | } |
|
|
330 | |
|
|
331 | } elsif (defined $len) { |
|
|
332 | $self->{eof} = 1; |
|
|
333 | delete $self->{rw}; |
|
|
334 | |
|
|
335 | } elsif ($! != EAGAIN && $! != EINTR) { |
|
|
336 | return $self->error; |
|
|
337 | } |
|
|
338 | |
|
|
339 | $self->_drain_rbuf; |
|
|
340 | }); |
|
|
341 | } |
|
|
342 | } |
465 | } |
343 | |
466 | |
344 | =item $handle->rbuf |
467 | =item $handle->rbuf |
345 | |
468 | |
346 | Returns the read buffer (as a modifiable lvalue). |
469 | Returns the read buffer (as a modifiable lvalue). |
… | |
… | |
365 | Append the given callback to the end of the queue (C<push_read>) or |
488 | Append the given callback to the end of the queue (C<push_read>) or |
366 | prepend it (C<unshift_read>). |
489 | prepend it (C<unshift_read>). |
367 | |
490 | |
368 | The callback is called each time some additional read data arrives. |
491 | The callback is called each time some additional read data arrives. |
369 | |
492 | |
370 | It must check wether enough data is in the read buffer already. |
493 | It must check whether enough data is in the read buffer already. |
371 | |
494 | |
372 | If not enough data is available, it must return the empty list or a false |
495 | If not enough data is available, it must return the empty list or a false |
373 | value, in which case it will be called repeatedly until enough data is |
496 | value, in which case it will be called repeatedly until enough data is |
374 | available (or an error condition is detected). |
497 | available (or an error condition is detected). |
375 | |
498 | |
… | |
… | |
404 | these C<$len> bytes will be passed to the callback. |
527 | these C<$len> bytes will be passed to the callback. |
405 | |
528 | |
406 | =cut |
529 | =cut |
407 | |
530 | |
408 | sub _read_chunk($$) { |
531 | sub _read_chunk($$) { |
409 | my ($len, $cb) = @_; |
532 | my ($self, $len, $cb) = @_; |
410 | |
533 | |
411 | sub { |
534 | sub { |
412 | $len <= length $_[0]{rbuf} or return; |
535 | $len <= length $_[0]{rbuf} or return; |
413 | $cb->($_[0], substr $_[0]{rbuf}, 0, $len, ""); |
536 | $cb->($_[0], substr $_[0]{rbuf}, 0, $len, ""); |
414 | 1 |
537 | 1 |
415 | } |
538 | } |
416 | } |
539 | } |
417 | |
540 | |
418 | sub push_read_chunk { |
541 | sub push_read_chunk { |
419 | my ($self, $len, $cb) = @_; |
|
|
420 | |
|
|
421 | $self->push_read (_read_chunk $len, $cb); |
542 | $_[0]->push_read (&_read_chunk); |
422 | } |
543 | } |
423 | |
544 | |
424 | |
545 | |
425 | sub unshift_read_chunk { |
546 | sub unshift_read_chunk { |
426 | my ($self, $len, $cb) = @_; |
|
|
427 | |
|
|
428 | $self->unshift_read (_read_chunk $len, $cb); |
547 | $_[0]->unshift_read (&_read_chunk); |
429 | } |
548 | } |
430 | |
549 | |
431 | =item $handle->push_read_line ([$eol, ]$cb->($self, $line, $eol)) |
550 | =item $handle->push_read_line ([$eol, ]$cb->($self, $line, $eol)) |
432 | |
551 | |
433 | =item $handle->unshift_read_line ([$eol, ]$cb->($self, $line, $eol)) |
552 | =item $handle->unshift_read_line ([$eol, ]$cb->($self, $line, $eol)) |
… | |
… | |
453 | not marked by the end of line marker. |
572 | not marked by the end of line marker. |
454 | |
573 | |
455 | =cut |
574 | =cut |
456 | |
575 | |
457 | sub _read_line($$) { |
576 | sub _read_line($$) { |
|
|
577 | my $self = shift; |
458 | my $cb = pop; |
578 | my $cb = pop; |
459 | my $eol = @_ ? shift : qr|(\015?\012)|; |
579 | my $eol = @_ ? shift : qr|(\015?\012)|; |
460 | my $pos; |
580 | my $pos; |
461 | |
581 | |
462 | $eol = qr|(\Q$eol\E)| unless ref $eol; |
582 | $eol = quotemeta $eol unless ref $eol; |
463 | $eol = qr|^(.*?)($eol)|; |
583 | $eol = qr|^(.*?)($eol)|s; |
464 | |
584 | |
465 | sub { |
585 | sub { |
466 | $_[0]{rbuf} =~ s/$eol// or return; |
586 | $_[0]{rbuf} =~ s/$eol// or return; |
467 | |
587 | |
468 | $cb->($1, $2); |
588 | $cb->($_[0], $1, $2); |
469 | 1 |
589 | 1 |
470 | } |
590 | } |
471 | } |
591 | } |
472 | |
592 | |
473 | sub push_read_line { |
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 readign again, call |
|
|
608 | C<start_read>. |
|
|
609 | |
|
|
610 | =cut |
|
|
611 | |
|
|
612 | sub stop_read { |
|
|
613 | my ($self) = @_; |
|
|
614 | |
|
|
615 | delete $self->{rw}; |
|
|
616 | } |
|
|
617 | |
|
|
618 | sub start_read { |
|
|
619 | my ($self) = @_; |
|
|
620 | |
|
|
621 | unless ($self->{rw} || $self->{eof}) { |
|
|
622 | Scalar::Util::weaken $self; |
|
|
623 | |
|
|
624 | $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
|
|
625 | my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf}; |
|
|
626 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
|
|
627 | |
|
|
628 | if ($len > 0) { |
|
|
629 | $self->{filter_r} |
|
|
630 | ? $self->{filter_r}->($self, $rbuf) |
|
|
631 | : $self->_drain_rbuf; |
|
|
632 | |
|
|
633 | } elsif (defined $len) { |
|
|
634 | delete $self->{rw}; |
|
|
635 | $self->{eof} = 1; |
|
|
636 | $self->_drain_rbuf; |
|
|
637 | |
|
|
638 | } elsif ($! != EAGAIN && $! != EINTR) { |
|
|
639 | return $self->error; |
|
|
640 | } |
|
|
641 | }); |
|
|
642 | } |
|
|
643 | } |
|
|
644 | |
|
|
645 | sub _dotls { |
|
|
646 | my ($self) = @_; |
|
|
647 | |
|
|
648 | if (length $self->{tls_wbuf}) { |
|
|
649 | my $len = Net::SSLeay::write ($self->{tls}, $self->{tls_wbuf}); |
|
|
650 | substr $self->{tls_wbuf}, 0, $len, "" if $len > 0; |
|
|
651 | } |
|
|
652 | |
|
|
653 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) { |
|
|
654 | $self->{wbuf} .= $buf; |
|
|
655 | $self->_drain_wbuf; |
|
|
656 | } |
|
|
657 | |
|
|
658 | if (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
|
|
659 | $self->{rbuf} .= $buf; |
|
|
660 | $self->_drain_rbuf; |
|
|
661 | } elsif ( |
|
|
662 | (my $err = Net::SSLeay::get_error ($self->{tls}, -1)) |
|
|
663 | != Net::SSLeay::ERROR_WANT_READ () |
|
|
664 | ) { |
|
|
665 | if ($err == Net::SSLeay::ERROR_SYSCALL ()) { |
|
|
666 | $self->error; |
|
|
667 | } elsif ($err == Net::SSLeay::ERROR_SSL ()) { |
|
|
668 | $! = &Errno::EIO; |
|
|
669 | $self->error; |
|
|
670 | } |
|
|
671 | |
|
|
672 | # all others are fine for our purposes |
|
|
673 | } |
|
|
674 | } |
|
|
675 | |
|
|
676 | # TODO: maybe document... |
|
|
677 | sub starttls { |
|
|
678 | my ($self, $ssl, $ctx) = @_; |
|
|
679 | |
|
|
680 | if ($ssl eq "accept") { |
|
|
681 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
|
|
682 | Net::SSLeay::set_accept_state ($ssl); |
|
|
683 | } elsif ($ssl eq "connect") { |
|
|
684 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
|
|
685 | Net::SSLeay::set_connect_state ($ssl); |
|
|
686 | } |
|
|
687 | |
|
|
688 | $self->{tls} = $ssl; |
|
|
689 | |
|
|
690 | # basically, this is deep magic (because SSL_read should have the same issues) |
|
|
691 | # but the openssl maintainers basically said: "trust us, it just works". |
|
|
692 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
|
|
693 | # and mismaintained ssleay-module doesn't even offer them). |
|
|
694 | Net::SSLeay::CTX_set_mode ($self->{tls}, |
|
|
695 | (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
|
|
696 | | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
|
|
697 | |
|
|
698 | $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
|
|
699 | $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
|
|
700 | |
|
|
701 | Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio}); |
|
|
702 | |
|
|
703 | $self->{filter_w} = sub { |
|
|
704 | $_[0]{tls_wbuf} .= ${$_[1]}; |
|
|
705 | &_dotls; |
|
|
706 | }; |
|
|
707 | $self->{filter_r} = sub { |
|
|
708 | Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); |
|
|
709 | &_dotls; |
|
|
710 | }; |
|
|
711 | } |
|
|
712 | |
|
|
713 | sub DESTROY { |
474 | my $self = shift; |
714 | my $self = shift; |
475 | |
715 | |
476 | $self->push_read (&_read_line); |
716 | Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; |
477 | } |
717 | } |
478 | |
718 | |
479 | sub unshift_read_line { |
719 | =item AnyEvent::Handle::TLS_CTX |
480 | my $self = shift; |
|
|
481 | |
720 | |
482 | $self->unshift_read (&_read_line); |
721 | This function creates and returns the Net::SSLeay::CTX object used by |
|
|
722 | default for TLS mode. |
|
|
723 | |
|
|
724 | The context is created like this: |
|
|
725 | |
|
|
726 | Net::SSLeay::load_error_strings; |
|
|
727 | Net::SSLeay::SSLeay_add_ssl_algorithms; |
|
|
728 | Net::SSLeay::randomize; |
|
|
729 | |
|
|
730 | my $CTX = Net::SSLeay::CTX_new; |
|
|
731 | |
|
|
732 | Net::SSLeay::CTX_set_options $CTX, Net::SSLeay::OP_ALL |
|
|
733 | |
|
|
734 | =cut |
|
|
735 | |
|
|
736 | our $TLS_CTX; |
|
|
737 | |
|
|
738 | sub TLS_CTX() { |
|
|
739 | $TLS_CTX || do { |
|
|
740 | require Net::SSLeay; |
|
|
741 | |
|
|
742 | Net::SSLeay::load_error_strings (); |
|
|
743 | Net::SSLeay::SSLeay_add_ssl_algorithms (); |
|
|
744 | Net::SSLeay::randomize (); |
|
|
745 | |
|
|
746 | $TLS_CTX = Net::SSLeay::CTX_new (); |
|
|
747 | |
|
|
748 | Net::SSLeay::CTX_set_options ($TLS_CTX, Net::SSLeay::OP_ALL ()); |
|
|
749 | |
|
|
750 | $TLS_CTX |
|
|
751 | } |
483 | } |
752 | } |
484 | |
753 | |
485 | =back |
754 | =back |
486 | |
755 | |
487 | =head1 AUTHOR |
756 | =head1 AUTHOR |