… | |
… | |
583 | } else { |
583 | } else { |
584 | if ($self->{on_connect_error}) { |
584 | if ($self->{on_connect_error}) { |
585 | $self->{on_connect_error}($self, "$!"); |
585 | $self->{on_connect_error}($self, "$!"); |
586 | $self->destroy if $self; |
586 | $self->destroy if $self; |
587 | } else { |
587 | } else { |
588 | $self->_error ($!, 1); |
588 | $self->error ($!, 1); |
589 | } |
589 | } |
590 | } |
590 | } |
591 | }, |
591 | }, |
592 | sub { |
592 | sub { |
593 | local $self->{fh} = $_[0]; |
593 | local $self->{fh} = $_[0]; |
… | |
… | |
643 | if $self->{on_read} || @{ $self->{_queue} }; |
643 | if $self->{on_read} || @{ $self->{_queue} }; |
644 | |
644 | |
645 | $self->_drain_wbuf; |
645 | $self->_drain_wbuf; |
646 | } |
646 | } |
647 | |
647 | |
|
|
648 | =item $handle->error ($errno[, $fatal[, $message]]) |
|
|
649 | |
|
|
650 | Generates an error event, just like AnyEvent::Handle itself would do, i.e. |
|
|
651 | calls the C<on_error> callback. |
|
|
652 | |
|
|
653 | Te only rerquired parameter is C<$errno>, which sets C<$!>. C<$fatal> |
|
|
654 | defaults to false and C<$message> defaults to the stringified version |
|
|
655 | of C<$1>. |
|
|
656 | |
|
|
657 | Example: generate C<EIO> when you read unexpected data. |
|
|
658 | |
|
|
659 | $handle->push_read (line => sub { |
|
|
660 | $_[1] eq "hello" |
|
|
661 | or return $handle->error (Errno::EIO); |
|
|
662 | }); |
|
|
663 | |
|
|
664 | =cut |
|
|
665 | |
648 | sub _error { |
666 | sub error { |
649 | my ($self, $errno, $fatal, $message) = @_; |
667 | my ($self, $errno, $fatal, $message) = @_; |
650 | |
668 | |
651 | $! = $errno; |
669 | $! = $errno; |
652 | $message ||= "$!"; |
670 | $message ||= "$!"; |
653 | |
671 | |
… | |
… | |
866 | $self->{$activity} = $NOW; |
884 | $self->{$activity} = $NOW; |
867 | |
885 | |
868 | if ($self->{$on_timeout}) { |
886 | if ($self->{$on_timeout}) { |
869 | $self->{$on_timeout}($self); |
887 | $self->{$on_timeout}($self); |
870 | } else { |
888 | } else { |
871 | $self->_error (Errno::ETIMEDOUT); |
889 | $self->error (Errno::ETIMEDOUT); |
872 | } |
890 | } |
873 | |
891 | |
874 | # callback could have changed timeout value, optimise |
892 | # callback could have changed timeout value, optimise |
875 | return unless $self->{$timeout}; |
893 | return unless $self->{$timeout}; |
876 | |
894 | |
… | |
… | |
957 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
975 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
958 | && $self->{on_drain}; |
976 | && $self->{on_drain}; |
959 | |
977 | |
960 | delete $self->{_ww} unless length $self->{wbuf}; |
978 | delete $self->{_ww} unless length $self->{wbuf}; |
961 | } elsif ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK) { |
979 | } elsif ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK) { |
962 | $self->_error ($!, 1); |
980 | $self->error ($!, 1); |
963 | } |
981 | } |
964 | }; |
982 | }; |
965 | |
983 | |
966 | # try to write data immediately |
984 | # try to write data immediately |
967 | $cb->() unless $self->{autocork}; |
985 | $cb->() unless $self->{autocork}; |
… | |
… | |
972 | |
990 | |
973 | if ( |
991 | if ( |
974 | defined $self->{wbuf_max} |
992 | defined $self->{wbuf_max} |
975 | && $self->{wbuf_max} < length $self->{wbuf} |
993 | && $self->{wbuf_max} < length $self->{wbuf} |
976 | ) { |
994 | ) { |
977 | $self->_error (Errno::ENOSPC, 1), return; |
995 | $self->error (Errno::ENOSPC, 1), return; |
978 | } |
996 | } |
979 | }; |
997 | }; |
980 | } |
998 | } |
981 | |
999 | |
982 | our %WH; |
1000 | our %WH; |
… | |
… | |
1313 | |
1331 | |
1314 | if (my $cb = shift @{ $self->{_queue} }) { |
1332 | if (my $cb = shift @{ $self->{_queue} }) { |
1315 | unless ($cb->($self)) { |
1333 | unless ($cb->($self)) { |
1316 | # no progress can be made |
1334 | # no progress can be made |
1317 | # (not enough data and no data forthcoming) |
1335 | # (not enough data and no data forthcoming) |
1318 | $self->_error (Errno::EPIPE, 1), return |
1336 | $self->error (Errno::EPIPE, 1), return |
1319 | if $self->{_eof}; |
1337 | if $self->{_eof}; |
1320 | |
1338 | |
1321 | unshift @{ $self->{_queue} }, $cb; |
1339 | unshift @{ $self->{_queue} }, $cb; |
1322 | last; |
1340 | last; |
1323 | } |
1341 | } |
… | |
… | |
1331 | && !@{ $self->{_queue} } # and the queue is still empty |
1349 | && !@{ $self->{_queue} } # and the queue is still empty |
1332 | && $self->{on_read} # but we still have on_read |
1350 | && $self->{on_read} # but we still have on_read |
1333 | ) { |
1351 | ) { |
1334 | # no further data will arrive |
1352 | # no further data will arrive |
1335 | # so no progress can be made |
1353 | # so no progress can be made |
1336 | $self->_error (Errno::EPIPE, 1), return |
1354 | $self->error (Errno::EPIPE, 1), return |
1337 | if $self->{_eof}; |
1355 | if $self->{_eof}; |
1338 | |
1356 | |
1339 | last; # more data might arrive |
1357 | last; # more data might arrive |
1340 | } |
1358 | } |
1341 | } else { |
1359 | } else { |
… | |
… | |
1346 | } |
1364 | } |
1347 | |
1365 | |
1348 | if ($self->{_eof}) { |
1366 | if ($self->{_eof}) { |
1349 | $self->{on_eof} |
1367 | $self->{on_eof} |
1350 | ? $self->{on_eof}($self) |
1368 | ? $self->{on_eof}($self) |
1351 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1369 | : $self->error (0, 1, "Unexpected end-of-file"); |
1352 | |
1370 | |
1353 | return; |
1371 | return; |
1354 | } |
1372 | } |
1355 | |
1373 | |
1356 | if ( |
1374 | if ( |
1357 | defined $self->{rbuf_max} |
1375 | defined $self->{rbuf_max} |
1358 | && $self->{rbuf_max} < length $self->{rbuf} |
1376 | && $self->{rbuf_max} < length $self->{rbuf} |
1359 | ) { |
1377 | ) { |
1360 | $self->_error (Errno::ENOSPC, 1), return; |
1378 | $self->error (Errno::ENOSPC, 1), return; |
1361 | } |
1379 | } |
1362 | |
1380 | |
1363 | # may need to restart read watcher |
1381 | # may need to restart read watcher |
1364 | unless ($self->{_rw}) { |
1382 | unless ($self->{_rw}) { |
1365 | $self->start_read |
1383 | $self->start_read |
… | |
… | |
1611 | return 1; |
1629 | return 1; |
1612 | } |
1630 | } |
1613 | |
1631 | |
1614 | # reject |
1632 | # reject |
1615 | if ($reject && $$rbuf =~ $reject) { |
1633 | if ($reject && $$rbuf =~ $reject) { |
1616 | $_[0]->_error (Errno::EBADMSG); |
1634 | $_[0]->error (Errno::EBADMSG); |
1617 | } |
1635 | } |
1618 | |
1636 | |
1619 | # skip |
1637 | # skip |
1620 | if ($skip && $$rbuf =~ $skip) { |
1638 | if ($skip && $$rbuf =~ $skip) { |
1621 | $data .= substr $$rbuf, 0, $+[0], ""; |
1639 | $data .= substr $$rbuf, 0, $+[0], ""; |
… | |
… | |
1637 | my ($self, $cb) = @_; |
1655 | my ($self, $cb) = @_; |
1638 | |
1656 | |
1639 | sub { |
1657 | sub { |
1640 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
1658 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
1641 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
1659 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
1642 | $_[0]->_error (Errno::EBADMSG); |
1660 | $_[0]->error (Errno::EBADMSG); |
1643 | } |
1661 | } |
1644 | return; |
1662 | return; |
1645 | } |
1663 | } |
1646 | |
1664 | |
1647 | my $len = $1; |
1665 | my $len = $1; |
… | |
… | |
1650 | my $string = $_[1]; |
1668 | my $string = $_[1]; |
1651 | $_[0]->unshift_read (chunk => 1, sub { |
1669 | $_[0]->unshift_read (chunk => 1, sub { |
1652 | if ($_[1] eq ",") { |
1670 | if ($_[1] eq ",") { |
1653 | $cb->($_[0], $string); |
1671 | $cb->($_[0], $string); |
1654 | } else { |
1672 | } else { |
1655 | $_[0]->_error (Errno::EBADMSG); |
1673 | $_[0]->error (Errno::EBADMSG); |
1656 | } |
1674 | } |
1657 | }); |
1675 | }); |
1658 | }); |
1676 | }); |
1659 | |
1677 | |
1660 | 1 |
1678 | 1 |
… | |
… | |
1746 | $json->incr_skip; |
1764 | $json->incr_skip; |
1747 | |
1765 | |
1748 | $_[0]{rbuf} = $json->incr_text; |
1766 | $_[0]{rbuf} = $json->incr_text; |
1749 | $json->incr_text = ""; |
1767 | $json->incr_text = ""; |
1750 | |
1768 | |
1751 | $_[0]->_error (Errno::EBADMSG); |
1769 | $_[0]->error (Errno::EBADMSG); |
1752 | |
1770 | |
1753 | () |
1771 | () |
1754 | } else { |
1772 | } else { |
1755 | $_[0]{rbuf} = ""; |
1773 | $_[0]{rbuf} = ""; |
1756 | |
1774 | |
… | |
… | |
1794 | 1 |
1812 | 1 |
1795 | } elsif ($@) { |
1813 | } elsif ($@) { |
1796 | # error case |
1814 | # error case |
1797 | $cbor->incr_reset; |
1815 | $cbor->incr_reset; |
1798 | |
1816 | |
1799 | $_[0]->_error (Errno::EBADMSG); |
1817 | $_[0]->error (Errno::EBADMSG); |
1800 | |
1818 | |
1801 | () |
1819 | () |
1802 | } else { |
1820 | } else { |
1803 | () |
1821 | () |
1804 | } |
1822 | } |
… | |
… | |
1831 | if ($format + $len <= length $_[0]{rbuf}) { |
1849 | if ($format + $len <= length $_[0]{rbuf}) { |
1832 | my $data = substr $_[0]{rbuf}, $format, $len; |
1850 | my $data = substr $_[0]{rbuf}, $format, $len; |
1833 | substr $_[0]{rbuf}, 0, $format + $len, ""; |
1851 | substr $_[0]{rbuf}, 0, $format + $len, ""; |
1834 | |
1852 | |
1835 | eval { $cb->($_[0], Storable::thaw ($data)); 1 } |
1853 | eval { $cb->($_[0], Storable::thaw ($data)); 1 } |
1836 | or return $_[0]->_error (Errno::EBADMSG); |
1854 | or return $_[0]->error (Errno::EBADMSG); |
1837 | } else { |
1855 | } else { |
1838 | # remove prefix |
1856 | # remove prefix |
1839 | substr $_[0]{rbuf}, 0, $format, ""; |
1857 | substr $_[0]{rbuf}, 0, $format, ""; |
1840 | |
1858 | |
1841 | # read remaining chunk |
1859 | # read remaining chunk |
1842 | $_[0]->unshift_read (chunk => $len, sub { |
1860 | $_[0]->unshift_read (chunk => $len, sub { |
1843 | eval { $cb->($_[0], Storable::thaw ($_[1])); 1 } |
1861 | eval { $cb->($_[0], Storable::thaw ($_[1])); 1 } |
1844 | or $_[0]->_error (Errno::EBADMSG); |
1862 | or $_[0]->error (Errno::EBADMSG); |
1845 | }); |
1863 | }); |
1846 | } |
1864 | } |
1847 | |
1865 | |
1848 | 1 |
1866 | 1 |
1849 | } |
1867 | } |
… | |
… | |
2025 | delete $self->{_rw}; |
2043 | delete $self->{_rw}; |
2026 | $self->{_eof} = 1; |
2044 | $self->{_eof} = 1; |
2027 | $self->_drain_rbuf; |
2045 | $self->_drain_rbuf; |
2028 | |
2046 | |
2029 | } elsif ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK) { |
2047 | } elsif ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK) { |
2030 | return $self->_error ($!, 1); |
2048 | return $self->error ($!, 1); |
2031 | } |
2049 | } |
2032 | }; |
2050 | }; |
2033 | } |
2051 | } |
2034 | } |
2052 | } |
2035 | |
2053 | |
… | |
… | |
2037 | our $ERROR_WANT_READ; |
2055 | our $ERROR_WANT_READ; |
2038 | |
2056 | |
2039 | sub _tls_error { |
2057 | sub _tls_error { |
2040 | my ($self, $err) = @_; |
2058 | my ($self, $err) = @_; |
2041 | |
2059 | |
2042 | return $self->_error ($!, 1) |
2060 | return $self->error ($!, 1) |
2043 | if $err == Net::SSLeay::ERROR_SYSCALL (); |
2061 | if $err == Net::SSLeay::ERROR_SYSCALL (); |
2044 | |
2062 | |
2045 | my $err = Net::SSLeay::ERR_error_string (Net::SSLeay::ERR_get_error ()); |
2063 | my $err = Net::SSLeay::ERR_error_string (Net::SSLeay::ERR_get_error ()); |
2046 | |
2064 | |
2047 | # reduce error string to look less scary |
2065 | # reduce error string to look less scary |
… | |
… | |
2050 | if ($self->{_on_starttls}) { |
2068 | if ($self->{_on_starttls}) { |
2051 | (delete $self->{_on_starttls})->($self, undef, $err); |
2069 | (delete $self->{_on_starttls})->($self, undef, $err); |
2052 | &_freetls; |
2070 | &_freetls; |
2053 | } else { |
2071 | } else { |
2054 | &_freetls; |
2072 | &_freetls; |
2055 | $self->_error (Errno::EPROTO, 1, $err); |
2073 | $self->error (Errno::EPROTO, 1, $err); |
2056 | } |
2074 | } |
2057 | } |
2075 | } |
2058 | |
2076 | |
2059 | # poll the write BIO and send the data if applicable |
2077 | # poll the write BIO and send the data if applicable |
2060 | # also decode read data if possible |
2078 | # also decode read data if possible |
… | |
… | |
2162 | unless (defined $AnyEvent::TLS::VERSION) { |
2180 | unless (defined $AnyEvent::TLS::VERSION) { |
2163 | eval { |
2181 | eval { |
2164 | require Net::SSLeay; |
2182 | require Net::SSLeay; |
2165 | require AnyEvent::TLS; |
2183 | require AnyEvent::TLS; |
2166 | 1 |
2184 | 1 |
2167 | } or return $self->_error (Errno::EPROTO, 1, "TLS support not available on this system"); |
2185 | } or return $self->error (Errno::EPROTO, 1, "TLS support not available on this system"); |
2168 | } |
2186 | } |
2169 | |
2187 | |
2170 | $self->{tls} = $tls; |
2188 | $self->{tls} = $tls; |
2171 | $self->{tls_ctx} = $ctx if @_ > 2; |
2189 | $self->{tls_ctx} = $ctx if @_ > 2; |
2172 | |
2190 | |