line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::PSYC::Event::Event; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.1'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
6
|
1
|
|
|
1
|
|
1643
|
use Event qw(loop unloop); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Net::PSYC qw(W); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use base qw(Exporter); |
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw(init can_read can_write has_exception add remove start_loop stop_loop revoke); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my (%s, %revoke); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub can_read { |
15
|
|
|
|
|
|
|
croak('can_read() is not yet implemented by Net::PSYC::Event::Event'); |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub can_write { |
19
|
|
|
|
|
|
|
croak('can_write() is not yet implemented by Net::PSYC::Event::Event'); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub has_exception { |
23
|
|
|
|
|
|
|
croak('has_exception() is not yet implemented by Net::PSYC::Event::Event'); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# add (\*fd, flags, cb, repeat) |
27
|
|
|
|
|
|
|
sub add { |
28
|
|
|
|
|
|
|
my ($fd, $flags, $cb, $repeat) = @_; |
29
|
|
|
|
|
|
|
W2('add(%s, %s, %p, %d)', $fd, $flags, $cb, $repeat||0); |
30
|
|
|
|
|
|
|
if (!$flags || !$cb || !ref $cb eq 'CODE') { |
31
|
|
|
|
|
|
|
croak('Net::PSYC::Event::Event::add() requires flags and a callback!'); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $watcher; |
35
|
|
|
|
|
|
|
if ($flags eq 't') { |
36
|
|
|
|
|
|
|
$watcher = Event->timer( after => $fd, |
37
|
|
|
|
|
|
|
repeat => defined($repeat) ? $repeat : 0, |
38
|
|
|
|
|
|
|
cb => (!$repeat) |
39
|
|
|
|
|
|
|
? sub { remove(($watcher)); $cb->() } |
40
|
|
|
|
|
|
|
: sub { remove(($watcher)) unless $cb->() }); |
41
|
|
|
|
|
|
|
$s{'t'}->{$watcher} = $watcher; |
42
|
|
|
|
|
|
|
return $watcher; |
43
|
|
|
|
|
|
|
} elsif ($flags !~ /[^rew]/) { |
44
|
|
|
|
|
|
|
my $temp = substr($flags, 0, 1); |
45
|
|
|
|
|
|
|
my $count; |
46
|
|
|
|
|
|
|
my $sub = sub { |
47
|
|
|
|
|
|
|
if ($cb->($fd, $count++) == -1) { |
48
|
|
|
|
|
|
|
$watcher->now(); |
49
|
|
|
|
|
|
|
} else { |
50
|
|
|
|
|
|
|
$count = 0; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
}; |
53
|
|
|
|
|
|
|
$watcher = Event->io( fd => $fd, |
54
|
|
|
|
|
|
|
cb => $sub, |
55
|
|
|
|
|
|
|
poll => $flags, |
56
|
|
|
|
|
|
|
repeat => defined($repeat) ? $repeat : 1); |
57
|
|
|
|
|
|
|
foreach ('r', 'w', 'e') { |
58
|
|
|
|
|
|
|
next if ($flags !~ /$_/); |
59
|
|
|
|
|
|
|
$s{$_}->{($fd)} = $watcher; |
60
|
|
|
|
|
|
|
$revoke{$_}->{($fd)} = $watcher if (defined($repeat) && $repeat == 0); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} else { |
63
|
|
|
|
|
|
|
die "read the docu, you punk! '$flags' is _not_ a valid set of flags."; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
# revoke( \*fd[, flags] ) |
68
|
|
|
|
|
|
|
sub revoke { |
69
|
|
|
|
|
|
|
my $sock = shift; |
70
|
|
|
|
|
|
|
my $name = ($sock); |
71
|
|
|
|
|
|
|
my $flags = shift; |
72
|
|
|
|
|
|
|
W2('revoked %s', $name); |
73
|
|
|
|
|
|
|
foreach ('r', 'w', 'e') { |
74
|
|
|
|
|
|
|
next if($flags && !$flags =~ /$_/); |
75
|
|
|
|
|
|
|
$s{$_}->{$name}->again() if(exists $s{$_}->{$name}); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# remove ( \*fd[, flags] ) |
80
|
|
|
|
|
|
|
sub remove { |
81
|
|
|
|
|
|
|
my $sock = shift; |
82
|
|
|
|
|
|
|
my $name = ($sock); |
83
|
|
|
|
|
|
|
my $flags = shift; |
84
|
|
|
|
|
|
|
W2('removing %s', $name); |
85
|
|
|
|
|
|
|
foreach ('r', 'w', 'e', 't') { |
86
|
|
|
|
|
|
|
next if($flags && $flags !~ /$_/); |
87
|
|
|
|
|
|
|
next unless (exists $s{$_}->{$name}); |
88
|
|
|
|
|
|
|
$s{$_}->{$name}->cancel(); |
89
|
|
|
|
|
|
|
delete $s{$_}->{$name}; |
90
|
|
|
|
|
|
|
delete $revoke{$_}->{$name}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub start_loop { |
95
|
|
|
|
|
|
|
!loop(); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub stop_loop { |
99
|
|
|
|
|
|
|
unloop(); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |