SocketUNIX.pm 1.46 KB
Newer Older
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
package NDB::Util::SocketUNIX;

use strict;
use Carp;
use Symbol;
use Socket;
use Errno;

require NDB::Util::Socket;

use vars qw(@ISA);
@ISA = qw(NDB::Util::Socket);

# constructors

my $log;

sub initmodule {
    $log = NDB::Util::Log->instance;
}

NDB::Util::SocketUNIX->attributes(
    path => sub { /^\S+$/ },
);

sub new {
    my $class = shift;
    @_ % 2 == 0 or confess 0+@_;
    my(%attr) = @_;
    my $socket = $class->SUPER::new(%attr,
	domain => PF_UNIX, type => SOCK_STREAM, proto => 0)
	or $log->push, return undef;
    return $socket;
}

sub connect {
    my $socket = shift;
    @_ == 1 or confess 0+@_;
    my($path) = @_;
    $path =~ /^\S+$/ or confess 'oops';
    my $paddr = pack_sockaddr_un($path);
    $socket->SUPER::connect($paddr)
	or $log->push, return undef;
    $socket->setpath($path)
	or $log->push, return undef;
    return 1;
}

sub bind {
    my $socket = shift;
    @_ == 1 or confess 0+@_;
    my($path) = @_;
    $path =~ /^\S+$/ or confess 'oops';
    my $paddr = pack_sockaddr_un($path);
    $socket->SUPER::bind($paddr)
	or $log->push, return undef;
    $socket->setpath($path)
	or $log->push, return undef;
    return 1;
}

sub acceptaddr {
    my $csocket = shift;
    @_ == 1 or confess 0+@_;
    my($paddr) = @_;
    return 1;		# crash
    my $path = unpack_sockaddr_un($paddr);
    $csocket->setpath($path)
	or $log->push, return undef;
    $log->put("%s accept: path=%s",
	$csocket->getpath)->push($csocket)->debug;
    return 1;
}

1;
# vim:set sw=4: