#pragma once #include "IBuffer.h" #include #include #include #include class ConstBuffer : public IBuffer { public: // 构造函数包含四个参数: // 1. 缓冲区长度 // 2. 是否等待读数据 // 3. 是否等待写数据 // 4. 固定帧长度 explicit ConstBuffer(unsigned long bufLength, bool readWait, bool writeWait, unsigned short frameLength); ~ConstBuffer(); int write(unsigned char* data, unsigned short length); // 写数据 unsigned short read(unsigned char* data, unsigned short length); // 读数据 void clear(); private: unsigned char* m_pBuffer; unsigned long m_iBufferLength; // 缓冲区长度 unsigned short m_iFrameLength; // 帧长 unsigned long m_iReadIndex; // 读索引 unsigned long m_iWriteIndex; // 写索引 QMutex m_mutex; bool m_bIsOneCycle; // 是否在一圈内 bool m_bReadWait; // 读等待 bool m_bWriteWait; // 写等待 QWaitCondition m_condRead; QWaitCondition m_condWrite; private: inline unsigned short GetFrameLength() override { return m_iFrameLength; } bool verifyFrameLength(unsigned char* buf, unsigned short length); // 验证帧长 bool IsFull(unsigned short length) override; bool IsEmpty(unsigned short length) override; };