… | |
… | |
284 | In some situations you want as low a delay as possible, which can be |
284 | In some situations you want as low a delay as possible, which can be |
285 | accomplishd by setting this option to a true value. |
285 | accomplishd by setting this option to a true value. |
286 | |
286 | |
287 | The default is your opertaing system's default behaviour (most likely |
287 | The default is your opertaing system's default behaviour (most likely |
288 | enabled), this option explicitly enables or disables it, if possible. |
288 | enabled), this option explicitly enables or disables it, if possible. |
|
|
289 | |
|
|
290 | =item keepalive => <boolean> |
|
|
291 | |
|
|
292 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
|
|
293 | normally, TCP connections have no time-out once established, so TCP |
|
|
294 | conenctions, once established, can stay alive forever even when the other |
|
|
295 | side has long gone. TCP keepalives are a cheap way to take down long-lived |
|
|
296 | TCP connections whent he other side becomes unreachable. While the default |
|
|
297 | is OS-dependent, TCP keepalives usually kick in after around two hours, |
|
|
298 | and, if the other side doesn't reply, take down the TCP connection some 10 |
|
|
299 | to 15 minutes later. |
|
|
300 | |
|
|
301 | It is harmless to specify this option for file handles that do not support |
|
|
302 | keepalives, and enabling it on connections that are potentially long-lived |
|
|
303 | is usually a good idea. |
|
|
304 | |
|
|
305 | =item oobinline => <boolean> |
|
|
306 | |
|
|
307 | BSD majorly fucked up the implementation of TCP urgent data. The result |
|
|
308 | is that almost no OS implements TCP according to the specs, and every OS |
|
|
309 | implements it slightly differently. |
|
|
310 | |
|
|
311 | If you want to handle TCP urgent data, then setting this flag (the default |
|
|
312 | is enabled) gives you the most portable way of getting urgent data, by |
|
|
313 | putting it into the stream. |
|
|
314 | |
|
|
315 | Since BSD emulation of OOB data on top of TCP's urgent data can have |
|
|
316 | security implications, AnyEvent::Handle sets this flag automatically |
|
|
317 | unless explicitly specified. |
289 | |
318 | |
290 | =item read_size => <bytes> |
319 | =item read_size => <bytes> |
291 | |
320 | |
292 | The default read block size (the amount of bytes this module will |
321 | The default read block size (the amount of bytes this module will |
293 | try to read during each loop iteration, which affects memory |
322 | try to read during each loop iteration, which affects memory |
… | |
… | |
490 | |
519 | |
491 | $self->{_activity} = |
520 | $self->{_activity} = |
492 | $self->{_ractivity} = |
521 | $self->{_ractivity} = |
493 | $self->{_wactivity} = AE::now; |
522 | $self->{_wactivity} = AE::now; |
494 | |
523 | |
495 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
524 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
496 | $self->rtimeout (delete $self->{rtimeout}) if $self->{rtimeout}; |
525 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
497 | $self->wtimeout (delete $self->{wtimeout}) if $self->{wtimeout}; |
526 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
498 | |
527 | |
499 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
528 | $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay} && $self->{no_delay}; |
|
|
529 | $self->keepalive (delete $self->{keepalive}) if exists $self->{keepalive} && $self->{keepalive}; |
500 | |
530 | |
|
|
531 | $self->oobinline (exists $self->{oobinline} ? delete $self->{oobinline} : 1); |
|
|
532 | |
501 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
533 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
502 | if $self->{tls}; |
534 | if $self->{tls}; |
503 | |
535 | |
504 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
536 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
505 | |
537 | |
506 | $self->start_read |
538 | $self->start_read |
507 | if $self->{on_read} || @{ $self->{_queue} }; |
539 | if $self->{on_read} || @{ $self->{_queue} }; |
508 | |
540 | |
509 | $self->_drain_wbuf; |
541 | $self->_drain_wbuf; |
… | |
… | |
587 | sub no_delay { |
619 | sub no_delay { |
588 | $_[0]{no_delay} = $_[1]; |
620 | $_[0]{no_delay} = $_[1]; |
589 | |
621 | |
590 | eval { |
622 | eval { |
591 | local $SIG{__DIE__}; |
623 | local $SIG{__DIE__}; |
592 | setsockopt $_[0]{fh}, &Socket::IPPROTO_TCP, &Socket::TCP_NODELAY, int $_[1] |
624 | setsockopt $_[0]{fh}, Socket::IPPROTO_TCP (), Socket::TCP_NODELAY (), int $_[1] |
|
|
625 | if $_[0]{fh}; |
|
|
626 | }; |
|
|
627 | } |
|
|
628 | |
|
|
629 | =item $handle->keepalive ($boolean) |
|
|
630 | |
|
|
631 | Enables or disables the C<keepalive> setting (see constructor argument of |
|
|
632 | the same name for details). |
|
|
633 | |
|
|
634 | =cut |
|
|
635 | |
|
|
636 | sub keepalive { |
|
|
637 | $_[0]{keepalive} = $_[1]; |
|
|
638 | |
|
|
639 | eval { |
|
|
640 | local $SIG{__DIE__}; |
|
|
641 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1] |
|
|
642 | if $_[0]{fh}; |
|
|
643 | }; |
|
|
644 | } |
|
|
645 | |
|
|
646 | =item $handle->oobinline ($boolean) |
|
|
647 | |
|
|
648 | Enables or disables the C<oobinline> setting (see constructor argument of |
|
|
649 | the same name for details). |
|
|
650 | |
|
|
651 | =cut |
|
|
652 | |
|
|
653 | sub oobinline { |
|
|
654 | $_[0]{oobinline} = $_[1]; |
|
|
655 | |
|
|
656 | eval { |
|
|
657 | local $SIG{__DIE__}; |
|
|
658 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_OOBINLINE (), int $_[1] |
|
|
659 | if $_[0]{fh}; |
|
|
660 | }; |
|
|
661 | } |
|
|
662 | |
|
|
663 | =item $handle->keepalive ($boolean) |
|
|
664 | |
|
|
665 | Enables or disables the C<keepalive> setting (see constructor argument of |
|
|
666 | the same name for details). |
|
|
667 | |
|
|
668 | =cut |
|
|
669 | |
|
|
670 | sub keepalive { |
|
|
671 | $_[0]{keepalive} = $_[1]; |
|
|
672 | |
|
|
673 | eval { |
|
|
674 | local $SIG{__DIE__}; |
|
|
675 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1] |
593 | if $_[0]{fh}; |
676 | if $_[0]{fh}; |
594 | }; |
677 | }; |
595 | } |
678 | } |
596 | |
679 | |
597 | =item $handle->on_starttls ($cb) |
680 | =item $handle->on_starttls ($cb) |
… | |
… | |
878 | Other languages could read single lines terminated by a newline and pass |
961 | Other languages could read single lines terminated by a newline and pass |
879 | this line into their JSON decoder of choice. |
962 | this line into their JSON decoder of choice. |
880 | |
963 | |
881 | =cut |
964 | =cut |
882 | |
965 | |
|
|
966 | sub json_coder() { |
|
|
967 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
968 | || do { require JSON; JSON->new->utf8 } |
|
|
969 | } |
|
|
970 | |
883 | register_write_type json => sub { |
971 | register_write_type json => sub { |
884 | my ($self, $ref) = @_; |
972 | my ($self, $ref) = @_; |
885 | |
973 | |
886 | require JSON; |
974 | my $json = $self->{json} ||= json_coder; |
887 | |
975 | |
888 | $self->{json} ? $self->{json}->encode ($ref) |
976 | $json->encode ($ref) |
889 | : JSON::encode_json ($ref) |
|
|
890 | }; |
977 | }; |
891 | |
978 | |
892 | =item storable => $reference |
979 | =item storable => $reference |
893 | |
980 | |
894 | Freezes the given reference using L<Storable> and writes it to the |
981 | Freezes the given reference using L<Storable> and writes it to the |
… | |
… | |
1187 | |
1274 | |
1188 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
1275 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
1189 | ->($self, $cb, @_); |
1276 | ->($self, $cb, @_); |
1190 | } |
1277 | } |
1191 | |
1278 | |
1192 | |
|
|
1193 | unshift @{ $self->{_queue} }, $cb; |
1279 | unshift @{ $self->{_queue} }, $cb; |
1194 | $self->_drain_rbuf; |
1280 | $self->_drain_rbuf; |
1195 | } |
1281 | } |
1196 | |
1282 | |
1197 | =item $handle->push_read (type => @args, $cb) |
1283 | =item $handle->push_read (type => @args, $cb) |
… | |
… | |
1448 | =cut |
1534 | =cut |
1449 | |
1535 | |
1450 | register_read_type json => sub { |
1536 | register_read_type json => sub { |
1451 | my ($self, $cb) = @_; |
1537 | my ($self, $cb) = @_; |
1452 | |
1538 | |
1453 | my $json = $self->{json} ||= |
1539 | my $json = $self->{json} ||= json_coder; |
1454 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
1455 | || do { require JSON; JSON->new->utf8 }; |
|
|
1456 | |
1540 | |
1457 | my $data; |
1541 | my $data; |
1458 | my $rbuf = \$self->{rbuf}; |
1542 | my $rbuf = \$self->{rbuf}; |
1459 | |
1543 | |
1460 | sub { |
1544 | sub { |
… | |
… | |
1734 | require Net::SSLeay; |
1818 | require Net::SSLeay; |
1735 | |
1819 | |
1736 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1820 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1737 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1821 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1738 | |
1822 | |
1739 | $tls = $self->{tls}; |
1823 | $tls = delete $self->{tls}; |
1740 | $ctx = $self->{tls_ctx}; |
1824 | $ctx = $self->{tls_ctx}; |
1741 | |
1825 | |
1742 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1826 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1743 | |
1827 | |
1744 | if ("HASH" eq ref $ctx) { |
1828 | if ("HASH" eq ref $ctx) { |