line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Event::IO::Server - general listener class, spawns client connections |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 METHODS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut |
8
|
|
|
|
|
|
|
package Event::IO::Server; |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
845
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
51
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
758
|
use Event; |
|
1
|
|
|
|
|
14755
|
|
|
1
|
|
|
|
|
6
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head2 new ( named parameters... ) |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=over 4 |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=item spawn |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Class for new connection objects, should inherit from Event::IO::Linear. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=item handle |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
IO::Socket handle for listener (should be ::INET or ::UNIX; it's a good idea |
27
|
|
|
|
|
|
|
to set ReuseAddr for INET clients). |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=item data |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Optional parameter, passed to child init_event. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=back |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=cut |
36
|
|
|
|
|
|
|
sub new { |
37
|
0
|
|
|
0
|
1
|
|
my ($class,%param) = @_; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# check parameters |
40
|
0
|
|
|
|
|
|
my ($spawn,$handle,$data) = delete @param{qw(spawn handle data)}; |
41
|
0
|
0
|
|
|
|
|
die 'unknown parameter(s): '.(join ', ',keys %param) if keys %param; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# create object |
44
|
0
|
|
0
|
|
|
|
my $self = bless { spawn => $spawn, data => $data, handle => $handle }, |
45
|
|
|
|
|
|
|
ref $class || $class; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# this is a listening socket |
48
|
0
|
|
|
|
|
|
$self->{handle}->listen(); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# we'd like to know when we get clients |
51
|
0
|
|
|
|
|
|
Event->io(fd => $self->{handle}, poll => 'r', cb => [$self,'client_event']); |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
return $self; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 data |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Get/set data parameter to pass to init_event (can also set in new). |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
62
|
|
|
|
|
|
|
sub data { |
63
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
64
|
0
|
0
|
|
|
|
|
$self->{data} = shift if @_; |
65
|
0
|
|
|
|
|
|
return $self->{data}; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 client_event |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Called when we get a new client (select 'read' event). |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=cut |
74
|
|
|
|
|
|
|
sub client_event { |
75
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
76
|
0
|
|
|
|
|
|
my $sock = $self->{handle}->accept(); |
77
|
0
|
|
|
|
|
|
my $client = $self->{spawn}->new(handle => $sock, init => 0); |
78
|
0
|
0
|
|
|
|
|
$client->init_event($self->{data}) if $client->can('init_event'); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 AUTHOR |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
David B. Robins Edbrobins@davidrobins.netE |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
1; |