|
|
@ -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,13 +78,17 @@ 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 = ''; |
|
|
|
|
|
|
|
$left = $len; |
|
|
|
|
|
|
|
while ($left > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$line = socket_read($socket, $left, PHP_BINARY_READ); |
|
|
|
if ($line === false) |
|
|
|
if ($line === false) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$sockerr = socket_strerror(socket_last_error()); |
|
|
|
$sockerr = socket_strerror(socket_last_error()); |
|
|
@ -88,15 +96,20 @@ function readsockline($fun, $socket) |
|
|
|
error_log("CKPERR: $msg '$sockerr'"); |
|
|
|
error_log("CKPERR: $msg '$sockerr'"); |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
$red = strlen($line); |
|
|
|
if (strlen($line) != $len) |
|
|
|
if ($red == 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$msg = "$fun() readsockline() incomplete ($len)"; |
|
|
|
$msg = "$fun() readsockline() incomplete (".($len-$left)." vs $len)"; |
|
|
|
error_log("CKPERR: $msg '$line'"); |
|
|
|
$sub = "'".substr($line, 0, 30)."'"; |
|
|
|
|
|
|
|
if (strlen($line) > 30) |
|
|
|
|
|
|
|
$sub .= '...'; |
|
|
|
|
|
|
|
error_log("CKPERR: $msg $sub"); |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$left -= $red; |
|
|
|
return $line; |
|
|
|
$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,7 +141,12 @@ function dosend($fun, $socket, $msg) |
|
|
|
error_log("CKPERR: $msg '$sockerr'"); |
|
|
|
error_log("CKPERR: $msg '$sockerr'"); |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
if ($res == 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$msg = "$fun() sendsock() incomplete (".($len-$left)." vs $len)"; |
|
|
|
|
|
|
|
error_log("CKPERR: $msg"); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
$left -= $res; |
|
|
|
$left -= $res; |
|
|
|
} |
|
|
|
} |
|
|
|
if ($left == 0) |
|
|
|
if ($left == 0) |
|
|
|