home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / doc / libio-multiplex-perl / README < prev    next >
Encoding:
Text File  |  2002-02-04  |  3.0 KB  |  99 lines

  1. README for IO::Multiplex
  2.  
  3. IO::Multiplex is designed to take the effort out of managing
  4. multiple file handles.  It is essentially a really fancy front end to
  5. the C<select> system call.  In addition to maintaining the C<select>
  6. loop, it buffers all input and output to/from the file handles.  It
  7. can also accept incoming connections on one or more listen sockets.
  8.  
  9. It is object oriented in design, and will notify you of significant events
  10. by calling methods on an object that you supply.  If you are not using
  11. objects, you can simply supply __PACKAGE__ instead of an object reference.
  12.  
  13. You may have one callback object registered for each file handle, or
  14. one global one.  Possibly both -- the per-file handle callback object
  15. will be used instead of the global one.
  16.  
  17. Each file handle may also have a timer associated with it.  A callback
  18. function is called when the timer expires.
  19.  
  20. Here's an example which implements the beginnings of a multiuser game:
  21.  
  22.     use IO::Socket;
  23.     use IO::Multiplex;
  24.     use Tie::RefHash;
  25.     
  26.     my $mux  = new IO::Multiplex;
  27.  
  28.     # Create a listening socket
  29.     my $sock = new IO::Socket::INET(Proto     => 'tcp',
  30.                                     LocalPort => shift || 2300,
  31.                                     Listen    => 4)
  32.         or die "socket: $@";
  33.  
  34.     # We use the listen method instead of the add method.
  35.     $mux->listen($sock);
  36.     
  37.     $mux->set_callback_object(__PACKAGE__);
  38.     $mux->loop;
  39.     
  40.     # mux_connection is called when a new connection is accepted.
  41.     sub mux_connection {
  42.         my $package = shift;
  43.         my $mux     = shift;
  44.         my $fh      = shift;
  45.  
  46.         # Construct a new player object
  47.         Player->new($mux, $fh);
  48.     }
  49.  
  50.     package Player;
  51.  
  52.     my %players = ();
  53.  
  54.     sub new {
  55.         my $package = shift;
  56.         my $self    = bless { mux  => shift,
  57.                               fh   => shift } => $package;
  58.  
  59.         # Register the new player object as the callback specifically for
  60.         # this file handle.
  61.         $mux->set_callback_object($self, $self->{fh});
  62.         print $self->{fh}
  63.             "Greetings, Professor.  Would you like to play a game?\n";
  64.  
  65.         # Register this player object in the main list of players
  66.         $players{$self} = $self;
  67.         $mux->set_timeout($self->{fh}, 1);
  68.     }
  69.  
  70.     sub players { return values %players; }
  71.  
  72.     sub mux_input {
  73.         my $self = shift;
  74.         shift; shift;         # These two args are boring
  75.         my $input = shift;    # Scalar reference to the input
  76.  
  77.         # Process each line in the input, leaving partial lines
  78.         # in the input buffer
  79.         while ($$input =~ s/^(.*?\n)//) {
  80.             $self->process_command($1);
  81.         }
  82.     }
  83.  
  84.     sub mux_close {
  85.        my $self = shift;
  86.  
  87.        # Player disconnected;
  88.        # [Notify other players or something...]
  89.        delete $players{$self};
  90.     }
  91.     # This gets called every second to update player info, etc...
  92.     sub mux_timeout {
  93.         my $self = shift;
  94.         my $mux  = shift;
  95.         
  96.         $self->heartbeat;
  97.         $mux->set_timeout($self->{fh}, 1);
  98.     }
  99.