ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Handle.pm (file contents):
Revision 1.178 by root, Tue Aug 11 01:15:17 2009 UTC vs.
Revision 1.183 by root, Thu Sep 3 12:45:35 2009 UTC

284In some situations you want as low a delay as possible, which can be 284In some situations you want as low a delay as possible, which can be
285accomplishd by setting this option to a true value. 285accomplishd by setting this option to a true value.
286 286
287The default is your opertaing system's default behaviour (most likely 287The default is your opertaing system's default behaviour (most likely
288enabled), this option explicitly enables or disables it, if possible. 288enabled), this option explicitly enables or disables it, if possible.
289
290=item keepalive => <boolean>
291
292Enables (default disable) the SO_KEEPALIVE option on the stream socket:
293normally, TCP connections have no time-out once established, so TCP
294conenctions, once established, can stay alive forever even when the other
295side has long gone. TCP keepalives are a cheap way to take down long-lived
296TCP connections whent he other side becomes unreachable. While the default
297is OS-dependent, TCP keepalives usually kick in after around two hours,
298and, if the other side doesn't reply, take down the TCP connection some 10
299to 15 minutes later.
300
301It is harmless to specify this option for file handles that do not support
302keepalives, and enabling it on connections that are potentially long-lived
303is usually a good idea.
304
305=item oobinline => <boolean>
306
307BSD majorly fucked up the implementation of TCP urgent data. The result
308is that almost no OS implements TCP according to the specs, and every OS
309implements it slightly differently.
310
311If you want to handle TCP urgent data, then setting this flag (the default
312is enabled) gives you the most portable way of getting urgent data, by
313putting it into the stream.
314
315Since BSD emulation of OOB data on top of TCP's urgent data can have
316security implications, AnyEvent::Handle sets this flag automatically
317unless explicitly specified.
289 318
290=item read_size => <bytes> 319=item read_size => <bytes>
291 320
292The default read block size (the amount of bytes this module will 321The default read block size (the amount of bytes this module will
293try to read during each loop iteration, which affects memory 322try 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;
587sub no_delay { 619sub 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
631Enables or disables the C<keepalive> setting (see constructor argument of
632the same name for details).
633
634=cut
635
636sub 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
648Enables or disables the C<oobinline> setting (see constructor argument of
649the same name for details).
650
651=cut
652
653sub 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
665Enables or disables the C<keepalive> setting (see constructor argument of
666the same name for details).
667
668=cut
669
670sub 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)
878Other languages could read single lines terminated by a newline and pass 961Other languages could read single lines terminated by a newline and pass
879this line into their JSON decoder of choice. 962this line into their JSON decoder of choice.
880 963
881=cut 964=cut
882 965
966sub json_coder() {
967 eval { require JSON::XS; JSON::XS->new->utf8 }
968 || do { require JSON; JSON->new->utf8 }
969}
970
883register_write_type json => sub { 971register_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
894Freezes the given reference using L<Storable> and writes it to the 981Freezes 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
1450register_read_type json => sub { 1536register_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) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines