File Coverage

blib/lib/Sys/Async/Virt/Connection.pm
Criterion Covered Total %
statement 23 103 22.3
branch 0 14 0.0
condition 0 3 0.0
subroutine 8 22 36.3
pod 4 8 50.0
total 35 150 23.3


line stmt bran cond sub pod time code
1             ####################################################################
2             #
3             # This file was generated using XDR::Parse version v1.0.1
4             # and LibVirt version v12.1.0
5             #
6             # Don't edit this file, use the source template instead
7             #
8             # ANY CHANGES HERE WILL BE LOST !
9             #
10             ####################################################################
11              
12              
13 1     1   15 use v5.26;
  1         4  
14 1     1   7 use warnings;
  1         2  
  1         63  
15 1     1   5 use experimental 'signatures';
  1         3  
  1         7  
16 1     1   166 use Future::AsyncAwait;
  1         3  
  1         7  
17 1     1   84 use Object::Pad ':experimental(inherit_field)';
  1         3  
  1         8  
18              
19             class Sys::Async::Virt::Connection v0.6.1;
20              
21             field $_in :inheritable = undef;
22             field $_out :inheritable = undef;
23              
24             # Futures indicating status of the last read/write action
25             field $_read_f = Future->done;
26             field $_write_f = Future->done;
27             field $_eof;
28              
29 1     1   651 use Carp qw(croak);
  1         3  
  1         86  
30 1     1   8 use Future::IO;
  1         2  
  1         67  
31 1     1   9 use Log::Any qw($log);
  1         2  
  1         15  
32              
33 0     0     method _finalize_io() {
  0            
  0            
34 0           $_eof = 1;
35 0 0         $_write_f->fail( 'closed' ) unless $_write_f->is_ready;
36 0 0         $_read_f->fail( 'closed' ) unless $_read_f->is_ready;
37 0           $_read_f = Future->fail( 'closed' );
38 0           $_write_f = Future->fail( 'closed' );
39             }
40              
41 0     0 0   async method close() {
  0            
  0            
  0            
42 0           die $log->fatal(
43             "The 'close' method must be implemented by concrete sub-classes");
44             }
45              
46 0     0 1   async method connect() {
  0            
  0            
  0            
47 0           die $log->fatal(
48             "The 'connect' method must be implemented by concrete sub-classes");
49             }
50              
51 0     0 0   method connected() {
  0            
  0            
52 0   0       return ($_in and $_out);
53             }
54              
55 0     0 0   method is_read_eof() {
  0            
  0            
56 0           return $_eof;
57             }
58              
59 0     0 1   method is_secure() {
  0            
  0            
60 0           return 0;
61             }
62              
63 0     0 0   method is_write_eof() {
  0            
  0            
64 0           return $_eof;
65             }
66              
67 0     0     method _read_internal( $len ) {
  0            
  0            
  0            
68 0           Future::IO->read_exactly( $_in, $len )
69             }
70              
71 0     0 1   async method read($type, $len) {
  0            
  0            
  0            
  0            
  0            
72 0 0         die $log->fatal( "Unsupported transfer type $type" ) unless $type eq 'data';
73 0 0         return (undef, 1) if $_eof;
74              
75 0           $log->trace( "Starting read of length $len" );
76 0     0     $_read_f = $_read_f->then(sub { $self->_read_internal( $len ) });
  0            
77              
78 0           my $data = await $_read_f;
79 0           $log->trace( "Finished read of length $len" );
80 0           $_eof = not defined $data;
81              
82 0           return ($data, 0);
83             }
84              
85 0     0     method _write_internal( $data ) {
  0            
  0            
  0            
86 0           Future::IO->write_exactly( $_out, $data )
87             }
88              
89 0     0     method _write_chunk($data) {
  0            
  0            
  0            
90 0 0         die "Write to closed file" if $_eof;
91              
92 0           $log->trace( 'Low-level write of ' . length($data) . ' bytes' );
93 0     0     $_write_f = $_write_f->then(sub { $self->_write_internal( $data ) });
  0            
94 0           return $_write_f;
95             }
96              
97              
98 0     0 1   async method write(@data) {
  0            
  0            
  0            
  0            
99 0 0         return if @data == 0;
100              
101             # use the first data element as backpressure
102             # but don't await it here: we want to send
103             # all data into the send queue at once, so
104             # other write calls can't mix their data
105             # with ours
106 0           my $f = $self->_write_chunk( shift @data );
107              
108 0           while (@data) {
109 0           my $data = shift @data;
110 0 0         next unless $data;
111              
112 0           $self->_write_chunk( $data );
113             }
114              
115 0           return await $f;
116             }
117              
118             1;
119              
120             __END__