-
Notifications
You must be signed in to change notification settings - Fork 22
/
Reading.pm
58 lines (45 loc) · 1.24 KB
/
Reading.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
package Reflex::Role::Reading;
use Reflex::Role;
attribute_parameter handle => "handle";
callback_parameter cb_data => qw( on handle data );
callback_parameter cb_error => qw( on handle error );
callback_parameter cb_closed => qw( on handle closed );
# Matches Reflex::Role::Readable's default callback.
# TODO - Any way we can coordinate this so it's obvious in the code
# but not too verbose?
method_parameter method_read => qw( on handle readable );
role {
my $p = shift;
my $h = $p->handle();
my $cb_data = $p->cb_data();
my $cb_error = $p->cb_error();
my $cb_closed = $p->cb_closed();
my $method_read = $p->method_read();
requires $cb_error;
method $method_read => sub {
my ($self, $arg) = @_;
my $octet_count = sysread($arg->{handle}, my $buffer = "", 65536);
# Got data.
if ($octet_count) {
$self->$cb_data({ data => $buffer });
return;
}
# EOF
if (defined $octet_count) {
$self->$cb_closed({ });
return;
}
# Quelle erreur!
$self->$cb_error(
{
errnum => ($! + 0),
errstr => "$!",
errfun => "sysread",
}
);
};
# Default callbacks that re-emit their parameters.
method_emit $cb_data => "data";
method_emit_and_stop $cb_closed => "closed";
};
1;