1 | package AnyEvent::Handle; |
1 | package AnyEvent::Handle; |
2 | |
2 | |
3 | no warnings; |
|
|
4 | use strict qw(subs vars); |
|
|
5 | |
|
|
6 | use AnyEvent (); |
|
|
7 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
|
|
8 | use Scalar::Util (); |
3 | use Scalar::Util (); |
9 | use Carp (); |
4 | use Carp (); |
10 | use Fcntl (); |
|
|
11 | use Errno qw(EAGAIN EINTR); |
5 | use Errno qw(EAGAIN EINTR); |
12 | |
6 | |
|
|
7 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
|
|
8 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
|
|
9 | |
13 | =head1 NAME |
10 | =head1 NAME |
14 | |
11 | |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
12 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
16 | |
13 | |
17 | =cut |
14 | =cut |
18 | |
15 | |
19 | our $VERSION = 4.82; |
16 | our $VERSION = 4.86; |
20 | |
17 | |
21 | =head1 SYNOPSIS |
18 | =head1 SYNOPSIS |
22 | |
19 | |
23 | use AnyEvent; |
20 | use AnyEvent; |
24 | use AnyEvent::Handle; |
21 | use AnyEvent::Handle; |
… | |
… | |
26 | my $cv = AnyEvent->condvar; |
23 | my $cv = AnyEvent->condvar; |
27 | |
24 | |
28 | my $hdl; $hdl = new AnyEvent::Handle |
25 | my $hdl; $hdl = new AnyEvent::Handle |
29 | fh => \*STDIN, |
26 | fh => \*STDIN, |
30 | on_error => sub { |
27 | on_error => sub { |
|
|
28 | my ($hdl, $fatal, $msg) = @_; |
31 | warn "got error $_[2]\n"; |
29 | warn "got error $msg\n"; |
|
|
30 | $hdl->destroy; |
32 | $cv->send; |
31 | $cv->send; |
33 | ); |
32 | ); |
34 | |
33 | |
35 | # send some request line |
34 | # send some request line |
36 | $hdl->push_write ("getinfo\015\012"); |
35 | $hdl->push_write ("getinfo\015\012"); |
… | |
… | |
388 | $! = $errno; |
387 | $! = $errno; |
389 | $message ||= "$!"; |
388 | $message ||= "$!"; |
390 | |
389 | |
391 | if ($self->{on_error}) { |
390 | if ($self->{on_error}) { |
392 | $self->{on_error}($self, $fatal, $message); |
391 | $self->{on_error}($self, $fatal, $message); |
393 | $self->destroy; |
392 | $self->destroy if $fatal; |
394 | } elsif ($self->{fh}) { |
393 | } elsif ($self->{fh}) { |
395 | $self->destroy; |
394 | $self->destroy; |
396 | Carp::croak "AnyEvent::Handle uncaught error: $message"; |
395 | Carp::croak "AnyEvent::Handle uncaught error: $message"; |
397 | } |
396 | } |
398 | } |
397 | } |
… | |
… | |
1520 | |
1519 | |
1521 | Instead of starting TLS negotiation immediately when the AnyEvent::Handle |
1520 | Instead of starting TLS negotiation immediately when the AnyEvent::Handle |
1522 | object is created, you can also do that at a later time by calling |
1521 | object is created, you can also do that at a later time by calling |
1523 | C<starttls>. |
1522 | C<starttls>. |
1524 | |
1523 | |
|
|
1524 | Starting TLS is currently an asynchronous operation - when you push some |
|
|
1525 | write data and then call C<< ->starttls >> then TLS negotiation will start |
|
|
1526 | immediately, after which the queued write data is then sent. |
|
|
1527 | |
1525 | The first argument is the same as the C<tls> constructor argument (either |
1528 | The first argument is the same as the C<tls> constructor argument (either |
1526 | C<"connect">, C<"accept"> or an existing Net::SSLeay object). |
1529 | C<"connect">, C<"accept"> or an existing Net::SSLeay object). |
1527 | |
1530 | |
1528 | The second argument is the optional C<AnyEvent::TLS> object that is used |
1531 | The second argument is the optional C<AnyEvent::TLS> object that is used |
1529 | when AnyEvent::Handle has to create its own TLS connection object, or |
1532 | when AnyEvent::Handle has to create its own TLS connection object, or |
… | |
… | |
1553 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1556 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1554 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1557 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1555 | |
1558 | |
1556 | $ctx ||= $self->{tls_ctx}; |
1559 | $ctx ||= $self->{tls_ctx}; |
1557 | |
1560 | |
|
|
1561 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
|
|
1562 | |
1558 | if ("HASH" eq ref $ctx) { |
1563 | if ("HASH" eq ref $ctx) { |
1559 | require AnyEvent::TLS; |
1564 | require AnyEvent::TLS; |
1560 | |
|
|
1561 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context |
|
|
1562 | |
1565 | |
1563 | if ($ctx->{cache}) { |
1566 | if ($ctx->{cache}) { |
1564 | my $key = $ctx+0; |
1567 | my $key = $ctx+0; |
1565 | $ctx = $TLS_CACHE{$key} ||= new AnyEvent::TLS %$ctx; |
1568 | $ctx = $TLS_CACHE{$key} ||= new AnyEvent::TLS %$ctx; |
1566 | } else { |
1569 | } else { |
… | |
… | |
1638 | |
1641 | |
1639 | &_freetls; |
1642 | &_freetls; |
1640 | |
1643 | |
1641 | my $linger = exists $self->{linger} ? $self->{linger} : 3600; |
1644 | my $linger = exists $self->{linger} ? $self->{linger} : 3600; |
1642 | |
1645 | |
1643 | if ($linger && length $self->{wbuf}) { |
1646 | if ($linger && length $self->{wbuf} && $self->{fh}) { |
1644 | my $fh = delete $self->{fh}; |
1647 | my $fh = delete $self->{fh}; |
1645 | my $wbuf = delete $self->{wbuf}; |
1648 | my $wbuf = delete $self->{wbuf}; |
1646 | |
1649 | |
1647 | my @linger; |
1650 | my @linger; |
1648 | |
1651 | |