1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
NAME
IO::Pipely - Portably create pipe() or pipe-like handles, one way or
another.
SYNOPSIS
Please read DESCRIPTION for detailed semantics and caveats.
use IO::Pipely qw(pipely socketpairly);
# Create a one-directional pipe() or pipe-like thing
# the best conduit type available.
my ($read, $write) = pipely();
# Create a one-directional pipe-like thing using an
# INET socket specifically. Other types are available.
my ($read, $write) = pipely(type => 'inet');
# Create a bidirectional pipe-like thing using
# the best conduit type available.
my (
$side_a_read, $side_a_write,
$side_b_read, $side_b_write,
) = socketpairly();
# Create a bidirectional pipe-like thing using an INET socket
# specifically.
my (
$side_a_read, $side_a_write,
$side_b_read, $side_b_write,
) = socketpairly(type => 'inet');
DESCRIPTION
Pipes are troublesome beasts because there are a few different,
incompatible ways to create them. Not all platforms support all ways,
and some platforms may have hidden difficulties like incomplete or
buggy support.
IO::Pipely provides a couple functions to portably create one- and
two-way pipes and pipe-like socket pairs. It acknowledges and works
around known platform issues so you don't have to.
On the other hand, it doesn't work around unknown issues, so please
report any problems early and often.
IO::Pipely currently understands pipe(), UNIX-domain socketpair() and
regular IPv4 localhost sockets. This covers every platform tested so
far, but it's hardly complete. Please help support other mechanisms,
such as INET-domain socketpair() and IPv6 localhost sockets.
IO::Pipely will use different kinds of pipes or sockets depending on
the operating system's capabilities and the number of directions
requested. The autodetection may be overridden by specifying a
particular pipe type.
pipely
pipely() creates a one-directional pipe() or socket. It's modeled after
Perl's built-in pipe() function, but it creates and returns handles
rather than opening ones given to it.
On success, pipely() returns two file handles, the first to read from
the pipe, and the second writes into the pipe. It returns nothing on
failure.
use IO::Pipely qw(pipely);
my ($a_read, $b_write) = pipely();
die "pipely() failed: $!" unless $a_read;
When given a choice, it will prefer to use leaner pipe() calls instead
of socketpair() and socket().
pipely()'s choice can be forced using an optional named "type"
parameter. See "PIPE TYPES" for the types that can be used.
my ($a_read, $b_write) = pipely(
type => 'pipe',
);
On most systems, pipely() will prefer to open a pipe() first. It will
fall back to a UNIX socketpair() or two localhost Internet sockets, in
that order.
On Windows (ActiveState and Strawberry Perl), pipely() prefers two
localhost Internet sockets. It will fall back to socketpair() and
pipe(), both of which will probably fail.
Cygwin Perl prefers pipe() first, localhost Internet sockets, and then
socketpair(). socketpair() has been known to have problems on Cygwin.
MacPerl (MacOS 9 and earlier) has similar capaibilities to Windows.
socketpairly
socketpairly() creates a two-directional socket pair. It's modeled
after Perl's built-in socketpair(), but it creates and returns handles
rather than opening ones given to it.
On success, socketpairly() returns four file handles, read and write
for one end, and read and write for the other. On failure, it returns
nothing.
use IO::Pipely qw(socketpairly);
my ($a_read, $a_write, $b_read, $b_write) = socketpairly();
die "socketpairly() failed: $!" unless $a_read;
socketpairly() returns two extra "writer" handles. They exist for the
fallback case where two pipe() calls are needed instead of one socket
pair. The extra handles can be ignored whenever pipe() will never be
used. For example:
use IO::Pipely qw(socketpairly);
my ($side_a, undef, $side_b, undef) = socketpairly( type => 'socketpair' );
die "socketpairly() failed: $!" unless $side_a;
When given a choice, it will prefer bidirectional sockets instead of
pipe() calls.
socketpairly()'s choice can be forced using an optional named "type"
parameter. See "PIPE TYPES" for the types that can be used. In this
example, two unidirectional pipes wil be used instead of a more
efficient pair of sockets:
my ($a_read, $a_write, $b_read, $b_write) = socketpairly(
type => 'pipe',
);
On most systems, socketpairly() will try to open a UNIX socketpair()
first. It will then fall back to a pair of localhost Internet sockets,
and finally it will try a pair of pipe() calls.
On Windows (ActiveState and Strawberry Perl), socketpairly() prefers a
pair of localhost Internet sockets first. It will then fall back to a
UNIX socketpair(), and finally a couple of pipe() calls. The fallback
options will probably fail, but the code remains hopeful.
Cygwin Perl prefers localhost Internet sockets first, followed by a
pair of pipe() calls, and finally a UNIX socketpair(). Those who know
may find this counter-intuitive, but it works around known issues in
some versions of Cygwin socketpair().
MacPerl (MacOS 9 and earlier) has similar capaibilities to Windows.
PIPE TYPES
IO::Pipely currently supports three types of pipe and socket. Other
types are possible, but these three cover all known uses so far. Please
ask (or send patches) if additional types are needed.
pipe
Attempt to establish a one-way pipe using one pipe() filehandle pair (2
file descriptors), or a two-way pipe-like connection using two pipe()
pairs (4 file descriptors).
IO::Pipely prefers to use pipe() for one-way pipes and some form of
socket pair for two-way pipelike things.
socketpair
Attempt to establish a one- or two-way pipelike connection using a
single socketpair() call. This uses two file descriptors regardless
whether the connection is one- or two-way.
IO::Pipely prefers socketpair() for two-way connections, unless the
current platform has known issues with the socketpair() call.
Socket pairs are UNIX domain only for now. INET domain may be added if
it improves compatibility on some platform, or if someone contributes
the code.
inet
Attempt to establish a one- or two-way pipelike connection using
localhost socket() calls. This uses two file descriptors regardless
whether the connection is one- or two-way.
Localhost INET domain sockets are a last resort for platforms that
don't support something better. They are the least secure method of
communication since tools like tcpdump and Wireshark can tap into them.
On the other hand, this makes them easiest to debug.
KNOWN ISSUES
These are issues known to the developers at the time of this writing.
Things change, so check back now and then.
Cygwin
CygWin seems to have a problem with socketpair() and exec(). When an
exec'd process closes, any data on sockets created with socketpair() is
not flushed. From irc.perl.org channel #poe:
<dngnand> Sounds like a lapse in cygwin's exec implementation.
It works ok under Unix-ish systems?
<jdeluise2> yes, it works perfectly
<jdeluise2> but, if we just use POE::Pipe::TwoWay->new("pipe")
it always works fine on cygwin
<jdeluise2> by the way, it looks like the reason is that
POE::Pipe::OneWay works because it tries to make a
pipe first instead of a socketpair
<jdeluise2> this socketpair problem seems like a long-standing
one with cygwin, according to searches on google,
but never been fixed.
MacOS 9
IO::Pipely supports MacOS 9 for historical reasons. It's unclear
whether anyone still uses MacPerl, but the support is cheap since pipes
and sockets there have many of the same caveats as they do on Windows.
Symbol::gensym
IO::Pipely uses Symbol::gensym() instead of autovivifying file handles.
The main reasons against gensym() have been stylistic ones so far.
Meanwhile, gensym() is compatible farther back than handle
autovivification.
Windows
ActiveState and Strawberry Perl don't support pipe() or UNIX
socketpair(). Localhost Internet sockets are used for everything there,
including one-way pipes.
For one-way pipes, the unused socket directions are shut down to avoid
sending data the wrong way through them. Use socketpairly() instead.
BUGS
The functions implemented here die outright upon failure, requiring
eval{} around their calls.
The following conduit types are currently unsupported because nobody
has needed them so far. Please submit a request (and/or a patch) if any
of these is needed:
UNIX socket()
INET-domain socketpair()
IPv4-specific localhost sockets
IPv6-specific localhost sockets
AUTHOR & COPYRIGHT
IO::Pipely is copyright 2000-2021 by Rocco Caputo. All rights reserved.
IO::Pipely is free software; you may redistribute it and/or modify it
under the same terms as Perl itself.
HISTORY
IO::Pipely is a spin-off of the POE project's portable pipes. Earlier
versions of the code have been tested and used in production systems
for over a decade.
|