| 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, 2011-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package IO::Async::Channel 0.805; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use base qw( IO::Async::Notifier ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 15 | ▶ | use IO::Async::Stream; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | C<IO::Async::Channel> - pass values into or out from an L<IO::Async::Routine> | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =for highlighter language=perl | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | A C<IO::Async::Channel> object allows Perl values to be passed into or out of | ||||||||||||||||||||||||||||
| 25 | an L<IO::Async::Routine>. It is intended to be used primarily with a Routine | ||||||||||||||||||||||||||||
| 26 | object rather than independently. For more detail and examples on how to use | ||||||||||||||||||||||||||||
| 27 | this object see also the documentation for L<IO::Async::Routine>. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | A Channel object is shared between the main process of the program and the | ||||||||||||||||||||||||||||
| 30 | process running within the Routine. In the main process it will be used in | ||||||||||||||||||||||||||||
| 31 | asynchronous mode, and in the Routine process it will be used in synchronous | ||||||||||||||||||||||||||||
| 32 | mode. In asynchronous mode all methods return immediately and use | ||||||||||||||||||||||||||||
| 33 | L<IO::Async>-style futures or callback functions. In synchronous within the | ||||||||||||||||||||||||||||
| 34 | Routine process the methods block until they are ready and may be used for | ||||||||||||||||||||||||||||
| 35 | flow-control within the routine. Alternatively, a Channel may be shared | ||||||||||||||||||||||||||||
| 36 | between two different Routine objects, and not used directly by the | ||||||||||||||||||||||||||||
| 37 | controlling program. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | The channel itself represents a FIFO of Perl reference values. New values may | ||||||||||||||||||||||||||||
| 40 | be put into the channel by the C<send> method in either mode. Values may be | ||||||||||||||||||||||||||||
| 41 | retrieved from it by the C<recv> method. Values inserted into the Channel are | ||||||||||||||||||||||||||||
| 42 | snapshot by the C<send> method. Any changes to referred variables will not be | ||||||||||||||||||||||||||||
| 43 | observed by the other end of the Channel after the C<send> method returns. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 PARAMETERS | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | The following named parameters may be passed to C<new> or C<configure>: | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head2 codec => STR | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Gives the name of the encoding method used to represent values over the | ||||||||||||||||||||||||||||
| 52 | channel. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | This can be set to C<Storable> to use the core L<Storable> module. As this | ||||||||||||||||||||||||||||
| 55 | only supports references, to pass a single scalar value, C<send> a SCALAR | ||||||||||||||||||||||||||||
| 56 | reference to it, and dereference the result of C<recv>. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | If the L<Sereal::Encoder> and L<Sereal::Decoder> modules are installed, this | ||||||||||||||||||||||||||||
| 59 | can be set to C<Sereal> instead, and will use those to perform the encoding | ||||||||||||||||||||||||||||
| 60 | and decoding. This optional dependency may give higher performance than using | ||||||||||||||||||||||||||||
| 61 | C<Storable>. If these modules are available, then this option is picked by | ||||||||||||||||||||||||||||
| 62 | default. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head2 new | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | $channel = IO::Async::Channel->new; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Returns a new C<IO::Async::Channel> object. This object reference itself | ||||||||||||||||||||||||||||
| 75 | should be shared by both sides of a C<fork()>ed process. After C<fork()> the | ||||||||||||||||||||||||||||
| 76 | two C<setup_*> methods may be used to configure the object for operation on | ||||||||||||||||||||||||||||
| 77 | either end. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | While this object does in fact inherit from L<IO::Async::Notifier>, it should | ||||||||||||||||||||||||||||
| 80 | not be added to a Loop object directly; event management will be handled by | ||||||||||||||||||||||||||||
| 81 | its containing L<IO::Async::Routine> object. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Undocumented convenience constructors for running IaRoutine in 'spawn' mode | ||||||||||||||||||||||||||||
| 86 | sub new_sync | ||||||||||||||||||||||||||||
| 87 | { | ||||||||||||||||||||||||||||
| 88 | 4 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new_sync: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 4 | my ( $fd ) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 4 | my $self = $class->new; | |||||||||||||||||||||||||||
| 92 | 4 | $self->setup_sync_mode( $fd ); | |||||||||||||||||||||||||||
| 93 | 4 | return $self; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | sub new_stdin { shift->new_sync( \*STDIN ); } | ||||||||||||||||||||||||||
|
Subroutine
new_stdin: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 2 | ▶ | sub new_stdout { shift->new_sync( \*STDOUT ); } | ||||||||||||||||||||||||||
|
Subroutine
new_stdout: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub DESTROY | ||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 130 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | 130 | eval { $self->close }; # ignore any error | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 METHODS | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | The following methods documented in C<await> expressions return L<Future> | ||||||||||||||||||||||||||||
| 108 | instances. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 configure | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | $channel->configure( %params ); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Similar to the standard C<configure> method on L<IO::Async::Notifier>, this is | ||||||||||||||||||||||||||||
| 117 | used to change details of the Channel's operation. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =over 4 | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =item on_recv => CODE | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | May only be set on an async mode channel. If present, will be invoked whenever | ||||||||||||||||||||||||||||
| 124 | a new value is received, rather than using the C<recv> method. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | $on_recv->( $channel, $data ); | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =item on_eof => CODE | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | May only be set on an async mode channel. If present, will be invoked when the | ||||||||||||||||||||||||||||
| 131 | channel gets closed by the peer. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | $on_eof->( $channel ); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =back | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 15 | my $DEFAULT_CODEC; | |||||||||||||||||||||||||||
| 140 | sub _default_codec | ||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||
| 142 | 157 | ▶ | $DEFAULT_CODEC ||= do { | ||||||||||||||||||||||||||
|
Subroutine
_default_codec: called CC=3 SCAR=11.2
Condition66%2/3
$DEFAULT_CODEC ||= do {
my $HAVE_SEREAL = defined eval {
do {
require Sereal::Encoder;
require Sereal::Decoder
}
};
$HAVE_SEREAL ? "Sereal" : "Storable"
}
MC/DC0%0/2
$DEFAULT_CODEC ||= do {
my $HAVE_SEREAL = defined eval {
do {
require Sereal::Encoder;
require Sereal::Decoder
}
};
$HAVE_SEREAL ? "Sereal" : "Storable"
}
$DEFAULT_CODEC
do {
my $HAVE_SEREAL = defined eval {
do {
require Sereal::Encoder;
require Sereal::Decoder
}
};
$HAVE_SEREAL ? "Sereal" : "Storable"
}
Truth table66%2/3
A ||= B
A= $DEFAULT_CODECB= do {
my $HAVE_SEREAL = defined eval {
do {
require Sereal::Encoder;
require Sereal::Decoder
}
};
$HAVE_SEREAL ? "Sereal" : "Storable"
}
| |||||||||||||||||||||||||||||
| 143 | 10 | my $HAVE_SEREAL = defined eval { | |||||||||||||||||||||||||||
| 144 | 10 | require Sereal::Encoder; require Sereal::Decoder }; | |||||||||||||||||||||||||||
| 145 | 10 | ▶ | $HAVE_SEREAL ? "Sereal" : "Storable"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | }; | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | sub _init | ||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | 152 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 152 | 152 | my ( $params ) = @_; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 152 | ▶ | $params->{codec} //= _default_codec; | ||||||||||||||||||||||||||
|
Condition66%2/3
$params->{"codec"} //= _default_codec()
MC/DC0%0/2
$params->{"codec"} //= _default_codec()
$params->{"codec"}
_default_codec()
Truth table66%2/3
A //= B
A= $params->{"codec"}B= _default_codec()
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 152 | $self->SUPER::_init( $params ); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | sub configure | ||||||||||||||||||||||||||||
| 160 | { | ||||||||||||||||||||||||||||
| 161 | 153 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
configure: called CC=7 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 153 | my %params = @_; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 153 | foreach (qw( on_recv on_eof )) { | |||||||||||||||||||||||||||
| 165 | 306 | ▶ | next unless exists $params{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 2 | ▶ | $self->{mode} and $self->{mode} eq "async" or | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"mode"} and $self->{"mode"} eq "async"
MC/DC0%0/2
$self->{"mode"} and $self->{"mode"} eq "async"
$self->{"mode"}
$self->{"mode"} eq "async"
Truth table33%1/3
A and B
A= $self->{"mode"}B= $self->{"mode"} eq "async"
| |||||||||||||||||||||||||||||
| 167 | croak "Can only configure $_ in async mode"; | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 2 | $self->{$_} = delete $params{$_}; | |||||||||||||||||||||||||||
| 170 | 2 | $self->_build_stream; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 153 | ▶ | if( my $codec = delete $params{codec} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 152 | ▶ | @{ $self }{qw( encode decode )} = ( | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->can("_make_codec_$codec") || croak("Unrecognised codec name '${codec}'")
MC/DC0%0/2
$self->can("_make_codec_$codec") || croak("Unrecognised codec name '${codec}'")
$self->can("_make_codec_$codec")
croak("Unrecognised codec name '${codec}'")
Truth table33%1/3
A || B
A= $self->can("_make_codec_$codec")B= croak("Unrecognised codec name '${codec}'")
| |||||||||||||||||||||||||||||
| 175 | $self->can( "_make_codec_$codec" ) or croak "Unrecognised codec name '$codec'" | ||||||||||||||||||||||||||||
| 176 | )->(); | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 153 | $self->SUPER::configure( %params ); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | sub _make_codec_Storable | ||||||||||||||||||||||||||||
| 183 | { | ||||||||||||||||||||||||||||
| 184 | 0 | ▶ | require Storable; | ||||||||||||||||||||||||||
|
Subroutine
_make_codec_Storable: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | return | ||||||||||||||||||||||||||||
| 187 | 0 | \&Storable::freeze, | |||||||||||||||||||||||||||
| 188 | \&Storable::thaw; | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | sub _make_codec_Sereal | ||||||||||||||||||||||||||||
| 192 | { | ||||||||||||||||||||||||||||
| 193 | 158 | ▶ | require Sereal::Encoder; | ||||||||||||||||||||||||||
|
Subroutine
_make_codec_Sereal: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 194 | 158 | require Sereal::Decoder; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 158 | my $encoder; | |||||||||||||||||||||||||||
| 197 | my $decoder; | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # "thread safety" to Sereal::{Encoder,Decoder} means that the variables get | ||||||||||||||||||||||||||||
| 200 | # reset to undef in new threads. We should defend against that. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | return | ||||||||||||||||||||||||||||
| 203 | 113 | ▶ | sub { ( $encoder ||= Sereal::Encoder->new )->encode( $_[0] ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Condition66%2/3
$encoder ||= "Sereal::Encoder"->new
MC/DC0%0/2
$encoder ||= "Sereal::Encoder"->new
$encoder
"Sereal::Encoder"->new
Truth table66%2/3
A ||= B
A= $encoderB= "Sereal::Encoder"->new
| |||||||||||||||||||||||||||||
| 204 | 158 | ▶ | sub { ( $decoder ||= Sereal::Decoder->new )->decode( $_[0] ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Condition66%2/3
$decoder ||= "Sereal::Decoder"->new
MC/DC0%0/2
$decoder ||= "Sereal::Decoder"->new
$decoder
"Sereal::Decoder"->new
Truth table66%2/3
A ||= B
A= $decoderB= "Sereal::Decoder"->new
| |||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head2 send | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | $channel->send( $data ); | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | Pushes the data stored in the given Perl reference into the FIFO of the | ||||||||||||||||||||||||||||
| 212 | Channel, where it can be received by the other end. When called on a | ||||||||||||||||||||||||||||
| 213 | synchronous mode Channel this method may block if a C<write()> call on the | ||||||||||||||||||||||||||||
| 214 | underlying filehandle blocks. When called on an asynchronous mode channel this | ||||||||||||||||||||||||||||
| 215 | method will not block. | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =cut | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | my %SENDMETHODS; | ||||||||||||||||||||||||||||
| 220 | sub send | ||||||||||||||||||||||||||||
| 221 | { | ||||||||||||||||||||||||||||
| 222 | 23 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
send: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 223 | 23 | my ( $data ) = @_; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 23 | ▶ | defined( my $mode = $self->{mode} ) or die "Cannot ->send without being set up"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 23 | ▶ | my $code = ( $SENDMETHODS{$mode} ||= $self->can( "_send_$mode" ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$SENDMETHODS{$mode} ||= $self->can("_send_$mode")
MC/DC0%0/2
$SENDMETHODS{$mode} ||= $self->can("_send_$mode")
$SENDMETHODS{$mode}
$self->can("_send_$mode")
Truth table66%2/3
A ||= B
A= $SENDMETHODS{$mode}B= $self->can("_send_$mode")
| |||||||||||||||||||||||||||||
| 228 | or die "IO::Async::Channel cannot send in unrecognised mode '$mode'"; | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 23 | $self->$code( $data ); | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | *_send_sync = *_send_async = sub { | ||||||||||||||||||||||||||||
| 234 | 23 | ▶ | my ( $self, $data ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 235 | 23 | $self->send_encoded( $self->{encode}->( $data ) ); | |||||||||||||||||||||||||||
| 236 | 15 | }; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =head2 send_encoded | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | $channel->send_encoded( $record ); | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | A variant of the C<send> method; this method pushes the byte record given. | ||||||||||||||||||||||||||||
| 243 | This should be the result of a call to C<encode>. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =cut | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | sub send_encoded | ||||||||||||||||||||||||||||
| 248 | { | ||||||||||||||||||||||||||||
| 249 | 111 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
send_encoded: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 250 | 111 | my ( $record ) = @_; | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 111 | my $bytes = pack( "I", length $record ) . $record; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 111 | ▶ | defined $self->{mode} or die "Cannot ->send without being set up"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 111 | ▶ | return $self->_sendbytes_sync( $bytes ) if $self->{mode} eq "sync"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 99 | ▶ | return $self->_sendbytes_async( $bytes ) if $self->{mode} eq "async"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | =head2 encode | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | $record = $channel->encode( $data ); | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | Takes a Perl reference and returns a serialised string that can be passed to | ||||||||||||||||||||||||||||
| 265 | C<send_encoded>. The following two forms are equivalent | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | $channel->send( $data ); | ||||||||||||||||||||||||||||
| 268 | $channel->send_encoded( $channel->encode( $data ) ); | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | This is provided for the use-case where data needs to be serialised into a | ||||||||||||||||||||||||||||
| 271 | fixed string to "snapshot it" but not sent yet; the returned string can be | ||||||||||||||||||||||||||||
| 272 | saved and sent at a later time. | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | $record = IO::Async::Channel->encode( $data ); | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | This can also be used as a class method, in case it is inconvenient to operate | ||||||||||||||||||||||||||||
| 277 | on a particular object instance, or when one does not exist yet. In this case | ||||||||||||||||||||||||||||
| 278 | it will encode using whatever is the default codec for C<IO::Async::Channel>. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =cut | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 15 | my $default_encode; | |||||||||||||||||||||||||||
| 283 | sub encode | ||||||||||||||||||||||||||||
| 284 | { | ||||||||||||||||||||||||||||
| 285 | 90 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 286 | 90 | my ( $data ) = @_; | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | return ( ref $self ? | ||||||||||||||||||||||||||||
| 289 | $self->{encode} : | ||||||||||||||||||||||||||||
| 290 | 90 | ▶ | $default_encode ||= do { ( $self->can( "_make_codec_" . _default_codec )->() )[0] } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $self ? $self->{"encode"} : $default_encode ||= do {
($self->can('_make_codec_' . _default_codec())->())[0]
}
MC/DC0%0/2
ref $self ? $self->{"encode"} : $default_encode ||= do {
($self->can('_make_codec_' . _default_codec())->())[0]
}
ref $self ? $self->{"encode"} : $default_encode
do {
($self->can('_make_codec_' . _default_codec())->())[0]
}
Truth table66%2/3
A ||= B
A= ref $self ? $self->{"encode"} : $default_encodeB= do {
($self->can('_make_codec_' . _default_codec())->())[0]
}
| |||||||||||||||||||||||||||||
| 291 | )->( $data ); | ||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =head2 recv | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | $data = $channel->recv; | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | When called on a synchronous mode Channel this method will block until a Perl | ||||||||||||||||||||||||||||
| 299 | reference value is available from the other end and then return it. If the | ||||||||||||||||||||||||||||
| 300 | Channel is closed this method will return C<undef>. Since only references may | ||||||||||||||||||||||||||||
| 301 | be passed and all Perl references are true the truth of the result of this | ||||||||||||||||||||||||||||
| 302 | method can be used to detect that the channel is still open and has not yet | ||||||||||||||||||||||||||||
| 303 | been closed. | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | $data = await $channel->recv; | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | When called on an asynchronous mode Channel this method returns a future which | ||||||||||||||||||||||||||||
| 308 | will eventually yield the next Perl reference value that becomes available | ||||||||||||||||||||||||||||
| 309 | from the other end. If the Channel is closed, the future will fail with an | ||||||||||||||||||||||||||||
| 310 | C<eof> failure. | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | $channel->recv( %args ); | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | When not returning a future, takes the following named arguments: | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =over 8 | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =item on_recv => CODE | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | Called when a new Perl reference value is available. Will be passed the | ||||||||||||||||||||||||||||
| 321 | Channel object and the reference data. | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | $on_recv->( $channel, $data ); | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | =item on_eof => CODE | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | Called if the Channel was closed before a new value was ready. Will be passed | ||||||||||||||||||||||||||||
| 328 | the Channel object. | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | $on_eof->( $channel ); | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =back | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =cut | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | my %RECVMETHODS; | ||||||||||||||||||||||||||||
| 337 | sub recv | ||||||||||||||||||||||||||||
| 338 | { | ||||||||||||||||||||||||||||
| 339 | 118 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
recv: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 118 | ▶ | defined( my $mode = $self->{mode} ) or die "Cannot ->recv without being set up"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 118 | ▶ | my $code = ( $RECVMETHODS{$mode} ||= $self->can( "_recv_$mode" ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$RECVMETHODS{$mode} ||= $self->can("_recv_$mode")
MC/DC0%0/2
$RECVMETHODS{$mode} ||= $self->can("_recv_$mode")
$RECVMETHODS{$mode}
$self->can("_recv_$mode")
Truth table66%2/3
A ||= B
A= $RECVMETHODS{$mode}B= $self->can("_recv_$mode")
| |||||||||||||||||||||||||||||
| 344 | or die "IO::Async::Channel cannot recv in unrecognised mode '$mode'"; | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 118 | return $self->$code( @_ ); | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =head2 close | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | $channel->close; | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | Closes the channel. Causes a pending C<recv> on the other end to return undef | ||||||||||||||||||||||||||||
| 354 | or the queued C<on_eof> callbacks to be invoked. | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =cut | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 15 | my %CLOSEMETHODS; | |||||||||||||||||||||||||||
| 359 | sub close | ||||||||||||||||||||||||||||
| 360 | { | ||||||||||||||||||||||||||||
| 361 | 186 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 186 | ▶ | defined( my $mode = $self->{mode} ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 186 | ▶ | my $code = ( $CLOSEMETHODS{$mode} ||= $self->can( "_close_$mode" ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$CLOSEMETHODS{$mode} ||= $self->can("_close_$mode")
MC/DC0%0/2
$CLOSEMETHODS{$mode} ||= $self->can("_close_$mode")
$CLOSEMETHODS{$mode}
$self->can("_close_$mode")
Truth table66%2/3
A ||= B
A= $CLOSEMETHODS{$mode}B= $self->can("_close_$mode")
| |||||||||||||||||||||||||||||
| 366 | or die "IO::Async::Channel cannot close in unrecognised mode '$mode'"; | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 186 | return $self->$code; | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | # Leave this undocumented for now | ||||||||||||||||||||||||||||
| 372 | sub setup_sync_mode | ||||||||||||||||||||||||||||
| 373 | { | ||||||||||||||||||||||||||||
| 374 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
setup_sync_mode: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 375 | 15 | ( $self->{fh} ) = @_; | |||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 15 | $self->{mode} = "sync"; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | # Since we're communicating binary structures and not Unicode text we need to | ||||||||||||||||||||||||||||
| 380 | # enable binmode | ||||||||||||||||||||||||||||
| 381 | 15 | binmode $self->{fh}; | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 15 | ▶ | defined and $_->blocking( 1 ) for $self->{read_handle}, $self->{write_handle}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 15 | $self->{fh}->autoflush(1); | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | sub _read_exactly | ||||||||||||||||||||||||||||
| 388 | { | ||||||||||||||||||||||||||||
| 389 | 20 | ▶ | $_[1] = ""; | ||||||||||||||||||||||||||
|
Subroutine
_read_exactly: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 20 | ▶ | while( length $_[1] < $_[2] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 20 | my $n = read( $_[0], $_[1], $_[2]-length $_[1], length $_[1] ); | |||||||||||||||||||||||||||
| 393 | 20 | ▶ | defined $n or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 20 | ▶ | $n or return ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 16 | return $_[2]; | |||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | sub _recv_sync | ||||||||||||||||||||||||||||
| 401 | { | ||||||||||||||||||||||||||||
| 402 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_recv_sync: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 12 | my $n = _read_exactly( $self->{fh}, my $lenbuffer, 4 ); | |||||||||||||||||||||||||||
| 405 | 12 | ▶ | defined $n or die "Cannot read - $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 406 | 12 | ▶ | length $n or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 8 | my $len = unpack( "I", $lenbuffer ); | |||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 8 | $n = _read_exactly( $self->{fh}, my $record, $len ); | |||||||||||||||||||||||||||
| 411 | 8 | ▶ | defined $n or die "Cannot read - $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 412 | 8 | ▶ | length $n or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 8 | return $self->{decode}->( $record ); | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | sub _sendbytes_sync | ||||||||||||||||||||||||||||
| 418 | { | ||||||||||||||||||||||||||||
| 419 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_sendbytes_sync: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 420 | 12 | my ( $bytes ) = @_; | |||||||||||||||||||||||||||
| 421 | 12 | $self->{fh}->print( $bytes ); | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | sub _close_sync | ||||||||||||||||||||||||||||
| 425 | { | ||||||||||||||||||||||||||||
| 426 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_close_sync: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 427 | 18 | $self->{fh}->close; | |||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | # Leave this undocumented for now | ||||||||||||||||||||||||||||
| 431 | sub setup_async_mode | ||||||||||||||||||||||||||||
| 432 | { | ||||||||||||||||||||||||||||
| 433 | 140 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
setup_async_mode: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 434 | 140 | my %args = @_; | |||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 140 | ▶ | exists $args{$_} and $self->{$_} = delete $args{$_} for qw( read_handle write_handle ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 140 | ▶ | keys %args and croak "Unrecognised keys for setup_async_mode: " . join( ", ", keys %args ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 140 | ▶ | defined and $_->blocking( 0 ) for $self->{read_handle}, $self->{write_handle}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 441 | 140 | $self->{mode} = "async"; | |||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | sub _build_stream | ||||||||||||||||||||||||||||
| 445 | { | ||||||||||||||||||||||||||||
| 446 | 207 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_stream: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 447 | 207 | ▶ | return $self->{stream} ||= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"stream"} ||= do {
$self->{"on_result_queue"} = [];
my $stream = "IO::Async::Stream"->new("read_handle", $self->{"read_handle"}, "write_handle", $self->{"write_handle"}, "autoflush", 1, "on_read", $self->_capture_weakself("_on_stream_read"));
$self->add_child($stream);
$stream
}
MC/DC0%0/2
$self->{"stream"} ||= do {
$self->{"on_result_queue"} = [];
my $stream = "IO::Async::Stream"->new("read_handle", $self->{"read_handle"}, "write_handle", $self->{"write_handle"}, "autoflush", 1, "on_read", $self->_capture_weakself("_on_stream_read"));
$self->add_child($stream);
$stream
}
$self->{"stream"}
do {
$self->{"on_result_queue"} = [];
my $stream = "IO::Async::Stream"->new("read_handle", $self->{"read_handle"}, "write_handle", $self->{"write_handle"}, "autoflush", 1, "on_read", $self->_capture_weakself("_on_stream_read"));
$self->add_child($stream);
$stream
}
Truth table66%2/3
A ||= B
A= $self->{"stream"}B= do {
$self->{"on_result_queue"} = [];
my $stream = "IO::Async::Stream"->new("read_handle", $self->{"read_handle"}, "write_handle", $self->{"write_handle"}, "autoflush", 1, "on_read", $self->_capture_weakself("_on_stream_read"));
$self->add_child($stream);
$stream
}
| |||||||||||||||||||||||||||||
| 448 | 128 | $self->{on_result_queue} = []; | |||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | my $stream = IO::Async::Stream->new( | ||||||||||||||||||||||||||||
| 451 | read_handle => $self->{read_handle}, | ||||||||||||||||||||||||||||
| 452 | write_handle => $self->{write_handle}, | ||||||||||||||||||||||||||||
| 453 | 128 | autoflush => 1, | |||||||||||||||||||||||||||
| 454 | on_read => $self->_capture_weakself( '_on_stream_read' ) | ||||||||||||||||||||||||||||
| 455 | ); | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 128 | $self->add_child( $stream ); | |||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 128 | $stream; | |||||||||||||||||||||||||||
| 460 | }; | ||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | sub _sendbytes_async | ||||||||||||||||||||||||||||
| 464 | { | ||||||||||||||||||||||||||||
| 465 | 99 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_sendbytes_async: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 466 | 99 | my ( $bytes ) = @_; | |||||||||||||||||||||||||||
| 467 | 99 | $self->_build_stream->write( $bytes ); | |||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | sub _recv_async | ||||||||||||||||||||||||||||
| 471 | { | ||||||||||||||||||||||||||||
| 472 | 106 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_recv_async: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 473 | 106 | my %args = @_; | |||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 106 | my $on_recv = $args{on_recv}; | |||||||||||||||||||||||||||
| 476 | 106 | my $on_eof = $args{on_eof}; | |||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 106 | my $stream = $self->_build_stream; | |||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 106 | my $f; | |||||||||||||||||||||||||||
| 481 | 106 | ▶ | $f = $stream->loop->new_future unless !defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | 106 | push @{ $self->{on_result_queue} }, sub { | |||||||||||||||||||||||||||
| 484 | 106 | ▶ | my ( $self, $type, $result ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 485 | 106 | ▶ | if( $type eq "recv" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 486 | 102 | ▶ | $f->done( $result ) if $f and !$f->is_cancelled; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$f and not $f->is_cancelled
MC/DC100%2/2
$f and not $f->is_cancelled
$f
not $f->is_cancelled
Truth table100%3/3
A and not(B)
A= $fB= not $f->is_cancelled
| |||||||||||||||||||||||||||||
| 487 | 102 | ▶ | $on_recv->( $self, $result ) if $on_recv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||
| 489 | else { | ||||||||||||||||||||||||||||
| 490 | 4 | ▶ | $f->fail( "EOF waiting for Channel recv", eof => ) if $f and !$f->is_cancelled; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$f and not $f->is_cancelled
MC/DC50%1/2
$f and not $f->is_cancelled
$f
not $f->is_cancelled
Truth table66%2/3
A and not(B)
A= $fB= not $f->is_cancelled
| |||||||||||||||||||||||||||||
| 491 | 4 | ▶ | $on_eof->( $self ) if $on_eof; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||
| 493 | 106 | }; | |||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | 106 | return $f; | |||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | sub _close_async | ||||||||||||||||||||||||||||
| 499 | { | ||||||||||||||||||||||||||||
| 500 | 168 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_close_async: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 501 | 168 | ▶ | if( my $stream = $self->{stream} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 502 | 163 | $stream->close_when_empty; | |||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||
| 504 | else { | ||||||||||||||||||||||||||||
| 505 | 5 | ▶ | $_ and $_->close for $self->{read_handle}, $self->{write_handle}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | 168 | undef $_ for $self->{read_handle}, $self->{write_handle}; | |||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | sub _on_stream_read | ||||||||||||||||||||||||||||
| 512 | { | ||||||||||||||||||||||||||||
| 513 | 110 | ▶ | my $self = shift or return; | ||||||||||||||||||||||||||
|
Subroutine
_on_stream_read: called CC=8 SCAR=20.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 514 | 110 | my ( $stream, $buffref, $eof ) = @_; | |||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | 110 | ▶ | if( $eof ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 517 | 7 | ▶ | while( my $on_result = shift @{ $self->{on_result_queue} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 518 | 4 | $on_result->( $self, eof => ); | |||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||
| 520 | 7 | ▶ | $self->{on_eof}->( $self ) if $self->{on_eof}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 521 | 7 | return; | |||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | 103 | ▶ | return 0 unless length( $$buffref ) >= 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 525 | 103 | my $len = unpack( "I", $$buffref ); | |||||||||||||||||||||||||||
| 526 | 103 | ▶ | return 0 unless length( $$buffref ) >= 4 + $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | 103 | my $record = $self->{decode}->( substr( $$buffref, 4, $len ) ); | |||||||||||||||||||||||||||
| 529 | 103 | substr( $$buffref, 0, 4 + $len ) = ""; | |||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | 103 | ▶ | if( my $on_result = shift @{ $self->{on_result_queue} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 532 | 102 | $on_result->( $self, recv => $record ); | |||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||
| 534 | else { | ||||||||||||||||||||||||||||
| 535 | 1 | $self->{on_recv}->( $self, $record ); | |||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | 103 | return 1; | |||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | sub _extract_read_handle | ||||||||||||||||||||||||||||
| 542 | { | ||||||||||||||||||||||||||||
| 543 | 66 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_extract_read_handle: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | 66 | ▶ | return undef if !$self->{mode}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | 1 | ▶ | croak "Cannot extract filehandle" if $self->{mode} ne "async"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 548 | 1 | $self->{mode} = "dead"; | |||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | 1 | return $self->{read_handle}; | |||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | sub _extract_write_handle | ||||||||||||||||||||||||||||
| 554 | { | ||||||||||||||||||||||||||||
| 555 | 67 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_extract_write_handle: called CC=3 SCAR=17.3
| |||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | 67 | ▶ | return undef if !$self->{mode}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | 0 | ▶ | croak "Cannot extract filehandle" if $self->{mode} ne "async"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 560 | 0 | $self->{mode} = "dead"; | |||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | 0 | return $self->{write_handle}; | |||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | =cut | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | 15 | 0x55AA; | |||||||||||||||||||||||||||