• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

contrib/H09-Apr-2015-194141

lib/IO/H09-Apr-2015-1,114428

t/H09-Apr-2015-365285

ChangesH A D09-Apr-20153.8 KiB127101

MANIFESTH A D02-Feb-2011137 1312

META.ymlH A D09-Apr-2015517 2221

Makefile.PLH A D03-Jun-2002643 2619

READMEH A D04-Feb-20023 KiB9975

TODOH A D31-Jul-2003407 1412

README

1README for IO::Multiplex
2
3IO::Multiplex is designed to take the effort out of managing
4multiple file handles.  It is essentially a really fancy front end to
5the C<select> system call.  In addition to maintaining the C<select>
6loop, it buffers all input and output to/from the file handles.  It
7can also accept incoming connections on one or more listen sockets.
8
9It is object oriented in design, and will notify you of significant events
10by calling methods on an object that you supply.  If you are not using
11objects, you can simply supply __PACKAGE__ instead of an object reference.
12
13You may have one callback object registered for each file handle, or
14one global one.  Possibly both -- the per-file handle callback object
15will be used instead of the global one.
16
17Each file handle may also have a timer associated with it.  A callback
18function is called when the timer expires.
19
20Here'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