Pair Networks


IO::Event - Tied Filehandles for Nonblocking IO with Object Callbacks


 use IO::Event;
 use IO::Event 'emulate_Event';
 use IO::Event 'AnyEvent';

 my $ioe = IO::Event->new($filehandle);

 my $ioe = IO::Event::Socket::INET->new( [ARGS] )

 my $ioe = IO::Event::Socket::UNIX->new( [ARGS] )

 my $timer = IO::Event->timer(
	[after => $seconds],
	interval => $seconds,
	cb => CODE);

 my $idler = IO::Event->idle(
	[min => $seconds], 
	[max => $seconds],
	[reentrant => 0],
	cb => CODE);




IO::Event provides a object-based callback system for handling nonblocking IO. The design goal is to provide a system that just does the right thing w/o the user needing to think about it much.

All APIs are kept as simple as possible yet at the same time, all functionality is accesible if needed. Simple things are easy. Hard things are possible.

Most of the time file handling syntax will work fine: <$filehandle> and print $filehandle 'stuff'.

IO::Event provides automatic buffering of output (with a callback to throttle). It provides automatic line-at-a-time input.

After initial setup, call IO::Event::loop().

IO::Event was originally written to use Event. IO::Event still defaults to using Event but it can now use AnyEvent or its own event loop.


Until you create your first IO::Event object, you can choose which underlying event handler to use. The default is Event. To choose an event handler, use one of the following lines, import no_emulate_Event, emulate_Event, or AnyEvent.

 use IO::Event 'no_emulate_Event'
 use IO::Event 'emulate_Event'
 use IO::Event 'AnyEvent'

The no_emulate_Event option means: use Event. The emulate_Event option means IO::Event should use its own event loop.


You should use AnyEvent if you want to have compatibility with other event loops. You should use emulate_Event if you don't need compatibility with other event loops and you have missing-event bugs when using Event. You should use Event if it works for you.

The APIs are a bit different depending on which event loop you're using.


To use Event's event loop:

 use IO::Event 'no_emulate_Event';

or just:

 use IO::Event

IO::Event's definition for loop(), timer(), idle() and unloop_all() all default to the Event version unless emulate_Event or AnyEvent have been imported. This allows you to easily switch back and forth between Event's API and the others.


To use AnyEvent's select loop, import AnyEvent.

 use IO::Event 'AnyEvent';

You can use AnyEvent's API directly or you can use IO::Event's emulated APIs: IO::Event::loop(), IO::Event::unloop(), IO::Event::timer(), and IO::Event::idle(). These behave like Event's routines of the same name but use AnyEvent underneath.

During testing, using the pure-perl event loop of AnyEvent::Impl::Perl from AnyEvent version 5.271, some read events were dropped. To work around this, a synthetic read-ready event is dispatched for all connected read filehandles every two seconds. Turn this off or adjust its frequency by changing $IO::Event::AnyEvent::lost_event_hack. A numeric value is the time (in seconds) between dispatching read events. A false value turns off this performance-sapping hack.

AnyEvent only provides basic support for idle() events: it promises to invoke them "every now and then".


To use IO::Event's own select loop, import emulate_Event.

 use IO::Event 'emulate_Event';

IO::Event does not provide a complete emulation of everything that Event does. It provides the full timer API:

 my $timer = IO::Event::timer( [ARGS] )

instead of

 my $timer = Event::timer( [ARGS] )

However it does not provide timer events on filehandles, nor does it provide events for signals, or variable accesses.

Use IO::Event::loop() instead of Event::loop(). Use IO::Event::unloop_all() instead of Event::unloop_all(). Use IO::Event::idle() instead of Event::idle(). It does not provide any other methods or functions from Event. If you need them, please send a patch.


IO::Event->new($filehandle, [ $handler, [ $options ]])

The basic new constructor takes a filehandle and returns a psuedo-filehandle. Treat the IO::Event object as a filehandle. Do not use the original filehandle without good reason (let us know if you find a good reason so we can fix the problem).

The handler is the class or object where you provide callback functions to handle IO events. It defaults to the package of the calling context.

If present, $options is a hash reference with the following possible keys:

A text description of this filehandle. Used for debugging and error messages.
Set to true if this is a read-only filehandle. Do not accept output.
Set to true if this is a write-only filehandle. Do not attept to read.
Set to 0 if this should not be an auto-read filehandle.
IO::Event::Socket::INET->new( [ARGS] )

This constructor uses IO::Socket::INET->new() to create a socket using the ARGS provided. It returns an IO::Event object.

The handler defaults as above or can be set with an additional pseudo-parameter for IO::Socket::UNIX->new(): Handler. A description for the socket can be provided with an additional psuedo-parameter: Description.

IO::Event::Socket::UNIX->new( [ARGS] )

This constructor uses IO::Socket::UNIX->new() to create a socket using the ARGS provided. It returns an IO::Event object.

The handler defaults as above or can be set with an additional pseudo-parameter for IO::Socket::UNIX->new(): Handler. A description for the socket can be provided with an additional psuedo-parameter: Description.


These handler methods must be available in the handler object/class if the situation in which they would be called arises.

ie_input($handler, $ioe, $input_buffer_reference)

