Browse Source

php - socket read should allow partial - plus set a 1s timeout

master
kanoi 10 years ago
parent
commit
7fae30b431
  1. 56
      pool/socket.php

56
pool/socket.php

@ -54,6 +54,10 @@ function _getsock($fun, $port, $unix=true)
} }
} }
} }
# Avoid getting locked up for long
$tmo = array('sec' => 1, 'usec' => 0);
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $tmo);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $tmo);
return $socket; return $socket;
} }
# #
@ -64,7 +68,7 @@ function getsock($fun)
# #
function readsockline($fun, $socket) function readsockline($fun, $socket)
{ {
$siz = socket_read($socket, 4); $siz = socket_read($socket, 4, PHP_BINARY_READ);
if ($siz === false) if ($siz === false)
{ {
$sockerr = socket_strerror(socket_last_error()); $sockerr = socket_strerror(socket_last_error());
@ -74,29 +78,38 @@ function readsockline($fun, $socket)
} }
if (strlen($siz) != 4) if (strlen($siz) != 4)
{ {
$msg = "$fun() readsockline() short read $siz vs ".strlen($siz); $msg = "$fun() readsockline() short 4 read got ".strlen($siz);
error_log("CKPERR: $msg"); error_log("CKPERR: $msg");
return false; return false;
} }
$len = ord($siz[0]) + ord($siz[1])*256 + $len = ord($siz[0]) + ord($siz[1])*256 +
ord($siz[2])*65536 + ord($siz[3])*16777216; ord($siz[2])*65536 + ord($siz[3])*16777216;
$line = socket_read($socket, $len); $ans = '';
if ($line === false) $left = $len;
while ($left > 0)
{ {
$sockerr = socket_strerror(socket_last_error()); $line = socket_read($socket, $left, PHP_BINARY_READ);
$msg = "$fun() readsockline() failed"; if ($line === false)
error_log("CKPERR: $msg '$sockerr'");
return false;
}
else
if (strlen($line) != $len)
{ {
$msg = "$fun() readsockline() incomplete ($len)"; $sockerr = socket_strerror(socket_last_error());
error_log("CKPERR: $msg '$line'"); $msg = "$fun() readsockline() failed";
error_log("CKPERR: $msg '$sockerr'");
return false; return false;
} }
$red = strlen($line);
return $line; if ($red == 0)
{
$msg = "$fun() readsockline() incomplete (".($len-$left)." vs $len)";
$sub = "'".substr($line, 0, 30)."'";
if (strlen($line) > 30)
$sub .= '...';
error_log("CKPERR: $msg $sub");
return false;
}
$left -= $red;
$ans .= $line;
}
return $ans;
} }
# #
function dosend($fun, $socket, $msg) function dosend($fun, $socket, $msg)
@ -115,7 +128,9 @@ function dosend($fun, $socket, $msg)
$msg = $siz . $msg; $msg = $siz . $msg;
$left = $len + 4; $len += 4;
$left = $len;
$ret = false;
while ($left > 0) while ($left > 0)
{ {
$res = socket_write($socket, substr($msg, 0 - $left), $left); $res = socket_write($socket, substr($msg, 0 - $left), $left);
@ -126,8 +141,13 @@ function dosend($fun, $socket, $msg)
error_log("CKPERR: $msg '$sockerr'"); error_log("CKPERR: $msg '$sockerr'");
break; break;
} }
else if ($res == 0)
$left -= $res; {
$msg = "$fun() sendsock() incomplete (".($len-$left)." vs $len)";
error_log("CKPERR: $msg");
break;
}
$left -= $res;
} }
if ($left == 0) if ($left == 0)
$ret = true; $ret = true;

Loading…
Cancel
Save