NAME

Net::Tools::Socket::Pair - non-blocking message passing over socketpair()

SYNOPSIS

use Net::Tools::Socket::Pair;
use Net::Tools::Socket::Err; # only if error details are needed

my ( $parent_sock, $child_sock ) = socketpair_create()
    or die "socketpair: $SOCK_ERRSTR\n";

# sender
my $res = socketpair_send( $parent_sock, $message_bytes );
# retry later if $SOCK_ERR == SOCK_E_AGAIN

# receiver, read all pending messages
my $data;
while( socketpair_recv( $child_sock, \$data ) )
  {
  # one complete message is in $data
  }
# peer closed if $SOCK_ERR == SOCK_E_EOF

DESCRIPTION

Message passing over socketpair( AF_UNIX, SOCK_SEQPACKET ) or socketpair( AF_UNIX, SOCK_DGRAM ), see "SOCKET TYPES". Each socketpair_send() is received by exactly one socketpair_recv() on the other side, always as a whole message. Messages are byte strings, data structures must be serialized before sending.

socketpair_send() and socketpair_recv() never sleep, regardless of the blocking mode of the handle (MSG_DONTWAIT is used on every call), and never raise SIGPIPE (MSG_NOSIGNAL is used, where the system does not have it, SIGPIPE is ignored only for the time of the send() call, the caller's $SIG{ 'PIPE' } is restored). They return:

1      one whole message sent or received successfully
0      nothing to read or peer's queue is full (SOCK_E_AGAIN), message
       too big (SOCK_E_TOOBIG), invalid message or system error
       those are considered temporary and operation should be repeated
undef  peer closed (SOCK_E_EOF), for recv after all queued messages
       were read, socket must be closed

So the result is true only for a message, false for "nothing now", and undef when the peer is gone, which allows:

while( socketpair_recv( $fh, \$data ) ) { ... }       # read all pending
if( ! defined socketpair_recv( $fh, \$data ) ) { ... } # peer closed

The reason for 0 or undef is in $SOCK_ERR, see "ERRORS".

socketpair_recv() sets $data to undef whenever the result is not 1, so a stale value is never left there.

Empty or undef messages are not sent, socketpair_send() returns 0 with SOCK_E_EMPTY, because the peer would read an empty message as end of file.

Maximum message size is $Net::Tools::Socket::Pair::MSG_MAX (128 KiB by default).

All functions are exported by default.

SOCKET TYPES

my ( $s1, $s2 ) = socketpair_create();            # SOCK_SEQPACKET
my ( $s1, $s2 ) = socketpair_create( 'dgram' );   # SOCK_DGRAM

The type is 'seqpacket' (the default) or 'dgram', case insensitive. An unknown type returns an empty list with SOCK_E_INVAL.

Both keep message boundaries, never drop messages (a full queue returns SOCK_E_AGAIN to the sender) and hold about the same count of messages. They differ only when the peer closes:

seqpacket  recv returns undef (SOCK_E_EOF) after all queued messages are
           read, poll() reports POLLHUP, send returns undef (SOCK_E_EOF)

dgram      recv never returns EOF, it returns 0 (SOCK_E_AGAIN) forever,
           poll() reports nothing, only send returns undef (SOCK_E_EOF)

So with 'dgram' a receiver which never sends must find out that its peer is gone in another way (getppid(), waitpid(), etc.). On the other hand, a 'dgram' handle may be shared by many processes writing to one reader, which never sees EOF while any of them is still writing.

ERRORS

All functions clear $SOCK_ERR and $SOCK_ERRSTR on entry and set them when the result is not 1:

SOCK_E_AGAIN    nothing to read, or peer's queue is full
SOCK_E_EOF      peer closed
SOCK_E_TOOBIG   message over $MSG_MAX or the socket buffer, an incoming
                message over $MSG_MAX is read and discarded
SOCK_E_EMPTY    empty or undef message, not sent
SOCK_E_INVAL    send: message is a reference or has wide (over 255)
                characters, encode it first
                recv: $data is not a scalar reference
errno           any other system error, the positive errno value,
                its text is in $SOCK_ERRSTR

socketpair_create() sets errno on failure, SOCK_E_INVAL for an unknown socket type.

errno values are always positive, SOCK_E_* codes are negative, so they never collide. sock_err_name() returns 'EBADF', etc. for errno values.

They are exported by Net::Tools::Socket::Err, which must be used by the caller to read them. The error state describes the last call only.

NOTES

  • Both functions must be given a socket handle, undef or a value that is not a handle will die (a closed handle returns 0 with errno). socketpair_recv() must be given a reference to a writable scalar (\$data) for the received data: anything that is not a scalar reference returns 0 with SOCK_E_INVAL and the message stays queued, a reference to a read-only value (\'text') will die.

  • Tested on Linux only. SOCK_SEQPACKET on AF_UNIX sockets is not supported on macOS, use 'dgram' there, but the system may limit 'dgram' messages far below $MSG_MAX (net.local.dgram.maxdgram). Where MSG_NOSIGNAL is not available (macOS), SIGPIPE is ignored during send instead, see above.

SEE ALSO

Net::Tools::Socket, Net::Tools::Socket::Err, socketpair(2), Socket

AUTHOR

Vladi Belperchinov-Shabanski "Cade"
      <cade@noxrun.com>
http://cade.noxrun.com/

COPYRIGHT AND LICENSE

Copyright (c) 2026 Vladi Belperchinov-Shabanski "Cade"

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.