Commit | Line | Data |
8add82fc |
1 | # IO::Select.pm |
7a4c00b4 |
2 | # |
cf7fe8a2 |
3 | # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved. |
4 | # This program is free software; you can redistribute it and/or |
5 | # modify it under the same terms as Perl itself. |
8add82fc |
6 | |
7 | package IO::Select; |
8 | |
8add82fc |
9 | use strict; |
10 | use vars qw($VERSION @ISA); |
11 | require Exporter; |
12 | |
cf7fe8a2 |
13 | $VERSION = "1.13"; |
8add82fc |
14 | |
15 | @ISA = qw(Exporter); # This is only so we can do version checking |
16 | |
7a4c00b4 |
17 | sub VEC_BITS () {0} |
18 | sub FD_COUNT () {1} |
19 | sub FIRST_FD () {2} |
760ac839 |
20 | |
8add82fc |
21 | sub new |
22 | { |
23 | my $self = shift; |
24 | my $type = ref($self) || $self; |
25 | |
760ac839 |
26 | my $vec = bless [undef,0], $type; |
8add82fc |
27 | |
28 | $vec->add(@_) |
29 | if @_; |
30 | |
31 | $vec; |
32 | } |
33 | |
34 | sub add |
35 | { |
7a4c00b4 |
36 | shift->_update('add', @_); |
37 | } |
38 | |
39 | |
40 | sub remove |
41 | { |
42 | shift->_update('remove', @_); |
43 | } |
44 | |
45 | |
46 | sub exists |
47 | { |
8add82fc |
48 | my $vec = shift; |
4fdd9276 |
49 | my $fno = $vec->_fileno(shift); |
50 | return undef unless defined $fno; |
51 | $vec->[$fno + FIRST_FD]; |
7a4c00b4 |
52 | } |
8add82fc |
53 | |
760ac839 |
54 | |
7a4c00b4 |
55 | sub _fileno |
56 | { |
57 | my($self, $f) = @_; |
58 | $f = $f->[0] if ref($f) eq 'ARRAY'; |
59 | ($f =~ /^\d+$/) ? $f : fileno($f); |
8add82fc |
60 | } |
61 | |
7a4c00b4 |
62 | sub _update |
8add82fc |
63 | { |
64 | my $vec = shift; |
7a4c00b4 |
65 | my $add = shift eq 'add'; |
8add82fc |
66 | |
7a4c00b4 |
67 | my $bits = $vec->[VEC_BITS]; |
68 | $bits = '' unless defined $bits; |
69 | |
70 | my $count = 0; |
71 | my $f; |
8add82fc |
72 | foreach $f (@_) |
73 | { |
7a4c00b4 |
74 | my $fn = $vec->_fileno($f); |
75 | next unless defined $fn; |
76 | my $i = $fn + FIRST_FD; |
77 | if ($add) { |
78 | if (defined $vec->[$i]) { |
79 | $vec->[$i] = $f; # if array rest might be different, so we update |
80 | next; |
81 | } |
82 | $vec->[FD_COUNT]++; |
83 | vec($bits, $fn, 1) = 1; |
84 | $vec->[$i] = $f; |
85 | } else { # remove |
86 | next unless defined $vec->[$i]; |
87 | $vec->[FD_COUNT]--; |
88 | vec($bits, $fn, 1) = 0; |
89 | $vec->[$i] = undef; |
90 | } |
91 | $count++; |
8add82fc |
92 | } |
7a4c00b4 |
93 | $vec->[VEC_BITS] = $vec->[FD_COUNT] ? $bits : undef; |
94 | $count; |
8add82fc |
95 | } |
96 | |
97 | sub can_read |
98 | { |
99 | my $vec = shift; |
100 | my $timeout = shift; |
27d4819a |
101 | my $r = $vec->[VEC_BITS]; |
8add82fc |
102 | |
27d4819a |
103 | defined($r) && (select($r,undef,undef,$timeout) > 0) |
7a4c00b4 |
104 | ? handles($vec, $r) |
8add82fc |
105 | : (); |
106 | } |
107 | |
108 | sub can_write |
109 | { |
110 | my $vec = shift; |
111 | my $timeout = shift; |
27d4819a |
112 | my $w = $vec->[VEC_BITS]; |
8add82fc |
113 | |
27d4819a |
114 | defined($w) && (select(undef,$w,undef,$timeout) > 0) |
7a4c00b4 |
115 | ? handles($vec, $w) |
8add82fc |
116 | : (); |
117 | } |
118 | |
cf7fe8a2 |
119 | sub has_exception |
8add82fc |
120 | { |
121 | my $vec = shift; |
122 | my $timeout = shift; |
27d4819a |
123 | my $e = $vec->[VEC_BITS]; |
8add82fc |
124 | |
27d4819a |
125 | defined($e) && (select(undef,undef,$e,$timeout) > 0) |
7a4c00b4 |
126 | ? handles($vec, $e) |
8add82fc |
127 | : (); |
128 | } |
129 | |
cf7fe8a2 |
130 | sub has_error |
131 | { |
132 | require Carp; |
133 | Carp::carp("Call to depreciated method 'has_error', use 'has_exception'") |
134 | if $^W; |
135 | goto &has_exception; |
136 | } |
137 | |
760ac839 |
138 | sub count |
139 | { |
140 | my $vec = shift; |
141 | $vec->[FD_COUNT]; |
142 | } |
143 | |
7a4c00b4 |
144 | sub bits |
145 | { |
146 | my $vec = shift; |
147 | $vec->[VEC_BITS]; |
148 | } |
149 | |
150 | sub as_string # for debugging |
151 | { |
152 | my $vec = shift; |
153 | my $str = ref($vec) . ": "; |
154 | my $bits = $vec->bits; |
155 | my $count = $vec->count; |
156 | $str .= defined($bits) ? unpack("b*", $bits) : "undef"; |
157 | $str .= " $count"; |
158 | my @handles = @$vec; |
159 | splice(@handles, 0, FIRST_FD); |
160 | for (@handles) { |
161 | $str .= " " . (defined($_) ? "$_" : "-"); |
162 | } |
163 | $str; |
164 | } |
165 | |
8add82fc |
166 | sub _max |
167 | { |
168 | my($a,$b,$c) = @_; |
169 | $a > $b |
170 | ? $a > $c |
171 | ? $a |
172 | : $c |
173 | : $b > $c |
174 | ? $b |
175 | : $c; |
176 | } |
177 | |
178 | sub select |
179 | { |
180 | shift |
181 | if defined $_[0] && !ref($_[0]); |
182 | |
183 | my($r,$w,$e,$t) = @_; |
184 | my @result = (); |
185 | |
760ac839 |
186 | my $rb = defined $r ? $r->[VEC_BITS] : undef; |
7a4c00b4 |
187 | my $wb = defined $w ? $w->[VEC_BITS] : undef; |
188 | my $eb = defined $e ? $e->[VEC_BITS] : undef; |
8add82fc |
189 | |
190 | if(select($rb,$wb,$eb,$t) > 0) |
191 | { |
192 | my @r = (); |
193 | my @w = (); |
194 | my @e = (); |
760ac839 |
195 | my $i = _max(defined $r ? scalar(@$r)-1 : 0, |
196 | defined $w ? scalar(@$w)-1 : 0, |
197 | defined $e ? scalar(@$e)-1 : 0); |
8add82fc |
198 | |
760ac839 |
199 | for( ; $i >= FIRST_FD ; $i--) |
8add82fc |
200 | { |
760ac839 |
201 | my $j = $i - FIRST_FD; |
8add82fc |
202 | push(@r, $r->[$i]) |
760ac839 |
203 | if defined $rb && defined $r->[$i] && vec($rb, $j, 1); |
8add82fc |
204 | push(@w, $w->[$i]) |
760ac839 |
205 | if defined $wb && defined $w->[$i] && vec($wb, $j, 1); |
8add82fc |
206 | push(@e, $e->[$i]) |
760ac839 |
207 | if defined $eb && defined $e->[$i] && vec($eb, $j, 1); |
8add82fc |
208 | } |
209 | |
210 | @result = (\@r, \@w, \@e); |
211 | } |
212 | @result; |
213 | } |
214 | |
7a4c00b4 |
215 | |
216 | sub handles |
8add82fc |
217 | { |
218 | my $vec = shift; |
219 | my $bits = shift; |
220 | my @h = (); |
221 | my $i; |
7a4c00b4 |
222 | my $max = scalar(@$vec) - 1; |
8add82fc |
223 | |
7a4c00b4 |
224 | for ($i = FIRST_FD; $i <= $max; $i++) |
8add82fc |
225 | { |
226 | next unless defined $vec->[$i]; |
227 | push(@h, $vec->[$i]) |
7a4c00b4 |
228 | if !defined($bits) || vec($bits, $i - FIRST_FD, 1); |
8add82fc |
229 | } |
230 | |
231 | @h; |
232 | } |
233 | |
234 | 1; |
cf7fe8a2 |
235 | __END__ |
236 | |
237 | =head1 NAME |
238 | |
239 | IO::Select - OO interface to the select system call |
240 | |
241 | =head1 SYNOPSIS |
242 | |
243 | use IO::Select; |
244 | |
245 | $s = IO::Select->new(); |
246 | |
247 | $s->add(\*STDIN); |
248 | $s->add($some_handle); |
249 | |
250 | @ready = $s->can_read($timeout); |
251 | |
252 | @ready = IO::Select->new(@handles)->read(0); |
253 | |
254 | =head1 DESCRIPTION |
255 | |
256 | The C<IO::Select> package implements an object approach to the system C<select> |
257 | function call. It allows the user to see what IO handles, see L<IO::Handle>, |
258 | are ready for reading, writing or have an error condition pending. |
259 | |
260 | =head1 CONSTRUCTOR |
261 | |
262 | =over 4 |
263 | |
264 | =item new ( [ HANDLES ] ) |
265 | |
266 | The constructor creates a new object and optionally initialises it with a set |
267 | of handles. |
268 | |
269 | =back |
270 | |
271 | =head1 METHODS |
272 | |
273 | =over 4 |
274 | |
275 | =item add ( HANDLES ) |
276 | |
277 | Add the list of handles to the C<IO::Select> object. It is these values that |
278 | will be returned when an event occurs. C<IO::Select> keeps these values in a |
279 | cache which is indexed by the C<fileno> of the handle, so if more than one |
280 | handle with the same C<fileno> is specified then only the last one is cached. |
281 | |
282 | Each handle can be an C<IO::Handle> object, an integer or an array |
283 | reference where the first element is a C<IO::Handle> or an integer. |
284 | |
285 | =item remove ( HANDLES ) |
286 | |
287 | Remove all the given handles from the object. This method also works |
288 | by the C<fileno> of the handles. So the exact handles that were added |
289 | need not be passed, just handles that have an equivalent C<fileno> |
290 | |
291 | =item exists ( HANDLE ) |
292 | |
293 | Returns a true value (actually the handle itself) if it is present. |
294 | Returns undef otherwise. |
295 | |
296 | =item handles |
297 | |
298 | Return an array of all registered handles. |
299 | |
300 | =item can_read ( [ TIMEOUT ] ) |
301 | |
302 | Return an array of handles that are ready for reading. C<TIMEOUT> is |
303 | the maximum amount of time to wait before returning an empty list. If |
304 | C<TIMEOUT> is not given and any handles are registered then the call |
305 | will block. |
306 | |
307 | =item can_write ( [ TIMEOUT ] ) |
308 | |
309 | Same as C<can_read> except check for handles that can be written to. |
310 | |
311 | =item has_exception ( [ TIMEOUT ] ) |
312 | |
313 | Same as C<can_read> except check for handles that have an exception |
314 | condition, for example pending out-of-band data. |
315 | |
316 | =item count () |
317 | |
318 | Returns the number of handles that the object will check for when |
319 | one of the C<can_> methods is called or the object is passed to |
320 | the C<select> static method. |
321 | |
322 | =item bits() |
323 | |
324 | Return the bit string suitable as argument to the core select() call. |
325 | |
326 | =item select ( READ, WRITE, ERROR [, TIMEOUT ] ) |
327 | |
328 | C<select> is a static method, that is you call it with the package |
329 | name like C<new>. C<READ>, C<WRITE> and C<ERROR> are either C<undef> |
330 | or C<IO::Select> objects. C<TIMEOUT> is optional and has the same |
331 | effect as for the core select call. |
332 | |
333 | The result will be an array of 3 elements, each a reference to an array |
334 | which will hold the handles that are ready for reading, writing and have |
335 | error conditions respectively. Upon error an empty array is returned. |
336 | |
337 | =back |
338 | |
339 | =head1 EXAMPLE |
340 | |
341 | Here is a short example which shows how C<IO::Select> could be used |
342 | to write a server which communicates with several sockets while also |
343 | listening for more connections on a listen socket |
344 | |
345 | use IO::Select; |
346 | use IO::Socket; |
347 | |
348 | $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080); |
349 | $sel = new IO::Select( $lsn ); |
3cb6de81 |
350 | |
cf7fe8a2 |
351 | while(@ready = $sel->can_read) { |
352 | foreach $fh (@ready) { |
353 | if($fh == $lsn) { |
354 | # Create a new socket |
355 | $new = $lsn->accept; |
356 | $sel->add($new); |
357 | } |
358 | else { |
359 | # Process socket |
360 | |
361 | # Maybe we have finished with the socket |
362 | $sel->remove($fh); |
363 | $fh->close; |
364 | } |
365 | } |
366 | } |
367 | |
368 | =head1 AUTHOR |
369 | |
854822f1 |
370 | Graham Barr. Currently maintained by the Perl Porters. Please report all |
371 | bugs to <perl5-porters@perl.org>. |
cf7fe8a2 |
372 | |
373 | =head1 COPYRIGHT |
374 | |
375 | Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved. |
376 | This program is free software; you can redistribute it and/or |
377 | modify it under the same terms as Perl itself. |
378 | |
379 | =cut |
380 | |