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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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()
  29. : bufSize(DEFAULT_BUF_SIZE), offset(0)
  30. {
  31. ptr = start = sentUpTo = new U8[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. while (sentUpTo < ptr) {
  49. size_t len;
  50. len = (ptr - sentUpTo);
  51. if (!flushBuffer())
  52. break;
  53. offset += len - (ptr - sentUpTo);
  54. }
  55. // Managed to flush everything?
  56. if (sentUpTo == ptr)
  57. ptr = sentUpTo = start;
  58. // Time to shrink an excessive buffer?
  59. gettimeofday(&now, NULL);
  60. if ((sentUpTo == ptr) && (bufSize > DEFAULT_BUF_SIZE) &&
  61. ((now.tv_sec < lastSizeCheck.tv_sec) ||
  62. (now.tv_sec > (lastSizeCheck.tv_sec + 5)))) {
  63. if (peakUsage < (bufSize / 2)) {
  64. size_t newSize;
  65. newSize = DEFAULT_BUF_SIZE;
  66. while (newSize < peakUsage)
  67. newSize *= 2;
  68. // We know the buffer is empty, so just reset everything
  69. delete [] start;
  70. ptr = start = sentUpTo = new U8[newSize];
  71. end = start + newSize;
  72. bufSize = newSize;
  73. }
  74. gettimeofday(&lastSizeCheck, NULL);
  75. peakUsage = 0;
  76. }
  77. }
  78. bool BufferedOutStream::hasBufferedData()
  79. {
  80. return sentUpTo != ptr;
  81. }
  82. void BufferedOutStream::overrun(size_t needed)
  83. {
  84. size_t totalNeeded, newSize;
  85. U8* newBuffer;
  86. // First try to get rid of the data we have
  87. flush();
  88. // Make note of the total needed space
  89. totalNeeded = needed + (ptr - sentUpTo);
  90. if (totalNeeded > peakUsage)
  91. peakUsage = totalNeeded;
  92. // Enough free space now?
  93. if (avail() > needed)
  94. return;
  95. // Can we shuffle things around?
  96. if (needed < bufSize - (ptr - sentUpTo)) {
  97. memmove(start, sentUpTo, ptr - sentUpTo);
  98. ptr = start + (ptr - sentUpTo);
  99. sentUpTo = start;
  100. return;
  101. }
  102. // We'll need to allocate more buffer space...
  103. if (totalNeeded > MAX_BUF_SIZE)
  104. throw Exception("BufferedOutStream overrun: requested size of "
  105. "%lu bytes exceeds maximum of %lu bytes",
  106. (long unsigned)totalNeeded,
  107. (long unsigned)MAX_BUF_SIZE);
  108. newSize = DEFAULT_BUF_SIZE;
  109. while (newSize < totalNeeded)
  110. newSize *= 2;
  111. newBuffer = new U8[newSize];
  112. memcpy(newBuffer, sentUpTo, ptr - sentUpTo);
  113. delete [] start;
  114. bufSize = newSize;
  115. ptr = newBuffer + (ptr - sentUpTo);
  116. sentUpTo = start = newBuffer;
  117. end = newBuffer + newSize;
  118. gettimeofday(&lastSizeCheck, NULL);
  119. peakUsage = totalNeeded;
  120. return;
  121. }