diff --git a/source/ByteBuffer.cpp b/source/ByteBuffer.cpp index 0f360a10..4f74ca19 100644 --- a/source/ByteBuffer.cpp +++ b/source/ByteBuffer.cpp @@ -48,6 +48,11 @@ cByteBuffer::~cByteBuffer() bool cByteBuffer::Write(const char * a_Bytes, int a_Count) { + // DEBUG: Store the current free space for a check after writing + int CurFreeSpace = GetFreeSpace(); + int CurReadableSpace = GetReadableSpace(); + int WrittenBytes = 0; + if (GetFreeSpace() < a_Count) { return false; @@ -60,11 +65,16 @@ bool cByteBuffer::Write(const char * a_Bytes, int a_Count) m_WritePos = 0; a_Bytes += TillEnd; a_Count -= TillEnd; + WrittenBytes = TillEnd; } // We're guaranteed that we'll fit in a single write op memcpy(m_Buffer + m_WritePos, a_Bytes, a_Count); m_WritePos += a_Count; + WrittenBytes += a_Count; + + ASSERT(GetFreeSpace() == CurFreeSpace - WrittenBytes); + ASSERT(GetReadableSpace() == CurReadableSpace + WrittenBytes); return true; } @@ -467,6 +477,24 @@ void cByteBuffer::ResetRead(void) +void cByteBuffer::ReadAgain(AString & a_Out) +{ + // Return the data between m_DataStart and m_ReadPos (the data that has been read but not committed) + // Used by ProtoProxy to repeat communication twice, once for parsing and the other time for the remote party + int DataStart = m_DataStart; + if (m_ReadPos < m_DataStart) + { + // Across the ringbuffer end, read the first part and adjust next part's start: + a_Out.append(m_Buffer + m_DataStart, m_BufferSize - m_DataStart); + DataStart = 0; + } + a_Out.append(m_Buffer + DataStart, m_ReadPos - DataStart); +} + + + + + void cByteBuffer::AdvanceReadPos(int a_Count) { m_ReadPos += a_Count; diff --git a/source/ByteBuffer.h b/source/ByteBuffer.h index c0ffed43..3981ab06 100644 --- a/source/ByteBuffer.h +++ b/source/ByteBuffer.h @@ -94,6 +94,9 @@ public: /// Restarts next reading operation at the start of the ringbuffer void ResetRead(void); + /// Re-reads the data that has been read since the last commit to the current readpos. Used by ProtoProxy to duplicate communication + void ReadAgain(AString & a_Out); + protected: char * m_Buffer; int m_BufferSize; // Total size of the ringbuffer