File Coverage

blib/lib/Future/IO/ImplBase.pm
Criterion Covered Total %
statement 51 62 82.2
branch 11 20 55.0
condition 2 11 18.1
subroutine 15 17 88.2
pod 6 6 100.0
total 85 116 73.2


line stmt bran cond sub pod time code
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, 2019-2021 -- leonerd@leonerd.org.uk
5              
6             package Future::IO::ImplBase 0.12;
7              
8 13     13   173 use v5.14;
  13         44  
9 13     13   78 use warnings;
  13         22  
  13         441  
10              
11 13     13   2697 use Errno qw( EAGAIN EWOULDBLOCK EINPROGRESS );
  13         8141  
  13         1225  
12 13     13   4503 use Socket qw( SOL_SOCKET SO_ERROR );
  13         29331  
  13         1687  
13              
14             # connect() yields EWOULDBLOCK on MSWin32
15 13     13   119 use constant CONNECT_EWOULDBLOCK => ( $^O eq "MSWin32" );
  13         26  
  13         1479  
16              
17 13     13   126 use constant HAVE_MULTIPLE_FILEHANDLES => 1;
  13         72  
  13         893  
18              
19             =head1 NAME
20              
21             C - base class for C implementations
22              
23             =head1 DESCRIPTION
24              
25             This package provides a few utility methods that may help writing actual
26             L implementation classes. It is entirely optional; implementations
27             are not required to use it.
28              
29             =cut
30              
31             =head1 CLASS METHODS
32              
33             =cut
34              
35             =head2 APPLY
36              
37             __PACKAGE__->APPLY
38              
39             Attempts to set the value of the C<$Future::IO::IMPL> variable to the name of
40             the calling package.
41              
42             =cut
43              
44             sub APPLY
45             {
46 0     0 1 0 my $pkg = shift;
47              
48 13     13   177 no warnings 'once';
  13         47  
  13         8433  
49 0 0 0     0 ( $Future::IO::IMPL //= $pkg ) eq $pkg or
50             warn "Unable to set Future::IO implementation to $pkg".
51             " as it is already $Future::IO::IMPL\n";
52             }
53              
54             =head1 DEFAULT METHODS
55              
56             These methods are provided based on lower-level functionallity that the
57             implementing class should provide.
58              
59             =cut
60              
61             =head2 accept
62              
63             Implemented by wrapping C, as L uses.
64              
65             =cut
66              
67             sub accept
68             {
69 1     1 1 4 my $self = shift;
70 1         2 my ( $fh ) = @_;
71              
72             return $self->ready_for_read( $fh )->then( sub {
73 1     1   118 my $accepted = $fh->accept;
74 1 50       139 if( $accepted ) {
75 1         13 return Future->done( $accepted );
76             }
77             else {
78 0         0 return Future->fail( "accept: $!\n", accept => $fh, $! );
79             }
80 1         5 } );
81             }
82              
83             =head2 alarm
84              
85             Implemented by wrapping C.
86              
87             =cut
88              
89             sub alarm
90             {
91 0     0 1 0 my $self = shift;
92 0         0 my ( $time ) = @_;
93              
94 0         0 return $self->sleep( $time - Time::HiRes::time() );
95             }
96              
97             =head2 connect
98              
99             Implemented by wrapping C, as L uses.
100              
101             =cut
102              
103             sub connect
104             {
105 2     2 1 15 my $self = shift;
106 2         7 my ( $fh, $name ) = @_;
107              
108             # We can't use IO::Socket->connect here because
109             # https://github.com/Perl/perl5/issues/19326
110              
111 2         153 my $ret = CORE::connect( $fh, $name );
112 2         34 my $errno = $!;
113              
114 2 50 50     15 if( $ret ) {
    50          
115 0         0 return Future->done;
116             }
117             elsif( $errno != EINPROGRESS and !CONNECT_EWOULDBLOCK || $errno != EWOULDBLOCK ) {
118 0         0 return Future->fail( "connect: $errno\n", connect => $fh, $errno );
119             }
120              
121             # not synchronous result
122              
123             return $self->ready_for_write( $fh )->then( sub {
124 2     2   208 $errno = $fh->getsockopt( SOL_SOCKET, SO_ERROR );
125              
126 2 100       55 if( $errno ) {
127 1         3 $! = $errno;
128 1         15 return Future->fail( "connect: $!\n", connect => $fh, $! );
129             }
130              
131 1         9 return Future->done;
132 2         12 } );
133             }
134              
135             =head2 sysread
136              
137             Requires a lower-level method
138              
139             $f = $class->ready_for_read( $fh )
140              
141             which should return a Future that completes when the given filehandle may be
142             ready for reading.
143              
144             =cut
145              
146             sub sysread
147             {
148 15     15 1 27 my $self = shift;
149 15         36 my ( $fh, $length ) = @_;
150              
151             $self->ready_for_read( $fh )->then( sub {
152 13     13   1095 my $ret = $fh->sysread( my $buf, $length );
153 13 100 0     240 if( $ret ) {
    50          
    0          
154 10         58 return Future->done( $buf );
155             }
156             elsif( defined $ret ) {
157             # EOF
158 3         17 return Future->done();
159             }
160             elsif( $! == EAGAIN or $! == EWOULDBLOCK ) {
161             # Try again
162 0         0 return $self->sysread( $fh, $length );
163             }
164             else {
165 0         0 return Future->fail( "sysread: $!\n", sysread => $fh, $! );
166             }
167 15         54 });
168             }
169              
170             =head2 syswrite
171              
172             Requires a lower-level method
173              
174             $f = $class->ready_for_write( $fh )
175              
176             which should return a Future that completes when the given filehandle may be
177             ready for writing.
178              
179             =cut
180              
181             sub syswrite
182             {
183 8     8 1 19 my $self = shift;
184 8         21 my ( $fh, $data ) = @_;
185              
186             return $self->ready_for_write( $fh )->then( sub {
187 7     7   623 my $len = $fh->syswrite( $data );
188 7 100 33     173 if( defined $len ) {
    50          
189 6         37 return Future->done( $len );
190             }
191             elsif( $! == EAGAIN or $! == EWOULDBLOCK ) {
192             # Try again
193 0         0 return $self->syswrite( $fh, $data );
194             }
195             else {
196 1         14 return Future->fail( "syswrite: $!\n", syswrite => $fh, $! );
197             }
198 8         30 });
199             }
200              
201             =head1 AUTHOR
202              
203             Paul Evans
204              
205             =cut
206              
207             0x55AA;