Using IO::Poll


 
Network Programming with Perl
By Lincoln  D.  Stein
Slots : 1
Table of Contents
Chapter  16.   IO::Poll

    Content

IO::Poll is a little like IO::Select turned inside out. With the IO::Select API, you create multiple IO::Select sets ”typically one each for reading and writing ”and monitor them with a call to IO::Select->select() . With IO::Poll, you create a single IO::Poll object and add filehandles to it one at a time, each with a mask that indicates the conditions you are interested in monitoring. You then call the IO::Poll object's poll() method, which blocks until one or more of the conditions is met. After poll() returns, you interrogate the object to learn which handles were affected.

A typical program begins like this:

 use IO::Poll qw(POLLIN POLLOUT POLLHUP); 

This loads the IO::Poll module and brings in the three constants POLLIN , POLLOUT , and POLLHUP . These constants will be used in forming a mask to indicate what conditions of filehandles you are interested in monitoring.

The next step is to create an IO::Poll object, then add to it the handle(s) you wish to monitor:

 my $poll = IO::Poll->new; poll->mask(\*STDIN  => POLLIN); $poll->mask(\*STDOUT => POLLOUT); $poll->mask($socket  => POLLINPOLLOUT); 
The mask() method is used both to add handles to the IO::Poll object and to remove them. It takes two arguments: the handle to be watched and a bitmask designating the conditions to monitor. In the example, STDIN is monitored for the POLLIN condition, STDOUT for the POLLOUT condition, and the handle named $socket is monitored for both POLLIN or POLLOUT events, formed by logically ORing the two constants. As described in more detail later, POLLIN and POLLOUT conditions occur when the handle is ready for reading or writing, respectively.

Having set up the IO::Poll object, you usually enter an I/O loop. Each time through the loop, you call the poll object's poll() method to wait for an event to occur and then call handles() to determine which handles were affected:

 while (1) {    $poll->poll();    my @readers = $poll->handles(POLLINPOLLHUPPOLLERR);    my @writers = $poll->handles(POLLOUT);    foreach (@readers) {       do_reader($_);    }    foreach (@writers) {       do_writers($_);    }  } 

The poll() method waits until one of the requested conditions becomes true and returns the number of handles that had events. As with select() , you can provide an optional timeout value to return if no events occur within a designated period. The handles() method returns all the handles that have conditions indicated by the passed bitmask. This example calls handles() twice using different bitmasks . The first checks for handles that are ready to be read from ( POLLIN ), those that were closed by the peer ( POLLHUP ), and those that have some other error ( POLLERR ). The second call looks for handles that are ready for writing. The remainder of the example loop processes these handles in an application-specific manner.

Like select(), poll() must be used with sysread () and syswrite() only. Mixing poll() with routines that use standard I/O buffering (the <> operator or plain read() and write() ) does not work.


   
Top


Network Programming with Perl
Network Programming with Perl
ISBN: 0201615711
EAN: 2147483647
Year: 2000
Pages: 173

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net