"Fossies" - the Fresh Open Source Software Archive

Member "install-tl-20200916/tlpkg/tlperl/lib/IO/Socket/UNIX.pm" (7 Mar 2020, 3384 Bytes) of package /windows/misc/install-tl.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # IO::Socket::UNIX.pm
    2 #
    3 # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
    4 # This program is free software; you can redistribute it and/or
    5 # modify it under the same terms as Perl itself.
    6 
    7 package IO::Socket::UNIX;
    8 
    9 use strict;
   10 use IO::Socket;
   11 use Carp;
   12 
   13 our @ISA = qw(IO::Socket);
   14 our $VERSION = "1.40";
   15 
   16 IO::Socket::UNIX->register_domain( AF_UNIX );
   17 
   18 sub new {
   19     my $class = shift;
   20     unshift(@_, "Peer") if @_ == 1;
   21     return $class->SUPER::new(@_);
   22 }
   23 
   24 sub configure {
   25     my($sock,$arg) = @_;
   26     my($bport,$cport);
   27 
   28     my $type = $arg->{Type} || SOCK_STREAM;
   29 
   30     $sock->socket(AF_UNIX, $type, 0) or
   31     return undef;
   32 
   33     if(exists $arg->{Local}) {
   34     my $addr = sockaddr_un($arg->{Local});
   35     $sock->bind($addr) or
   36         return undef;
   37     }
   38     if(exists $arg->{Listen} && $type != SOCK_DGRAM) {
   39     $sock->listen($arg->{Listen} || 5) or
   40         return undef;
   41     }
   42     elsif(exists $arg->{Peer}) {
   43     my $addr = sockaddr_un($arg->{Peer});
   44     $sock->connect($addr) or
   45         return undef;
   46     }
   47 
   48     $sock;
   49 }
   50 
   51 sub hostpath {
   52     @_ == 1 or croak 'usage: $sock->hostpath()';
   53     my $n = $_[0]->sockname || return undef;
   54     (sockaddr_un($n))[0];
   55 }
   56 
   57 sub peerpath {
   58     @_ == 1 or croak 'usage: $sock->peerpath()';
   59     my $n = $_[0]->peername || return undef;
   60     (sockaddr_un($n))[0];
   61 }
   62 
   63 1; # Keep require happy
   64 
   65 __END__
   66 
   67 =head1 NAME
   68 
   69 IO::Socket::UNIX - Object interface for AF_UNIX domain sockets
   70 
   71 =head1 SYNOPSIS
   72 
   73     use IO::Socket::UNIX;
   74 
   75     my $SOCK_PATH = "$ENV{HOME}/unix-domain-socket-test.sock";
   76 
   77     # Server:
   78     my $server = IO::Socket::UNIX->new(
   79         Type => SOCK_STREAM(),
   80         Local => $SOCK_PATH,
   81         Listen => 1,
   82     );
   83 
   84     my $count = 1;
   85     while (my $conn = $server->accept()) {
   86         $conn->print("Hello " . ($count++) . "\n");
   87     }
   88 
   89     # Client:
   90     my $client = IO::Socket::UNIX->new(
   91         Type => SOCK_STREAM(),
   92         Peer => $SOCK_PATH,
   93     );
   94 
   95     # Now read and write from $client
   96 
   97 =head1 DESCRIPTION
   98 
   99 C<IO::Socket::UNIX> provides an object interface to creating and using sockets
  100 in the AF_UNIX domain. It is built upon the L<IO::Socket> interface and
  101 inherits all the methods defined by L<IO::Socket>.
  102 
  103 =head1 CONSTRUCTOR
  104 
  105 =over 4
  106 
  107 =item new ( [ARGS] )
  108 
  109 Creates an C<IO::Socket::UNIX> object, which is a reference to a
  110 newly created symbol (see the C<Symbol> package). C<new>
  111 optionally takes arguments, these arguments are in key-value pairs.
  112 
  113 In addition to the key-value pairs accepted by L<IO::Socket>,
  114 C<IO::Socket::UNIX> provides.
  115 
  116     Type        Type of socket (eg SOCK_STREAM or SOCK_DGRAM)
  117     Local       Path to local fifo
  118     Peer        Path to peer fifo
  119     Listen      Queue size for listen
  120 
  121 If the constructor is only passed a single argument, it is assumed to
  122 be a C<Peer> specification.
  123 
  124 If the C<Listen> argument is given, but false, the queue size will be set to 5.
  125 
  126 =back
  127 
  128 =head1 METHODS
  129 
  130 =over 4
  131 
  132 =item hostpath()
  133 
  134 Returns the pathname to the fifo at the local end
  135 
  136 =item peerpath()
  137 
  138 Returns the pathanme to the fifo at the peer end
  139 
  140 =back
  141 
  142 =head1 SEE ALSO
  143 
  144 L<Socket>, L<IO::Socket>
  145 
  146 =head1 AUTHOR
  147 
  148 Graham Barr. Currently maintained by the Perl Porters.  Please report all
  149 bugs to <perlbug@perl.org>.
  150 
  151 =head1 COPYRIGHT
  152 
  153 Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
  154 This program is free software; you can redistribute it and/or
  155 modify it under the same terms as Perl itself.
  156 
  157 =cut