14
my $flags = fcntl($socket, F_GETFL, 0) or die "fcntl F_GETFL: $!";
15
fcntl($socket, F_SETFL, $flags | O_NONBLOCK) or die "fcntl F_SETFL: $!";
18
sub get_listening_nb_socket {
20
my $backlog = shift // SOMAXCONN;
22
socket($socket, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
23
bind($socket, sockaddr_un($path)) or die "bind: $!";
24
unlink $path or die "unlink: $!";
25
listen($socket, $backlog) or die "listen: $!";
26
eval { set_nb_flag($socket); };
27
+($@ ne '') and die "set_nb_flag: $@";
31
sub accept_on_nb_socket {
32
my $listening_nb_socket = shift;
34
my $a = accept $socket, $listening_nb_socket;
39
elsif($!{EWOULDBLOCK}) {
40
# If succeeded, or if the failure is only due to nonblocking
51
vec($bits, $fd, 1) = 1;
56
sub wait_for_readable {
57
# nfound = select rbits, wbits, ebits, timeout
58
# xbits is constructed by setting the nth bit for any handle that applies,
59
# where n is fileno(handle); i.e. the fd of the handle.
62
for my $h (@handles) {
66
my $rbits = fds_to_bits(keys %map);
68
select($rout, undef, undef, undef);
72
my $sock = get_listening_nb_socket("./example.sock");
74
say "looks good, $sock";
77
my($newsock, $a) = accept_on_nb_socket($sock);
79
say "Connected - $a on $newsock";