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);
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";
error_log("CKPERR: $msg '$sockerr'");
error_log("CKPERR: $msg ($sle) '$sockerr'");
return false;
}
@ -43,21 +44,24 @@ function _getsock($fun, $port, $unix=true)
}
if ($res === false)
{
$sockerr = socket_strerror(socket_last_error());
$sle = socket_last_error();
$sockerr = socket_strerror($sle);
if ($unix === true)
$msg = "$fun() _getsock() connect($port) failed 3x";
else
$msg = "$fun() _getsock() connect($port) failed 3x (+2+5s sleep)";
error_log("CKPERR: $msg '$sockerr'");
error_log("CKPERR: $msg ($sle) '$sockerr'");
socket_close($socket);
return false;
}
}
}
# 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_RCVTIMEO, $tmo);
# Enable timeout
socket_set_block($socket);
return $socket;
}
#
@ -71,7 +75,8 @@ function readsockline($fun, $socket)
$siz = socket_read($socket, 4, PHP_BINARY_READ);
if ($siz === false)
{
$sockerr = socket_strerror(socket_last_error());
$sle = socket_last_error();
$sockerr = socket_strerror($sle);
$msg = "$fun() readsockline() failed";
error_log("CKPERR: $msg '$sockerr'");
return false;
@ -91,9 +96,10 @@ function readsockline($fun, $socket)
$line = socket_read($socket, $left, PHP_BINARY_READ);
if ($line === false)
{
$sockerr = socket_strerror(socket_last_error());
$msg = "$fun() readsockline() failed";
error_log("CKPERR: $msg '$sockerr'");
$sle = socket_last_error();
$sockerr = socket_strerror($sle);
$msg = "$fun() readsockline() $left failed (len=$len)";
error_log("CKPERR: $msg ($sle) '$sockerr'");
return false;
}
$red = strlen($line);

Loading…
Cancel
Save