| 1 | package Net::Curl::Promiser::Select; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | =encoding utf-8 | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Net::Curl::Promiser::Select | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | This module implements L<Net::Curl::Promiser> via Perl’s | ||
| 15 | L<select()|perlfunc/select> built-in. | ||
| 16 | |||
| 17 | See F</examples> in the distribution for a fleshed-out demonstration. | ||
| 18 | |||
| 19 | This is “the hard way” to do this, by the way. Your life will be simpler | ||
| 20 | if you use (or create) an event-loop-based implementation like | ||
| 21 | L<Net::Curl::Promiser::AnyEvent> or L<Net::Curl::Promiser::IOAsync>. | ||
| 22 | See F</examples> for comparisons. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | #---------------------------------------------------------------------- | ||
| 27 | |||
| 28 | 1 | ▶ | use parent 'Net::Curl::Promiser'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | |||
| 30 | use Net::Curl::Promiser::Backend::Select; | ||
| 31 | |||
| 32 | #---------------------------------------------------------------------- | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | The following are added in addition to the base class methods: | ||
| 37 | |||
| 38 | =head2 ($rmask, $wmask, $emask) = I<OBJ>->get_vecs(); | ||
| 39 | |||
| 40 | Returns the bitmasks to use as input to C<select()>. | ||
| 41 | |||
| 42 | Note that, since these are copies of I<OBJ>’s internal values, you don’t | ||
| 43 | need to copy them again before calling C<select()>. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub get_vecs { | ||
| 48 | return shift()->{'backend'}->get_vecs(); | ||
| 49 | } | ||
| 50 | |||
| 51 | #---------------------------------------------------------------------- | ||
| 52 | |||
| 53 | =head2 @fds = I<OBJ>->get_fds(); | ||
| 54 | |||
| 55 | Returns the file descriptors that I<OBJ> tracks—or, in scalar context, the | ||
| 56 | count of such. Useful to check for exception events. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub get_fds { | ||
| 61 | return shift()->{'backend'}->get_fds(); | ||
| 62 | } | ||
| 63 | |||
| 64 | #---------------------------------------------------------------------- | ||
| 65 | |||
| 66 | =head2 $obj = I<OBJ>->process( $READ_MASK, $WRITE_MASK ) | ||
| 67 | |||
| 68 | Tell the underlying L<Net::Curl::Multi> object which socket events have | ||
| 69 | happened. $READ_MASK and $WRITE_MASK are as “left” by Perl’s | ||
| 70 | C<select()> built-in. | ||
| 71 | |||
| 72 | If, in fact, no events have happened, then this calls | ||
| 73 | C<socket_action(CURL_SOCKET_TIMEOUT)> on the | ||
| 74 | L<Net::Curl::Multi> object (similar to C<time_out()>). | ||
| 75 | |||
| 76 | Finally, this reaps whatever pending HTTP responses may be ready and | ||
| 77 | resolves or rejects the corresponding Promise objects. | ||
| 78 | |||
| 79 | Returns I<OBJ>. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub process { | ||
| 84 | my ($self, @fd_action_args) = @_; | ||
| 85 | |||
| 86 | $self->{'backend'}->process( $self->{'multi'}, \@fd_action_args ); | ||
| 87 | |||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | #---------------------------------------------------------------------- | ||
| 92 | |||
| 93 | =head2 $is_active = I<OBJ>->time_out(); | ||
| 94 | |||
| 95 | Tell the underlying L<Net::Curl::Multi> object that a timeout happened, | ||
| 96 | and reap whatever pending HTTP responses may be ready. | ||
| 97 | |||
| 98 | Calls C<socket_action(CURL_SOCKET_TIMEOUT)> on the | ||
| 99 | underlying L<Net::Curl::Multi> object. The return is the same as | ||
| 100 | that operation returns. | ||
| 101 | |||
| 102 | Since C<process()> can also do the work of this function, a call to this | ||
| 103 | function is just an optimization. | ||
| 104 | |||
| 105 | This should only be called from event loop logic. | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub time_out { | ||
| 110 | my ($self) = @_; | ||
| 111 | |||
| 112 | return $self->{'backend'}->time_out( $self->{'multi'} ); | ||
| 113 | } | ||
| 114 | |||
| 115 | #---------------------------------------------------------------------- | ||
| 116 | |||
| 117 | =head2 $num = I<OBJ>->get_timeout() | ||
| 118 | |||
| 119 | Like libcurl’s L<curl_multi_timeout(3)>, but sometimes returns different | ||
| 120 | values depending on the needs of I<OBJ>. | ||
| 121 | |||
| 122 | (NB: This value is in I<seconds>, not milliseconds.) | ||
| 123 | |||
| 124 | This should only be called (if it’s called at all) from event loop logic. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | sub get_timeout { | ||
| 129 | my ($self) = @_; | ||
| 130 | |||
| 131 | my $timeout = $self->{'backend'}->get_timeout( $self->{'multi'} ); | ||
| 132 | |||
| 133 | return( ($timeout == -1) ? $timeout : $timeout / 1000 ); | ||
| 134 | } | ||
| 135 | |||
| 136 | #---------------------------------------------------------------------- | ||
| 137 | |||
| 138 | sub _INIT { | ||
| 139 | return Net::Curl::Promiser::Backend::Select->new(); | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | 1; | ||