… | |
… | |
14 | |
14 | |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
16 | |
16 | |
17 | =cut |
17 | =cut |
18 | |
18 | |
19 | our $VERSION = 4.14; |
19 | our $VERSION = 4.151; |
20 | |
20 | |
21 | =head1 SYNOPSIS |
21 | =head1 SYNOPSIS |
22 | |
22 | |
23 | use AnyEvent; |
23 | use AnyEvent; |
24 | use AnyEvent::Handle; |
24 | use AnyEvent::Handle; |
… | |
… | |
124 | This sets the callback that is called when the write buffer becomes empty |
124 | This sets the callback that is called when the write buffer becomes empty |
125 | (or when the callback is set and the buffer is empty already). |
125 | (or when the callback is set and the buffer is empty already). |
126 | |
126 | |
127 | To append to the write buffer, use the C<< ->push_write >> method. |
127 | To append to the write buffer, use the C<< ->push_write >> method. |
128 | |
128 | |
|
|
129 | This callback is useful when you don't want to put all of your write data |
|
|
130 | into the queue at once, for example, when you want to write the contents |
|
|
131 | of some file to the socket you might not want to read the whole file into |
|
|
132 | memory and push it into the queue, but instead only read more data from |
|
|
133 | the file when the write queue becomes empty. |
|
|
134 | |
129 | =item timeout => $fractional_seconds |
135 | =item timeout => $fractional_seconds |
130 | |
136 | |
131 | If non-zero, then this enables an "inactivity" timeout: whenever this many |
137 | If non-zero, then this enables an "inactivity" timeout: whenever this many |
132 | seconds pass without a successful read or write on the underlying file |
138 | seconds pass without a successful read or write on the underlying file |
133 | handle, the C<on_timeout> callback will be invoked (and if that one is |
139 | handle, the C<on_timeout> callback will be invoked (and if that one is |
… | |
… | |
241 | |
247 | |
242 | $self->{_activity} = AnyEvent->now; |
248 | $self->{_activity} = AnyEvent->now; |
243 | $self->_timeout; |
249 | $self->_timeout; |
244 | |
250 | |
245 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
251 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
|
|
252 | |
|
|
253 | $self->start_read |
|
|
254 | if $self->{on_read}; |
246 | |
255 | |
247 | $self |
256 | $self |
248 | } |
257 | } |
249 | |
258 | |
250 | sub _shutdown { |
259 | sub _shutdown { |
… | |
… | |
500 | =cut |
509 | =cut |
501 | |
510 | |
502 | register_write_type packstring => sub { |
511 | register_write_type packstring => sub { |
503 | my ($self, $format, $string) = @_; |
512 | my ($self, $format, $string) = @_; |
504 | |
513 | |
505 | pack "$format/a", $string |
514 | pack "$format/a*", $string |
506 | }; |
515 | }; |
507 | |
516 | |
508 | =item json => $array_or_hashref |
517 | =item json => $array_or_hashref |
509 | |
518 | |
510 | Encodes the given hash or array reference into a JSON object. Unless you |
519 | Encodes the given hash or array reference into a JSON object. Unless you |
… | |
… | |
544 | |
553 | |
545 | $self->{json} ? $self->{json}->encode ($ref) |
554 | $self->{json} ? $self->{json}->encode ($ref) |
546 | : JSON::encode_json ($ref) |
555 | : JSON::encode_json ($ref) |
547 | }; |
556 | }; |
548 | |
557 | |
|
|
558 | =item storable => $reference |
|
|
559 | |
|
|
560 | Freezes the given reference using L<Storable> and writes it to the |
|
|
561 | handle. Uses the C<nfreeze> format. |
|
|
562 | |
|
|
563 | =cut |
|
|
564 | |
|
|
565 | register_write_type storable => sub { |
|
|
566 | my ($self, $ref) = @_; |
|
|
567 | |
|
|
568 | require Storable; |
|
|
569 | |
|
|
570 | pack "w/a*", Storable::nfreeze ($ref) |
|
|
571 | }; |
|
|
572 | |
549 | =back |
573 | =back |
550 | |
574 | |
551 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
575 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
552 | |
576 | |
553 | This function (not method) lets you add your own types to C<push_write>. |
577 | This function (not method) lets you add your own types to C<push_write>. |
… | |
… | |
575 | ways, the "simple" way, using only C<on_read> and the "complex" way, using |
599 | ways, the "simple" way, using only C<on_read> and the "complex" way, using |
576 | a queue. |
600 | a queue. |
577 | |
601 | |
578 | In the simple case, you just install an C<on_read> callback and whenever |
602 | In the simple case, you just install an C<on_read> callback and whenever |
579 | new data arrives, it will be called. You can then remove some data (if |
603 | new data arrives, it will be called. You can then remove some data (if |
580 | enough is there) from the read buffer (C<< $handle->rbuf >>) if you want |
604 | enough is there) from the read buffer (C<< $handle->rbuf >>). Or you cna |
581 | or not. |
605 | leave the data there if you want to accumulate more (e.g. when only a |
|
|
606 | partial message has been received so far). |
582 | |
607 | |
583 | In the more complex case, you want to queue multiple callbacks. In this |
608 | In the more complex case, you want to queue multiple callbacks. In this |
584 | case, AnyEvent::Handle will call the first queued callback each time new |
609 | case, AnyEvent::Handle will call the first queued callback each time new |
585 | data arrives (also the first time it is queued) and removes it when it has |
610 | data arrives (also the first time it is queued) and removes it when it has |
586 | done its job (see C<push_read>, below). |
611 | done its job (see C<push_read>, below). |
… | |
… | |
604 | # handle xml |
629 | # handle xml |
605 | }); |
630 | }); |
606 | }); |
631 | }); |
607 | }); |
632 | }); |
608 | |
633 | |
609 | Example 2: Implement a client for a protocol that replies either with |
634 | Example 2: Implement a client for a protocol that replies either with "OK" |
610 | "OK" and another line or "ERROR" for one request, and 64 bytes for the |
635 | and another line or "ERROR" for the first request that is sent, and 64 |
611 | second request. Due tot he availability of a full queue, we can just |
636 | bytes for the second request. Due to the availability of a queue, we can |
612 | pipeline sending both requests and manipulate the queue as necessary in |
637 | just pipeline sending both requests and manipulate the queue as necessary |
613 | the callbacks: |
638 | in the callbacks. |
614 | |
639 | |
615 | # request one |
640 | When the first callback is called and sees an "OK" response, it will |
|
|
641 | C<unshift> another line-read. This line-read will be queued I<before> the |
|
|
642 | 64-byte chunk callback. |
|
|
643 | |
|
|
644 | # request one, returns either "OK + extra line" or "ERROR" |
616 | $handle->push_write ("request 1\015\012"); |
645 | $handle->push_write ("request 1\015\012"); |
617 | |
646 | |
618 | # we expect "ERROR" or "OK" as response, so push a line read |
647 | # we expect "ERROR" or "OK" as response, so push a line read |
619 | $handle->push_read (line => sub { |
648 | $handle->push_read (line => sub { |
620 | # if we got an "OK", we have to _prepend_ another line, |
649 | # if we got an "OK", we have to _prepend_ another line, |
… | |
… | |
627 | ... |
656 | ... |
628 | }); |
657 | }); |
629 | } |
658 | } |
630 | }); |
659 | }); |
631 | |
660 | |
632 | # request two |
661 | # request two, simply returns 64 octets |
633 | $handle->push_write ("request 2\015\012"); |
662 | $handle->push_write ("request 2\015\012"); |
634 | |
663 | |
635 | # simply read 64 bytes, always |
664 | # simply read 64 bytes, always |
636 | $handle->push_read (chunk => 64, sub { |
665 | $handle->push_read (chunk => 64, sub { |
637 | my $response = $_[1]; |
666 | my $response = $_[1]; |
… | |
… | |
1045 | the C<json> write type description, above, for an actual example. |
1074 | the C<json> write type description, above, for an actual example. |
1046 | |
1075 | |
1047 | =cut |
1076 | =cut |
1048 | |
1077 | |
1049 | register_read_type json => sub { |
1078 | register_read_type json => sub { |
1050 | my ($self, $cb, $accept, $reject, $skip) = @_; |
1079 | my ($self, $cb) = @_; |
1051 | |
1080 | |
1052 | require JSON; |
1081 | require JSON; |
1053 | |
1082 | |
1054 | my $data; |
1083 | my $data; |
1055 | my $rbuf = \$self->{rbuf}; |
1084 | my $rbuf = \$self->{rbuf}; |
… | |
… | |
1067 | 1 |
1096 | 1 |
1068 | } else { |
1097 | } else { |
1069 | $self->{rbuf} = ""; |
1098 | $self->{rbuf} = ""; |
1070 | () |
1099 | () |
1071 | } |
1100 | } |
|
|
1101 | } |
|
|
1102 | }; |
|
|
1103 | |
|
|
1104 | =item storable => $cb->($handle, $ref) |
|
|
1105 | |
|
|
1106 | Deserialises a L<Storable> frozen representation as written by the |
|
|
1107 | C<storable> write type (BER-encoded length prefix followed by nfreeze'd |
|
|
1108 | data). |
|
|
1109 | |
|
|
1110 | Raises C<EBADMSG> error if the data could not be decoded. |
|
|
1111 | |
|
|
1112 | =cut |
|
|
1113 | |
|
|
1114 | register_read_type storable => sub { |
|
|
1115 | my ($self, $cb) = @_; |
|
|
1116 | |
|
|
1117 | require Storable; |
|
|
1118 | |
|
|
1119 | sub { |
|
|
1120 | # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method |
|
|
1121 | defined (my $len = eval { unpack "w", $_[0]->{rbuf} }) |
|
|
1122 | or return; |
|
|
1123 | |
|
|
1124 | # remove prefix |
|
|
1125 | substr $_[0]->{rbuf}, 0, (length pack "w", $len), ""; |
|
|
1126 | |
|
|
1127 | # read rest |
|
|
1128 | $_[0]->unshift_read (chunk => $len, sub { |
|
|
1129 | if (my $ref = eval { Storable::thaw ($_[1]) }) { |
|
|
1130 | $cb->($_[0], $ref); |
|
|
1131 | } else { |
|
|
1132 | $self->_error (&Errno::EBADMSG); |
|
|
1133 | } |
|
|
1134 | }); |
1072 | } |
1135 | } |
1073 | }; |
1136 | }; |
1074 | |
1137 | |
1075 | =back |
1138 | =back |
1076 | |
1139 | |