- if (exists $conn->{msg}) {
- $msg = $conn->{msg};
- $offset = length($msg) - 1; # sysread appends to it.
- $bytes_to_read = $conn->{bytes_to_read};
- delete $conn->{'msg'}; # have made a copy
- } else {
- # The typical case ...
- $msg = ""; # Otherwise -w complains
- $offset = 0 ;
- $bytes_to_read = 0 ; # Will get set soon
- }
- # We want to read the message length in blocking mode. Quite
- # unlikely that we'll get blocked too long reading 4 bytes
- if (!$bytes_to_read) { # Get new length
- my $buf;
- $conn->set_blocking();
- $bytes_read = sysread($sock, $buf, 4);
- if ($! || ($bytes_read != 4)) {
- goto FINISH;
- }
- $bytes_to_read = unpack ('N', $buf);
- }
- $conn->set_non_blocking() unless $rcv_now;
- while ($bytes_to_read) {
- $bytes_read = sysread ($sock, $msg, $bytes_to_read, $offset);
- if (defined ($bytes_read)) {
- if ($bytes_read == 0) {
- last;
- }
- $bytes_to_read -= $bytes_read;
- $offset += $bytes_read;
- } else {
- if (_err_will_block($!)) {
- # Should come here only in non-blocking mode
- $conn->{msg} = $msg;
- $conn->{bytes_to_read} = $bytes_to_read;
- return ; # .. _rcv will be called later
- # when socket is readable again
- } else {
- last;
- }
- }
- }