… | |
… | |
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 |
… | |
… | |
593 | 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 |
594 | a queue. |
600 | a queue. |
595 | |
601 | |
596 | 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 |
597 | 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 |
598 | 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 |
599 | 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). |
600 | |
607 | |
601 | 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 |
602 | 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 |
603 | 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 |
604 | done its job (see C<push_read>, below). |
611 | done its job (see C<push_read>, below). |
… | |
… | |
622 | # handle xml |
629 | # handle xml |
623 | }); |
630 | }); |
624 | }); |
631 | }); |
625 | }); |
632 | }); |
626 | |
633 | |
627 | 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" |
628 | "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 |
629 | 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 |
630 | pipeline sending both requests and manipulate the queue as necessary in |
637 | just pipeline sending both requests and manipulate the queue as necessary |
631 | the callbacks: |
638 | in the callbacks. |
632 | |
639 | |
633 | # 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" |
634 | $handle->push_write ("request 1\015\012"); |
645 | $handle->push_write ("request 1\015\012"); |
635 | |
646 | |
636 | # 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 |
637 | $handle->push_read (line => sub { |
648 | $handle->push_read (line => sub { |
638 | # if we got an "OK", we have to _prepend_ another line, |
649 | # if we got an "OK", we have to _prepend_ another line, |
… | |
… | |
645 | ... |
656 | ... |
646 | }); |
657 | }); |
647 | } |
658 | } |
648 | }); |
659 | }); |
649 | |
660 | |
650 | # request two |
661 | # request two, simply returns 64 octets |
651 | $handle->push_write ("request 2\015\012"); |
662 | $handle->push_write ("request 2\015\012"); |
652 | |
663 | |
653 | # simply read 64 bytes, always |
664 | # simply read 64 bytes, always |
654 | $handle->push_read (chunk => 64, sub { |
665 | $handle->push_read (chunk => 64, sub { |
655 | my $response = $_[1]; |
666 | my $response = $_[1]; |