Debian Codegraph @

README

raw
1 NAME
2 IO::Event - Tied Filehandles for Nonblocking IO with Object Callbacks
3
4 SYNOPSIS
5 use IO::Event;
6 use IO::Event 'emulate_Event';
7 use IO::Event 'AnyEvent';
8
9 my $ioe = IO::Event->new($filehandle);
10
11 my $ioe = IO::Event::Socket::INET->new( [ARGS] )
12
13 my $ioe = IO::Event::Socket::UNIX->new( [ARGS] )
14
15 my $timer = IO::Event->timer(
16 [after => $seconds],
17 interval => $seconds,
18 cb => CODE);
19
20 my $idler = IO::Event->idle(
21 [min => $seconds],
22 [max => $seconds],
23 [reentrant => 0],
24 cb => CODE);
25
26 IO::Event::loop();
27
28 IO::Event::unloop_all();
29
30 DESCRIPTION
31 IO::Event provides a object-based callback system for handling
32 nonblocking IO. The design goal is to provide a system that just does
33 the right thing w/o the user needing to think about it much.
34
35 All APIs are kept as simple as possible yet at the same time, all
36 functionality is accesible if needed. Simple things are easy. Hard
37 things are possible.
38
39 Most of the time file handling syntax will work fine: "<$filehandle>"
40 and "print $filehandle 'stuff'".
41
42 IO::Event provides automatic buffering of output (with a callback to
43 throttle). It provides automatic line-at-a-time input.
44
45 After initial setup, call "IO::Event::loop()".
46
47 IO::Event was originally written to use Event. IO::Event still defaults
48 to using Event but it can now use AnyEvent or its own event loop.
49
50 CHOOSING AN EVENT HANDLER
51 Until you create your first IO::Event object, you can choose which
52 underlying event handler to use. The default is Event. To choose an
53 event handler, use one of the following lines, import
54 "no_emulate_Event", "emulate_Event", or "AnyEvent".
55
56 use IO::Event 'no_emulate_Event'
57 use IO::Event 'emulate_Event'
58 use IO::Event 'AnyEvent'
59
60 The "no_emulate_Event" option means: use Event. The "emulate_Event"
61 option means IO::Event should use its own event loop.
62
63 Why?
64
65 You should use AnyEvent if you want to have compatibility with other
66 event loops. You should use "emulate_Event" if you don't need
67 compatibility with other event loops and you have missing-event bugs
68 when using Event. You should use Event if it works for you.
69
70 The APIs are a bit different depending on which event loop you're using.
71
72 Event
73 To use Event's event loop:
74
75 use IO::Event 'no_emulate_Event';
76
77 or just:
78
79 use IO::Event
80
81 IO::Event's definition for "loop()", "timer()", "idle()" and
82 "unloop_all()" all default to the Event version unless "emulate_Event"
83 or "AnyEvent" have been imported. This allows you to easily switch back
84 and forth between Event's API and the others.
85
86 AnyEvent
87 To use AnyEvent's select loop, import "AnyEvent".
88
89 use IO::Event 'AnyEvent';
90
91 You can use AnyEvent's API directly or you can use IO::Event's emulated
92 APIs: "IO::Event::loop()", "IO::Event::unloop()", "IO::Event::timer()",
93 and "IO::Event::idle()". These behave like Event's routines of the same
94 name but use AnyEvent underneath.
95
96 During testing, using the pure-perl event loop of AnyEvent::Impl::Perl
97 from AnyEvent version 5.271, some read events were dropped. To work
98 around this, a synthetic read-ready event is dispatched for all
99 connected read filehandles every two seconds. Turn this off or adjust
100 its frequency by changing $IO::Event::AnyEvent::lost_event_hack. A
101 numeric value is the time (in seconds) between dispatching read events.
102 A false value turns off this performance-sapping hack.
103
104 AnyEvent only provides basic support for idle() events: it promises to
105 invoke them "every now and then".
106
107 "emulate_Event"
108 To use IO::Event's own select loop, import "emulate_Event".
109
110 use IO::Event 'emulate_Event';
111
112 IO::Event does not provide a complete emulation of everything that Event
113 does. It provides the full timer API:
114
115 my $timer = IO::Event::timer( [ARGS] )
116
117 instead of
118
119 my $timer = Event::timer( [ARGS] )
120
121 However it does not provide timer events on filehandles, nor does it
122 provide events for signals, or variable accesses.
123
124 Use "IO::Event::loop()" instead of "Event::loop()". Use
125 "IO::Event::unloop_all()" instead of "Event::unloop_all()". Use
126 "IO::Event::idle()" instead of "Event::idle()". It does not provide any
127 other methods or functions from Event. If you need them, please send a
128 patch.
129
130 CONSTRUCTORS
131 IO::Event->new($filehandle, [ $handler, [ $options ]])
132 The basic "new" constructor takes a filehandle and returns a
133 psuedo-filehandle. Treat the IO::Event object as a filehandle. Do
134 not use the original filehandle without good reason (let us know if
135 you find a good reason so we can fix the problem).
136
137 The handler is the class or object where you provide callback
138 functions to handle IO events. It defaults to the package of the
139 calling context.
140
141 If present, $options is a hash reference with the following possible
142 keys:
143
144 description A text description of this filehandle. Used for
145 debugging and error messages.
146
147 read_only Set to true if this is a read-only filehandle. Do not
148 accept output.
149
150 write_only Set to true if this is a write-only filehandle. Do not
151 attept to read.
152
153 autoread Set to 0 if this should not be an auto-read filehandle.
154
155 IO::Event::Socket::INET->new( [ARGS] )
156 This constructor uses IO::Socket::INET->new() to create a socket
157 using the ARGS provided. It returns an IO::Event object.
158
159 The handler defaults as above or can be set with an additional
160 pseudo-parameter for IO::Socket::UNIX->new(): "Handler". A
161 description for the socket can be provided with an additional
162 psuedo-parameter: "Description".
163
164 IO::Event::Socket::UNIX->new( [ARGS] )
165 This constructor uses IO::Socket::UNIX->new() to create a socket
166 using the ARGS provided. It returns an IO::Event object.
167
168 The handler defaults as above or can be set with an additional
169 pseudo-parameter for IO::Socket::UNIX->new(): "Handler". A
170 description for the socket can be provided with an additional
171 psuedo-parameter: "Description".
172
173 MANDATORY HANDLERS
174 These handler methods must be available in the handler object/class if
175 the situation in which they would be called arises.
176
177 ie_input($handler, $ioe, $input_buffer_reference)
178 Invoked when there is fresh data in the input buffer. The input can
179 be retrieved via directly reading it from $$input_buffer_reference
180 or via "read()" from the $ioe filehandle, or by using a variety of
181 standard methods for getting data:
182
183 <$ioe> like IO::Handle
184 $ioe->get() like Data::LineBuffer
185 $ioe->read() like IO::Handle
186 $ioe->sysread() like IO::Handle
187 $ioe->getline() like IO::Handle
188 $ioe->getlines() like IO::Handle
189 $ioe->getsome() see below
190 $ioe->ungets() like FileHandle::Unget
191
192 At end-of-file, ie_input will only be invoked once. There may or may
193 not be data in the input buffer.
194
195 ie_connection($handler, $ioe)
196 Invoked when a listen()ing socket is ready to accept(). It should
197 call accept:
198
199 sub ie_connection
200 {
201 my ($pkg, $ioe) = @_;
202 my $newfh = $ioe->accept()
203 }
204
205 ie_read_ready($handler, $ioe, $underlying_file_handle)
206 If autoreading is turned off then this will be invoked.
207
208 ie_werror($handler, $ioe, $output_buffer_reference)
209 A write error has occured when trying to drain the write buffer.
210 Provide an empty subroutine if you don't care.
211
212 OPTIONAL HANDLERS
213 These handler methods will be called if they are defined but it is not
214 required that they be defined.
215
216 ie_eof($handler, $ioe, $input_buffer_reference)
217 This is invoked when the read-side of the filehandle has been closed
218 by its source.
219
220 ie_output
221 This is invoked when data has just been written to the underlying
222 filehandle.
223
224 ie_outputdone
225 This is invoked when all pending data has just been written to the
226 underlying filehandle.
227
228 ie_connected
229 This is invoked when a "connect()" completes.
230
231 ie_connect_failed($handler, $ioe, $error_code)
232 This is invoked when a "connect()" fails. For a timeout, the error
233 code will be ETIMEOUT.
234
235 ie_died($handler, $ioe, $method, $@)
236 If another handler calls "die" then ie_died will be called with the
237 IO::Event object, the name of the method just invoked, and the die
238 string. If no ie_died() callback exists then execution will
239 terminate.
240
241 ie_timer
242 This is invoked for timer events.
243
244 ie_exception
245 Invoked when an exceptional condition arises on the underlying
246 filehandle
247
248 ie_outputoverflow($handler, $ioe, $overflowing,
249 $output_buffer_reference)
250 Invoked when there is too much output data and the output buffers
251 are overflowing. You can take some action to generate less output.
252 This will be invoked exactly once (with $overflowing == 1) when
253 there is too much data in the buffer and then exactly once again
254 (with $overflowing == 0) when there is no longer too much data in
255 the buffer.
256
257 METHODS
258 In addition to methods described in detail below, the following methods
259 behave like their "IO" (mostly "IO::Socket") counterparts (except for
260 being mostly non-blocking...):
261
262 connect
263 listen
264 open
265 read
266 sysread
267 syswrite
268 print
269 eof
270 shutdown
271
272 Through AUTOLOAD (see the SUBSTITUTED METHODS section) methods are
273 passed to underlying "Event" objects:
274
275 loop
276 unloop
277 and many more...
278
279 Through AUTOLOAD (see the SUBSTITUTED METHODS section) methods are
280 passed to underlying "IO" objects:
281
282 fileno
283 stat
284 truncate
285 error
286 opened
287 untaint
288 and many more...
289
290 IO::Event defines its own methods too:
291
292 ->accept($handler, %options)
293 accept() is nearly identical to the normal IO::Socket::accept()
294 method except that instead of optionally passing a class specifier
295 for the new socket, you optionally pass a handler object or class.
296 The returned filehandle is an IO::Event object.
297
298 Supported options:
299
300 description
301 Sets the description for the new socket
302
303 autoread
304 Set to 0 if you do not want auto-read
305
306 ->can_read($amount)
307 Returns true if $amount bytes worth of input is available for
308 reading. Note: this does not return true at EOF so be careful not to
309 hang forever at EOF.
310
311 ->getsome($amount)
312 Returns $amount bytes worth of input or undef if the request can't
313 be filled. Returns what it can at EOF.
314
315 ->get()
316 get() is like getline() except that it pre-chomp()s the results and
317 assumes the input_record_separator is "\n". This is like get() from
318 Data::LineBuffer.
319
320 ->unget()
321 Push chomp()ed lines back into the input buffer. This is like
322 unget() from Data::LineBuffer.
323
324 ->ungetline(), ->xungetc(), ->ungets()
325 This is what ungetc() should be: it pushes a string back into the
326 input buffer. This is unlike IO::Handle->ungetc which takes an
327 ordinal and pushes one character back into the the input buffer.
328 This is like FileHandle::Unget.
329
330 ->handler($new_handler)
331 Sets the handler object/class if $new_handler is provided. Returns
332 the old handler.
333
334 ->filehandle()
335 Returns the underlying "IO::Handle".
336
337 ->event()
338 Returns the underling "Event".
339
340 ->listener($listening)
341 Used to note that a filehandle is being used to listen for
342 connections (instead of receiving data). A passed parameter of 0
343 does the opposite. Returns the old value. This is mostly used
344 internally in IO::Event.
345
346 ->input_record_separator($new_sep)
347 IO::Handle doesn't allow input_record_separator's on a per
348 filehandle basis. IO::Event does. If you don't ever set a
349 filehandle's input record separator, then it contineously defaults
350 to the current value of $/. If you set it, then it will use your
351 value and never look at $/ again.
352
353 ->readevents($readevents)
354 Get/set listening for read-ready events on the underlying
355 filehandle. This could be used by ie_outputoverflow to control input
356 flows.
357
358 ->output_bufsize($output_bufsize)
359 Get/set the size of the output buffer.
360
361 ->autoread($autoread)
362 Get/set automatic reading if data when data can be read. Without
363 autoread turned on, the input buffer ins't filled and none of the
364 read methods will work. The point of this is for working with
365 non-data filehandles. This is an experts-only method that kinda
366 defeats the purpose of this module. This would be necessary using
367 recv() to get data.
368
369 ->drain()
370 Used to start looking for write-ready events on the underlying
371 filehandle. In normal operation this is handled automatically.
372 Deprecated: use writeevents(1) instead.
373
374 ->reentrant($reentrant)
375 Get/set reentrant callbacks. By default, IO::Event avoids making
376 reentrant callbacks. This is good because your code is less likely
377 to break. This is bad because you won't learn about things right
378 away. For example, you will not learn the the output buffer is
379 overflowing during print(). You'll have to wait for the output
380 buffer to begin draining to find out. This could be a problem.
381
382 ->close()
383 If there is output buffered, close will be delayed until the output
384 buffer drains.
385
386 ->forceclose
387 Close close immediately, even if there is output buffered.
388
389 ->ie_desc([new description])
390 Returns (and sets) the text description of the filehandle. For
391 debugging.
392
393 TIMER API
394 The following timer construction arguments are supported by IO::Event's
395 emulated event loop and IO::Event's API on top of AnyEvent:
396
397 cb A callback to invoke when the timer goes off. The callback can
398 either be a CODE reference or an array reference. If it's an array
399 reference, the array should be a two element tuple: the first
400 element is an object and the second object is a method to invoke on
401 the object. The only argument to the method call a reference to the
402 timer object:
403
404 my ($object, $method) = @{$timer->{cb}}
405 $object->$method($timer)
406
407 at A time at which to invoke the callback.
408
409 interval
410 An interval, in seconds between repeat invocations of the callback.
411
412 after
413 The interval until the first invocation of the callback. After that,
414 invoke every *interval*.
415
416 The following methods (from Event) are supported on timer objects:
417 start(), again(), now(), stop(), cancel(), is_cancelled(), is_running(),
418 is_suspended(), pending.
419
420 IDLE API
421 The following idle construction arguments are supported by IO::Event's
422 emulated event loop and IO::Event's API on top of AnyEvent:
423
424 cb A callback to invoke when the event loop is idle. The callback can
425 either be a CODE reference or an array reference. If it's an array
426 reference, the array should be a two element tuple: the first
427 element is an object and the second object is a method to invoke on
428 the object.
429
430 my ($object, $method) = @{$timer->{cb}}
431 $object->$method();
432
433 min The minimum time between invocations of the callback.
434
435 max The maximum time between invocations of the callback.
436
437 The following methods (from Event) are supported on idle objects:
438 start(), again(), now(), stop(), cancel(), is_cancelled(), is_running(),
439 is_suspended(), pending.
440
441 SUBSTITUED METHODS
442 Any method invocations that fail because the method isn't defined in
443 IO::Event will by tried twice more: once using trying for a method on
444 the inner (hidden) filehandle and once more trying for a method on the
445 Event object that's used to create the select loop for this module.
446
447 This dispatch is now deprecated with the choice of event handlers.
448
449 EXAMPLE SERVER
450 # This is a tcp line echo server
451
452 my $listener = IO::Event::Socket::INET->new(
453 Listen => 10,
454 Proto => 'tcp',
455 LocalPort => 2821,
456 );
457
458 Event::loop();
459
460 sub ie_connection
461 {
462 my ($pkg, $lstnr) = @_;
463 my $client = $lstnr->accept();
464 printf "accepted connection from %s:%s\n",
465 $client->peerhost, $client->peerport;
466 }
467
468 sub ie_input
469 {
470 my ($pkg, $client, $ibufref) = @_;
471 print $client <$client>;
472 }
473
474 SYSREAD and EOF
475 sysread() is incompatible with eof() because eof() uses getc(). Most of
476 the time this isn't a problem. In other words, some of the time this is
477 a problem: lines go missing.
478
479 For this reason, IO::Event never uses sysread(). In fact, if you ask it
480 to do a sysread() it does a read() for you instead.
481
482 On the other hand, at the current time no problems with syswrite have
483 come to light and IO::Event uses syswrite and never any other form of
484 write/print etc.
485
486 DESTRUCTION
487 IO::Event keeps copies of all of its registered filehandles. If you want
488 to close a filehandle, you'll need to actually call close on it.
489
490 DATA STRUCTURE
491 The filehandle object itself is a funny kind of hash reference. If you
492 want to use it to store your own data, you can. Please don't use hash
493 keys that begin "ie_" or "io_" as those are the prefixes used by
494 "IO::Event" and "IO::Socket".
495
496 The syntax is kinda funny:
497
498 ${*$filehandle}{'your_hash_key'}
499
500 SEE ALSO
501 For a different API on top of IO::Event, see IO::Event::Callback. It
502 uses IO::Event but provides a simpler and perhaps easier-to-use API.
503
504 The following perl modules do something that is kinda similar to what is
505 being done here:
506
507 AnyEvent::Handle, AnyEvent::AIO, IO::AIO, IO::Multiplex,
508 IO::NonBlocking, IO::Select Event, POE, POE::Component::Server::TCP,
509 Net::Socket::NonBlock, Net::Server::Multiplex, NetServer::Generic
510
511 The API borrows most heavily from IO::Multiplex. IO::Event uses Event.pm
512 and thus can be used in programs that are already using Event or POE.
513
514 Since the original writing of IO::Event, AnyEvent has been released and
515 now AnyEvent::AIO and <AnyEvent:Handle> should be considered the only
516 good alternatives to IO::Event.
517
518 For an example program using IO::Event, see IO::Event::rinetd which used
519 to be included in this package.
520
521 BUGS
522 The test suite only covers 40% of the code. The module is used by its
523 author and seems solid.
524
525 LICENSE
526 Copyright (C) 2002-2009 David Muir Sharnoff <[email protected]>. Copyright
527 (C) 2011-2013 Google, Inc. This module may be used/copied/etc on the
528 same terms as Perl itself.
529
530 This module is packaged for Fedora by Emmanuel Seyman
531 <[email protected]>
532
533 POD ERRORS
534 Hey! The above document had some coding errors, which are explained
535 below:
536
537 Around line 476:
538 '=item' outside of any '=over'
539