Buffer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_BUFFER_H
  13. #define SE_INCL_BUFFER_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #include <Engine/Base/Timer.h>
  18. /*
  19. * Class for buffering data streams at byte level.
  20. */
  21. class CBuffer {
  22. public:
  23. SLONG bu_slAllocationStep; // how many bytes to add when buffer overflows
  24. SLONG bu_slWriteOffset; // where to write to next
  25. SLONG bu_slReadOffset; // where to read from next
  26. SLONG bu_slFree; // number of bytes free
  27. SLONG bu_slSize; // current buffer size
  28. UBYTE *bu_pubBuffer; // buffer memory
  29. // default constructor
  30. CBuffer(void);
  31. // destructor
  32. ~CBuffer(void);
  33. // free buffer
  34. void Clear(void);
  35. // expand buffer to be given number of bytes in size
  36. void Expand(SLONG slNewSize);
  37. // set how many bytes to add when buffer overflows
  38. void SetAllocationStep(SLONG slStep);
  39. // read bytes from buffer
  40. SLONG ReadBytes(void *pv, SLONG slSize);
  41. // skip bytes from buffer (read without actually reading)
  42. SLONG SkipBytes(SLONG slSize);
  43. // read bytes from buffer to stream
  44. SLONG ReadBytesToStream(CTStream &strm, SLONG slSize);
  45. // unread bytes from buffer
  46. void UnreadBytes(SLONG slSize);
  47. // check how many bytes are there to read
  48. SLONG QueryReadBytes(void);
  49. // write bytes to buffer
  50. void WriteBytes(const void *pv, SLONG slSize);
  51. // move all data from another buffer to this one
  52. void MoveBuffer(CBuffer &buFrom);
  53. };
  54. // data for limiting bandwidth/lantency and calculating statistics in block-buffers
  55. class CBlockBufferStats {
  56. public:
  57. FLOAT bbs_fLatencyLimit; // minimum latency in seconds
  58. FLOAT bbs_fLatencyVariation;// additional latency variation
  59. FLOAT bbs_fBandwidthLimit; // maximum bandwidth in bps (bits per second)
  60. CTimerValue bbs_tvTimeUsed; // next point in time free for data receiving
  61. void Clear(void);
  62. // get time when block of given size will be finished if started now
  63. CTimerValue GetBlockFinalTime(SLONG slSize);
  64. };
  65. /*
  66. * Class for buffering data streams at block level.
  67. */
  68. class CBlockBuffer : public CBuffer {
  69. public:
  70. CBlockBufferStats *bb_pbbsStats; // for bandwidth/latency stats and limits
  71. SLONG bb_slBlockSizeRead; // block size left for reading (0 if not inside block)
  72. SLONG bb_slBlockSizeWrite; // block size left for writing (0 if not inside block)
  73. // default constructor
  74. CBlockBuffer(void);
  75. // destructor
  76. ~CBlockBuffer(void);
  77. // free buffer
  78. void Clear(void);
  79. // read one block if possible
  80. BOOL ReadBlock(void *pv, SLONG &slSize);
  81. // read one block from buffer to stream
  82. BOOL ReadBlockToStream(CTStream &strm);
  83. // write one block
  84. void WriteBlock(const void *pv, SLONG slSize);
  85. // unread one block
  86. void UnreadBlock(SLONG slSize);
  87. // read raw block data
  88. SLONG ReadRawBlock(void *pv, SLONG slSize);
  89. // write raw block data
  90. void WriteRawBlock(const void *pv, SLONG slSize);
  91. // unread raw block data
  92. void UnreadRawBlock(SLONG slSize);
  93. // move all data from another buffer to this one
  94. void MoveBlockBuffer(CBlockBuffer &buFrom);
  95. // peek sizes of next block
  96. void PeekBlockSize(SLONG &slExpectedSize, SLONG &slReceivedSoFar);
  97. };
  98. #endif /* include-once check. */