You are here

function _varnish_read_socket in Varnish 7

Same name and namespace in other branches
  1. 8 varnish.module \_varnish_read_socket()
  2. 6 varnish.module \_varnish_read_socket()

Low-level socket read function.

@params $client an initialized socket client

$retty how many times to retry on "temporarily unavalble" errors

2 calls to _varnish_read_socket()
_varnish_execute_command in ./varnish.module
Send command to varnish.
_varnish_terminal_run in ./varnish.module
Helper function that sends commands to Varnish.

File

./varnish.module, line 419
Common functions used for the module.

Code

function _varnish_read_socket($client, $retry = 2) {

  // Status and length info is always 13 characters.
  $header = socket_read($client, 13, PHP_BINARY_READ);
  if ($header == FALSE) {
    $error = socket_last_error();

    // 35 = socket-unavailable, so it might be blocked from our write.
    // This is an acceptable place to retry.
    if ($error == 35 && $retry > 0) {
      return _varnish_read_socket($client, $retry - 1);
    }
    else {
      watchdog('varnish', 'Socket error: @error', array(
        '@error' => socket_strerror($error),
      ), WATCHDOG_ERROR);
      return array(
        'code' => $error,
        'msg' => socket_strerror($error),
      );
    }
  }
  $msg_len = (int) substr($header, 4, 6) + 1;
  $status = array(
    'code' => substr($header, 0, 3),
    'msg' => socket_read($client, $msg_len, PHP_BINARY_READ),
  );
  return $status;
}