File Coverage

blib/lib/Plack/Middleware/SocketIO/Handle.pm
Criterion Covered Total %
statement 9 67 13.4
branch 0 6 0.0
condition n/a
subroutine 3 18 16.6
pod 7 8 87.5
total 19 99 19.1


line stmt bran cond sub pod time code
1             package Plack::Middleware::SocketIO::Handle;
2              
3 1     1   6 use strict;
  1         2  
  1         33  
4 1     1   4 use warnings;
  1         2  
  1         25  
5              
6 1     1   1125 use AnyEvent::Handle;
  1         30438  
  1         756  
7              
8             sub new {
9 0     0 1   my $class = shift;
10 0           my ($fh) = @_;
11              
12 0           my $self = {handle => AnyEvent::Handle->new(fh => $fh)};
13 0           bless $self, $class;
14              
15 0           $fh->autoflush;
16              
17 0           $self->{handle}->no_delay(1);
18 0     0     $self->{handle}->on_eof(sub { warn "Unhandled handle eof" });
  0            
19 0     0     $self->{handle}->on_error(sub { warn "Unhandled handle error: $_[2]" });
  0            
20              
21             # This is needed for the correct EOF handling
22 0     0     $self->{handle}->on_read(sub { });
  0            
23              
24 0           return $self;
25             }
26              
27             sub heartbeat_timeout {
28 0     0 1   my $self = shift;
29 0           my ($timeout) = @_;
30              
31 0           $self->{heartbeat_timeout} = $timeout;
32              
33 0           return $self;
34             }
35              
36             sub on_heartbeat {
37 0     0 1   my $self = shift;
38 0           my ($cb) = @_;
39              
40 0           $self->{handle}->timeout($self->{heartbeat_timeout});
41 0           $self->{handle}->on_timeout($cb);
42              
43 0           return $self;
44             }
45              
46             sub on_read {
47 0     0 1   my $self = shift;
48 0           my ($cb) = @_;
49              
50             $self->{handle}->on_read(
51             sub {
52 0     0     my $handle = shift;
53              
54             $handle->push_read(
55             sub {
56 0           $cb->($self, $_[0]->rbuf);
57             }
58 0           );
59             }
60 0           );
61              
62 0           return $self;
63             }
64              
65             sub on_eof {
66 0     0 1   my $self = shift;
67 0           my ($cb) = @_;
68              
69             $self->{handle}->on_eof(
70             sub {
71 0     0     $cb->($self);
72             }
73 0           );
74              
75 0           return $self;
76             }
77              
78             sub on_error {
79 0     0 0   my $self = shift;
80 0           my ($cb) = @_;
81              
82             $self->{handle}->on_error(
83             sub {
84 0     0     $cb->($self);
85             }
86 0           );
87              
88 0           return $self;
89             }
90              
91             sub write {
92 0     0 1   my $self = shift;
93 0           my ($chunk, $cb) = @_;
94              
95 0           my $handle = $self->{handle};
96 0 0         return $self unless $handle;
97              
98 0           $handle->push_write($chunk);
99              
100 0 0         if ($cb) {
101             $handle->on_drain(
102             sub {
103 0     0     $self->{handle}->on_drain(undef);
104              
105 0           $cb->($self);
106             }
107 0           );
108             }
109              
110 0           return $self;
111             }
112              
113             sub close {
114 0     0 1   my $self = shift;
115              
116 0           my $handle = delete $self->{handle};
117 0 0         return $self unless $handle;
118              
119 0           $handle->timeout_reset;
120              
121 0           shutdown $handle->fh, 2;
122 0           close $handle->fh;
123              
124 0           $handle->destroy;
125 0           undef $handle;
126              
127 0           return $self;
128             }
129              
130             1;
131             __END__