You are here

private function SocketHandler::writeToSocket in Lagoon Logs 8

1 call to SocketHandler::writeToSocket()
SocketHandler::write in src/Logger/SocketHandler.php
Connect (if necessary) and write to the socket

File

src/Logger/SocketHandler.php, line 331

Class

SocketHandler
Stores to any socket - uses fsockopen() or pfsockopen().

Namespace

Drupal\lagoon_logs\Logger

Code

private function writeToSocket($data) {
  $length = strlen($data);
  $sent = 0;
  $this->lastSentBytes = $sent;
  while ($this
    ->isConnected() && $sent < $length) {
    if (0 == $sent) {
      $chunk = $this
        ->fwrite($data);
    }
    else {
      $chunk = $this
        ->fwrite(substr($data, $sent));
    }
    if ($chunk === FALSE) {
      throw new \RuntimeException("Could not write to socket");
    }
    $sent += $chunk;
    $socketInfo = $this
      ->streamGetMetadata();
    if ($socketInfo['timed_out']) {
      throw new \RuntimeException("Write timed-out");
    }
    if ($this
      ->writingIsTimedOut($sent)) {
      throw new \RuntimeException("Write timed-out, no data sent for `{$this->writingTimeout}` seconds, probably we got disconnected (sent {$sent} of {$length})");
    }
  }
  if (!$this
    ->isConnected() && $sent < $length) {
    throw new \RuntimeException("End-of-file reached, probably we got disconnected (sent {$sent} of {$length})");
  }
}