… | |
… | |
80 | |
80 | |
81 | =item on_eof => $cb->($handle) |
81 | =item on_eof => $cb->($handle) |
82 | |
82 | |
83 | Set the callback to be called when an end-of-file condition is detected, |
83 | Set the callback to be called when an end-of-file condition is detected, |
84 | i.e. in the case of a socket, when the other side has closed the |
84 | i.e. in the case of a socket, when the other side has closed the |
85 | connection cleanly. |
85 | connection cleanly, and there are no outstanding read requests in the |
|
|
86 | queue (if there are read requests, then an EOF counts as an unexpected |
|
|
87 | connection close and will be flagged as an error). |
86 | |
88 | |
87 | For sockets, this just means that the other side has stopped sending data, |
89 | For sockets, this just means that the other side has stopped sending data, |
88 | you can still try to write data, and, in fact, one can return from the EOF |
90 | you can still try to write data, and, in fact, one can return from the EOF |
89 | callback and continue writing data, as only the read part has been shut |
91 | callback and continue writing data, as only the read part has been shut |
90 | down. |
92 | down. |
91 | |
|
|
92 | While not mandatory, it is I<highly> recommended to set an EOF callback, |
|
|
93 | otherwise you might end up with a closed socket while you are still |
|
|
94 | waiting for data. |
|
|
95 | |
93 | |
96 | If an EOF condition has been detected but no C<on_eof> callback has been |
94 | If an EOF condition has been detected but no C<on_eof> callback has been |
97 | set, then a fatal error will be raised with C<$!> set to <0>. |
95 | set, then a fatal error will be raised with C<$!> set to <0>. |
98 | |
96 | |
99 | =item on_error => $cb->($handle, $fatal, $message) |
97 | =item on_error => $cb->($handle, $fatal, $message) |
… | |
… | |
140 | |
138 | |
141 | When an EOF condition is detected then AnyEvent::Handle will first try to |
139 | When an EOF condition is detected then AnyEvent::Handle will first try to |
142 | feed all the remaining data to the queued callbacks and C<on_read> before |
140 | feed all the remaining data to the queued callbacks and C<on_read> before |
143 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
141 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
144 | error will be raised (with C<$!> set to C<EPIPE>). |
142 | error will be raised (with C<$!> set to C<EPIPE>). |
|
|
143 | |
|
|
144 | Note that, unlike requests in the read queue, an C<on_read> callback |
|
|
145 | doesn't mean you I<require> some data: if there is an EOF and there |
|
|
146 | are outstanding read requests then an error will be flagged. With an |
|
|
147 | C<on_read> callback, the C<on_eof> callback will be invoked. |
145 | |
148 | |
146 | =item on_drain => $cb->($handle) |
149 | =item on_drain => $cb->($handle) |
147 | |
150 | |
148 | This sets the callback that is called when the write buffer becomes empty |
151 | This sets the callback that is called when the write buffer becomes empty |
149 | (or when the callback is set and the buffer is empty already). |
152 | (or when the callback is set and the buffer is empty already). |
… | |
… | |
512 | $self->{_activity} = $NOW; |
515 | $self->{_activity} = $NOW; |
513 | |
516 | |
514 | if ($self->{on_timeout}) { |
517 | if ($self->{on_timeout}) { |
515 | $self->{on_timeout}($self); |
518 | $self->{on_timeout}($self); |
516 | } else { |
519 | } else { |
517 | $self->_error (&Errno::ETIMEDOUT); |
520 | $self->_error (Errno::ETIMEDOUT); |
518 | } |
521 | } |
519 | |
522 | |
520 | # callback could have changed timeout value, optimise |
523 | # callback could have changed timeout value, optimise |
521 | return unless $self->{timeout}; |
524 | return unless $self->{timeout}; |
522 | |
525 | |
… | |
… | |
864 | |
867 | |
865 | if ( |
868 | if ( |
866 | defined $self->{rbuf_max} |
869 | defined $self->{rbuf_max} |
867 | && $self->{rbuf_max} < length $self->{rbuf} |
870 | && $self->{rbuf_max} < length $self->{rbuf} |
868 | ) { |
871 | ) { |
869 | $self->_error (&Errno::ENOSPC, 1), return; |
872 | $self->_error (Errno::ENOSPC, 1), return; |
870 | } |
873 | } |
871 | |
874 | |
872 | while () { |
875 | while () { |
873 | # we need to use a separate tls read buffer, as we must not receive data while |
876 | # we need to use a separate tls read buffer, as we must not receive data while |
874 | # we are draining the buffer, and this can only happen with TLS. |
877 | # we are draining the buffer, and this can only happen with TLS. |
… | |
… | |
878 | |
881 | |
879 | if (my $cb = shift @{ $self->{_queue} }) { |
882 | if (my $cb = shift @{ $self->{_queue} }) { |
880 | unless ($cb->($self)) { |
883 | unless ($cb->($self)) { |
881 | if ($self->{_eof}) { |
884 | if ($self->{_eof}) { |
882 | # no progress can be made (not enough data and no data forthcoming) |
885 | # no progress can be made (not enough data and no data forthcoming) |
883 | $self->_error (&Errno::EPIPE, 1), return; |
886 | $self->_error (Errno::EPIPE, 1), return; |
884 | } |
887 | } |
885 | |
888 | |
886 | unshift @{ $self->{_queue} }, $cb; |
889 | unshift @{ $self->{_queue} }, $cb; |
887 | last; |
890 | last; |
888 | } |
891 | } |
… | |
… | |
896 | && !@{ $self->{_queue} } # and the queue is still empty |
899 | && !@{ $self->{_queue} } # and the queue is still empty |
897 | && $self->{on_read} # but we still have on_read |
900 | && $self->{on_read} # but we still have on_read |
898 | ) { |
901 | ) { |
899 | # no further data will arrive |
902 | # no further data will arrive |
900 | # so no progress can be made |
903 | # so no progress can be made |
901 | $self->_error (&Errno::EPIPE, 1), return |
904 | $self->_error (Errno::EPIPE, 1), return |
902 | if $self->{_eof}; |
905 | if $self->{_eof}; |
903 | |
906 | |
904 | last; # more data might arrive |
907 | last; # more data might arrive |
905 | } |
908 | } |
906 | } else { |
909 | } else { |
… | |
… | |
1156 | return 1; |
1159 | return 1; |
1157 | } |
1160 | } |
1158 | |
1161 | |
1159 | # reject |
1162 | # reject |
1160 | if ($reject && $$rbuf =~ $reject) { |
1163 | if ($reject && $$rbuf =~ $reject) { |
1161 | $self->_error (&Errno::EBADMSG); |
1164 | $self->_error (Errno::EBADMSG); |
1162 | } |
1165 | } |
1163 | |
1166 | |
1164 | # skip |
1167 | # skip |
1165 | if ($skip && $$rbuf =~ $skip) { |
1168 | if ($skip && $$rbuf =~ $skip) { |
1166 | $data .= substr $$rbuf, 0, $+[0], ""; |
1169 | $data .= substr $$rbuf, 0, $+[0], ""; |
… | |
… | |
1182 | my ($self, $cb) = @_; |
1185 | my ($self, $cb) = @_; |
1183 | |
1186 | |
1184 | sub { |
1187 | sub { |
1185 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
1188 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
1186 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
1189 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
1187 | $self->_error (&Errno::EBADMSG); |
1190 | $self->_error (Errno::EBADMSG); |
1188 | } |
1191 | } |
1189 | return; |
1192 | return; |
1190 | } |
1193 | } |
1191 | |
1194 | |
1192 | my $len = $1; |
1195 | my $len = $1; |
… | |
… | |
1195 | my $string = $_[1]; |
1198 | my $string = $_[1]; |
1196 | $_[0]->unshift_read (chunk => 1, sub { |
1199 | $_[0]->unshift_read (chunk => 1, sub { |
1197 | if ($_[1] eq ",") { |
1200 | if ($_[1] eq ",") { |
1198 | $cb->($_[0], $string); |
1201 | $cb->($_[0], $string); |
1199 | } else { |
1202 | } else { |
1200 | $self->_error (&Errno::EBADMSG); |
1203 | $self->_error (Errno::EBADMSG); |
1201 | } |
1204 | } |
1202 | }); |
1205 | }); |
1203 | }); |
1206 | }); |
1204 | |
1207 | |
1205 | 1 |
1208 | 1 |
… | |
… | |
1295 | $json->incr_skip; |
1298 | $json->incr_skip; |
1296 | |
1299 | |
1297 | $self->{rbuf} = $json->incr_text; |
1300 | $self->{rbuf} = $json->incr_text; |
1298 | $json->incr_text = ""; |
1301 | $json->incr_text = ""; |
1299 | |
1302 | |
1300 | $self->_error (&Errno::EBADMSG); |
1303 | $self->_error (Errno::EBADMSG); |
1301 | |
1304 | |
1302 | () |
1305 | () |
1303 | } else { |
1306 | } else { |
1304 | $self->{rbuf} = ""; |
1307 | $self->{rbuf} = ""; |
1305 | |
1308 | |
… | |
… | |
1342 | # read remaining chunk |
1345 | # read remaining chunk |
1343 | $_[0]->unshift_read (chunk => $len, sub { |
1346 | $_[0]->unshift_read (chunk => $len, sub { |
1344 | if (my $ref = eval { Storable::thaw ($_[1]) }) { |
1347 | if (my $ref = eval { Storable::thaw ($_[1]) }) { |
1345 | $cb->($_[0], $ref); |
1348 | $cb->($_[0], $ref); |
1346 | } else { |
1349 | } else { |
1347 | $self->_error (&Errno::EBADMSG); |
1350 | $self->_error (Errno::EBADMSG); |
1348 | } |
1351 | } |
1349 | }); |
1352 | }); |
1350 | } |
1353 | } |
1351 | |
1354 | |
1352 | 1 |
1355 | 1 |
… | |
… | |
1450 | if ($self->{_on_starttls}) { |
1453 | if ($self->{_on_starttls}) { |
1451 | (delete $self->{_on_starttls})->($self, undef, $err); |
1454 | (delete $self->{_on_starttls})->($self, undef, $err); |
1452 | &_freetls; |
1455 | &_freetls; |
1453 | } else { |
1456 | } else { |
1454 | &_freetls; |
1457 | &_freetls; |
1455 | $self->_error (&Errno::EPROTO, 1, $err); |
1458 | $self->_error (Errno::EPROTO, 1, $err); |
1456 | } |
1459 | } |
1457 | } |
1460 | } |
1458 | |
1461 | |
1459 | # poll the write BIO and send the data if applicable |
1462 | # poll the write BIO and send the data if applicable |
1460 | # also decode read data if possible |
1463 | # also decode read data if possible |