line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MojoX::LineStream; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
170817
|
use Mojo::Base 'Mojo::EventEmitter'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
16
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
2290
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
983
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has [qw(_buf debug stream)]; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
1
|
|
|
1
|
1
|
309
|
my ($class, %args) = @_; |
13
|
1
|
|
|
|
|
2
|
my $stream = $args{stream}; |
14
|
1
|
50
|
|
|
|
4
|
croak 'no stream?' unless $stream; |
15
|
1
|
|
|
|
|
8
|
my $self = $class->SUPER::new(); |
16
|
1
|
|
|
|
|
6
|
my $_buf = ''; |
17
|
1
|
|
|
|
|
5
|
$self->{_buf} = \$_buf; |
18
|
1
|
|
|
|
|
3
|
$self->{stream} = $stream; |
19
|
1
|
|
50
|
|
|
12
|
$self->{debug} = $args{debug} // 0; |
20
|
1
|
|
|
|
|
4
|
$stream->timeout(0); |
21
|
1
|
|
|
1
|
|
33
|
$stream->on(read => sub{ $self->on_read(@_); }); |
|
1
|
|
|
|
|
1074
|
|
22
|
1
|
|
|
0
|
|
11
|
$stream->on(close => sub{ $self->on_close(@_); }); |
|
0
|
|
|
|
|
0
|
|
23
|
1
|
|
|
|
|
7
|
return $self; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub on_read { |
27
|
1
|
|
|
1
|
0
|
3
|
my ($self, $stream, $bytes) = @_; |
28
|
1
|
|
|
|
|
5
|
my $_buf = $self->_buf; |
29
|
1
|
|
|
|
|
6
|
$$_buf .= $bytes; |
30
|
1
|
50
|
|
|
|
4
|
say '_buf now: ', $$_buf if $self->debug; |
31
|
1
|
50
|
|
|
|
26
|
return if $$_buf !~ /\n/; |
32
|
1
|
|
|
|
|
17
|
while ($$_buf =~ s/^([^\n]+)\n//) { |
33
|
1
|
|
|
|
|
6
|
my $line = $1; |
34
|
1
|
|
|
|
|
11
|
$self->emit(line => $line); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub on_close { |
39
|
0
|
|
|
0
|
0
|
0
|
my ($self, $stream) = @_; |
40
|
0
|
|
|
|
|
0
|
$self->emit(close => 1); |
41
|
0
|
0
|
|
|
|
0
|
say 'got close!' if $self->debug; |
42
|
0
|
|
|
|
|
0
|
delete $self->{stream}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub writeln { |
46
|
1
|
|
|
1
|
1
|
401
|
my ($self, $line) = @_; |
47
|
1
|
50
|
|
|
|
5
|
say 'writeln ', $line if $self->debug; |
48
|
1
|
|
|
|
|
9
|
$self->stream->write("$line\n"); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
1; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=encoding utf8 |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 NAME |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
MojoX::LineStream - Turn a (tcp) stream into a line based stream |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 SYNOPSIS |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
use MojoX::LineStream; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $clientid = Mojo::IOLoop->client({ |
64
|
|
|
|
|
|
|
port => $port, |
65
|
|
|
|
|
|
|
} => sub { |
66
|
|
|
|
|
|
|
my ($loop, $err, $stream) = @_; |
67
|
|
|
|
|
|
|
my $ls = MojoX::LineStream->new(stream => $stream); |
68
|
|
|
|
|
|
|
$ls->on(line => sub { |
69
|
|
|
|
|
|
|
my ($;s, $line) = @_; |
70
|
|
|
|
|
|
|
say 'got line: ', $line; |
71
|
|
|
|
|
|
|
... |
72
|
|
|
|
|
|
|
}); |
73
|
|
|
|
|
|
|
$ls->on(close => sub { |
74
|
|
|
|
|
|
|
say 'got close'; |
75
|
|
|
|
|
|
|
... |
76
|
|
|
|
|
|
|
}); |
77
|
|
|
|
|
|
|
}); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 DESCRIPTION |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
L is a wrapper around L that |
82
|
|
|
|
|
|
|
adds 'framing' based on lines terminated by a newline character. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 EVENTS |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
L inherits all events from L and can |
87
|
|
|
|
|
|
|
emit the following new ones. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 line |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
$ls->on(line => sub { |
92
|
|
|
|
|
|
|
my ($ls, $line) = @_; |
93
|
|
|
|
|
|
|
... |
94
|
|
|
|
|
|
|
}); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Emitted for every (full) line received on the underlying stream. The line |
97
|
|
|
|
|
|
|
passed on to the callback does not include the terminating newline |
98
|
|
|
|
|
|
|
character. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 close |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
$;s->on(close => sub { |
103
|
|
|
|
|
|
|
my $;s = shift; |
104
|
|
|
|
|
|
|
... |
105
|
|
|
|
|
|
|
}); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Emitted if the underlying stream gets closed. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
L implements the following attributes. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 stream |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
my $stream = $ls->stream; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
The underlying L-like stream |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head2 debug |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
$ls->debug = 1; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Enables or disables debugging output. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 METHODS |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
L inherits all methods from |
128
|
|
|
|
|
|
|
L and implements the following new ones. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 new |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
my $ls = MojoX::LineStream->new( |
133
|
|
|
|
|
|
|
stream => $stream, |
134
|
|
|
|
|
|
|
debug => $debug, |
135
|
|
|
|
|
|
|
); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Construct a new L object. The stream argument must |
138
|
|
|
|
|
|
|
behave like a L object. The debug argument is |
139
|
|
|
|
|
|
|
optional and just sets the debug attribute. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 writeln |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$ls->writeln($line); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Writes line to the underlying stream, adding a newline character at the end. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 SEE ALSO |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=over |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item * |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
L, L, L: the L Web framework |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=back |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENT |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
This software has been developed with support from L. |
160
|
|
|
|
|
|
|
In German: Diese Software wurde mit Unterstützung von L entwickelt. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 AUTHORS |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=over 4 |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=item * |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Wieger Opmeer |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=back |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
This software is copyright (c) 2016 by Wieger Opmeer. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
177
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=cut |
180
|
|
|
|
|
|
|
|