Future/IO/Impl/POE.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 92 CC 7 SCAR 33.0
CC
7
Cov
0%
poll
56.0
BEGIN
2.0
sleep
2.0
CRAP 27.2
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-2026 -- leonerd@leonerd.org.uk
5  
6 package Future::IO::Impl::POE 0.06;
7  
8 use v5.14;
9 use warnings;
10 use base qw( Future::IO::ImplBase );
11 BEGIN { Future::IO::ImplBase->VERSION( '0.19' ); }
12  
13 use Future::IO qw( POLLIN POLLOUT POLLHUP );
14  
15 =head1 NAME
16  
17 C<Future::IO::Impl::POE> - implement C<Future::IO> using C<POE>
18  
19 =head1 DESCRIPTION
20  
21 =for highlighter language=perl
22  
23 This module provides an implemention for L<Future::IO> which uses L<POE>.
24  
25 There are no additional methods to use in this module; it simply has to be
26 loaded, and will provide the C<Future::IO> implementation methods.
27  
28 use Future::IO;
29 use Future::IO::Impl::POE;
30  
31 my $f = Future::IO->sleep(5);
32 ...
33  
34 =head1 LIMITATIONS
35  
36 This module only provides a limited subset of the L<Future::IO/poll> method
37 API. It fully handles C<POLLIN> and C<POLLOUT> conditions, but is not able to
38 report on C<POLLHUP> and C<POLLERR> events.
39  
40 When a filehandle is at hangup condition it is reported as only C<POLLIN>, and
41 when at error condition it is reported as only C<POLLOUT>. This I<should> be
42 sufficient for most purposes, and works fine for internally providing
43 asynchronous reading and writing on regular filehandles, but may cause some
44 odd behaviours if you are attempting to detect those conditions directly.
45  
46 =cut
47  
48 use POE;
49  
50 __PACKAGE__->APPLY;
51  
52 sub sleep
53 {
54 shift;
55 my ( $secs ) = @_;
56  
57 return POE::Future->new_delay( $secs );
58 }
59  
60 my $iosession;
61 sub _mk_iosession
62 {
63 return $iosession //= POE::Session->create(
64 inline_states => {
65 _start => sub {
66 $_[KERNEL]->alias_set( __PACKAGE__ );
67 },
68  
69 invoke => sub { $_[-1]->() },
70  
71 select_read => sub {
72 $_[KERNEL]->select_read( $_[ARG0], invoke => $_[ARG1] );
73 },
74 unselect_read => sub {
75 $_[KERNEL]->select_read( $_[ARG0] );
76 },
77 select_write => sub {
78 $_[KERNEL]->select_write( $_[ARG0], invoke => $_[ARG1] );
79 },
80 unselect_write => sub {
81 $_[KERNEL]->select_write( $_[ARG0] );
82 },
83 },
84 );
85 }
86  
87 my %futures_read_by_fileno; # {fileno} => [@futures]
88 my %futures_write_by_fileno; # {fileno} => [@futures]
89  
90 sub poll
91 {
92 shift;
93 my ( $fh, $events ) = @_;
94  
95 my $fileno = $fh->fileno;
96  
97 my $f = POE::Future->new;
98 my $session = _mk_iosession;
99  
100 if( $events & (POLLIN|POLLHUP) ) {
101 my $futures = $futures_read_by_fileno{$fileno} //= [];
102  
103 my $was = scalar @$futures;
104 push @$futures, $f;
105  
106 POE::Kernel->call( $session, select_read => $fh, sub {
107 $futures->[0]->done( POLLIN ); # we can't distinguish IN from HUP
108 shift @$futures;
109  
110 return if scalar @$futures;
111  
112 POE::Kernel->call( $session, unselect_read => $fh );
113 delete $futures_read_by_fileno{$fileno};
114 } ) if !$was;
115 }
116 if( $events & POLLOUT ) {
117 my $futures = $futures_write_by_fileno{$fileno} //= [];
118  
119 my $was = scalar @$futures;
120 push @$futures, $f;
121  
122 POE::Kernel->call( $session, select_write => $fh, sub {
123 $futures->[0]->done( POLLOUT ); # we can't distinguish OUT from ERR
124 shift @$futures;
125  
126 return if scalar @$futures;
127  
128 POE::Kernel->call( $session, unselect_write => $fh );
129 delete $futures_write_by_fileno{$fileno};
130 } ) if !$was;
131 }
132  
133 return $f;
134 }
135  
136 =head1 AUTHOR
137  
138 Paul Evans <leonerd@leonerd.org.uk>
139  
140 =cut
141  
142 0x55AA;