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.218 by root, Thu Feb 24 12:04:20 2011 UTC vs.
Revision 1.220 by root, Sun Jul 24 13:10:43 2011 UTC

1543 1543
1544 sub { 1544 sub {
1545 # accept 1545 # accept
1546 if ($$rbuf =~ $accept) { 1546 if ($$rbuf =~ $accept) {
1547 $data .= substr $$rbuf, 0, $+[0], ""; 1547 $data .= substr $$rbuf, 0, $+[0], "";
1548 $cb->($self, $data); 1548 $cb->($_[0], $data);
1549 return 1; 1549 return 1;
1550 } 1550 }
1551 1551
1552 # reject 1552 # reject
1553 if ($reject && $$rbuf =~ $reject) { 1553 if ($reject && $$rbuf =~ $reject) {
1554 $self->_error (Errno::EBADMSG); 1554 $_[0]->_error (Errno::EBADMSG);
1555 } 1555 }
1556 1556
1557 # skip 1557 # skip
1558 if ($skip && $$rbuf =~ $skip) { 1558 if ($skip && $$rbuf =~ $skip) {
1559 $data .= substr $$rbuf, 0, $+[0], ""; 1559 $data .= substr $$rbuf, 0, $+[0], "";
1575 my ($self, $cb) = @_; 1575 my ($self, $cb) = @_;
1576 1576
1577 sub { 1577 sub {
1578 unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { 1578 unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) {
1579 if ($_[0]{rbuf} =~ /[^0-9]/) { 1579 if ($_[0]{rbuf} =~ /[^0-9]/) {
1580 $self->_error (Errno::EBADMSG); 1580 $_[0]->_error (Errno::EBADMSG);
1581 } 1581 }
1582 return; 1582 return;
1583 } 1583 }
1584 1584
1585 my $len = $1; 1585 my $len = $1;
1586 1586
1587 $self->unshift_read (chunk => $len, sub { 1587 $_[0]->unshift_read (chunk => $len, sub {
1588 my $string = $_[1]; 1588 my $string = $_[1];
1589 $_[0]->unshift_read (chunk => 1, sub { 1589 $_[0]->unshift_read (chunk => 1, sub {
1590 if ($_[1] eq ",") { 1590 if ($_[1] eq ",") {
1591 $cb->($_[0], $string); 1591 $cb->($_[0], $string);
1592 } else { 1592 } else {
1593 $self->_error (Errno::EBADMSG); 1593 $_[0]->_error (Errno::EBADMSG);
1594 } 1594 }
1595 }); 1595 });
1596 }); 1596 });
1597 1597
1598 1 1598 1
1671 1671
1672 my $data; 1672 my $data;
1673 my $rbuf = \$self->{rbuf}; 1673 my $rbuf = \$self->{rbuf};
1674 1674
1675 sub { 1675 sub {
1676 my $ref = eval { $json->incr_parse ($self->{rbuf}) }; 1676 my $ref = eval { $json->incr_parse ($_[0]{rbuf}) };
1677 1677
1678 if ($ref) { 1678 if ($ref) {
1679 $self->{rbuf} = $json->incr_text; 1679 $_[0]{rbuf} = $json->incr_text;
1680 $json->incr_text = ""; 1680 $json->incr_text = "";
1681 $cb->($self, $ref); 1681 $cb->($_[0], $ref);
1682 1682
1683 1 1683 1
1684 } elsif ($@) { 1684 } elsif ($@) {
1685 # error case 1685 # error case
1686 $json->incr_skip; 1686 $json->incr_skip;
1687 1687
1688 $self->{rbuf} = $json->incr_text; 1688 $_[0]{rbuf} = $json->incr_text;
1689 $json->incr_text = ""; 1689 $json->incr_text = "";
1690 1690
1691 $self->_error (Errno::EBADMSG); 1691 $_[0]->_error (Errno::EBADMSG);
1692 1692
1693 () 1693 ()
1694 } else { 1694 } else {
1695 $self->{rbuf} = ""; 1695 $_[0]{rbuf} = "";
1696 1696
1697 () 1697 ()
1698 } 1698 }
1699 } 1699 }
1700}; 1700};
1733 # read remaining chunk 1733 # read remaining chunk
1734 $_[0]->unshift_read (chunk => $len, sub { 1734 $_[0]->unshift_read (chunk => $len, sub {
1735 if (my $ref = eval { Storable::thaw ($_[1]) }) { 1735 if (my $ref = eval { Storable::thaw ($_[1]) }) {
1736 $cb->($_[0], $ref); 1736 $cb->($_[0], $ref);
1737 } else { 1737 } else {
1738 $self->_error (Errno::EBADMSG); 1738 $_[0]->_error (Errno::EBADMSG);
1739 } 1739 }
1740 }); 1740 });
1741 } 1741 }
1742 1742
1743 1 1743 1
2010 Net::SSLeay::CTX_set_mode ($tls, 1|2); 2010 Net::SSLeay::CTX_set_mode ($tls, 1|2);
2011 2011
2012 $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); 2012 $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ());
2013 $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); 2013 $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ());
2014 2014
2015 Net::SSLeay::BIO_write ($self->{_rbio}, delete $self->{rbuf}); 2015 Net::SSLeay::BIO_write ($self->{_rbio}, $self->{rbuf});
2016 $self->{rbuf} = "";
2016 2017
2017 Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); 2018 Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio});
2018 2019
2019 $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } 2020 $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) }
2020 if $self->{on_starttls}; 2021 if $self->{on_starttls};
2305 $handle->on_eof (undef); 2306 $handle->on_eof (undef);
2306 $handle->on_error (sub { 2307 $handle->on_error (sub {
2307 my $data = delete $_[0]{rbuf}; 2308 my $data = delete $_[0]{rbuf};
2308 }); 2309 });
2309 2310
2311Note that this example removes the C<rbuf> member from the handle object,
2312which is not normally allowed by the API. It is expressly permitted in
2313this case only, as the handle object needs to be destroyed afterwards.
2314
2310The reason to use C<on_error> is that TCP connections, due to latencies 2315The reason to use C<on_error> is that TCP connections, due to latencies
2311and packets loss, might get closed quite violently with an error, when in 2316and packets loss, might get closed quite violently with an error, when in
2312fact all data has been received. 2317fact all data has been received.
2313 2318
2314It is usually better to use acknowledgements when transferring data, 2319It is usually better to use acknowledgements when transferring data,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines