1 package Object::Remote::ReadChannel;
4 use Scalar::Util qw(weaken openhandle);
5 use Object::Remote::Logging qw(:log :dlog);
9 is => 'ro', required => 1,
13 log_trace { "Watching filehandle via trigger on 'fh' attribute in Object::Remote::ReadChannel" };
14 Object::Remote->current_loop
17 on_read_ready => sub { $self->_receive_data_from($fh) }
22 has on_close_call => (
23 is => 'rw', default => sub { sub {} },
26 has on_line_call => (is => 'rw');
28 has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x });
30 sub _receive_data_from {
32 Dlog_trace { "Preparing to read data from $_" } $fh;
33 my $rb = $self->_receive_data_buffer;
34 my $len = sysread($fh, $$rb, 32768, length($$rb));
35 my $err = defined($len) ? 'eof' : ": $!";
36 if (defined($len) and $len > 0) {
37 log_trace { "Read $len bytes of data" };
38 while (my $cb = $self->on_line_call and $$rb =~ s/^(.*)\n//) {
42 log_trace { "Got EOF or error, this read channel is done" };
43 Object::Remote->current_loop
48 log_trace { "Invoking on_close_call() for dead read channel" };
49 $self->on_close_call->($err);
56 log_trace { "read channel is being demolished" };
58 Object::Remote->current_loop