RetryWrapper.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Robin Appelman <robin@icewind.nl>
  4. * SPDX-License-Identifier: MIT
  5. */
  6. namespace Icewind\Streams;
  7. /**
  8. * Wrapper that retries reads/writes to remote streams that dont deliver/recieve all requested data at once
  9. */
  10. class RetryWrapper extends Wrapper {
  11. public static function wrap($source) {
  12. return self::wrapSource($source);
  13. }
  14. public function dir_opendir($path, $options) {
  15. return false;
  16. }
  17. public function stream_open($path, $mode, $options, &$opened_path) {
  18. $this->loadContext();
  19. return true;
  20. }
  21. public function stream_read($count) {
  22. $result = parent::stream_read($count);
  23. $bytesReceived = strlen($result);
  24. while (strlen($result) > 0 && $bytesReceived < $count && !$this->stream_eof()) {
  25. $result .= parent::stream_read($count - $bytesReceived);
  26. $bytesReceived = strlen($result);
  27. }
  28. return $result;
  29. }
  30. public function stream_write($data) {
  31. $bytesToSend = strlen($data);
  32. $bytesWritten = parent::stream_write($data);
  33. $result = $bytesWritten;
  34. while ($bytesWritten > 0 && $result < $bytesToSend && !$this->stream_eof()) {
  35. $dataLeft = substr($data, $result);
  36. $bytesWritten = parent::stream_write($dataLeft);
  37. $result += $bytesWritten;
  38. }
  39. return $result;
  40. }
  41. }