NAME

IO::Async::Channel - pass values into or out from an IO::Async::Routine

DESCRIPTION

A IO::Async::Channel object allows Perl values to be passed into or out of an IO::Async::Routine. It is intended to be used primarily with a Routine object rather than independently. For more detail and examples on how to use this object see also the documentation for IO::Async::Routine.

A Channel object is shared between the main process of the program and the process running within the Routine. In the main process it will be used in asynchronous mode, and in the Routine process it will be used in synchronous mode. In asynchronous mode all methods return immediately and use IO::Async-style callback functions. In synchronous within the Routine process the methods block until they are ready and may be used for flow-control within the routine. Alternatively, a Channel may be shared between two different Routine objects, and not used directly by the controlling program.

The channel itself represents a FIFO of Perl reference values. New values may be put into the channel by the send method in either mode. Values may be retrieved from it by the recv method. Values inserted into the Channel are snapshot by the send method. Any changes to referred variables will not be observed by the other end of the Channel after the send method returns.

Since the channel uses Storable to serialise values to write over the communication filehandle only reference values may be passed. To pass a single scalar value, send a SCALAR reference to it, and dereference the result of recv.

CONSTRUCTOR

$channel = IO::Async::Channel->new

Returns a new IO::Async::Channel object. This object reference itself should be shared by both sides of a fork()ed process. After fork() the two setup_* methods may be used to configure the object for operation on either end.

While this object does in fact inherit from IO::Async::Notifier for implementation reasons it is not intended that this object be used as a Notifier. It should not be added to a Loop object directly; event management will be handled by its containing IO::Async::Routine object.

METHODS

$channel->configure( %params )

Similar to the standard configure method on IO::Async::Notifier, this is used to change details of the Channel's operation.

on_recv => CODE

May only be set on an async mode channel. If present, will be invoked whenever a new value is received, rather than using the recv method.

$on_recv->( $channel, $data )
on_eof => CODE

May only be set on an async mode channel. If present, will be invoked when the channel gets closed by the peer.

$on_eof->( $channel )

$channel->send( $data )

Pushes the data stored in the given Perl reference into the FIFO of the Channel, where it can be received by the other end. When called on a synchronous mode Channel this method may block if a write() call on the underlying filehandle blocks. When called on an asynchronous mode channel this method will not block.

$channel->send_frozen( $record )

A variant of the send method; this method pushes the byte record given. This should be the result of a call to Storable::freeze().

$data = $channel->recv

When called on a synchronous mode Channel this method will block until a Perl reference value is available from the other end and then return it. If the Channel is closed this method will return undef. Since only references may be passed and all Perl references are true the truth of the result of this method can be used to detect that the channel is still open and has not yet been closed.

$channel->recv( %args )

When called on an asynchronous mode Channel this method appends a callback function to the receiver queue to handle the next Perl reference value that becomes available from the other end. Takes the following named arguments:

on_recv => CODE

Called when a new Perl reference value is available. Will be passed the Channel object and the reference data.

$on_recv->( $channel, $data )
on_eof => CODE

Called if the Channel was closed before a new value was ready. Will be passed the Channel object.

$on_eof->( $channel )

$channel->close

Closes the channel. Causes a pending recv on the other end to return undef or the queued on_eof callbacks to be invoked.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>