Invoked when there is fresh data in the input buffer. The input can be retrieved via directly reading it from $$input_buffer_reference or via read() from the $ioe filehandle, or by using a variety of standard methods for getting data:

	<$ioe>			like IO::Handle
	$ioe->get()		like Data::LineBuffer
	$ioe->read()		like IO::Handle
	$ioe->sysread()		like IO::Handle
	$ioe->getline()		like IO::Handle
	$ioe->getlines()	like IO::Handle
	$ioe->getsome()		see below
	$ioe->ungets()		like FileHandle::Unget

At end-of-file, ie_input will only be invoked once. There may or may not be data in the input buffer.

ie_connection($handler, $ioe)

Invoked when a listen()ing socket is ready to accept(). It should call accept:

	sub ie_connection
		my ($pkg, $ioe) = @_;
		my $newfh = $ioe->accept()
ie_read_ready($handler, $ioe, $underlying_file_handle)
If autoreading is turned off then this will be invoked.
ie_werror($handler, $ioe, $output_buffer_reference)
A write error has occured when trying to drain the write buffer. Provide an empty subroutine if you don't care.


These handler methods will be called if they are defined but it is not required that they be defined.

ie_eof($handler, $ioe, $input_buffer_reference)
This is invoked when the read-side of the filehandle has been closed by its source.
This is invoked when data has just been written to the underlying filehandle.
This is invoked when all pending data has just been written to the underlying filehandle.
This is invoked when a connect() completes.
ie_connect_failed($handler, $ioe, $error_code)
This is invoked when a connect() fails. For a timeout, the error code will be ETIMEOUT.
ie_died($handler, $ioe, $method, $@)
If another handler calls die then ie_died will be called with the IO::Event object, the name of the method just invoked, and the die string. If no ie_died() callback exists then execution will terminate.
This is invoked for timer events.
Invoked when an exceptional condition arises on the underlying filehandle
ie_outputoverflow($handler, $ioe, $overflowing, $output_buffer_reference)
Invoked when there is too much output data and the output buffers are overflowing. You can take some action to generate less output. This will be invoked exactly once (with $overflowing == 1) when there is too much data in the buffer and then exactly once again (with $overflowing == 0) when there is no longer too much data in the buffer.


In addition to methods described in detail below, the following methods behave like their IO (mostly IO::Socket) counterparts (except for being mostly non-blocking...):


Through AUTOLOAD (see the SUBSTITUTED METHODS section) methods are passed to underlying Event objects:

	and many more...

Through AUTOLOAD (see the SUBSTITUTED METHODS section) methods are passed to underlying IO objects:

	and many more...

IO::Event defines its own methods too:

->accept($handler, %options)

accept() is nearly identical to the normal IO::Socket::accept() method except that instead of optionally passing a class specifier for the new socket, you optionally pass a handler object or class. The returned filehandle is an IO::Event object.

Supported options:

Sets the description for the new socket
Set to 0 if you do not want auto-read
Returns true if $amount bytes worth of input is available for reading. Note: this does not return true at EOF so be careful not to hang forever at EOF.
Returns $amount bytes worth of input or undef if the request can't be filled. Returns what it can at EOF.
get() is like getline() except that it pre-chomp()s the results and assumes the input_record_separator is "\n". This is like get() from Data::LineBuffer.
Push chomp()ed lines back into the input buffer. This is like unget() from Data::LineBuffer.
->ungetline(), ->xungetc(), ->ungets()
This is what ungetc() should be: it pushes a string back into the input buffer. This is unlike IO::Handle->ungetc which takes an ordinal and pushes one character back into the the input buffer. This is like FileHandle::Unget.
Sets the handler object/class if $new_handler is provided. Returns the old handler.
Returns the underlying IO::Handle.
Returns the underling Event.
Used to note that a filehandle is being used to listen for connections (instead of receiving data). A passed parameter of 0 does the opposite. Returns the old value. This is mostly used internally in IO::Event.
IO::Handle doesn't allow input_record_separator's on a per filehandle basis. IO::Event does. If you don't ever set a filehandle's input record separator, then it contineously defaults to the current value of $/. If you set it, then it will use your value and never look at $/ again.
Get/set listening for read-ready events on the underlying filehandle. This could be used by ie_outputoverflow to control input flows.
Get/set the size of the output buffer.
Get/set automatic reading if data when data can be read. Without autoread turned on, the input buffer ins't filled and none of the read methods will work. The point of this is for working with non-data filehandles. This is an experts-only method that kinda defeats the purpose of this module. This would be necessary using recv() to get data.
Used to start looking for write-ready events on the underlying filehandle. In normal operation this is handled automatically. Deprecated: use writeevents(1) instead.
Get/set reentrant callbacks. By default, IO::Event avoids making reentrant callbacks. This is good because your code is less likely to break. This is bad because you won't learn about things right away. For example, you will not learn the the output buffer is overflowing during print(). You'll have to wait for the output buffer to begin draining to find out. This could be a problem.
If there is output buffered, close will be delayed until the output buffer drains.
Close close immediately, even if there is output buffered.
->ie_desc([new description])
Returns (and sets) the text description of the filehandle. For debugging.