| 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::Multi (); | ||
| 31 | |||
| 32 | #---------------------------------------------------------------------- | ||
| 33 | |||
| 34 | =head1 C<process( $READ_MASK, $WRITE_MASK )> | ||
| 35 | |||
| 36 | Instances of this class should pass the read and write bitmasks | ||
| 37 | to the C<process()> method that otherwise would be passed to Perl’s | ||
| 38 | C<select()> built-in. | ||
| 39 | |||
| 40 | =head1 METHODS | ||
| 41 | |||
| 42 | The following are added in addition to the base class methods: | ||
| 43 | |||
| 44 | =head2 ($rmask, $wmask, $emask) = I<OBJ>->get_vecs(); | ||
| 45 | |||
| 46 | Returns the bitmasks to use as input to C<select()>. | ||
| 47 | |||
| 48 | Note that, since these are copies of I<OBJ>’s internal values, you don’t | ||
| 49 | need to copy them again before calling C<select()>. | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub get_vecs { | ||
| 54 | my ($self) = @_; | ||
| 55 | |||
| 56 | return @{$self}{'rin', 'win', 'ein'}; | ||
| 57 | } | ||
| 58 | |||
| 59 | #---------------------------------------------------------------------- | ||
| 60 | |||
| 61 | =head2 @fds = I<OBJ>->get_fds(); | ||
| 62 | |||
| 63 | Returns the file descriptors that I<OBJ> tracks—or, in scalar context, the | ||
| 64 | count of such. Useful to check for exception events. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub get_fds { | ||
| 69 | return keys %{ $_[0]{'rfds'} }; | ||
| 70 | } | ||
| 71 | |||
| 72 | #---------------------------------------------------------------------- | ||
| 73 | |||
| 74 | =head2 @fds = I<OBJ>->get_timeout(); | ||
| 75 | |||
| 76 | Translates the base class’s implementation of this method to seconds | ||
| 77 | (since that’s what C<select()> expects). | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub get_timeout { | ||
| 82 | return $_[0]->SUPER::get_timeout() / 1000; | ||
| 83 | } | ||
| 84 | |||
| 85 | #---------------------------------------------------------------------- | ||
| 86 | |||
| 87 | sub _INIT { | ||
| 88 | my ($self) = @_; | ||
| 89 | |||
| 90 | $_ = q<> for @{$self}{ qw( rin win ein ) }; | ||
| 91 | |||
| 92 | $_ = {} for @{$self}{ qw( rfds wfds ) }; | ||
| 93 | |||
| 94 | return; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _GET_FD_ACTION { | ||
| 98 | my ($self, $args_ar) = @_; | ||
| 99 | |||
| 100 | my %fd_action; | ||
| 101 | |||
| 102 | $fd_action{$_} = Net::Curl::Multi::CURL_CSELECT_IN() for keys %{ $self->{'rfds'} }; | ||
| 103 | $fd_action{$_} += Net::Curl::Multi::CURL_CSELECT_OUT() for keys %{ $self->{'wfds'} }; | ||
| 104 | |||
| 105 | return \%fd_action; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _SET_POLL_IN { | ||
| 109 | my ($self, $fd) = @_; | ||
| 110 | $self->{'rfds'}{$fd} = $self->{'fds'}{$fd} = delete $self->{'wfds'}{$fd}; | ||
| 111 | |||
| 112 | vec( $self->{'rin'}, $fd, 1 ) = 1; | ||
| 113 | vec( $self->{'win'}, $fd, 1 ) = 0; | ||
| 114 | vec( $self->{'ein'}, $fd, 1 ) = 1; | ||
| 115 | |||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _SET_POLL_OUT { | ||
| 120 | my ($self, $fd) = @_; | ||
| 121 | $self->{'wfds'}{$fd} = $self->{'fds'}{$fd} = delete $self->{'rfds'}{$fd}; | ||
| 122 | |||
| 123 | vec( $self->{'rin'}, $fd, 1 ) = 0; | ||
| 124 | vec( $self->{'win'}, $fd, 1 ) = 1; | ||
| 125 | vec( $self->{'ein'}, $fd, 1 ) = 1; | ||
| 126 | |||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub _SET_POLL_INOUT { | ||
| 131 | my ($self, $fd) = @_; | ||
| 132 | $self->{'rfds'}{$fd} = $self->{'wfds'}{$fd} = $self->{'fds'}{$fd} = undef; | ||
| 133 | |||
| 134 | vec( $self->{'rin'}, $fd, 1 ) = 1; | ||
| 135 | vec( $self->{'win'}, $fd, 1 ) = 1; | ||
| 136 | vec( $self->{'ein'}, $fd, 1 ) = 1; | ||
| 137 | |||
| 138 | return; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _STOP_POLL { | ||
| 142 | my ($self, $fd) = @_; | ||
| 143 | delete $self->{'rfds'}{$fd}; | ||
| 144 | delete $self->{'wfds'}{$fd}; | ||
| 145 | delete $self->{'fds'}{$fd}; | ||
| 146 | |||
| 147 | vec( $self->{'rin'}, $fd, 1 ) = 0; | ||
| 148 | vec( $self->{'win'}, $fd, 1 ) = 0; | ||
| 149 | vec( $self->{'ein'}, $fd, 1 ) = 0; | ||
| 150 | |||
| 151 | return; | ||
| 152 | } | ||
| 153 | |||
| 154 | 1; | ||