| 1 | ############################################################################## | ||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||
| 3 | # Net::Tools::Socket perl module | ||||||||||||||||||||||||||||||||
| 4 | # Copyright (c) 2013-2026 Vladi Belperchinov-Shabanski "Cade" | ||||||||||||||||||||||||||||||||
| 5 | # <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||||||||||||||||||||||||||||||||
| 6 | # http://cade.noxrun.com/ | ||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||
| 8 | # GPL | ||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||
| 10 | ############################################################################## | ||||||||||||||||||||||||||||||||
| 11 | package Net::Tools::Socket; | ||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Time::HiRes qw( time ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Net::Tools::Socket::Err; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 2 | our $VERSION = '1.60'; | |||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 2 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||||||
| 20 | 2 | our @EXPORT = qw( | |||||||||||||||||||||||||||||||
| 21 | socket_read | ||||||||||||||||||||||||||||||||
| 22 | socket_write | ||||||||||||||||||||||||||||||||
| 23 | socket_print | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | socket_read_message | ||||||||||||||||||||||||||||||||
| 26 | socket_write_message | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | socket_can_write | ||||||||||||||||||||||||||||||||
| 29 | socket_can_read | ||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 2 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||
| 33 | 'all' => \@EXPORT, | ||||||||||||||||||||||||||||||||
| 34 | 'none' => [], | ||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | # socket_write_message() sends messages up to this size (length + data) with a | ||||||||||||||||||||||||||||||||
| 38 | # single write, bigger ones with two writes, so their data is not copied | ||||||||||||||||||||||||||||||||
| 39 | 2 | our $WRITE_MESSAGE_JOIN_MAX = 64 * 1024; | |||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | ############################################################################## | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | sub socket_read | ||||||||||||||||||||||||||||||||
| 44 | 113 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
socket_read: called CC=11 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 45 | 113 | my $sock = shift; | |||||||||||||||||||||||||||||||
| 46 | 113 | my $data = shift; | |||||||||||||||||||||||||||||||
| 47 | 113 | my $readlen = shift; | |||||||||||||||||||||||||||||||
| 48 | 113 | my $timeout = shift; # seconds, fractions allowed, undef waits forever, 0 does not wait | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 113 | sock_err_clear(); | |||||||||||||||||||||||||||||||
| 51 | 113 | my $stime = time(); | |||||||||||||||||||||||||||||||
| 52 | 113 | $$data = undef; | |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 113 | my $rlen = $readlen; | |||||||||||||||||||||||||||||||
| 55 | 113 | ▶ | while( $rlen > 0 ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||||||
| 57 | 116 | ▶ | return undef unless socket_can_read( $sock, $timeout ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | # by design $timeout limits each wait for the socket and the total is checked | ||||||||||||||||||||||||||||||||
| 59 | # only after it, so one call may take up to about twice $timeout | ||||||||||||||||||||||||||||||||
| 60 | 112 | ▶ | return sock_err( SOCK_E_TIMEOUT, undef ) if $timeout > 0 and time() - $stime > $timeout; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$timeout > 0 and &Net::Tools::Socket::time() - $stime > $timeout
MC/DC0%0/2
$timeout > 0 and &Net::Tools::Socket::time() - $stime > $timeout
$timeout > 0
&Net::Tools::Socket::time() - $stime > $timeout
Truth table66%2/3
A and B
A= $timeout > 0B= &Net::Tools::Socket::time() - $stime > $timeout
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 112 | my $part; | |||||||||||||||||||||||||||||||
| 63 | 112 | my $plen = $sock->sysread( $part, $rlen ); | |||||||||||||||||||||||||||||||
| 64 | 112 | ▶ | next if ! defined $plen and $!{ EINTR }; # interrupted by signal, retry within timeout | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $plen and $!{"EINTR"}
MC/DC0%0/2
not defined $plen and $!{"EINTR"}
not defined $plen
$!{"EINTR"}
Truth table33%1/3
not(A) and B
A= not defined $plenB= $!{"EINTR"}
| |||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 112 | ▶ | return sock_err( SOCK_E_EOF, undef ) if defined $plen and $plen == 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $plen and $plen == 0
MC/DC50%1/2
defined $plen and $plen == 0
defined $plen
$plen == 0
Truth table66%2/3
A and B
A= defined $plenB= $plen == 0
| |||||||||||||||||||||||||||||||||
| 67 | 108 | ▶ | return $!{ ECONNRESET } ? sock_err( SOCK_E_EOF, undef ) : sock_err_sys( undef ) unless $plen > 0; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 108 | $$data .= $part; | |||||||||||||||||||||||||||||||
| 70 | 108 | $rlen -= $plen; | |||||||||||||||||||||||||||||||
| 71 | #print STDERR "SOCKET_READ: part [$part] [$plen] [$rlen]\n"; | ||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | #print STDERR "SOCKET_READ: incoming data [$$data]\n"; | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 105 | return $readlen - $rlen; | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | sub socket_write | ||||||||||||||||||||||||||||||||
| 80 | 70 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
socket_write: called CC=12 SCAR=27.9
| |||||||||||||||||||||||||||||||||
| 81 | 70 | my $sock = shift; | |||||||||||||||||||||||||||||||
| 82 | 70 | my $data = shift; | |||||||||||||||||||||||||||||||
| 83 | 70 | my $writelen = shift; | |||||||||||||||||||||||||||||||
| 84 | 70 | my $timeout = shift; # seconds, fractions allowed, undef waits forever, 0 does not wait | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 70 | sock_err_clear(); | |||||||||||||||||||||||||||||||
| 87 | # cannot write more than given, wide (over 255) characters cannot be written as bytes | ||||||||||||||||||||||||||||||||
| 88 | 70 | ▶ | return sock_err( SOCK_E_INVAL, undef ) if $writelen > length( $data ) or ! utf8::downgrade( $data, 1 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$writelen > length $data or not utf8::downgrade($data, 1)
MC/DC100%2/2
$writelen > length $data or not utf8::downgrade($data, 1)
$writelen > length $data
not utf8::downgrade($data, 1)
Truth table100%3/3
A or not(B)
A= $writelen > length $dataB= not utf8::downgrade($data, 1)
| |||||||||||||||||||||||||||||||||
| 89 | 67 | my $stime = time(); | |||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | #print STDERR "SOCKET_WRITE: outgoing data [$data]\n"; | ||||||||||||||||||||||||||||||||
| 92 | 67 | my $wpos = 0; | |||||||||||||||||||||||||||||||
| 93 | 67 | ▶ | while( $wpos < $writelen ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||||||
| 95 | 67 | ▶ | return undef unless socket_can_write( $sock, $timeout ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | # by design $timeout limits each wait for the socket and the total is checked | ||||||||||||||||||||||||||||||||
| 97 | # only after it, so one call may take up to about twice $timeout | ||||||||||||||||||||||||||||||||
| 98 | 67 | ▶ | return sock_err( SOCK_E_TIMEOUT, undef ) if $timeout > 0 and time() - $stime > $timeout; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$timeout > 0 and &Net::Tools::Socket::time() - $stime > $timeout
MC/DC0%0/2
$timeout > 0 and &Net::Tools::Socket::time() - $stime > $timeout
$timeout > 0
&Net::Tools::Socket::time() - $stime > $timeout
Truth table33%1/3
A and B
A= $timeout > 0B= &Net::Tools::Socket::time() - $stime > $timeout
| |||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | # WARNING! closed peer raises SIGPIPE, caller must ignore it, see POD | ||||||||||||||||||||||||||||||||
| 101 | 67 | my $plen = $sock->syswrite( $data, $writelen - $wpos, $wpos ); | |||||||||||||||||||||||||||||||
| 102 | 67 | ▶ | next if ! defined $plen and $!{ EINTR }; # interrupted by signal, retry within timeout | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $plen and $!{"EINTR"}
MC/DC0%0/2
not defined $plen and $!{"EINTR"}
not defined $plen
$!{"EINTR"}
Truth table33%1/3
not(A) and B
A= not defined $plenB= $!{"EINTR"}
| |||||||||||||||||||||||||||||||||
| 103 | #print STDERR "SOCKET_WRITE: part [$plen]\n"; | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 67 | ▶ | return $!{ EPIPE } || $!{ ECONNRESET } ? sock_err( SOCK_E_EOF, undef ) : sock_err_sys( undef ) unless $plen > 0; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$!{"EPIPE"} or $!{"ECONNRESET"}
MC/DC0%0/2
$!{"EPIPE"} or $!{"ECONNRESET"}
$!{"EPIPE"}
$!{"ECONNRESET"}
Truth table0%0/3
A or B
A= $!{"EPIPE"}B= $!{"ECONNRESET"}
| |||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 67 | $wpos += $plen; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | #print STDERR "SOCKET_WRITE: part [$wpos] == writelen [$writelen]\n"; | ||||||||||||||||||||||||||||||||
| 111 | 67 | return $wpos; | |||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | sub socket_print | ||||||||||||||||||||||||||||||||
| 115 | 8 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
socket_print: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 116 | 8 | my $sock = shift; | |||||||||||||||||||||||||||||||
| 117 | 8 | my $data = shift; | |||||||||||||||||||||||||||||||
| 118 | 8 | my $timeout = shift; # seconds, fractions allowed, undef waits forever, 0 does not wait | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 8 | return socket_write( $sock, $data, length( $data ), $timeout ); | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | ############################################################################## | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | # returns read data, "" if incoming length is 0, i.e. empty data, or undef on | ||||||||||||||||||||||||||||||||
| 126 | # error with $SOCK_ERR set: SOCK_E_EOF, SOCK_E_TIMEOUT, SOCK_E_TOOBIG or errno | ||||||||||||||||||||||||||||||||
| 127 | # (EBADF, etc.). on any error after the first byte of the length was read the | ||||||||||||||||||||||||||||||||
| 128 | # stream is out of sync, the caller must close the socket | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | sub socket_read_message | ||||||||||||||||||||||||||||||||
| 131 | 59 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
socket_read_message: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 132 | 59 | my $sock = shift; | |||||||||||||||||||||||||||||||
| 133 | 59 | my $timeout = shift; # seconds, fractions allowed, undef waits forever, 0 does not wait | |||||||||||||||||||||||||||||||
| 134 | 59 | my $maxlen = shift; | |||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 59 | sock_err_clear(); | |||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 59 | ▶ | $maxlen = 0xFFFFFFFF if $maxlen <= 0 or $maxlen > 0xFFFFFFFF; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$maxlen <= 0 or $maxlen > 4294967295
MC/DC50%1/2
$maxlen <= 0 or $maxlen > 4294967295
$maxlen <= 0
$maxlen > 4294967295
Truth table66%2/3
A or B
A= $maxlen <= 0B= $maxlen > 4294967295
| |||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | # socket_read() clears and sets $SOCK_ERR | ||||||||||||||||||||||||||||||||
| 141 | 59 | my $data_len_N32; | |||||||||||||||||||||||||||||||
| 142 | 59 | ▶ | socket_read( $sock, \$data_len_N32, 4, $timeout ) // return undef; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
socket_read($sock, \$data_len_N32, 4, $timeout) // (return undef)
MC/DC100%1/1
socket_read($sock, \$data_len_N32, 4, $timeout) // (return undef)
socket_read($sock, \$data_len_N32, 4, $timeout)
Truth table100%2/2
A
A= socket_read($sock, \$data_len_N32, 4, $timeout)
| |||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 54 | my $data_len = unpack( 'N', $data_len_N32 ); | |||||||||||||||||||||||||||||||
| 145 | 54 | ▶ | return sock_err( SOCK_E_TOOBIG ) if $data_len > $maxlen; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | 51 | ▶ | return '' if $data_len == 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 46 | my $read_data; | |||||||||||||||||||||||||||||||
| 149 | 46 | ▶ | socket_read( $sock, \$read_data, $data_len, $timeout ) // return undef; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
socket_read($sock, \$read_data, $data_len, $timeout) // (return undef)
MC/DC100%1/1
socket_read($sock, \$read_data, $data_len, $timeout) // (return undef)
socket_read($sock, \$read_data, $data_len, $timeout)
Truth table100%2/2
A
A= socket_read($sock, \$read_data, $data_len, $timeout)
| |||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | 45 | return $read_data; | |||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | sub socket_write_message | ||||||||||||||||||||||||||||||||
| 155 | 54 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
socket_write_message: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 156 | 54 | my $sock = shift; | |||||||||||||||||||||||||||||||
| 157 | 54 | my $data = shift; | |||||||||||||||||||||||||||||||
| 158 | 54 | my $timeout = shift; # seconds, fractions allowed, undef waits forever, 0 does not wait | |||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | 54 | sock_err_clear(); | |||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 54 | ▶ | return sock_err( SOCK_E_INVAL, undef ) if ! utf8::downgrade( $data, 1 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 52 | my $data_len = length( $data ); | |||||||||||||||||||||||||||||||
| 165 | 52 | ▶ | return sock_err( SOCK_E_TOOBIG, undef ) if $data_len > 0xFFFFFFFF; # 32bit length field would wrap | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | 52 | ▶ | if( 4 + $data_len <= $WRITE_MESSAGE_JOIN_MAX ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 168 | { | ||||||||||||||||||||||||||||||||
| 169 | # small message, length and data in one write. a separate small length | ||||||||||||||||||||||||||||||||
| 170 | # write stalls on TCP for ~40ms per message (Nagle + delayed ACK) | ||||||||||||||||||||||||||||||||
| 171 | 47 | ▶ | socket_write( $sock, pack( 'N', $data_len ) . $data, 4 + $data_len, $timeout ) // return undef; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
socket_write($sock, pack("N", $data_len) . $data, 4 + $data_len, $timeout) // (return undef)
MC/DC0%0/1
socket_write($sock, pack("N", $data_len) . $data, 4 + $data_len, $timeout) // (return undef)
socket_write($sock, pack("N", $data_len) . $data, 4 + $data_len, $timeout)
Truth table50%1/2
A
A= socket_write($sock, pack("N", $data_len) . $data, 4 + $data_len, $timeout)
| |||||||||||||||||||||||||||||||||
| 172 | 47 | return 1; | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | # big message, two writes, so $data is not copied | ||||||||||||||||||||||||||||||||
| 176 | 5 | ▶ | socket_write( $sock, pack( 'N', $data_len ), 4, $timeout ) // return undef; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
socket_write($sock, pack("N", $data_len), 4, $timeout) // (return undef)
MC/DC0%0/1
socket_write($sock, pack("N", $data_len), 4, $timeout) // (return undef)
socket_write($sock, pack("N", $data_len), 4, $timeout)
Truth table50%1/2
A
A= socket_write($sock, pack("N", $data_len), 4, $timeout)
| |||||||||||||||||||||||||||||||||
| 177 | 5 | ▶ | socket_write( $sock, $data, $data_len, $timeout ) // return undef; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
socket_write($sock, $data, $data_len, $timeout) // (return undef)
MC/DC0%0/1
socket_write($sock, $data, $data_len, $timeout) // (return undef)
socket_write($sock, $data, $data_len, $timeout)
Truth table50%1/2
A
A= socket_write($sock, $data, $data_len, $timeout)
| |||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | 5 | return 1; | |||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | ############################################################################## | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | sub socket_can_write | ||||||||||||||||||||||||||||||||
| 185 | 69 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
socket_can_write: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 186 | 69 | my $sock = shift; | |||||||||||||||||||||||||||||||
| 187 | 69 | my $timeout = shift; # seconds, fractions allowed, undef waits forever, 0 does not wait | |||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | 69 | sock_err_clear(); | |||||||||||||||||||||||||||||||
| 190 | 69 | my $win; | |||||||||||||||||||||||||||||||
| 191 | 69 | my $fdno = fileno( $sock ); | |||||||||||||||||||||||||||||||
| 192 | 69 | ▶ | return undef unless defined $fdno and $fdno >= 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $fdno and $fdno >= 0
MC/DC50%1/2
defined $fdno and $fdno >= 0
defined $fdno
$fdno >= 0
Truth table66%2/3
A and B
A= defined $fdnoB= $fdno >= 0
| |||||||||||||||||||||||||||||||||
| 193 | 68 | vec( $win, $fdno, 1 ) = 1; | |||||||||||||||||||||||||||||||
| 194 | 68 | return __socket_select( undef, $win, $timeout ); | |||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | sub socket_can_read | ||||||||||||||||||||||||||||||||
| 198 | 124 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
socket_can_read: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 199 | 124 | my $sock = shift; | |||||||||||||||||||||||||||||||
| 200 | 124 | my $timeout = shift; # seconds, fractions allowed, undef waits forever, 0 does not wait | |||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | 124 | sock_err_clear(); | |||||||||||||||||||||||||||||||
| 203 | 124 | my $rin; | |||||||||||||||||||||||||||||||
| 204 | 124 | my $fdno = fileno( $sock ); | |||||||||||||||||||||||||||||||
| 205 | 124 | ▶ | return undef unless defined $fdno and $fdno >= 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $fdno and $fdno >= 0
MC/DC100%2/2
defined $fdno and $fdno >= 0
defined $fdno
$fdno >= 0
Truth table100%3/3
A and B
A= defined $fdnoB= $fdno >= 0
| |||||||||||||||||||||||||||||||||
| 206 | 121 | vec( $rin, $fdno, 1 ) = 1; | |||||||||||||||||||||||||||||||
| 207 | 121 | return __socket_select( $rin, undef, $timeout ); | |||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | # select() on one read or write bit vector, restarted on EINTR with the | ||||||||||||||||||||||||||||||||
| 211 | # remaining time. returns 1 if ready, 0 on timeout (SOCK_E_TIMEOUT) or undef | ||||||||||||||||||||||||||||||||
| 212 | # on error ($SOCK_ERR is errno). undef $timeout waits forever, 0 does not | ||||||||||||||||||||||||||||||||
| 213 | # wait, select() only checks once | ||||||||||||||||||||||||||||||||
| 214 | sub __socket_select | ||||||||||||||||||||||||||||||||
| 215 | 189 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
__socket_select: called CC=7 SCAR=20.7
| |||||||||||||||||||||||||||||||||
| 216 | 189 | my $rin = shift; | |||||||||||||||||||||||||||||||
| 217 | 189 | my $win = shift; | |||||||||||||||||||||||||||||||
| 218 | 189 | my $timeout = shift; # seconds, fractions allowed, undef waits forever, 0 does not wait | |||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | 189 | ▶ | my $etime = defined $timeout ? time() + $timeout : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 221 | 189 | while( 4 ) | |||||||||||||||||||||||||||||||
| 222 | { | ||||||||||||||||||||||||||||||||
| 223 | 189 | my $left; | |||||||||||||||||||||||||||||||
| 224 | 189 | ▶ | if( defined $etime ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 225 | { | ||||||||||||||||||||||||||||||||
| 226 | 189 | $left = $etime - time(); | |||||||||||||||||||||||||||||||
| 227 | 189 | ▶ | $left = 0 if $left < 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||
| 229 | # sets are not modified when select() fails (EINTR), so restart can reuse them | ||||||||||||||||||||||||||||||||
| 230 | 189 | my $n = select( $rin, $win, undef, $left ); | |||||||||||||||||||||||||||||||
| 231 | 189 | ▶ | return 1 if $n > 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 232 | 8 | ▶ | return sock_err( SOCK_E_TIMEOUT, 0 ) if $n == 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | next if $!{ EINTR }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 234 | 0 | return sock_err_sys( undef ); | |||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | ############################################################################## | ||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | =pod | ||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | Net::Tools::Socket - set of socket I/O functions | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | use Net::Tools::Socket qw( :all ); # import all functions | ||||||||||||||||||||||||||||||||
| 250 | use Net::Tools::Socket; # the same as :all :) | ||||||||||||||||||||||||||||||||
| 251 | use Net::Tools::Socket qw( :none ); # do not import anything, use full package names | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | my $read_res_len = socket_read( $socket, $data_ref, $length, $timeout ); | ||||||||||||||||||||||||||||||||
| 256 | my $write_res_len = socket_write( $socket, $data, $length, $timeout ); | ||||||||||||||||||||||||||||||||
| 257 | my $write_res_len = socket_print( $socket, $data, $timeout ); | ||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||
| 259 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | my $read_data = socket_read_message( $socket, $timeout, $maxlen ); | ||||||||||||||||||||||||||||||||
| 262 | my $write_res = socket_write_message( $socket, $data, $timeout ); | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | # error details, see ERRORS | ||||||||||||||||||||||||||||||||
| 267 | use Net::Tools::Socket::Err; | ||||||||||||||||||||||||||||||||
| 268 | print "error: $SOCK_ERRSTR\n" unless defined $read_data; | ||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | =head2 socket_read( $socket, $data_ref, $length, $timeout ) | ||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | Reads $length sized data from the $socket and stores it to $data_ref scalar | ||||||||||||||||||||||||||||||||
| 277 | reference. | ||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | Returns read length, which is always the requested $length, or undef on | ||||||||||||||||||||||||||||||||
| 280 | timeout, end of file or error, see L</ERRORS>. On undef, $data_ref may hold | ||||||||||||||||||||||||||||||||
| 281 | the partially read data. | ||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | $timeout is optional, it is in seconds and can be less than 1 second. | ||||||||||||||||||||||||||||||||
| 284 | If $timeout is undef (not given) it waits forever, if it is 0 it does not | ||||||||||||||||||||||||||||||||
| 285 | wait at all, only checks once if the socket is ready. | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | $timeout limits each wait for the socket to become ready, the total time | ||||||||||||||||||||||||||||||||
| 288 | is checked only after each wait, so one call may take up to about twice | ||||||||||||||||||||||||||||||||
| 289 | $timeout when the data arrives slowly in parts. | ||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | =head2 socket_write( $socket, $data, $length, $timeout ) | ||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||
| 293 | Writes $length sized data from $data scalar to the $socket. If $length is | ||||||||||||||||||||||||||||||||
| 294 | larger than the length of $data, or $data contains wide (over 255) characters (encode it | ||||||||||||||||||||||||||||||||
| 295 | first), nothing is written and undef is returned with SOCK_E_INVAL. | ||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | Returns write length, which is always the requested $length, or undef on | ||||||||||||||||||||||||||||||||
| 298 | timeout, closed peer or error, see L</ERRORS>. On undef, part of the data may | ||||||||||||||||||||||||||||||||
| 299 | already be sent. | ||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | WARNING! Writing to a socket closed by the peer raises SIGPIPE, which kills | ||||||||||||||||||||||||||||||||
| 302 | the process by default. This module does not touch signals, handling them | ||||||||||||||||||||||||||||||||
| 303 | is the caller's responsibility. Set, once at program start: | ||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | $SIG{ 'PIPE' } = 'IGNORE'; | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | then such writes fail with $SOCK_ERR set to SOCK_E_EOF instead. | ||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||
| 309 | $timeout is optional, it is in seconds and can be less than 1 second. | ||||||||||||||||||||||||||||||||
| 310 | If $timeout is undef (not given) it waits forever, if it is 0 it does not | ||||||||||||||||||||||||||||||||
| 311 | wait at all, only checks once if the socket is ready. | ||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | $timeout limits each wait for the socket to become writable, the total time | ||||||||||||||||||||||||||||||||
| 314 | is checked only after each wait, so one call may take up to about twice | ||||||||||||||||||||||||||||||||
| 315 | $timeout when the peer reads slowly. | ||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | =head2 socket_print( $socket, $data, $timeout ) | ||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | Same as socket_write() but calculates requested length from the $data scalar. | ||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | WARNING! SIGPIPE on closed peer, see socket_write(). | ||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | $timeout is optional, it is in seconds and can be less than 1 second. | ||||||||||||||||||||||||||||||||
| 324 | If $timeout is undef (not given) it waits forever, if it is 0 it does not | ||||||||||||||||||||||||||||||||
| 325 | wait at all, only checks once if the socket is ready. | ||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | See socket_write() for how $timeout is applied. | ||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | =head2 socket_read_message( $socket, $timeout, $maxlen ) | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | Reads 32bit network-order integer, which then is used as data size to be read | ||||||||||||||||||||||||||||||||
| 332 | from the socket (i.e. message = 32bit-integer + data ). | ||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | Returns the read data, "" for an empty message, or undef on error with | ||||||||||||||||||||||||||||||||
| 335 | $SOCK_ERR set (see L</ERRORS>): | ||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||
| 337 | SOCK_E_EOF peer closed, also in the middle of a message | ||||||||||||||||||||||||||||||||
| 338 | SOCK_E_TIMEOUT $timeout expired | ||||||||||||||||||||||||||||||||
| 339 | SOCK_E_TOOBIG message length is over $maxlen | ||||||||||||||||||||||||||||||||
| 340 | errno system call failed (EBADF, etc.) | ||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | For a closed handle, or one without a real file descriptor, it returns | ||||||||||||||||||||||||||||||||
| 343 | undef and $SOCK_ERR is not set, see socket_can_read(). | ||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | On any error after the first byte of the length was read (a timeout, EOF or | ||||||||||||||||||||||||||||||||
| 346 | system error in the middle of the 4 length bytes, SOCK_E_TOOBIG, or an | ||||||||||||||||||||||||||||||||
| 347 | error while reading the message data), the rest of the message is left | ||||||||||||||||||||||||||||||||
| 348 | unread, so the socket stream is out of sync and cannot be used anymore: | ||||||||||||||||||||||||||||||||
| 349 | the caller must close the socket and reconnect. With $timeout 0 this happens | ||||||||||||||||||||||||||||||||
| 350 | whenever the message has not fully arrived yet. | ||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | $timeout is optional, it is in seconds and can be less than 1 second. | ||||||||||||||||||||||||||||||||
| 353 | If $timeout is undef (not given) it waits forever, if it is 0 it does not | ||||||||||||||||||||||||||||||||
| 354 | wait at all, only checks once if the socket is ready. | ||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||
| 356 | $timeout limits each of the two socket_read() calls (length and data) | ||||||||||||||||||||||||||||||||
| 357 | separately, see socket_read() for how $timeout is applied. | ||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||
| 359 | $maxlen is optional, if it is not given, 0 or negative, the default is used. | ||||||||||||||||||||||||||||||||
| 360 | The default and the maximum are 2**32-1 (0xFFFFFFFF). | ||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||
| 362 | =head2 socket_write_message( $socket, $data, $timeout ) | ||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||
| 364 | Writes 32bit network-order integer, which is the size of the given $data to be | ||||||||||||||||||||||||||||||||
| 365 | written to the $socket and then writes the data | ||||||||||||||||||||||||||||||||
| 366 | (i.e. message = 32bit-integer + data ). | ||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||
| 368 | Messages up to $Net::Tools::Socket::WRITE_MESSAGE_JOIN_MAX bytes (length + | ||||||||||||||||||||||||||||||||
| 369 | data, 64 KiB by default) are sent with one socket_write() call. Bigger ones | ||||||||||||||||||||||||||||||||
| 370 | are sent with two socket_write() calls (the length and the data), so a big | ||||||||||||||||||||||||||||||||
| 371 | $data is not copied. A separate small write of the length would stall every | ||||||||||||||||||||||||||||||||
| 372 | small message on TCP for about 40 ms (Nagle algorithm and delayed ACK). | ||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | NOTE: messages longer than $WRITE_MESSAGE_JOIN_MAX are split into two | ||||||||||||||||||||||||||||||||
| 375 | socket_write() calls, the 4 bytes length is written alone first. On TCP | ||||||||||||||||||||||||||||||||
| 376 | without TCP_NODELAY, each such message may still stall once for about 40 ms. | ||||||||||||||||||||||||||||||||
| 377 | This is small compared to the time to send that much data, but if many long | ||||||||||||||||||||||||||||||||
| 378 | messages are exchanged, set TCP_NODELAY on the socket: | ||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||
| 380 | use Socket qw( IPPROTO_TCP TCP_NODELAY ); | ||||||||||||||||||||||||||||||||
| 381 | setsockopt( $socket, IPPROTO_TCP, TCP_NODELAY, 1 ); | ||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||
| 383 | or raise $Net::Tools::Socket::WRITE_MESSAGE_JOIN_MAX, at the cost of | ||||||||||||||||||||||||||||||||
| 384 | copying the data of each message up to that size. | ||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||
| 386 | On any error after the first byte was written (a timeout, closed peer or | ||||||||||||||||||||||||||||||||
| 387 | system error in the middle of the length or of the data), only part of the | ||||||||||||||||||||||||||||||||
| 388 | message is sent, so the socket stream is out of sync and cannot be used | ||||||||||||||||||||||||||||||||
| 389 | anymore: the caller must close the socket and reconnect. With $timeout 0 | ||||||||||||||||||||||||||||||||
| 390 | this happens whenever the peer does not read fast enough. | ||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||
| 392 | Returns 1 on success or undef for message or network error, see L</ERRORS>. | ||||||||||||||||||||||||||||||||
| 393 | $data must not contain wide (over 255) characters, encode it first, | ||||||||||||||||||||||||||||||||
| 394 | otherwise undef is returned with SOCK_E_INVAL. An empty or undef $data is | ||||||||||||||||||||||||||||||||
| 395 | sent as an empty message, which socket_read_message() returns as "". | ||||||||||||||||||||||||||||||||
| 396 | $data larger than 4 GiB - 1 (2**32 - 1) bytes does not fit the 32bit length, | ||||||||||||||||||||||||||||||||
| 397 | nothing is written and undef is returned with SOCK_E_TOOBIG. | ||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||
| 399 | WARNING! SIGPIPE on closed peer, see socket_write(). | ||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||
| 401 | $timeout is optional, it is in seconds and can be less than 1 second. | ||||||||||||||||||||||||||||||||
| 402 | If $timeout is undef (not given) it waits forever, if it is 0 it does not | ||||||||||||||||||||||||||||||||
| 403 | wait at all, only checks once if the socket is ready. | ||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||
| 405 | $timeout limits each socket_write() call separately (one or two, see above), | ||||||||||||||||||||||||||||||||
| 406 | see socket_write() for how $timeout is applied. | ||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||
| 408 | =head2 socket_can_read( $socket, $timeout ) / socket_can_write( $socket, $timeout ) | ||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||
| 410 | Return 1 if the $socket is ready for reading/writing within $timeout, 0 on | ||||||||||||||||||||||||||||||||
| 411 | timeout ($SOCK_ERR is SOCK_E_TIMEOUT), undef on error ($SOCK_ERR is errno). | ||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | If the $socket is closed or has no real file descriptor (fileno() is undef | ||||||||||||||||||||||||||||||||
| 414 | or -1, for example an in-memory handle), they return undef at once and | ||||||||||||||||||||||||||||||||
| 415 | $SOCK_ERR is not set, since such a handle cannot be read from or written to. | ||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||
| 417 | $timeout is in seconds and can be less than 1 second. If $timeout is undef | ||||||||||||||||||||||||||||||||
| 418 | (not given) it waits forever, if it is 0 it does not wait at all, only | ||||||||||||||||||||||||||||||||
| 419 | checks once if the socket is ready. | ||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||
| 421 | =head1 ERRORS | ||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | All functions clear $SOCK_ERR and $SOCK_ERRSTR on entry and set them on | ||||||||||||||||||||||||||||||||
| 424 | failure, except for a closed handle, see socket_can_read(). $SOCK_ERR is a | ||||||||||||||||||||||||||||||||
| 425 | negative SOCK_E_* code, or the positive errno value if a system call failed | ||||||||||||||||||||||||||||||||
| 426 | (except EPIPE and ECONNRESET, which are reported as SOCK_E_EOF), and | ||||||||||||||||||||||||||||||||
| 427 | $SOCK_ERRSTR is its text: | ||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||
| 429 | SOCK_E_EOF peer closed (also EPIPE and ECONNRESET) | ||||||||||||||||||||||||||||||||
| 430 | SOCK_E_TIMEOUT $timeout expired | ||||||||||||||||||||||||||||||||
| 431 | SOCK_E_TOOBIG message over $maxlen, or $data over 2**32-1 bytes | ||||||||||||||||||||||||||||||||
| 432 | SOCK_E_INVAL $length larger than $data, or wide characters in $data | ||||||||||||||||||||||||||||||||
| 433 | errno any other system error, the positive errno value, | ||||||||||||||||||||||||||||||||
| 434 | its text is in $SOCK_ERRSTR | ||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||
| 436 | $SOCK_ERR and $SOCK_ERRSTR are exported by L<Net::Tools::Socket::Err>, | ||||||||||||||||||||||||||||||||
| 437 | which must be used by the caller to read them: | ||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||
| 439 | use Net::Tools::Socket::Err; | ||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||
| 441 | my $data = socket_read_message( $socket, $timeout ); | ||||||||||||||||||||||||||||||||
| 442 | print "error: $SOCK_ERRSTR\n" if $SOCK_ERR; | ||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||
| 444 | The error state describes the last call only, read it right after the call. | ||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||
| 446 | =head1 TODO | ||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||
| 448 | * more docs | ||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||
| 450 | =head1 REQUIRED MODULES | ||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||
| 452 | Net::Tools::Socket uses: | ||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||
| 454 | * Time::HiRes | ||||||||||||||||||||||||||||||||
| 455 | * Net::Tools::Socket::Err | ||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||
| 457 | =head1 GITHUB REPOSITORY | ||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||
| 459 | git@github.com:cade-vs/perl-net-tools.git | ||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||
| 461 | git clone https://github.com/cade-vs/perl-net-tools.git | ||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||
| 463 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||
| 465 | Vladi Belperchinov-Shabanski "Cade" | ||||||||||||||||||||||||||||||||
| 466 | <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||||||||||||||||||||||||||||||||
| 467 | http://cade.noxrun.com/ | ||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||
| 469 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||
| 471 | Copyright (c) 2013-2026 Vladi Belperchinov-Shabanski "Cade" | ||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||
| 473 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||
| 474 | under the terms of the GNU General Public License as published by the | ||||||||||||||||||||||||||||||||
| 475 | Free Software Foundation; either version 2 of the License, or (at your | ||||||||||||||||||||||||||||||||
| 476 | option) any later version. | ||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||
| 478 | =cut | ||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||
| 480 | ############################################################################## | ||||||||||||||||||||||||||||||||
| 481 | 2 | 1; | |||||||||||||||||||||||||||||||
| 482 | ###EOF######################################################################## | ||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||