-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.pm
251 lines (201 loc) · 4.82 KB
/
event.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package event;
use warnings;
use strict;
sub D() { 0 }
our $timei = time;
my @files;
my $nfiles;
my %timers;
my $quit = 0;
my $ri = '';
my $wi = '';
my $ro = '';
my $wo = '';
sub file_new {
my (%arg) = @_;
die 'should be called with fh'
if !$arg{fh};
die 'should be called with (std_)read_cb'
if !$arg{read_cb} && !$arg{std_read_cb};
my $fileno = $arg{fh}->fileno;
die 'bad fileno'
if !defined $fileno;
my $file = {
fileno => $fileno,
read_cb => \&file_read_cb,
write_cb => \&file_write_cb,
%arg,
};
$files[$fileno] = $file;
vec ($ri, $fileno, 1) = 1;
$nfiles++;
return $file;
}
sub file_write {
my ($file, $data) = @_;
$file->{dataout} .= $data;
$file->{writebytes} += length $data;
vec ($wi, $file->{fileno}, 1) = 1;
}
sub file_write_cb {
my ($file) = @_;
my $bytes = syswrite $file->{fh}, $file->{dataout};
die 'syswrite'
if !defined $bytes;
substr ($file->{dataout}, 0, $bytes) = '';
vec ($wi, $file->{fileno}, 1) = $file->{dataout} ne '';
}
sub file_pktwrite {
my ($file, $data) = @_;
vec ($wi, $file->{fileno}, 1) = 1;
push @{ $file->{datapktout} }, $data;
$file->{writebytes} += length $data;
}
sub file_pktwrite_cb {
my ($file) = @_;
return if !@{ $file->{datapktout} };
my $bytes = syswrite $file->{fh}, $file->{datapktout}[0];
die 'syswrite'
if !defined $bytes;
if ($bytes == length $file->{datapktout}[0]) {
shift @{ $file->{datapktout} };
vec ($wi, $file->{fileno}, 1) = @{ $file->{datapktout} } > 0;
} else {
substr ($file->{datapktout}[0], 0, $bytes) = '';
}
}
sub file_read_cb {
my ($file) = @_;
my $bytes = sysread $file->{fh}, my $data, 2**16, 0;
if (!$bytes) {
file_close_now ($file, "sysread $!(" . int ($!) . ')');
return;
}
$file->{readbytes} += $bytes;
$file->{datain} .= $data;
$file->{std_read_cb} ($file);
}
sub file_read {
my ($file, $bytes) = @_;
return length $file->{datain} < $bytes ? undef :
substr $file->{datain}, 0, $bytes, '';
}
sub server_new {
my (%arg) = @_;
die 'should be called with client'
if !exists $arg{client};
die 'should be called with client (std_)read_cb'
if !exists $arg{client}{read_cb} &&
!exists $arg{client}{std_read_cb};
return file_new (read_cb => \&file_accept_cb, %arg);
}
sub file_accept_cb {
my ($file) = @_;
my $new = $file->{fh}->accept;
die 'accept' if !defined $new;
die 'blocking' if !defined $new->blocking (0);
die 'binmode' if !binmode $new;
$file = file_new (fh => $new, %{ $file->{client} });
$file->{start_cb} ($file) if $file->{start_cb};
}
sub file_sock_send {
my ($file, $data, $to) = @_;
push @{ $file->{sockout} }, { data => $data, to => $to };
vec ($wi, $file->{fileno}, 1) = 1;
}
sub file_sock_write_cb {
my ($file) = @_;
my $dataref = $file->{sockout};
my $send = shift @$dataref
or die 'no data';
my $bytes = send $file->{fh}, $send->{data}, 0, $send->{to};
die 'send'
if !defined $bytes || $bytes != length $send->{data};
vec ($wi, $file->{fileno}, 1) = @$dataref > 0;
}
sub file_close {
my ($file, $reason) = @_;
$file->{closereason} = $reason;
vec ($ri, $file->{fileno}, 1) = 0;
vec ($ro, $file->{fileno}, 1) = 0;
$file->{close} = 1;
}
sub file_close_now {
my ($file, $reason) = @_;
$file->{closereason} = $reason if $reason;
my $i = $file->{fileno};
delete $files[$i];
vec ($_, $i, 1) = 0 for $ri, $wi, $ro, $wo;
$file->{fh}->close; $! = 0;
$file->{close_cb} ($file) if $file->{close_cb};
$nfiles--;
# undef %$file;
}
sub timer_new {
my (%arg) = @_;
die 'should be called with timer or period'
if !exists $arg{timer} && !exists $arg{period};
die 'should be called with cb'
if !exists $arg{cb};
$arg{timer} ||= $timei + ($arg{now} ? 0 : $arg{period})
if $arg{period};
my $timer = \%arg;
$timers{$timer} = $timer;
D && warn "debug $timer";
return $timer;
}
sub timer_del {
my ($timer) = @_;
D && warn "debug $timer" if $timer;
delete $timers{$timer};
# undef %$timer;
}
sub timer_reset {
my ($timer) = @_;
D && warn "debug $timer";
$timer->{timer} = $timei + $timer->{period};
}
sub loop_one {
D && warn "debug";
my ($t, $timer);
!$timer || $_->{timer} < $timer->{timer} and $timer = $_
for values %timers;
if ($timer) {
$t = $timer->{timer} - time;
if ($t <= 0) {
D && "timer cb $timer";
$timer->{cb} ($timer);
if ($timer->{period}) {
$timer->{timer} += $timer->{period};
} else {
timer_del ($timer);
}
return;
}
}
D && warn "select $t sec";
select ($ro = $ri, $wo = $wi, undef, $t) >= 0
or die 'select';
$timei = time;
for my $i (0 .. $#files) {
my $file = $files[$i];
next if !$file;
$file->{read_cb} ($file)
if vec $ro, $i, 1;
$file->{write_cb} ($file)
if vec $wo, $i, 1;
file_close_now ($file)
if $file->{close} && !vec $wi, $i, 1;
}
}
sub quit {
print "quiting, please wait\n";
$quit = 1;
}
sub loop {
$SIG{INT} = \&quit;
warn "started\n";
loop_one () while !$quit;
warn "stopped\n";
}
1;