Browse Source

php - more descriptive socket errors and enable the timeout

master
kanoi 10 years ago
parent
commit
3d9f055cdc
  1. 24
      pool/socket.php

24
pool/socket.php

@ -12,9 +12,10 @@ function _getsock($fun, $port, $unix=true)
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false || $socket === null) if ($socket === false || $socket === null)
{ {
$sockerr = socket_strerror(socket_last_error()); $sle = socket_last_error();
$sockerr = socket_strerror($sle);
$msg = "$fun() _getsock() create($port) failed"; $msg = "$fun() _getsock() create($port) failed";
error_log("CKPERR: $msg '$sockerr'"); error_log("CKPERR: $msg ($sle) '$sockerr'");
return false; return false;
} }
@ -43,21 +44,24 @@ function _getsock($fun, $port, $unix=true)
} }
if ($res === false) if ($res === false)
{ {
$sockerr = socket_strerror(socket_last_error()); $sle = socket_last_error();
$sockerr = socket_strerror($sle);
if ($unix === true) if ($unix === true)
$msg = "$fun() _getsock() connect($port) failed 3x"; $msg = "$fun() _getsock() connect($port) failed 3x";
else else
$msg = "$fun() _getsock() connect($port) failed 3x (+2+5s sleep)"; $msg = "$fun() _getsock() connect($port) failed 3x (+2+5s sleep)";
error_log("CKPERR: $msg '$sockerr'"); error_log("CKPERR: $msg ($sle) '$sockerr'");
socket_close($socket); socket_close($socket);
return false; return false;
} }
} }
} }
# Avoid getting locked up for long # Avoid getting locked up for long
$tmo = array('sec' => 1, 'usec' => 0); $tmo = array('sec' => 2, 'usec' => 0);
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $tmo); socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $tmo);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $tmo); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $tmo);
# Enable timeout
socket_set_block($socket);
return $socket; return $socket;
} }
# #
@ -71,7 +75,8 @@ function readsockline($fun, $socket)
$siz = socket_read($socket, 4, PHP_BINARY_READ); $siz = socket_read($socket, 4, PHP_BINARY_READ);
if ($siz === false) if ($siz === false)
{ {
$sockerr = socket_strerror(socket_last_error()); $sle = socket_last_error();
$sockerr = socket_strerror($sle);
$msg = "$fun() readsockline() failed"; $msg = "$fun() readsockline() failed";
error_log("CKPERR: $msg '$sockerr'"); error_log("CKPERR: $msg '$sockerr'");
return false; return false;
@ -91,9 +96,10 @@ function readsockline($fun, $socket)
$line = socket_read($socket, $left, PHP_BINARY_READ); $line = socket_read($socket, $left, PHP_BINARY_READ);
if ($line === false) if ($line === false)
{ {
$sockerr = socket_strerror(socket_last_error()); $sle = socket_last_error();
$msg = "$fun() readsockline() failed"; $sockerr = socket_strerror($sle);
error_log("CKPERR: $msg '$sockerr'"); $msg = "$fun() readsockline() $left failed (len=$len)";
error_log("CKPERR: $msg ($sle) '$sockerr'");
return false; return false;
} }
$red = strlen($line); $red = strlen($line);

Loading…
Cancel
Save