blib/lib/IO/Async/Protocol/Stream.pm

filter: Statementstmt 100% 48 / 48 Branchbran 72.7% 16 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 93.5% 87 / 93 CC 14 SCAR 12.3
CC
14
Cov
92%
configure
7.0
write
4.0
_add_to_loop
2.1
CRAP 3.4
« blib/lib/IO/Async/Internals/TimeQueue.pm ↑ Summary blib/lib/IO/Async/Timer.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2010-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Protocol::Stream 0.805;
7  
8 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use base qw( IO::Async::Protocol );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 C<IO::Async::Protocol::Stream> - base class for stream-based protocols
18  
19 =head1 SYNOPSIS
20  
21 =for highlighter language=perl
22  
23 Most likely this class will be subclassed to implement a particular network
24 protocol.
25  
26 package Net::Async::HelloWorld;
27  
28 use strict;
29 use warnings;
30 use base qw( IO::Async::Protocol::Stream );
31  
32 sub on_read
33 {
34 my $self = shift;
35 my ( $buffref, $eof ) = @_;
36  
37 return 0 unless $$buffref =~ s/^(.*)\n//;
38 my $line = $1;
39  
40 if( $line =~ m/^HELLO (.*)/ ) {
41 my $name = $1;
42  
43 $self->invoke_event( on_hello => $name );
44 }
45  
46 return 1;
47 }
48  
49 sub send_hello
50 {
51 my $self = shift;
52 my ( $name ) = @_;
53  
54 $self->write( "HELLO $name\n" );
55 }
56  
57 This small example elides such details as error handling, which a real
58 protocol implementation would be likely to contain.
59  
60 =head1 DESCRIPTION
61  
62 This subclass of L<IO::Async::Protocol> is intended to stand as a base class
63 for implementing stream-based protocols. It provides an interface similar to
64 L<IO::Async::Stream>, primarily, a C<write> method and an C<on_read> event
65 handler.
66  
67 It contains an instance of an L<IO::Async::Stream> object which it uses for
68 actual communication, rather than being a subclass of it, allowing a level of
69 independence from the actual stream being used. For example, the stream may
70 actually be an L<IO::Async::SSLStream> to allow the protocol to be used over
71 SSL.
72  
73 As with L<IO::Async::Stream>, it is required that by the time the protocol
74 object is added to a Loop, that it either has an C<on_read> method, or has
75 been configured with an C<on_read> callback handler.
76  
77 =cut
78  
79 =head1 EVENTS
80  
81 The following events are invoked, either using subclass methods or CODE
82 references in parameters:
83  
84 =head2 $ret = on_read \$buffer, $eof
85  
86 =head2 on_read_eof
87  
88 =head2 on_write_eof
89  
90 The event handlers are invoked identically to L<IO::Async::Stream>.
91  
92 =head2 on_closed
93  
94 The C<on_closed> handler is optional, but if provided, will be invoked after
95 the stream is closed by either side (either because the C<close()> method has
96 been invoked on it, or on an incoming EOF).
97  
98 =cut
99  
100 =head1 PARAMETERS
101  
102 The following named parameters may be passed to C<new> or C<configure>:
103  
104 =head2 on_read => CODE
105  
106 =head2 on_read_eof => CODE
107  
108 =head2 on_write_eof => CODE
109  
110 CODE references for the events.
111  
112 =head2 handle => IO
113  
114 A shortcut for the common case where the transport only needs to be a plain
115 L<IO::Async::Stream> object. If this argument is provided without a
116 C<transport> object, a new L<IO::Async::Stream> object will be built around
117 the given IO handle, and used as the transport.
118  
119 =cut
120  
121 sub configure
122 {
123 12 ▶ my $self = shift;
Subroutine configure: called CC=7 SCAR=19.5
Pod documented
124 12 my %params = @_;
125  
126 12 for (qw( on_read on_read_eof on_write_eof )) {
127 36 ▶ $self->{$_} = delete $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 5 31 36
128 }
129  
130 12 ▶ if( !exists $params{transport} and my $handle = delete $params{handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $params{"transport"} and my $handle = delete $params{"handle"}) 2 10 12
Condition100%3/3
not exists $params{"transport"} and my $handle = delete $params{"handle"}
!ll&&!rl&&r
552
MC/DC100%2/2
not exists $params{"transport"} and my $handle = delete $params{"handle"}
not exists $params{"transport"} my $handle = delete $params{"handle"}
Truth table100%3/3
not(A) and B
A= not exists $params{"transport"}B= my $handle = delete $params{"handle"}
ABresult
0X0
100
111
131 2 require IO::Async::Stream;
132 2 $params{transport} = IO::Async::Stream->new( handle => $handle );
133 }
134  
135 12 $self->SUPER::configure( %params );
136  
137 12 ▶ if( $self->loop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->loop) 5 7 12
138 5 ▶ $self->can_event( "on_read" ) or
Branch: 1 branch
BranchTrueFalseTotal
$self->can_event("on_read") or croak("Expected either an on_read callback or to be able to ->on_read") 0 5 5
139 croak 'Expected either an on_read callback or to be able to ->on_read';
140 }
141 }
142  
143 sub _add_to_loop
144 {
145 5 ▶ my $self = shift;
Subroutine _add_to_loop: called CC=2 SCAR=7.2
146  
147 5 ▶ $self->can_event( "on_read" ) or
Branch: 1 branch
BranchTrueFalseTotal
$self->can_event("on_read") or croak("Expected either an on_read callback or to be able to ->on_read") 0 5 5
148 croak 'Expected either an on_read callback or to be able to ->on_read';
149 }
150  
151 sub setup_transport
152 {
153 6 ▶ my $self = shift;
Subroutine setup_transport: called CC=1 SCAR=0.1
Pod documented
154 6 my ( $transport ) = @_;
155  
156 6 $self->SUPER::setup_transport( $transport );
157  
158 $transport->configure(
159 on_read => $self->_replace_weakself( sub {
160 9 ▶ my $self = shift or return;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 9 9
161 9 $self->invoke_event( on_read => @_ );
162 } ),
163 on_read_eof => $self->_replace_weakself( sub {
164 2 ▶ my $self = shift or return;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 2 2
165 2 $self->maybe_invoke_event( on_read_eof => @_ );
166 } ),
167 on_write_eof => $self->_replace_weakself( sub {
168 1 ▶ my $self = shift or return;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 1 1
169 1 $self->maybe_invoke_event( on_write_eof => @_ );
170 6 } ),
171 );
172 }
173  
174 sub teardown_transport
175 {
176 1 ▶ my $self = shift;
Subroutine teardown_transport: called CC=1 SCAR=0.0
Pod documented
177 1 my ( $transport ) = @_;
178  
179 1 $transport->configure(
180 on_read => undef,
181 );
182  
183 1 $self->SUPER::teardown_transport( $transport );
184 }
185  
186 =head1 METHODS
187  
188 =cut
189  
190 =head2 write
191  
192 $protocol->write( $data );
193  
194 Writes the given data by calling the C<write> method on the contained
195 transport stream.
196  
197 =cut
198  
199 sub write
200 {
201 3 ▶ my $self = shift;
Subroutine write: called CC=4 SCAR=13.9
Pod documented
202 3 my ( $data, %args ) = @_;
203  
204 3 ▶ if( ref $data eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "CODE") 1 2 3
205 1 $data = $self->_replace_weakself( $data );
206 }
207  
208 3 ▶ if( $args{on_flush} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"on_flush"}) 1 2 3
209 1 $args{on_flush} = $self->_replace_weakself( $args{on_flush} );
210 }
211  
212 3 ▶ my $transport = $self->transport or croak "Attempted to ->write to a ".ref($self)." with no transport";
Branch: 1 branch
BranchTrueFalseTotal
my $transport = $self->transport or croak('Attempted to ->write to a ' . ref($self) . ' with no transport') 0 3 3
213 3 $transport->write( $data, %args );
214 }
215  
216 =head2 connect
217  
218 $protocol->connect( %args );
219  
220 Sets up a connection to a peer, and configures the underlying C<transport> for
221 the Protocol. Calls L<IO::Async::Protocol> C<connect> with C<socktype> set to
222 C<"stream">.
223  
224 =cut
225  
226 sub connect
227 {
228 1 ▶ my $self = shift;
Subroutine connect: called CC=1 SCAR=0.0
Pod documented
229 1 $self->SUPER::connect(
230 @_,
231 socktype => "stream",
232 );
233 }
234  
235 =head1 AUTHOR
236  
237 Paul Evans <leonerd@leonerd.org.uk>
238  
239 =cut
240  
241 3 0x55AA;
« blib/lib/IO/Async/Internals/TimeQueue.pm ↑ Summary blib/lib/IO/Async/Timer.pm »