… | |
… | |
496 | callback. |
496 | callback. |
497 | |
497 | |
498 | This callback will only be called on TLS shutdowns, not when the |
498 | This callback will only be called on TLS shutdowns, not when the |
499 | underlying handle signals EOF. |
499 | underlying handle signals EOF. |
500 | |
500 | |
501 | =item json => JSON or JSON::XS object |
501 | =item json => L<JSON> or L<JSON::XS> object |
502 | |
502 | |
503 | This is the json coder object used by the C<json> read and write types. |
503 | This is the json coder object used by the C<json> read and write types. |
504 | |
504 | |
505 | If you don't supply it, then AnyEvent::Handle will create and use a |
505 | If you don't supply it, then AnyEvent::Handle will create and use a |
506 | suitable one (on demand), which will write and expect UTF-8 encoded JSON |
506 | suitable one (on demand), which will write and expect UTF-8 encoded JSON |
507 | texts. |
507 | texts. |
508 | |
508 | |
509 | Note that you are responsible to depend on the JSON module if you want to |
509 | Note that you are responsible to depend on the L<JSON> module if you want |
510 | use this functionality, as AnyEvent does not have a dependency itself. |
510 | to use this functionality, as AnyEvent does not have a dependency on it |
|
|
511 | itself. |
|
|
512 | |
|
|
513 | =item cbor => L<CBOR::XS> object |
|
|
514 | |
|
|
515 | This is the cbor coder object used by the C<cbor> read and write types. |
|
|
516 | |
|
|
517 | If you don't supply it, then AnyEvent::Handle will create and use a |
|
|
518 | suitable one (on demand), which will write CBOR without using extensions, |
|
|
519 | if possible. texts. |
|
|
520 | |
|
|
521 | Note that you are responsible to depend on the L<CBOR::XS> module if you |
|
|
522 | want to use this functionality, as AnyEvent does not have a dependency on |
|
|
523 | it itself. |
511 | |
524 | |
512 | =back |
525 | =back |
513 | |
526 | |
514 | =cut |
527 | =cut |
515 | |
528 | |
… | |
… | |
1053 | |
1066 | |
1054 | The generated JSON text is guaranteed not to contain any newlines: While |
1067 | The generated JSON text is guaranteed not to contain any newlines: While |
1055 | this module doesn't need delimiters after or between JSON texts to be |
1068 | this module doesn't need delimiters after or between JSON texts to be |
1056 | able to read them, many other languages depend on that. |
1069 | able to read them, many other languages depend on that. |
1057 | |
1070 | |
1058 | A simple RPC protocol that interoperates easily with others is to send |
1071 | A simple RPC protocol that interoperates easily with other languages is |
1059 | JSON arrays (or objects, although arrays are usually the better choice as |
1072 | to send JSON arrays (or objects, although arrays are usually the better |
1060 | they mimic how function argument passing works) and a newline after each |
1073 | choice as they mimic how function argument passing works) and a newline |
1061 | JSON text: |
1074 | after each JSON text: |
1062 | |
1075 | |
1063 | $handle->push_write (json => ["method", "arg1", "arg2"]); # whatever |
1076 | $handle->push_write (json => ["method", "arg1", "arg2"]); # whatever |
1064 | $handle->push_write ("\012"); |
1077 | $handle->push_write ("\012"); |
1065 | |
1078 | |
1066 | An AnyEvent::Handle receiver would simply use the C<json> read type and |
1079 | An AnyEvent::Handle receiver would simply use the C<json> read type and |
… | |
… | |
1069 | $handle->push_read (json => sub { my $array = $_[1]; ... }); |
1082 | $handle->push_read (json => sub { my $array = $_[1]; ... }); |
1070 | |
1083 | |
1071 | Other languages could read single lines terminated by a newline and pass |
1084 | Other languages could read single lines terminated by a newline and pass |
1072 | this line into their JSON decoder of choice. |
1085 | this line into their JSON decoder of choice. |
1073 | |
1086 | |
|
|
1087 | =item cbor => $perl_scalar |
|
|
1088 | |
|
|
1089 | Encodes the given scalar into a CBOR value. Unless you provide your own |
|
|
1090 | L<CBOR::XS> object, this means it will be encoded to a CBOR string not |
|
|
1091 | using any extensions, if possible. |
|
|
1092 | |
|
|
1093 | CBOR values are self-delimiting, so you can write CBOR at one end of |
|
|
1094 | a handle and read them at the other end without using any additional |
|
|
1095 | framing. |
|
|
1096 | |
|
|
1097 | A simple nd very very fast RPC protocol that interoperates with |
|
|
1098 | other languages is to send CBOR and receive CBOR values (arrays are |
|
|
1099 | recommended): |
|
|
1100 | |
|
|
1101 | $handle->push_write (cbor => ["method", "arg1", "arg2"]); # whatever |
|
|
1102 | |
|
|
1103 | An AnyEvent::Handle receiver would simply use the C<cbor> read type: |
|
|
1104 | |
|
|
1105 | $handle->push_read (cbor => sub { my $array = $_[1]; ... }); |
|
|
1106 | |
1074 | =cut |
1107 | =cut |
1075 | |
1108 | |
1076 | sub json_coder() { |
1109 | sub json_coder() { |
1077 | eval { require JSON::XS; JSON::XS->new->utf8 } |
1110 | eval { require JSON::XS; JSON::XS->new->utf8 } |
1078 | || do { require JSON; JSON->new->utf8 } |
1111 | || do { require JSON; JSON->new->utf8 } |
1079 | } |
1112 | } |
1080 | |
1113 | |
1081 | register_write_type json => sub { |
1114 | register_write_type json => sub { |
1082 | my ($self, $ref) = @_; |
1115 | my ($self, $ref) = @_; |
1083 | |
1116 | |
1084 | my $json = $self->{json} ||= json_coder; |
1117 | ($self->{json} ||= json_coder) |
1085 | |
|
|
1086 | $json->encode ($ref) |
1118 | ->encode ($ref) |
|
|
1119 | }; |
|
|
1120 | |
|
|
1121 | sub cbor_coder() { |
|
|
1122 | require CBOR::XS; |
|
|
1123 | CBOR::XS->new |
|
|
1124 | } |
|
|
1125 | |
|
|
1126 | register_write_type cbor => sub { |
|
|
1127 | my ($self, $scalar) = @_; |
|
|
1128 | |
|
|
1129 | ($self->{cbor} ||= cbor_coder) |
|
|
1130 | ->encode ($scalar) |
1087 | }; |
1131 | }; |
1088 | |
1132 | |
1089 | =item storable => $reference |
1133 | =item storable => $reference |
1090 | |
1134 | |
1091 | Freezes the given reference using L<Storable> and writes it to the |
1135 | Freezes the given reference using L<Storable> and writes it to the |
… | |
… | |
1485 | |
1529 | |
1486 | register_read_type line => sub { |
1530 | register_read_type line => sub { |
1487 | my ($self, $cb, $eol) = @_; |
1531 | my ($self, $cb, $eol) = @_; |
1488 | |
1532 | |
1489 | if (@_ < 3) { |
1533 | if (@_ < 3) { |
1490 | # this is more than twice as fast as the generic code below |
1534 | # this is faster then the generic code below |
1491 | sub { |
1535 | sub { |
1492 | $_[0]{rbuf} =~ s/^([^\015\012]*)(\015?\012)// or return; |
1536 | (my $pos = index $_[0]{rbuf}, "\012") >= 0 |
|
|
1537 | or return; |
1493 | |
1538 | |
|
|
1539 | (my $str = substr $_[0]{rbuf}, 0, $pos + 1, "") =~ s/(\015?\012)\Z// or die; |
1494 | $cb->($_[0], "$1", "$2"); |
1540 | $cb->($_[0], $str, "$1"); |
1495 | 1 |
1541 | 1 |
1496 | } |
1542 | } |
1497 | } else { |
1543 | } else { |
1498 | $eol = quotemeta $eol unless ref $eol; |
1544 | $eol = quotemeta $eol unless ref $eol; |
1499 | $eol = qr|^(.*?)($eol)|s; |
1545 | $eol = qr|^(.*?)($eol)|s; |
… | |
… | |
1682 | my ($self, $cb) = @_; |
1728 | my ($self, $cb) = @_; |
1683 | |
1729 | |
1684 | my $json = $self->{json} ||= json_coder; |
1730 | my $json = $self->{json} ||= json_coder; |
1685 | |
1731 | |
1686 | my $data; |
1732 | my $data; |
1687 | my $rbuf = \$self->{rbuf}; |
|
|
1688 | |
1733 | |
1689 | sub { |
1734 | sub { |
1690 | my $ref = eval { $json->incr_parse ($_[0]{rbuf}) }; |
1735 | my $ref = eval { $json->incr_parse ($_[0]{rbuf}) }; |
1691 | |
1736 | |
1692 | if ($ref) { |
1737 | if ($ref) { |
… | |
… | |
1706 | |
1751 | |
1707 | () |
1752 | () |
1708 | } else { |
1753 | } else { |
1709 | $_[0]{rbuf} = ""; |
1754 | $_[0]{rbuf} = ""; |
1710 | |
1755 | |
|
|
1756 | () |
|
|
1757 | } |
|
|
1758 | } |
|
|
1759 | }; |
|
|
1760 | |
|
|
1761 | =item cbor => $cb->($handle, $scalar) |
|
|
1762 | |
|
|
1763 | Reads a CBOR value, decodes it and passes it to the callback. When a parse |
|
|
1764 | error occurs, an C<EBADMSG> error will be raised. |
|
|
1765 | |
|
|
1766 | If a L<CBOR::XS> object was passed to the constructor, then that will be |
|
|
1767 | used for the final decode, otherwise it will create a CBOR coder without |
|
|
1768 | enabling any options. |
|
|
1769 | |
|
|
1770 | You have to provide a dependency to L<CBOR::XS> on your own: this module |
|
|
1771 | will load the L<CBOR::XS> module, but AnyEvent does not depend on it |
|
|
1772 | itself. |
|
|
1773 | |
|
|
1774 | Since CBOR values are fully self-delimiting, the C<cbor> read and write |
|
|
1775 | types are an ideal simple RPC protocol: just exchange CBOR datagrams. See |
|
|
1776 | the C<cbor> write type description, above, for an actual example. |
|
|
1777 | |
|
|
1778 | =cut |
|
|
1779 | |
|
|
1780 | register_read_type cbor => sub { |
|
|
1781 | my ($self, $cb) = @_; |
|
|
1782 | |
|
|
1783 | my $cbor = $self->{cbor} ||= cbor_coder; |
|
|
1784 | |
|
|
1785 | my $data; |
|
|
1786 | |
|
|
1787 | sub { |
|
|
1788 | my (@value) = eval { $cbor->incr_parse ($_[0]{rbuf}) }; |
|
|
1789 | |
|
|
1790 | if (@value) { |
|
|
1791 | $cb->($_[0], @value); |
|
|
1792 | |
|
|
1793 | 1 |
|
|
1794 | } elsif ($@) { |
|
|
1795 | # error case |
|
|
1796 | $cbor->incr_reset; |
|
|
1797 | |
|
|
1798 | $_[0]->_error (Errno::EBADMSG); |
|
|
1799 | |
|
|
1800 | () |
|
|
1801 | } else { |
1711 | () |
1802 | () |
1712 | } |
1803 | } |
1713 | } |
1804 | } |
1714 | }; |
1805 | }; |
1715 | |
1806 | |
… | |
… | |
1972 | sub _dotls { |
2063 | sub _dotls { |
1973 | my ($self) = @_; |
2064 | my ($self) = @_; |
1974 | |
2065 | |
1975 | my $tmp; |
2066 | my $tmp; |
1976 | |
2067 | |
1977 | if (length $self->{_tls_wbuf}) { |
2068 | while (length $self->{_tls_wbuf}) { |
1978 | while (($tmp = Net::SSLeay::write ($self->{tls}, $self->{_tls_wbuf})) > 0) { |
2069 | if (($tmp = Net::SSLeay::write ($self->{tls}, $self->{_tls_wbuf})) <= 0) { |
1979 | substr $self->{_tls_wbuf}, 0, $tmp, ""; |
2070 | $tmp = Net::SSLeay::get_error ($self->{tls}, $tmp); |
|
|
2071 | |
|
|
2072 | return $self->_tls_error ($tmp) |
|
|
2073 | if $tmp != $ERROR_WANT_READ |
|
|
2074 | && ($tmp != $ERROR_SYSCALL || $!); |
|
|
2075 | |
|
|
2076 | last; |
1980 | } |
2077 | } |
1981 | |
2078 | |
1982 | $tmp = Net::SSLeay::get_error ($self->{tls}, $tmp); |
2079 | substr $self->{_tls_wbuf}, 0, $tmp, ""; |
1983 | return $self->_tls_error ($tmp) |
|
|
1984 | if $tmp != $ERROR_WANT_READ |
|
|
1985 | && ($tmp != $ERROR_SYSCALL || $!); |
|
|
1986 | } |
2080 | } |
1987 | |
2081 | |
1988 | while (defined ($tmp = Net::SSLeay::read ($self->{tls}))) { |
2082 | while (defined ($tmp = Net::SSLeay::read ($self->{tls}))) { |
1989 | unless (length $tmp) { |
2083 | unless (length $tmp) { |
1990 | $self->{_on_starttls} |
2084 | $self->{_on_starttls} |
… | |
… | |
2004 | $self->{_tls_rbuf} .= $tmp; |
2098 | $self->{_tls_rbuf} .= $tmp; |
2005 | $self->_drain_rbuf; |
2099 | $self->_drain_rbuf; |
2006 | $self->{tls} or return; # tls session might have gone away in callback |
2100 | $self->{tls} or return; # tls session might have gone away in callback |
2007 | } |
2101 | } |
2008 | |
2102 | |
2009 | $tmp = Net::SSLeay::get_error ($self->{tls}, -1); |
2103 | $tmp = Net::SSLeay::get_error ($self->{tls}, -1); # -1 is not neccessarily correct, but Net::SSLeay doesn't tell us |
2010 | return $self->_tls_error ($tmp) |
2104 | return $self->_tls_error ($tmp) |
2011 | if $tmp != $ERROR_WANT_READ |
2105 | if $tmp != $ERROR_WANT_READ |
2012 | && ($tmp != $ERROR_SYSCALL || $!); |
2106 | && ($tmp != $ERROR_SYSCALL || $!); |
2013 | |
2107 | |
2014 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
2108 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |