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