| 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, 2012-2013 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | package IO::Async::OS::MSWin32 0.805; | ||
| 7 | |||
| 8 | use v5.14; | ||
| 9 | use warnings; | ||
| 10 | |||
| 11 | our @ISA = qw( IO::Async::OS::_Base ); | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | |||
| 15 | use Socket qw( AF_INET SOCK_STREAM SOCK_DGRAM INADDR_LOOPBACK pack_sockaddr_in ); | ||
| 16 | |||
| 17 | use IO::Socket (); # empty import | ||
| 18 | |||
| 19 | use constant HAVE_FAKE_ISREG_READY => 1; | ||
| 20 | |||
| 21 | # Also select() only reports connect() failures by evec, not wvec | ||
| 22 | use constant HAVE_SELECT_CONNECT_EVEC => 1; | ||
| 23 | |||
| 24 | use constant HAVE_POLL_CONNECT_POLLPRI => 1; | ||
| 25 | |||
| 26 | use constant HAVE_CONNECT_EWOULDBLOCK => 1; | ||
| 27 | |||
| 28 | use constant HAVE_RENAME_OPEN_FILES => 0; | ||
| 29 | |||
| 30 | # poll(2) on Windows is emulated by wrapping select(2) anyway, so we might as | ||
| 31 | # well try the Select loop first | ||
| 32 | use constant LOOP_BUILTIN_CLASSES => qw( Select Poll ); | ||
| 33 | |||
| 34 | # CORE::fork() does not provide full POSIX semantics | ||
| 35 | use constant HAVE_POSIX_FORK => 0; | ||
| 36 | |||
| 37 | # Windows does not have signals, and SIGCHLD is not available | ||
| 38 | use constant HAVE_SIGNALS => 0; | ||
| 39 | |||
| 40 | =head1 NAME | ||
| 41 | |||
| 42 | C<IO::Async::OS::MSWin32> - operating system abstractions on C<MSWin32> for C<IO::Async> | ||
| 43 | |||
| 44 | =head1 DESCRIPTION | ||
| 45 | |||
| 46 | This module contains OS support code for C<MSWin32>. | ||
| 47 | |||
| 48 | See instead L<IO::Async::OS>. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | # Win32's pipes don't actually work with select(). We'll have to create | ||
| 53 | # sockets instead | ||
| 54 | sub pipepair | ||
| 55 | { | ||
| 56 | shift->socketpair( 'inet', 'stream' ); | ||
| 57 | } | ||
| 58 | |||
| 59 | # Win32 doesn't have a socketpair(). We'll fake one up | ||
| 60 | sub socketpair | ||
| 61 | { | ||
| 62 | my $self = shift; | ||
| 63 | my ( $family, $socktype, $proto ) = @_; | ||
| 64 | |||
| 65 | $family = $self->getfamilybyname( $family ) || AF_INET; | ||
| 66 | |||
| 67 | # SOCK_STREAM is the most likely | ||
| 68 | $socktype = $self->getsocktypebyname( $socktype ) || SOCK_STREAM; | ||
| 69 | |||
| 70 | $proto ||= 0; | ||
| 71 | |||
| 72 | $family == AF_INET or croak "Cannot emulate ->socketpair except on AF_INET"; | ||
| 73 | |||
| 74 | my $Stmp = $self->socket( $family, $socktype ) or return; | ||
| 75 | $Stmp->bind( pack_sockaddr_in( 0, INADDR_LOOPBACK ) ) or return; | ||
| 76 | |||
| 77 | my $S1 = $self->socket( $family, $socktype ) or return; | ||
| 78 | |||
| 79 | my $S2; | ||
| 80 | if( $socktype == SOCK_STREAM ) { | ||
| 81 | $Stmp->listen( 1 ) or return; | ||
| 82 | $S1->connect( getsockname $Stmp ) or return; | ||
| 83 | $S2 = $Stmp->accept or return; | ||
| 84 | |||
| 85 | # There's a bug in IO::Socket here, in that $S2 's ->socktype won't | ||
| 86 | # yet be set. We can apply a horribly hacky fix here | ||
| 87 | # defined $S2->socktype and $S2->socktype == $socktype or | ||
| 88 | # ${*$S2}{io_socket_type} = $socktype; | ||
| 89 | # But for now we'll skip the test for it instead | ||
| 90 | } | ||
| 91 | elsif( $socktype == SOCK_DGRAM ) { | ||
| 92 | $S2 = $Stmp; | ||
| 93 | $S1->connect( getsockname $S2 ) or return; | ||
| 94 | $S2->connect( getsockname $S1 ) or return; | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | croak "Unrecognised socktype $socktype"; | ||
| 98 | } | ||
| 99 | |||
| 100 | return ( $S1, $S2 ); | ||
| 101 | }; | ||
| 102 | |||
| 103 | =head1 AUTHOR | ||
| 104 | |||
| 105 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | 0x55AA; |