| 1 | package Net::NATS2::Connection; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Errno qw(EAGAIN EINTR EWOULDBLOCK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Encode qw(encode_utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use constant CRLF => "\r\n"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use constant _0E0 => '0E0'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub _new { | ||||||||||||||||||||||||||||
| 15 | 5 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
_new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 5 | return bless {@_}, $class; | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub socket_args { | ||||||||||||||||||||||||||||
| 20 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
socket_args: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 12 | ▶ | $self->{socket_args} = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 12 | return $self->{socket_args}; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub _socket { | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_socket: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $self->{_socket} = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | return $self->{_socket}; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | sub buffer : lvalue { $_[0]->{buffer} } | ||||||||||||||||||||||||||
|
Subroutine
buffer: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | sub default_buffer_size : lvalue { $_[0]->{default_buffer_size} } | ||||||||||||||||||||||||||
|
Subroutine
default_buffer_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub eobuf : lvalue { $_[0]->{eobuf} } | ||||||||||||||||||||||||||
|
Subroutine
eobuf: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub eof : lvalue { $_[0]->{eof} } | ||||||||||||||||||||||||||
|
Subroutine
eof: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub error : lvalue { $_[0]->{error} } | ||||||||||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub new { | ||||||||||||||||||||||||||||
| 38 | 4 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=17.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | 4 | my $self = $class->_new(@_); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 4 | $self->socket_args->{Proto} = 'tcp'; | |||||||||||||||||||||||||||
| 42 | 4 | ▶ | $self->default_buffer_size ||= $self->socket_args->{BufferSize} || 1024; | ||||||||||||||||||||||||||
|
Condition40%2/5
$self->default_buffer_size ||= $self->socket_args->{"BufferSize"} || 1024
$self->socket_args->{'BufferSize'} || 1024
MC/DC0%0/2
$self->default_buffer_size ||= $self->socket_args->{"BufferSize"} || 1024
$self->default_buffer_size
$self->socket_args->{'BufferSize'}
Truth table0%0/3
A ||= B
A= $self->default_buffer_sizeB= $self->socket_args->{'BufferSize'}
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | my $socket = IO::Socket::INET->new(%{$self->socket_args}) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | $self->_socket($socket); | |||||||||||||||||||||||||||
| 46 | 0 | $socket->blocking(0); | |||||||||||||||||||||||||||
| 47 | 0 | $self->flush(); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | return $self; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub upgrade { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
upgrade: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | unless ($IO::Socket::SSL::VERSION) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | eval { require IO::Socket::SSL }; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $socket = IO::Socket::SSL->start_SSL($self->_socket, %{$self->socket_args}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | or die "failed to start_SSL: error=$!, ssl_error=$IO::Socket::SSL_ERROR"; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | $self->_socket($socket); | |||||||||||||||||||||||||||
| 64 | 0 | $socket->blocking(0); | |||||||||||||||||||||||||||
| 65 | 0 | $self->flush(); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # clear out the buffered data | ||||||||||||||||||||||||||||
| 69 | sub flush { | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
flush: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | $self->buffer = ''; | |||||||||||||||||||||||||||
| 73 | 0 | $self->eobuf = 0; | |||||||||||||||||||||||||||
| 74 | 0 | $self->eof = 0; | |||||||||||||||||||||||||||
| 75 | 0 | $self->error = ''; | |||||||||||||||||||||||||||
| 76 | 0 | return 1; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # test if we have data on the handle, with optional timeout. | ||||||||||||||||||||||||||||
| 80 | sub can_read { | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
can_read: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | return IO::Select->new($self->_socket)->can_read(@_); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # block until the handle is ready to write, with optional timeout. | ||||||||||||||||||||||||||||
| 87 | sub can_write { | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
can_write: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | return IO::Select->new($self->_socket)->can_write(@_); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # implement non-blocking getline() function by managing our own data buffer | ||||||||||||||||||||||||||||
| 95 | # based on sample code from "Network Programming with Perl" by L.D. Stein. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # $bytes = $self->nb_getline($data); | ||||||||||||||||||||||||||||
| 98 | # data is stored in $data, returns number of bytes on success | ||||||||||||||||||||||||||||
| 99 | # returns undef on error and sets $self->error, $data has any partial read | ||||||||||||||||||||||||||||
| 100 | # returns 0 on EOF, $data has partial read | ||||||||||||||||||||||||||||
| 101 | # returns 0E0 if would block (ie, not a full line read), data is unchanged. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub nb_getline { | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
nb_getline: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 0 | my $buffer = $self->buffer; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | return 0 if $self->eof; # previous read reached EOF | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | return if $self->error; # previous read encountered error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # look up position of EOL in the buffer | ||||||||||||||||||||||||||||
| 111 | 0 | my $idx = index($buffer, $/); | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ($idx < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # EOL was not found, so suck in more data if we can | ||||||||||||||||||||||||||||
| 115 | 0 | $self->eobuf = length $buffer; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # append to our buffer from the file handle if any data is there. | ||||||||||||||||||||||||||||
| 118 | 0 | my $chunk = ''; | |||||||||||||||||||||||||||
| 119 | 0 | my $count = sysread($self->_socket, $chunk, $self->default_buffer_size); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (!defined $count) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | return _0E0 if $! == EWOULDBLOCK; # we handle this error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | $self->error = $!; # remember the error for later | |||||||||||||||||||||||||||
| 125 | 0 | $_[0] = $buffer; # return whatever we read | |||||||||||||||||||||||||||
| 126 | 0 | return length($_[0]); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | elsif ($count == 0) { # EOF | ||||||||||||||||||||||||||||
| 129 | 0 | $self->eof = 1; # remember for later | |||||||||||||||||||||||||||
| 130 | 0 | $_[0] = $buffer; # return whatever we read | |||||||||||||||||||||||||||
| 131 | 0 | return length($_[0]); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||||||||||
| 134 | 0 | $buffer .= $chunk; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # look for EOL again in the newly read data | ||||||||||||||||||||||||||||
| 137 | 0 | $idx = index($buffer, $/, $self->eobuf); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # if not found, pretend this was EWOULDBLOCK | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($idx < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | $self->buffer = $buffer; | |||||||||||||||||||||||||||
| 142 | 0 | $self->eobuf = length $buffer; | |||||||||||||||||||||||||||
| 143 | 0 | return _0E0; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # we successfully read what we needed up to a new line | ||||||||||||||||||||||||||||
| 149 | 0 | $_[0] = substr($buffer, 0, $idx + length($/)); | |||||||||||||||||||||||||||
| 150 | 0 | substr($buffer, 0, $idx + length($/)) = ''; | |||||||||||||||||||||||||||
| 151 | 0 | $self->buffer = $buffer; | |||||||||||||||||||||||||||
| 152 | 0 | $self->eobuf = length $buffer; | |||||||||||||||||||||||||||
| 153 | 0 | return length($_[0]); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # implement read() function upon our data buffer. | ||||||||||||||||||||||||||||
| 158 | sub nb_read { | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
nb_read: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 160 | 0 | my $length = $_[1]; | |||||||||||||||||||||||||||
| 161 | 0 | my $buffer = $self->buffer; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | return 0 if $self->eof; # previous read reached EOF | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | return if $self->error; # previous read encountered error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # do we have enough data? | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | if (length $buffer < $length) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # not enough, so suck in more data if we can | ||||||||||||||||||||||||||||
| 170 | 0 | $self->eobuf = length $buffer; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # append to our buffer from the file handle if any data is there. | ||||||||||||||||||||||||||||
| 173 | 0 | my $chunk = ''; | |||||||||||||||||||||||||||
| 174 | 0 | my $count = sysread($self->_socket, $chunk, $self->default_buffer_size); | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | if (!defined $count) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | return _0E0 if $! == EWOULDBLOCK; # we handle this error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | $self->error = $!; # remember the error for later | |||||||||||||||||||||||||||
| 180 | 0 | $_[0] = $buffer; # return whatever we read | |||||||||||||||||||||||||||
| 181 | 0 | return length($_[0]); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | elsif ($count == 0) { # EOF | ||||||||||||||||||||||||||||
| 184 | 0 | $self->eof = 1; # remember for later | |||||||||||||||||||||||||||
| 185 | 0 | $_[0] = $buffer; # return whatever we read | |||||||||||||||||||||||||||
| 186 | 0 | return length($_[0]); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | else { | ||||||||||||||||||||||||||||
| 189 | 0 | $buffer .= $chunk; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # check length again | ||||||||||||||||||||||||||||
| 192 | 0 | $self->eobuf = length $buffer; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # if not, pretend this was EWOULDBLOCK | ||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if ($self->eobuf < $length) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | $self->buffer = $buffer; | |||||||||||||||||||||||||||
| 197 | 0 | return _0E0; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # we successfully read what we needed | ||||||||||||||||||||||||||||
| 203 | 0 | $_[0] = substr($buffer, 0, $length); | |||||||||||||||||||||||||||
| 204 | 0 | substr($buffer, 0, $length) = ''; | |||||||||||||||||||||||||||
| 205 | 0 | $self->buffer = $buffer; | |||||||||||||||||||||||||||
| 206 | 0 | $self->eobuf = length $buffer; | |||||||||||||||||||||||||||
| 207 | 0 | return length($_[0]); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # blocking send | ||||||||||||||||||||||||||||
| 211 | # return 1 on success, undef on failure | ||||||||||||||||||||||||||||
| 212 | sub send { | ||||||||||||||||||||||||||||
| 213 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 214 | 0 | my $msg = $_[0] . CRLF; | |||||||||||||||||||||||||||
| 215 | 0 | ▶ | $msg = encode_utf8($msg) if utf8::is_utf8($msg); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | my $len = length $msg; | |||||||||||||||||||||||||||
| 218 | 0 | my $offset = 0; | |||||||||||||||||||||||||||
| 219 | 0 | ▶ | while ($len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 0 | my $written = syswrite $self->_socket, $msg, $len, $offset; | |||||||||||||||||||||||||||
| 221 | 0 | ▶ | if (defined $written) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | $len -= $written; | |||||||||||||||||||||||||||
| 223 | 0 | $offset += $written; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | else { | ||||||||||||||||||||||||||||
| 226 | 0 | ▶ | if ($! == EAGAIN || $! == EINTR) { # retry sending | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$! == 11 or $! == 4
MC/DC0%0/2
$! == 11 or $! == 4
$! == 11
$! == 4
Truth table0%0/3
A or B
A= $! == 11B= $! == 4
| |||||||||||||||||||||||||||||
| 227 | #warn "waiting for socket ready after $!\n"; | ||||||||||||||||||||||||||||
| 228 | 0 | $self->can_write(); # block until ready to write | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | else { | ||||||||||||||||||||||||||||
| 231 | 0 | $self->error = $!; | |||||||||||||||||||||||||||
| 232 | 0 | return; # can't do anything with failed write. socket likely closed. | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | 0 | return 1; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 5 | 1; | |||||||||||||||||||||||||||