| 1 | package IPC::Manager::Client::ConnectionUnix; | ||||||||||||||||||||||||||||||||
| 2 | 87 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 87 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 87 | our $VERSION = '0.000036'; | |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 87 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 87 | ▶ | use Errno qw/EAGAIN EWOULDBLOCK EINTR/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 87 | ▶ | use Fcntl qw/O_WRONLY O_CREAT O_EXCL/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 87 | ▶ | use Time::HiRes qw/time/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 87 | ▶ | use IO::Socket::UNIX 1.55 qw/SOCK_STREAM SOMAXCONN/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 87 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 87 | ▶ | use IPC::Manager::Util qw/USE_IO_SELECT/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 87 | ▶ | use parent 'IPC::Manager::Base::FS'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 87 | ▶ | use Role::Tiny::With; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 87 | with 'IPC::Manager::Role::Client::Connection'; | |||||||||||||||||||||||||||||||
| 19 | 87 | with 'IPC::Manager::Role::Outbox'; | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 87 | use Object::HashBase qw{ | |||||||||||||||||||||||||||||||
| 22 | listen | ||||||||||||||||||||||||||||||||
| 23 | +listen_socket | ||||||||||||||||||||||||||||||||
| 24 | +connections | ||||||||||||||||||||||||||||||||
| 25 | +backlog | ||||||||||||||||||||||||||||||||
| 26 | 87 | ▶ | }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | # sun_path budget — same constraint as Client::UnixSocket. | ||||||||||||||||||||||||||||||||
| 29 | 87 | ▶ | use constant _SUN_PATH_LIMIT => 104; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | sub _viable { require IO::Socket::UNIX; IO::Socket::UNIX->VERSION('1.55'); 1 } | ||||||||||||||||||||||||||||||
|
Subroutine
_viable: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 46 | ▶ | sub suspend_supported { 0 } | ||||||||||||||||||||||||||||||
|
Subroutine
suspend_supported: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub suspend { croak "suspend is not supported by the ConnectionUnix driver" } | ||||||||||||||||||||||||||||||
|
Subroutine
suspend: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 670 | ▶ | sub check_path { -S $_[1] || -f $_[1] } | ||||||||||||||||||||||||||||||
|
Subroutine
check_path: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 141 | ▶ | sub path_type { 'UNIX Socket or marker file' } | ||||||||||||||||||||||||||||||
|
Subroutine
path_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | sub max_on_disk_name_length { | ||||||||||||||||||||||||||||||||
| 40 | 1589 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
max_on_disk_name_length: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 41 | 1589 | return _SUN_PATH_LIMIT - length($self->{+ROUTE}) - 2; # '/' + NUL | |||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 121 | ▶ | sub have_handles_for_select { 1 } | ||||||||||||||||||||||||||||||
|
Subroutine
have_handles_for_select: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 45 | 120 | ▶ | sub have_dynamic_handles_for_select { 1 } | ||||||||||||||||||||||||||||||
|
Subroutine
have_dynamic_handles_for_select: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | sub handles_for_select { | ||||||||||||||||||||||||||||||||
| 48 | 379 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
handles_for_select: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 49 | 379 | my @h; | |||||||||||||||||||||||||||||||
| 50 | 379 | ▶ | push @h => $self->{+LISTEN_SOCKET} if $self->{+LISTEN_SOCKET}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 379 | ▶ | for my $entry (values %{$self->{+CONNECTIONS} // {}}) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"connections"} // {}
MC/DC0%0/1
$self->{"connections"} // {}
$self->{"connections"}
Truth table50%1/2
A
A= $self->{"connections"}
| |||||||||||||||||||||||||||||||||
| 52 | 224 | ▶ | push @h => $entry->{fh} if $entry->{fh}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | 379 | return @h; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | sub init { | ||||||||||||||||||||||||||||||||
| 58 | 140 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 140 | ▶ | $self->{+LISTEN} //= 1; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"listen"} //= 1
MC/DC100%1/1
$self->{"listen"} //= 1
$self->{"listen"}
Truth table100%2/2
A
A= $self->{"listen"}
| |||||||||||||||||||||||||||||||||
| 61 | 140 | ▶ | $self->{+CONNECTIONS} //= {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"connections"} //= {}
MC/DC0%0/1
$self->{"connections"} //= {}
$self->{"connections"}
Truth table50%1/2
A
A= $self->{"connections"}
| |||||||||||||||||||||||||||||||||
| 62 | 140 | ▶ | $self->{+BACKLOG} //= SOMAXCONN; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"backlog"} //= 4096
MC/DC0%0/1
$self->{"backlog"} //= 4096
$self->{"backlog"}
Truth table50%1/2
A
A= $self->{"backlog"}
| |||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 140 | $self->SUPER::init(); | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | sub make_path { | ||||||||||||||||||||||||||||||||
| 68 | 140 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
make_path: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 69 | 140 | my $path = $self->path; | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 140 | ▶ | if ($self->{+LISTEN}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | my $s = IO::Socket::UNIX->new( | ||||||||||||||||||||||||||||||||
| 73 | Type => SOCK_STREAM, | ||||||||||||||||||||||||||||||||
| 74 | Local => $path, | ||||||||||||||||||||||||||||||||
| 75 | 136 | ▶ | Listen => $self->{+BACKLOG}, | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | Blocking => 0, | ||||||||||||||||||||||||||||||||
| 77 | ) or die "Cannot create listen socket '$path': $!"; | ||||||||||||||||||||||||||||||||
| 78 | 136 | $self->{+LISTEN_SOCKET} = $s; | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | else { | ||||||||||||||||||||||||||||||||
| 81 | # Drop a regular empty file as a presence marker. Use sysopen with | ||||||||||||||||||||||||||||||||
| 82 | # O_EXCL so we never silently clobber an existing socket file. | ||||||||||||||||||||||||||||||||
| 83 | 4 | ▶ | sysopen(my $fh, $path, O_WRONLY | O_CREAT | O_EXCL, 0600) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | or die "Cannot create marker file '$path': $!"; | ||||||||||||||||||||||||||||||||
| 85 | 4 | close($fh); | |||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | sub pre_disconnect_hook { | ||||||||||||||||||||||||||||||||
| 90 | 113 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
pre_disconnect_hook: called CC=4 SCAR=14.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 91 | # Unlink the on-disk entry so no new peer can connect to us, but keep | ||||||||||||||||||||||||||||||||
| 92 | # the listen socket fd and existing connection fds open: the base | ||||||||||||||||||||||||||||||||
| 93 | # disconnect loop runs after this and still needs them to drain any | ||||||||||||||||||||||||||||||||
| 94 | # pending inbound messages. | ||||||||||||||||||||||||||||||||
| 95 | 113 | ▶ | return unless defined $self->{+PATH}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 113 | ▶ | return unless -e $self->{+PATH}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | 113 | ▶ | unlink($self->{+PATH}) or warn "Could not unlink '$self->{+PATH}': $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | sub post_disconnect_hook { | ||||||||||||||||||||||||||||||||
| 101 | 113 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
post_disconnect_hook: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | # Now safe to tear down sockets — base disconnect has already drained | ||||||||||||||||||||||||||||||||
| 104 | # whatever messages it was going to drain. | ||||||||||||||||||||||||||||||||
| 105 | 113 | ▶ | for my $peer (keys %{$self->{+CONNECTIONS} // {}}) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"connections"} // {}
MC/DC0%0/1
$self->{"connections"} // {}
$self->{"connections"}
Truth table50%1/2
A
A= $self->{"connections"}
| |||||||||||||||||||||||||||||||||
| 106 | 56 | $self->_close_connection($peer); | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 113 | ▶ | if (my $s = delete $self->{+LISTEN_SOCKET}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 109 | eval { $s->close }; | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 113 | $self->SUPER::post_disconnect_hook; | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | sub peer_is_listener { | ||||||||||||||||||||||||||||||||
| 117 | 86 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
peer_is_listener: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 118 | 86 | my ($peer) = @_; | |||||||||||||||||||||||||||||||
| 119 | 86 | ▶ | my $path = $self->peer_exists($peer) or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | 86 | ▶ | return -S $path ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | sub listening_peers { | ||||||||||||||||||||||||||||||||
| 124 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
listening_peers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 125 | 1 | return grep { $self->peer_is_listener($_) } $self->peers; | |||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | # --- Role::Client::Connection requirements --- | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 18 | ▶ | sub _connections { $_[0]->{+CONNECTIONS} } | ||||||||||||||||||||||||||||||
|
Subroutine
_connections: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | sub _close_connection { | ||||||||||||||||||||||||||||||||
| 133 | 126 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_close_connection: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||
| 134 | 126 | my ($peer) = @_; | |||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 126 | ▶ | my $entry = delete $self->{+CONNECTIONS}->{$peer} or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 137 | 126 | ▶ | if (my $fh = $entry->{fh}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | 126 | eval { $fh->close }; | |||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | 126 | return; | |||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | # --- Wire framing --- | ||||||||||||||||||||||||||||||||
| 144 | # | ||||||||||||||||||||||||||||||||
| 145 | # Each frame: 4-byte big-endian length + JSON payload. | ||||||||||||||||||||||||||||||||
| 146 | # | ||||||||||||||||||||||||||||||||
| 147 | # After connecting outbound, the initiator immediately sends a hello frame: | ||||||||||||||||||||||||||||||||
| 148 | # {"hello": "<my id>"} | ||||||||||||||||||||||||||||||||
| 149 | # so the acceptor can key the cached connection by remote peer id. | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | sub _send_frame { | ||||||||||||||||||||||||||||||||
| 152 | 174 | ▶ | my ($self, $fh, $payload) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_send_frame: called CC=6 SCAR=18.8
| |||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | # Frame on the wire: 4-byte big-endian length prefix, then payload bytes. | ||||||||||||||||||||||||||||||||
| 155 | 174 | my $bytes = pack('N', length $payload) . $payload; | |||||||||||||||||||||||||||||||
| 156 | 174 | my $total = length $bytes; | |||||||||||||||||||||||||||||||
| 157 | 174 | my $off = 0; | |||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | # Sockets are non-blocking, but we need to deliver the whole frame before | ||||||||||||||||||||||||||||||||
| 160 | # returning, so loop until every byte is written. Partial writes advance | ||||||||||||||||||||||||||||||||
| 161 | # $off; EAGAIN/EWOULDBLOCK means the kernel buffer is full so we wait | ||||||||||||||||||||||||||||||||
| 162 | # briefly with select() for it to drain. EINTR retries; anything else is | ||||||||||||||||||||||||||||||||
| 163 | # a real I/O error and we propagate it to the caller. | ||||||||||||||||||||||||||||||||
| 164 | 174 | ▶ | while ($off < $total) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 165 | 174 | my $n; | |||||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||||||
| 167 | 87 | ▶ | no warnings 'closed', 'unopened'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 168 | 174 | $n = syswrite($fh, $bytes, $total - $off, $off); | |||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | 174 | ▶ | if (defined $n) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 171 | 173 | $off += $n; | |||||||||||||||||||||||||||||||
| 172 | 173 | next; | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | 1 | ▶ | if ($! == EAGAIN || $! == EWOULDBLOCK) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$! == 11 or $! == 11
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11
$! == 11
Truth table33%1/3
A or B
A= $! == 11B= $! == 11
| |||||||||||||||||||||||||||||||||
| 175 | 0 | my $w = ''; | |||||||||||||||||||||||||||||||
| 176 | 0 | vec($w, fileno($fh), 1) = 1; | |||||||||||||||||||||||||||||||
| 177 | 0 | select(undef, $w, undef, 0.5); | |||||||||||||||||||||||||||||||
| 178 | 0 | next; | |||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | next if $! == EINTR; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 181 | 1 | die "write failed: $!"; | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | # Non-blocking: drain as much of $entry->{send_buffer} as the kernel will | ||||||||||||||||||||||||||||||||
| 186 | # accept right now, then return. Stops on EAGAIN/EWOULDBLOCK without | ||||||||||||||||||||||||||||||||
| 187 | # blocking; retries on EINTR; raises on any other error. | ||||||||||||||||||||||||||||||||
| 188 | sub _flush_send_buffer { | ||||||||||||||||||||||||||||||||
| 189 | 37 | ▶ | my ($self, $entry) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_flush_send_buffer: called CC=8 SCAR=25.4
| |||||||||||||||||||||||||||||||||
| 190 | 37 | ▶ | my $fh = $entry->{fh} or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | 37 | ▶ | while (length $entry->{send_buffer}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 193 | 37 | my $n; | |||||||||||||||||||||||||||||||
| 194 | { | ||||||||||||||||||||||||||||||||
| 195 | 87 | ▶ | no warnings 'closed', 'unopened'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 196 | 37 | $n = syswrite($fh, $entry->{send_buffer}); | |||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||
| 198 | 37 | ▶ | if (defined $n) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | 37 | substr($entry->{send_buffer}, 0, $n) = ''; | |||||||||||||||||||||||||||||||
| 200 | 37 | next; | |||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | last if $! == EAGAIN || $! == EWOULDBLOCK; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$! == 11 or $! == 11
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11
$! == 11
Truth table0%0/3
A or B
A= $! == 11B= $! == 11
| |||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | next if $! == EINTR; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | 0 | die "write failed: $!"; | |||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | 37 | ▶ | return length($entry->{send_buffer}) ? 0 : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | sub _drain_reads { | ||||||||||||||||||||||||||||||||
| 211 | 240 | ▶ | my ($self, $fh, $buf_ref) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_drain_reads: called CC=7 SCAR=21.2
| |||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | # During global destruction perl may have already destroyed the | ||||||||||||||||||||||||||||||||
| 214 | # IO::Socket::UNIX in $entry->{fh} before our Client's DESTROY runs | ||||||||||||||||||||||||||||||||
| 215 | # disconnect. Treat a missing handle as already-closed. | ||||||||||||||||||||||||||||||||
| 216 | 240 | ▶ | return 1 unless defined $fh; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||
| 218 | # Pull everything currently readable on $fh into the caller's buffer | ||||||||||||||||||||||||||||||||
| 219 | # without blocking, and report whether the peer has closed. Caller is | ||||||||||||||||||||||||||||||||
| 220 | # responsible for slicing complete frames out of the buffer afterwards | ||||||||||||||||||||||||||||||||
| 221 | # via _extract_frames. | ||||||||||||||||||||||||||||||||
| 222 | 240 | my $eof = 0; | |||||||||||||||||||||||||||||||
| 223 | 240 | while (1) { | |||||||||||||||||||||||||||||||
| 224 | 361 | my $chunk; | |||||||||||||||||||||||||||||||
| 225 | 361 | my $n = sysread($fh, $chunk, 65536); | |||||||||||||||||||||||||||||||
| 226 | 361 | ▶ | if (defined $n) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 227 | # 0 bytes = peer closed cleanly; otherwise append and keep reading. | ||||||||||||||||||||||||||||||||
| 228 | 200 | ▶ | if ($n == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 229 | 79 | $eof = 1; | |||||||||||||||||||||||||||||||
| 230 | 79 | last; | |||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||
| 232 | 121 | $$buf_ref .= $chunk; | |||||||||||||||||||||||||||||||
| 233 | 121 | next; | |||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||
| 235 | # No more data right now — done draining, not an error. | ||||||||||||||||||||||||||||||||
| 236 | 161 | ▶ | last if $! == EAGAIN || $! == EWOULDBLOCK; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$! == 11 or $! == 11
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11
$! == 11
Truth table33%1/3
A or B
A= $! == 11B= $! == 11
| |||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | next if $! == EINTR; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | # Treat other read errors as EOF: connection is broken. | ||||||||||||||||||||||||||||||||
| 239 | 0 | $eof = 1; | |||||||||||||||||||||||||||||||
| 240 | 0 | last; | |||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | 240 | return $eof; | |||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | sub _extract_frames { | ||||||||||||||||||||||||||||||||
| 247 | 240 | ▶ | my ($self, $buf_ref) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_extract_frames: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | # Slice as many complete frames as are present out of the caller's read | ||||||||||||||||||||||||||||||||
| 250 | # buffer, leaving any partial trailing frame in place for the next call. | ||||||||||||||||||||||||||||||||
| 251 | # Each frame is `pack('N', $len) . $payload`; we peek at the length | ||||||||||||||||||||||||||||||||
| 252 | # prefix, bail if the full payload has not arrived yet, and otherwise | ||||||||||||||||||||||||||||||||
| 253 | # consume the prefix+payload from the buffer. | ||||||||||||||||||||||||||||||||
| 254 | 240 | my @out; | |||||||||||||||||||||||||||||||
| 255 | 240 | ▶ | while (length($$buf_ref) >= 4) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 256 | 198 | my $len = unpack('N', substr($$buf_ref, 0, 4)); | |||||||||||||||||||||||||||||||
| 257 | 198 | ▶ | last if length($$buf_ref) < 4 + $len; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 258 | 198 | my $payload = substr($$buf_ref, 4, $len); | |||||||||||||||||||||||||||||||
| 259 | 198 | substr($$buf_ref, 0, 4 + $len) = ''; | |||||||||||||||||||||||||||||||
| 260 | 198 | push @out => $payload; | |||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||
| 262 | 240 | return @out; | |||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||
| 265 | sub _connect_to_peer { | ||||||||||||||||||||||||||||||||
| 266 | 77 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_connect_to_peer: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||
| 267 | 77 | my ($peer_id) = @_; | |||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | 77 | my $sock = $self->peer_exists($peer_id); | |||||||||||||||||||||||||||||||
| 270 | 77 | ▶ | die "'$peer_id' is not a valid message recipient" unless $sock; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 271 | 77 | ▶ | die "'$peer_id' is not listening" unless -S $sock; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||
| 273 | 77 | ▶ | my $s = IO::Socket::UNIX->new( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 274 | Type => SOCK_STREAM, | ||||||||||||||||||||||||||||||||
| 275 | Peer => $sock, | ||||||||||||||||||||||||||||||||
| 276 | ) or die "Cannot connect to '$sock': $!"; | ||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | 77 | $s->blocking(0); | |||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | 77 | my $entry = { | |||||||||||||||||||||||||||||||
| 281 | fh => $s, | ||||||||||||||||||||||||||||||||
| 282 | last_active => time, | ||||||||||||||||||||||||||||||||
| 283 | role => 'initiator', | ||||||||||||||||||||||||||||||||
| 284 | read_buffer => '', | ||||||||||||||||||||||||||||||||
| 285 | send_buffer => '', | ||||||||||||||||||||||||||||||||
| 286 | sent_hello => 0, | ||||||||||||||||||||||||||||||||
| 287 | recv_hello => 1, # we initiated; we know who they are by definition | ||||||||||||||||||||||||||||||||
| 288 | }; | ||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | # Send hello frame first. | ||||||||||||||||||||||||||||||||
| 291 | 77 | my $hello = $self->{+SERIALIZER}->serialize({hello => $self->{+ID}}); | |||||||||||||||||||||||||||||||
| 292 | 77 | $self->_send_frame($s, $hello); | |||||||||||||||||||||||||||||||
| 293 | 77 | $entry->{sent_hello} = 1; | |||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | 77 | $self->{+CONNECTIONS}->{$peer_id} = $entry; | |||||||||||||||||||||||||||||||
| 296 | 77 | return $entry; | |||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||
| 299 | sub _accept_pending { | ||||||||||||||||||||||||||||||||
| 300 | 156 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_accept_pending: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | 156 | ▶ | my $ls = $self->{+LISTEN_SOCKET} or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||
| 304 | 155 | ▶ | while (my $client = $ls->accept) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 305 | 66 | $client->blocking(0); | |||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | # Park under a temporary key until we read the hello frame. We use a | ||||||||||||||||||||||||||||||||
| 308 | # special hash slot keyed by stringified handle so the new connection | ||||||||||||||||||||||||||||||||
| 309 | # is included in select handles immediately. | ||||||||||||||||||||||||||||||||
| 310 | 66 | my $tmp_key = "_pending_" . fileno($client); | |||||||||||||||||||||||||||||||
| 311 | 66 | $self->{+CONNECTIONS}->{$tmp_key} = { | |||||||||||||||||||||||||||||||
| 312 | fh => $client, | ||||||||||||||||||||||||||||||||
| 313 | last_active => time, | ||||||||||||||||||||||||||||||||
| 314 | role => 'listener', | ||||||||||||||||||||||||||||||||
| 315 | read_buffer => '', | ||||||||||||||||||||||||||||||||
| 316 | send_buffer => '', | ||||||||||||||||||||||||||||||||
| 317 | sent_hello => 0, | ||||||||||||||||||||||||||||||||
| 318 | recv_hello => 0, | ||||||||||||||||||||||||||||||||
| 319 | _pending_key => $tmp_key, | ||||||||||||||||||||||||||||||||
| 320 | }; | ||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | sub _resolve_pending { | ||||||||||||||||||||||||||||||||
| 325 | 156 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_resolve_pending: called CC=16 SCAR=34.8
| |||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | 156 | my $cons = $self->{+CONNECTIONS}; | |||||||||||||||||||||||||||||||
| 328 | 156 | for my $key (keys %$cons) { | |||||||||||||||||||||||||||||||
| 329 | 174 | ▶ | next unless $key =~ /^_pending_/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 330 | 68 | my $entry = $cons->{$key}; | |||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||
| 332 | 68 | my $eof = $self->_drain_reads($entry->{fh}, \$entry->{read_buffer}); | |||||||||||||||||||||||||||||||
| 333 | 68 | my @frames = $self->_extract_frames(\$entry->{read_buffer}); | |||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | 68 | ▶ | if (@frames) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 336 | 66 | my $hello_raw = shift @frames; | |||||||||||||||||||||||||||||||
| 337 | 66 | my $hello; | |||||||||||||||||||||||||||||||
| 338 | 66 | my $hello_ok = eval { $hello = $self->{+SERIALIZER}->deserialize($hello_raw); 1 }; | |||||||||||||||||||||||||||||||
| 339 | 66 | ▶ | unless ($hello_ok) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 340 | # Malformed hello — drop the connection. | ||||||||||||||||||||||||||||||||
| 341 | 0 | warn "ConnectionUnix: malformed hello frame: $@"; | |||||||||||||||||||||||||||||||
| 342 | 0 | eval { $entry->{fh}->close }; | |||||||||||||||||||||||||||||||
| 343 | 0 | delete $cons->{$key}; | |||||||||||||||||||||||||||||||
| 344 | 0 | next; | |||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||
| 347 | 66 | ▶ | my $peer_id = ref($hello) eq 'HASH' ? $hello->{hello} : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 348 | 66 | ▶ | unless (defined $peer_id && length $peer_id) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $peer_id and length $peer_id
MC/DC0%0/2
defined $peer_id and length $peer_id
defined $peer_id
length $peer_id
Truth table33%1/3
A and B
A= defined $peer_idB= length $peer_id
| |||||||||||||||||||||||||||||||||
| 349 | 0 | warn "ConnectionUnix: hello frame missing peer id"; | |||||||||||||||||||||||||||||||
| 350 | 0 | eval { $entry->{fh}->close }; | |||||||||||||||||||||||||||||||
| 351 | 0 | delete $cons->{$key}; | |||||||||||||||||||||||||||||||
| 352 | 0 | next; | |||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||
| 355 | # If we already have a connection to this peer it is necessarily | ||||||||||||||||||||||||||||||||
| 356 | # stale: the remote side dropped the previous fd and reconnected. | ||||||||||||||||||||||||||||||||
| 357 | # Drain anything still buffered on the old entry, then close it, | ||||||||||||||||||||||||||||||||
| 358 | # so the new connection becomes the live one. | ||||||||||||||||||||||||||||||||
| 359 | 66 | ▶ | if (my $old = delete $cons->{$peer_id}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 360 | 4 | ▶ | if (my $lf = delete $old->{leftover_frames}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | $entry->{leftover_frames} ||= []; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$entry->{"leftover_frames"} ||= []
MC/DC0%0/1
$entry->{"leftover_frames"} ||= []
$entry->{"leftover_frames"}
Truth table0%0/2
A
A= $entry->{"leftover_frames"}
| |||||||||||||||||||||||||||||||||
| 362 | 0 | unshift @{$entry->{leftover_frames}}, @$lf; | |||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||
| 364 | 4 | $self->_drain_reads($old->{fh}, \$old->{read_buffer}); | |||||||||||||||||||||||||||||||
| 365 | 4 | my @stale = $self->_extract_frames(\$old->{read_buffer}); | |||||||||||||||||||||||||||||||
| 366 | 4 | ▶ | if (@stale) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 367 | 0 | ▶ | $entry->{leftover_frames} ||= []; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$entry->{"leftover_frames"} ||= []
MC/DC0%0/1
$entry->{"leftover_frames"} ||= []
$entry->{"leftover_frames"}
Truth table0%0/2
A
A= $entry->{"leftover_frames"}
| |||||||||||||||||||||||||||||||||
| 368 | 0 | unshift @{$entry->{leftover_frames}}, @stale; | |||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||
| 370 | 4 | eval { $old->{fh}->close }; | |||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||
| 373 | 66 | $entry->{recv_hello} = 1; | |||||||||||||||||||||||||||||||
| 374 | 66 | $entry->{last_active} = time; | |||||||||||||||||||||||||||||||
| 375 | # Move to its real key. | ||||||||||||||||||||||||||||||||
| 376 | 66 | delete $entry->{_pending_key}; | |||||||||||||||||||||||||||||||
| 377 | 66 | $cons->{$peer_id} = $entry; | |||||||||||||||||||||||||||||||
| 378 | 66 | delete $cons->{$key}; | |||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||
| 380 | # Any frames after hello are real messages — push them back into | ||||||||||||||||||||||||||||||||
| 381 | # the buffer so the normal reader picks them up. Easier: stash | ||||||||||||||||||||||||||||||||
| 382 | # leftover frames on the entry and let _read_messages pull them. | ||||||||||||||||||||||||||||||||
| 383 | 66 | ▶ | if (@frames) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 384 | 65 | ▶ | $entry->{leftover_frames} ||= []; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$entry->{"leftover_frames"} ||= []
MC/DC0%0/1
$entry->{"leftover_frames"} ||= []
$entry->{"leftover_frames"}
Truth table50%1/2
A
A= $entry->{"leftover_frames"}
| |||||||||||||||||||||||||||||||||
| 385 | 65 | push @{$entry->{leftover_frames}} => @frames; | |||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||
| 388 | elsif ($eof) { | ||||||||||||||||||||||||||||||||
| 389 | 0 | eval { $entry->{fh}->close }; | |||||||||||||||||||||||||||||||
| 390 | 0 | delete $cons->{$key}; | |||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | sub _read_messages { | ||||||||||||||||||||||||||||||||
| 396 | 156 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_read_messages: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||
| 398 | 156 | my @msgs; | |||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||
| 400 | 156 | my $cons = $self->{+CONNECTIONS}; | |||||||||||||||||||||||||||||||
| 401 | 156 | for my $peer (keys %$cons) { | |||||||||||||||||||||||||||||||
| 402 | 170 | ▶ | next if $peer =~ /^_pending_/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 403 | 168 | my $entry = $cons->{$peer}; | |||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||
| 405 | # Pop any leftover frames captured during hello resolution first. | ||||||||||||||||||||||||||||||||
| 406 | 168 | ▶ | if (my $lf = delete $entry->{leftover_frames}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 407 | 65 | for my $raw (@$lf) { | |||||||||||||||||||||||||||||||
| 408 | 71 | ▶ | my $msg = $self->_decode_message($raw, $entry, $peer) or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 409 | 71 | push @msgs => $msg; | |||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | 168 | my $eof = $self->_drain_reads($entry->{fh}, \$entry->{read_buffer}); | |||||||||||||||||||||||||||||||
| 414 | 168 | my @frames = $self->_extract_frames(\$entry->{read_buffer}); | |||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||
| 416 | 168 | for my $raw (@frames) { | |||||||||||||||||||||||||||||||
| 417 | 61 | ▶ | my $msg = $self->_decode_message($raw, $entry, $peer) or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 418 | 61 | push @msgs => $msg; | |||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||
| 421 | 168 | ▶ | if (@frames) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 422 | 55 | $entry->{last_active} = time; | |||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | 168 | ▶ | if ($eof && length($entry->{read_buffer}) == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$eof and length $entry->{"read_buffer"} == 0
MC/DC50%1/2
$eof and length $entry->{"read_buffer"} == 0
$eof
length $entry->{"read_buffer"} == 0
Truth table66%2/3
A and B
A= $eofB= length $entry->{"read_buffer"} == 0
| |||||||||||||||||||||||||||||||||
| 426 | 67 | $self->_close_connection($peer); | |||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||
| 430 | 156 | return @msgs; | |||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||
| 433 | sub _decode_message { | ||||||||||||||||||||||||||||||||
| 434 | 132 | ▶ | my ($self, $raw, $entry, $peer) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_decode_message: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||||||
| 435 | 132 | my $data; | |||||||||||||||||||||||||||||||
| 436 | 132 | ▶ | unless (eval { $data = $self->{+SERIALIZER}->deserialize($raw); 1 }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 437 | 0 | warn "ConnectionUnix: malformed frame from '$peer': $@"; | |||||||||||||||||||||||||||||||
| 438 | 0 | return undef; | |||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||
| 440 | 132 | ▶ | return undef unless ref($data) eq 'HASH'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 441 | # Tolerate a stray hello (shouldn't happen on an already-resolved entry). | ||||||||||||||||||||||||||||||||
| 442 | 132 | ▶ | return undef if exists $data->{hello} && keys(%$data) == 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $data->{"hello"} and keys %$data == 1
MC/DC0%0/2
exists $data->{"hello"} and keys %$data == 1
exists $data->{"hello"}
keys %$data == 1
Truth table33%1/3
A and B
A= exists $data->{"hello"}B= keys %$data == 1
| |||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||
| 444 | 132 | my $msg = IPC::Manager::Message->new($data); | |||||||||||||||||||||||||||||||
| 445 | 132 | $self->{+STATS}->{read}->{$msg->{from}}++; | |||||||||||||||||||||||||||||||
| 446 | 132 | return $msg; | |||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||
| 449 | # --- Public API --- | ||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||
| 451 | sub pending_messages { | ||||||||||||||||||||||||||||||||
| 452 | 258 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
pending_messages: called CC=10 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 453 | 258 | $self->pid_check; | |||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||
| 455 | 258 | ▶ | return 1 if $self->have_resume_file; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||
| 457 | # Any buffered frames already? | ||||||||||||||||||||||||||||||||
| 458 | 258 | my $cons = $self->{+CONNECTIONS}; | |||||||||||||||||||||||||||||||
| 459 | 258 | for my $entry (values %$cons) { | |||||||||||||||||||||||||||||||
| 460 | 150 | ▶ | return 1 if $entry->{leftover_frames} && @{$entry->{leftover_frames}}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$entry->{"leftover_frames"} and @{$entry->{"leftover_frames"};}
MC/DC0%0/2
$entry->{"leftover_frames"} and @{$entry->{"leftover_frames"};}
$entry->{"leftover_frames"}
@{$entry->{"leftover_frames"};}
Truth table33%1/3
A and B
A= $entry->{"leftover_frames"}B= @{$entry->{"leftover_frames"};}
| |||||||||||||||||||||||||||||||||
| 461 | return 1 if length($entry->{read_buffer} // '') >= 4 | ||||||||||||||||||||||||||||||||
| 462 | 150 | ▶ | && length($entry->{read_buffer}) >= 4 + unpack('N', substr($entry->{read_buffer}, 0, 4)); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
length($entry->{"read_buffer"} // "") >= 4 and length $entry->{"read_buffer"} >= 4 + unpack("N", substr($entry->{"read_buffer"}, 0, 4))
$entry->{"read_buffer"} // ""
MC/DC0%0/2
length($entry->{"read_buffer"} // "") >= 4 and length $entry->{"read_buffer"} >= 4 + unpack("N", substr($entry->{"read_buffer"}, 0, 4))
$entry->{"read_buffer"}
length $entry->{"read_buffer"} >= 4 + unpack("N", substr($entry->{"read_buffer"}, 0, 4))
Truth table0%0/3
A and B
A= $entry->{"read_buffer"}B= length $entry->{"read_buffer"} >= 4 + unpack("N", substr($entry->{"read_buffer"}, 0, 4))
| |||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||
| 465 | 258 | if (USE_IO_SELECT()) { | |||||||||||||||||||||||||||||||
| 466 | 258 | my @h = $self->handles_for_select; | |||||||||||||||||||||||||||||||
| 467 | 258 | ▶ | return 0 unless @h; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 468 | 252 | my $sel = IO::Select->new(@h); | |||||||||||||||||||||||||||||||
| 469 | 252 | ▶ | return $sel->can_read(0) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||
| 472 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||
| 475 | sub ready_messages { | ||||||||||||||||||||||||||||||||
| 476 | 129 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
ready_messages: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 477 | 129 | $self->pid_check; | |||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||
| 479 | 129 | ▶ | return 1 if $self->have_resume_file; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 480 | 129 | ▶ | return 1 if $self->pending_messages; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||
| 482 | 112 | return 0; | |||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||
| 485 | sub get_messages { | ||||||||||||||||||||||||||||||||
| 486 | 156 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
get_messages: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||
| 488 | 156 | my @out; | |||||||||||||||||||||||||||||||
| 489 | 156 | push @out => $self->read_resume_file; | |||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||
| 491 | 156 | $self->_accept_pending; | |||||||||||||||||||||||||||||||
| 492 | 156 | $self->_resolve_pending; | |||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||
| 494 | 156 | push @out => $self->_read_messages; | |||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||
| 496 | 156 | return $self->sort_messages(@out); | |||||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||
| 499 | sub send_message { | ||||||||||||||||||||||||||||||||
| 500 | 135 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
send_message: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 501 | 135 | my $msg = $self->build_message(@_); | |||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||
| 503 | 135 | ▶ | my $peer_id = $msg->to or croak "No peer specified"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||
| 505 | 135 | $self->pid_check; | |||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||
| 507 | 135 | my $payload = $self->{+SERIALIZER}->serialize($msg); | |||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||
| 509 | # Inside a service the client has set_send_blocking(0) and the | ||||||||||||||||||||||||||||||||
| 510 | # service event loop drains queued frames when the connection | ||||||||||||||||||||||||||||||||
| 511 | # becomes writable. Outside a service we still block until the | ||||||||||||||||||||||||||||||||
| 512 | # bytes are committed so short-lived caller code keeps the | ||||||||||||||||||||||||||||||||
| 513 | # synchronous semantics it has always had. | ||||||||||||||||||||||||||||||||
| 514 | 135 | ▶ | return $self->try_send_message($peer_id, $payload) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 515 | unless $self->send_blocking; | ||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||
| 517 | 99 | my $entry = $self->{+CONNECTIONS}->{$peer_id}; | |||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||
| 519 | 99 | ▶ | if (!$entry) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 520 | # No cached connection — must be able to connect. peer_exists will | ||||||||||||||||||||||||||||||||
| 521 | # be undef for unknown peers; peer_is_listener gates non-listeners. | ||||||||||||||||||||||||||||||||
| 522 | 76 | ▶ | die "'$peer_id' is not a valid message recipient" unless $self->peer_exists($peer_id); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 523 | 75 | ▶ | die "no active connection to '$peer_id' and peer is not listening" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 524 | unless $self->peer_is_listener($peer_id); | ||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||
| 526 | 74 | $entry = $self->_connect_to_peer($peer_id); | |||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||
| 529 | # Drain any leftover non-blocking partial frame for this peer first, | ||||||||||||||||||||||||||||||||
| 530 | # otherwise our new frame would interleave with it on the wire. | ||||||||||||||||||||||||||||||||
| 531 | 97 | ▶ | $self->_flush_send_buffer($entry) if length $entry->{send_buffer}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||
| 533 | # Send the frame. No auto-reconnect: SOCK_STREAM peer churn is the | ||||||||||||||||||||||||||||||||
| 534 | # caller's problem, just like with sockets generally. Auto-retrying | ||||||||||||||||||||||||||||||||
| 535 | # would also silently lose any queued bytes already in send_buffer | ||||||||||||||||||||||||||||||||
| 536 | # that were flushed (or partially flushed) before the EPIPE. | ||||||||||||||||||||||||||||||||
| 537 | 97 | my $ok = eval { $self->_send_frame($entry->{fh}, $payload); 1 }; | |||||||||||||||||||||||||||||||
| 538 | 97 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 539 | 1 | my $err = $@; | |||||||||||||||||||||||||||||||
| 540 | # Defensive eval: _close_connection only evals close() today, but | ||||||||||||||||||||||||||||||||
| 541 | # future hooks could throw and would mask the real send error. | ||||||||||||||||||||||||||||||||
| 542 | 1 | ▶ | eval { $self->_close_connection($peer_id); 1 } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 543 | or warn "ConnectionUnix: error closing connection to '$peer_id' after send failure: $@"; | ||||||||||||||||||||||||||||||||
| 544 | 1 | die $err; | |||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||
| 546 | 96 | $entry->{last_active} = time; | |||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||
| 548 | 96 | $self->{+STATS}->{sent}->{$msg->{to}}++; | |||||||||||||||||||||||||||||||
| 549 | 96 | return 1; | |||||||||||||||||||||||||||||||
| 550 | } | ||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||
| 552 | # --- Role::Outbox plumbing --- | ||||||||||||||||||||||||||||||||
| 553 | # | ||||||||||||||||||||||||||||||||
| 554 | # Connection-oriented framing means a partial write of a frame must NOT be | ||||||||||||||||||||||||||||||||
| 555 | # re-sent from the start: the peer would see a corrupt mix of trailing bytes | ||||||||||||||||||||||||||||||||
| 556 | # from the old attempt and length prefix from the new. So instead of using | ||||||||||||||||||||||||||||||||
| 557 | # the role's whole-payload queue we maintain a per-connection | ||||||||||||||||||||||||||||||||
| 558 | # $entry->{send_buffer} of raw outbound bytes. _outbox_try_write always | ||||||||||||||||||||||||||||||||
| 559 | # accepts the new frame (returns 1), appending to send_buffer if necessary, | ||||||||||||||||||||||||||||||||
| 560 | # and the role's queue stays empty. The framework calls drain_pending / | ||||||||||||||||||||||||||||||||
| 561 | # writable_handles which we override to walk send_buffers instead. | ||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||
| 563 | sub _outbox_try_write { | ||||||||||||||||||||||||||||||||
| 564 | 36 | ▶ | my ($self, $peer, $payload) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_outbox_try_write: called CC=6 SCAR=20.1
| |||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||
| 566 | 36 | $self->pid_check; | |||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||
| 568 | 36 | my $entry = $self->{+CONNECTIONS}->{$peer}; | |||||||||||||||||||||||||||||||
| 569 | 36 | ▶ | if (!$entry) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 570 | 3 | ▶ | die "'$peer' is not a valid message recipient" unless $self->peer_exists($peer); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 571 | 3 | ▶ | die "no active connection to '$peer' and peer is not listening" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 572 | unless $self->peer_is_listener($peer); | ||||||||||||||||||||||||||||||||
| 573 | 3 | $entry = $self->_connect_to_peer($peer); | |||||||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||
| 576 | 36 | my $frame = pack('N', length $payload) . $payload; | |||||||||||||||||||||||||||||||
| 577 | 36 | $entry->{send_buffer} .= $frame; | |||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||
| 579 | 36 | my $flushed_ok = eval { $self->_flush_send_buffer($entry); 1 }; | |||||||||||||||||||||||||||||||
| 580 | 36 | ▶ | unless ($flushed_ok) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 581 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||
| 582 | # Hard write error (e.g. EPIPE). Drop the connection and lose the | ||||||||||||||||||||||||||||||||
| 583 | # in-flight bytes; future sends to this peer reconnect. | ||||||||||||||||||||||||||||||||
| 584 | 0 | warn "ConnectionUnix: send to '$peer' failed: $err"; | |||||||||||||||||||||||||||||||
| 585 | 0 | ▶ | eval { $self->_close_connection($peer); 1 } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 586 | or warn "ConnectionUnix: error closing connection to '$peer' after send failure: $@"; | ||||||||||||||||||||||||||||||||
| 587 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 588 | } | ||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||
| 590 | 36 | $entry->{last_active} = time; | |||||||||||||||||||||||||||||||
| 591 | 36 | $self->{+STATS}->{sent}->{$peer}++; | |||||||||||||||||||||||||||||||
| 592 | 36 | return 1; | |||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||
| 595 | sub _outbox_writable_handle { | ||||||||||||||||||||||||||||||||
| 596 | 0 | ▶ | my ($self, $peer) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_outbox_writable_handle: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 597 | 0 | ▶ | my $entry = $self->{+CONNECTIONS}->{$peer} or return undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 598 | 0 | return $entry->{fh}; | |||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||
| 601 | sub _outbox_set_blocking { | ||||||||||||||||||||||||||||||||
| 602 | # Sockets stay non-blocking either way (reads need it). send_message | ||||||||||||||||||||||||||||||||
| 603 | # branches on send_blocking() to choose the synchronous _send_frame | ||||||||||||||||||||||||||||||||
| 604 | # loop or the deferred-via-send_buffer path. | ||||||||||||||||||||||||||||||||
| 605 | 29 | ▶ | return; | ||||||||||||||||||||||||||||||
|
Subroutine
_outbox_set_blocking: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||
| 608 | sub _outbox_can_send { | ||||||||||||||||||||||||||||||||
| 609 | 1 | ▶ | my ($self, $peer) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_outbox_can_send: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||
| 610 | 1 | ▶ | my $entry = $self->{+CONNECTIONS}->{$peer} or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 611 | 1 | ▶ | return length($entry->{send_buffer}) ? 0 : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||
| 614 | # Override the role's queue-based bookkeeping: our partial frames live in | ||||||||||||||||||||||||||||||||
| 615 | # per-connection send_buffers, not in the role's _OUTBOX hash, so the role | ||||||||||||||||||||||||||||||||
| 616 | # would otherwise always report "nothing pending" and the service loop | ||||||||||||||||||||||||||||||||
| 617 | # would never pick our writable handles. | ||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||
| 619 | sub pending_sends { | ||||||||||||||||||||||||||||||||
| 620 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
pending_sends: called CC=6 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 621 | 4 | my $n = 0; | |||||||||||||||||||||||||||||||
| 622 | 4 | ▶ | for my $key (keys %{$self->{+CONNECTIONS} // {}}) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"connections"} // {}
MC/DC0%0/1
$self->{"connections"} // {}
$self->{"connections"}
Truth table50%1/2
A
A= $self->{"connections"}
| |||||||||||||||||||||||||||||||||
| 623 | 3 | ▶ | next if $key =~ /^_pending_/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 624 | 3 | my $entry = $self->{+CONNECTIONS}->{$key}; | |||||||||||||||||||||||||||||||
| 625 | 3 | ▶ | $n++ if length($entry->{send_buffer} // ''); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$entry->{"send_buffer"} // ""
MC/DC0%0/1
$entry->{"send_buffer"} // ""
$entry->{"send_buffer"}
Truth table50%1/2
A
A= $entry->{"send_buffer"}
| |||||||||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||||||
| 627 | 4 | return $n; | |||||||||||||||||||||||||||||||
| 628 | } | ||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||
| 630 | sub have_pending_sends { | ||||||||||||||||||||||||||||||||
| 631 | 108 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
have_pending_sends: called CC=6 SCAR=18.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 632 | 108 | ▶ | for my $key (keys %{$self->{+CONNECTIONS} // {}}) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"connections"} // {}
MC/DC0%0/1
$self->{"connections"} // {}
$self->{"connections"}
Truth table50%1/2
A
A= $self->{"connections"}
| |||||||||||||||||||||||||||||||||
| 633 | 48 | ▶ | next if $key =~ /^_pending_/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 634 | 46 | ▶ | return 1 if length($self->{+CONNECTIONS}->{$key}->{send_buffer} // ''); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"connections"}{$key}{"send_buffer"} // ""
MC/DC0%0/1
$self->{"connections"}{$key}{"send_buffer"} // ""
$self->{"connections"}{$key}{"send_buffer"}
Truth table50%1/2
A
A= $self->{"connections"}{$key}{"send_buffer"}
| |||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||
| 636 | 108 | return 0; | |||||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||
| 639 | sub pending_sends_to { | ||||||||||||||||||||||||||||||||
| 640 | 0 | ▶ | my ($self, $peer) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
pending_sends_to: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 641 | 0 | ▶ | my $entry = $self->{+CONNECTIONS}->{$peer} or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 642 | 0 | ▶ | return length($entry->{send_buffer} // '') ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$entry->{"send_buffer"} // ""
MC/DC0%0/1
$entry->{"send_buffer"} // ""
$entry->{"send_buffer"}
Truth table0%0/2
A
A= $entry->{"send_buffer"}
| |||||||||||||||||||||||||||||||||
| 643 | } | ||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||
| 645 | sub drain_pending { | ||||||||||||||||||||||||||||||||
| 646 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
drain_pending: called CC=9 SCAR=25.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||
| 648 | 2 | my $delivered = 0; | |||||||||||||||||||||||||||||||
| 649 | 2 | ▶ | for my $key (keys %{$self->{+CONNECTIONS} // {}}) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"connections"} // {}
MC/DC0%0/1
$self->{"connections"} // {}
$self->{"connections"}
Truth table50%1/2
A
A= $self->{"connections"}
| |||||||||||||||||||||||||||||||||
| 650 | 2 | ▶ | next if $key =~ /^_pending_/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 651 | 2 | my $entry = $self->{+CONNECTIONS}->{$key}; | |||||||||||||||||||||||||||||||
| 652 | 2 | ▶ | next unless length($entry->{send_buffer} // ''); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$entry->{"send_buffer"} // ""
MC/DC0%0/1
$entry->{"send_buffer"} // ""
$entry->{"send_buffer"}
Truth table50%1/2
A
A= $entry->{"send_buffer"}
| |||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||
| 654 | 1 | my $ok = eval { $self->_flush_send_buffer($entry); 1 }; | |||||||||||||||||||||||||||||||
| 655 | 1 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 656 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||
| 657 | 0 | warn "ConnectionUnix: send to '$key' failed during drain: $err"; | |||||||||||||||||||||||||||||||
| 658 | 0 | ▶ | eval { $self->_close_connection($key); 1 } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 659 | or warn "ConnectionUnix: error closing connection to '$key' after drain failure: $@"; | ||||||||||||||||||||||||||||||||
| 660 | 0 | next; | |||||||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||
| 663 | 1 | ▶ | if (!length($entry->{send_buffer})) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 664 | 1 | $entry->{last_active} = time; | |||||||||||||||||||||||||||||||
| 665 | 1 | $delivered++; | |||||||||||||||||||||||||||||||
| 666 | } | ||||||||||||||||||||||||||||||||
| 667 | } | ||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||
| 669 | 2 | return $delivered; | |||||||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||
| 672 | sub have_writable_handles { | ||||||||||||||||||||||||||||||||
| 673 | 82 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
have_writable_handles: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 674 | 82 | ▶ | for my $key (keys %{$self->{+CONNECTIONS} // {}}) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"connections"} // {}
MC/DC0%0/1
$self->{"connections"} // {}
$self->{"connections"}
Truth table50%1/2
A
A= $self->{"connections"}
| |||||||||||||||||||||||||||||||||
| 675 | 36 | ▶ | next if $key =~ /^_pending_/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 676 | 34 | ▶ | return 1 if length($self->{+CONNECTIONS}->{$key}->{send_buffer} // ''); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"connections"}{$key}{"send_buffer"} // ""
MC/DC0%0/1
$self->{"connections"}{$key}{"send_buffer"} // ""
$self->{"connections"}{$key}{"send_buffer"}
Truth table50%1/2
A
A= $self->{"connections"}{$key}{"send_buffer"}
| |||||||||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||||||
| 678 | 81 | return 0; | |||||||||||||||||||||||||||||||
| 679 | } | ||||||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||||||
| 681 | sub writable_handles { | ||||||||||||||||||||||||||||||||
| 682 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
writable_handles: called CC=7 SCAR=20.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 683 | 1 | my @h; | |||||||||||||||||||||||||||||||
| 684 | 1 | ▶ | for my $key (keys %{$self->{+CONNECTIONS} // {}}) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"connections"} // {}
MC/DC0%0/1
$self->{"connections"} // {}
$self->{"connections"}
Truth table50%1/2
A
A= $self->{"connections"}
| |||||||||||||||||||||||||||||||||
| 685 | 1 | ▶ | next if $key =~ /^_pending_/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 686 | 1 | my $entry = $self->{+CONNECTIONS}->{$key}; | |||||||||||||||||||||||||||||||
| 687 | 1 | ▶ | next unless length($entry->{send_buffer} // ''); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$entry->{"send_buffer"} // ""
MC/DC0%0/1
$entry->{"send_buffer"} // ""
$entry->{"send_buffer"}
Truth table50%1/2
A
A= $entry->{"send_buffer"}
| |||||||||||||||||||||||||||||||||
| 688 | 1 | ▶ | push @h => $entry->{fh} if $entry->{fh}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 689 | } | ||||||||||||||||||||||||||||||||
| 690 | 1 | return @h; | |||||||||||||||||||||||||||||||
| 691 | } | ||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||
| 693 | 87 | 1; | |||||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||||||
| 695 | __END__ | ||||||||||||||||||||||||||||||||