You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BufferedOutStream.cxx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2020 Pierre Ossman for Cendio AB
  3. * Copyright 2017 Peter Astrand <astrand@cendio.se> for Cendio AB
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <rdr/BufferedOutStream.h>
  24. #include <rdr/Exception.h>
  25. using namespace rdr;
  26. static const size_t DEFAULT_BUF_SIZE = 16384;
  27. static const size_t MAX_BUF_SIZE = 32 * 1024 * 1024;
  28. BufferedOutStream::BufferedOutStream(bool emulateCork)
  29. : bufSize(DEFAULT_BUF_SIZE), offset(0), emulateCork(emulateCork)
  30. {
  31. ptr = start = sentUpTo = new uint8_t[bufSize];
  32. end = start + bufSize;
  33. gettimeofday(&lastSizeCheck, NULL);
  34. peakUsage = 0;
  35. }
  36. BufferedOutStream::~BufferedOutStream()
  37. {
  38. // FIXME: Complain about non-flushed buffer?
  39. delete [] start;
  40. }
  41. size_t BufferedOutStream::length()
  42. {
  43. return offset + ptr - sentUpTo;
  44. }
  45. void BufferedOutStream::flush()
  46. {
  47. struct timeval now;
  48. // Only give larger chunks if corked to minimize overhead
  49. if (corked && emulateCork && ((ptr - sentUpTo) < 1024))
  50. return;
  51. while (sentUpTo < ptr) {
  52. size_t len;
  53. len = (ptr - sentUpTo);
  54. if (!flushBuffer())
  55. break;
  56. offset += len - (ptr - sentUpTo);
  57. }
  58. // Managed to flush everything?
  59. if (sentUpTo == ptr)
  60. ptr = sentUpTo = start;
  61. // Time to shrink an excessive buffer?
  62. gettimeofday(&now, NULL);
  63. if ((sentUpTo == ptr) && (bufSize > DEFAULT_BUF_SIZE) &&
  64. ((now.tv_sec < lastSizeCheck.tv_sec) ||
  65. (now.tv_sec > (lastSizeCheck.tv_sec + 5)))) {
  66. if (peakUsage < (bufSize / 2)) {
  67. size_t newSize;
  68. newSize = DEFAULT_BUF_SIZE;
  69. while (newSize < peakUsage)
  70. newSize *= 2;
  71. // We know the buffer is empty, so just reset everything
  72. delete [] start;
  73. ptr = start = sentUpTo = new uint8_t[newSize];
  74. end = start + newSize;
  75. bufSize = newSize;
  76. }
  77. gettimeofday(&lastSizeCheck, NULL);
  78. peakUsage = 0;
  79. }
  80. }
  81. bool BufferedOutStream::hasBufferedData()
  82. {
  83. return sentUpTo != ptr;
  84. }
  85. void BufferedOutStream::overrun(size_t needed)
  86. {
  87. bool oldCorked;
  88. size_t totalNeeded, newSize;
  89. uint8_t* newBuffer;
  90. // First try to get rid of the data we have
  91. // (use corked to make things a bit more efficient since we're not
  92. // trying to flush out everything, just make some room)
  93. oldCorked = corked;
  94. cork(true);
  95. flush();
  96. cork(oldCorked);
  97. // Make note of the total needed space
  98. totalNeeded = needed + (ptr - sentUpTo);
  99. if (totalNeeded > peakUsage)
  100. peakUsage = totalNeeded;
  101. // Enough free space now?
  102. if (avail() > needed)
  103. return;
  104. // Can we shuffle things around?
  105. if (needed < bufSize - (ptr - sentUpTo)) {
  106. memmove(start, sentUpTo, ptr - sentUpTo);
  107. ptr = start + (ptr - sentUpTo);
  108. sentUpTo = start;
  109. return;
  110. }
  111. // We'll need to allocate more buffer space...
  112. if (totalNeeded > MAX_BUF_SIZE)
  113. throw Exception("BufferedOutStream overrun: requested size of "
  114. "%lu bytes exceeds maximum of %lu bytes",
  115. (long unsigned)totalNeeded,
  116. (long unsigned)MAX_BUF_SIZE);
  117. newSize = DEFAULT_BUF_SIZE;
  118. while (newSize < totalNeeded)
  119. newSize *= 2;
  120. newBuffer = new uint8_t[newSize];
  121. memcpy(newBuffer, sentUpTo, ptr - sentUpTo);
  122. delete [] start;
  123. bufSize = newSize;
  124. ptr = newBuffer + (ptr - sentUpTo);
  125. sentUpTo = start = newBuffer;
  126. end = newBuffer + newSize;
  127. gettimeofday(&lastSizeCheck, NULL);
  128. peakUsage = totalNeeded;
  129. return;
  130. }