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

ConstBuffer.cpp 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "ConstBuffer.h"
  2. ConstBuffer::ConstBuffer(unsigned long bufLength, bool bReadWait, bool bWriteWait,
  3. unsigned short frameLength)
  4. : m_iBufferLength(bufLength)
  5. , m_pBuffer(nullptr)
  6. , m_bIsOneCycle(true)
  7. , m_iReadIndex(0)
  8. , m_iWriteIndex(0)
  9. , m_bReadWait(bReadWait)
  10. , m_bWriteWait(bWriteWait)
  11. , m_iFrameLength(frameLength)
  12. {
  13. m_pBuffer = new unsigned char[bufLength];
  14. }
  15. ConstBuffer::~ConstBuffer()
  16. {
  17. if (m_pBuffer != nullptr) {
  18. delete m_pBuffer;
  19. }
  20. }
  21. // 写数据
  22. int ConstBuffer::write(unsigned char* data, unsigned short length)
  23. {
  24. if (length > m_iBufferLength) {
  25. // 写入帧长不满足要求
  26. return -1;
  27. }
  28. // if (!verifyFrameLength(data, length)) {
  29. // //帧长不匹配
  30. // return -2;
  31. //}
  32. /*QByteArray byteArr((char*)data, length);*/
  33. QMutexLocker locker(&m_mutex);
  34. while (IsFull(length)) { // 当读写指针在同一周期内时
  35. if (m_bWriteWait) {
  36. // add wait here,to wait for sufficient buf to write
  37. m_condWrite.wait(&m_mutex);
  38. } else {
  39. return 0;
  40. }
  41. }
  42. for (int i = 0; i < length; i++) {
  43. m_pBuffer[m_iWriteIndex] = data[i]; // 想缓存中写入数据
  44. m_iWriteIndex++;
  45. if (m_iWriteIndex == m_iBufferLength) { // 当缓存已经写满数据
  46. m_iWriteIndex = 0;
  47. m_bIsOneCycle = false;
  48. }
  49. }
  50. if (m_bWriteWait) {
  51. m_condRead.wakeOne();
  52. }
  53. return length;
  54. }
  55. // 读数据
  56. unsigned short ConstBuffer::read(unsigned char* data, unsigned short length)
  57. {
  58. if (m_iFrameLength > length) { // 读取数据长度小于帧长
  59. return -1;
  60. }
  61. {
  62. QMutexLocker locker(&m_mutex);
  63. // 当帧长为空时
  64. while (IsEmpty(m_iFrameLength)) {
  65. if (m_bReadWait) {
  66. // add wait here, to wait for sufficient data to read
  67. m_condRead.wait(&m_mutex);
  68. } else {
  69. return 0;
  70. }
  71. }
  72. for (int i = 0; i < m_iFrameLength; i++) {
  73. data[i] = m_pBuffer[m_iReadIndex];
  74. m_iReadIndex++;
  75. if (m_iReadIndex >= m_iBufferLength) {
  76. m_iReadIndex -= m_iBufferLength;
  77. m_bIsOneCycle = true;
  78. }
  79. }
  80. }
  81. if (m_bReadWait) {
  82. m_condWrite.wakeOne();
  83. }
  84. return m_iFrameLength;
  85. }
  86. void ConstBuffer::clear()
  87. {
  88. m_bIsOneCycle = true;
  89. m_iReadIndex = 0;
  90. m_iWriteIndex = 0;
  91. }
  92. // 验证帧长
  93. bool ConstBuffer::verifyFrameLength(unsigned char* buf, unsigned short length)
  94. {
  95. if (length != m_iFrameLength) {
  96. return 0;
  97. }
  98. return 1;
  99. }
  100. // memory is full, no place to write
  101. // two different situations: readIndex and writeIndex is in one cycle or not
  102. // one argument: the length of frame
  103. bool ConstBuffer::IsFull(unsigned short length)
  104. {
  105. if (m_bIsOneCycle) {
  106. return m_iWriteIndex + length > m_iReadIndex + m_iBufferLength;
  107. }
  108. return m_iWriteIndex + length > m_iReadIndex;
  109. }
  110. // memory is empty, no data to read
  111. // two different situations: readIndex and writeIndex is in one cycle or not
  112. bool ConstBuffer::IsEmpty(unsigned short length)
  113. {
  114. if (m_bIsOneCycle == true) {
  115. return m_iReadIndex + length > m_iWriteIndex;
  116. }
  117. return m_iReadIndex + length >= m_iBufferLength + m_iWriteIndex;
  118. }