捕获fwrite()/套接字错误

时间:2019-05-23 08:48:52

标签: php sockets

我正在创建一个library,它正在建立套接字连接:

public function __construct(Options $options)
{
    $this->responseBuffer = new Response();
    $this->connection = stream_socket_client($options->fullSocketAddress());

    if (!$this->isAlive($this->connection)) {
        throw new DeadSocket();
    }

    stream_set_timeout($this->connection, $this->timeout);
    $this->options = $options;
}

将数据发送到服务器通过send()方法,如下所示:

public function send(string $xml)
{
    try {
        fwrite($this->connection, $xml);
    } catch (Exception $e) {
        $this->options->getLogger()->error(__METHOD__ . '::' . __LINE__ . " fwrite() failed " . $e->getMessage());
        return;
    }
}

这里的问题是catch无法捕获PHP注意到的错误,在我看来,这表明连接本身已断开。

万一发生服务器错误,我会得到<stream:error>(XMPP标准),但是,如果套接字中断或类似这样的超时发生,我将无法捕获:

  

[2019年5月22日12:35:07 UTC] PHP注意:fwrite():94字节的发送失败,错误码为errno = 110在第52行的... / Socket.php中连接超时

这时候我想知道是否发生错误,以便我可以触发重新连接,但是执行以下任何一项操作似乎都没有帮助:

if (!is_resource($this->connection)) $this->reconnect();

if (!$this->connection) $this->reconnect();

除了检查socket_get_status()的任何属性之外,因为socket_get_status($this->connection)['timed_out']可以是true,即使存在活动连接也是如此。

有没有办法捕捉到这个通知?

还有一种方法可以模拟行为,以便即使连接没有超时也能重现它?

1 个答案:

答案 0 :(得分:0)

您可以通过调用error_reporting来设置级别,例如:

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

或者,您可以使用set_error_handler设置自己的错误处理程序。您也可以在页面上找到示例:

<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting, so let it fall
        // through to the standard PHP error handler
        return false;
    }

    switch ($errno) {
    case E_USER_ERROR:
        echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
        echo "  Fatal error on line $errline in file $errfile";
        echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
        echo "Aborting...<br />\n";
        exit(1);
        break;

    case E_USER_WARNING:
        echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
        break;

    case E_USER_NOTICE:
        echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
        break;

    default:
        echo "Unknown error type: [$errno] $errstr<br />\n";
        break;
    }

    /* Don't execute PHP internal error handler */
    return true;
}

// function to test the error handling
function scale_by_log($vect, $scale)
{
    if (!is_numeric($scale) || $scale <= 0) {
        trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
    }

    if (!is_array($vect)) {
        trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
        return null;
    }

    $temp = array();
    foreach($vect as $pos => $value) {
        if (!is_numeric($value)) {
            trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE);
            $value = 0;
        }
        $temp[$pos] = log($scale) * $value;
    }

    return $temp;
}

// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");

// trigger some errors, first define a mixed array with a non-numeric item
echo "vector a\n";
$a = array(2, 3, "foo", 5.5, 43.3, 21.11);
print_r($a);

// now generate second array
echo "----\nvector b - a notice (b = log(PI) * a)\n";
/* Value at position $pos is not a number, using 0 (zero) */
$b = scale_by_log($a, M_PI);
print_r($b);

// this is trouble, we pass a string instead of an array
echo "----\nvector c - a warning\n";
/* Incorrect input vector, array of values expected */
$c = scale_by_log("not array", 2.3);
var_dump($c); // NULL

// this is a critical error, log of zero or negative number is undefined
echo "----\nvector d - fatal error\n";
/* log(x) for x <= 0 is undefined, you used: scale = $scale" */
$d = scale_by_log($a, -2.5);
var_dump($d); // Never reached
?>