int CMsgBufVSE::nHandleBufferOverrun(INT nSize) { int nResult = CALL_SUCCESS; if (m_nIndexRead < m_nIndexWrite) { if ((m_nIndexRead + m_nBufferSize) <= (m_nIndexWrite + nSize)) { nResult = WARN_BUFFER_OVERRUN; nAdvanceReadIndex(); nHandleBufferOverrun(nSize); } } else if (m_nIndexRead > m_nIndexWrite) { if ((m_nIndexRead) <= (m_nIndexWrite + nSize)) { nResult = WARN_BUFFER_OVERRUN; nAdvanceReadIndex(); nHandleBufferOverrun(nSize); } } else { if (m_nMsgCount != 0) { nResult = WARN_BUFFER_OVERRUN; nAdvanceReadIndex(); nHandleBufferOverrun(nSize); } } return nResult; }
/********************************************************************************** Function Name : ReadEntry Input : INDEX, bSetNextIndexStartPos. Output : CALL_SUCCESS for success. ERR_READ_MEMORY_SHORT when caller specifies size of the msg less than required size. ERR_INVALID_INDEX for invalid index. Functionality : Interface function. Reads the entry from the given index and user will have an option set the next entry start pos. Member of : CMsgBufVVSE Friend of : - Authors : Pradeep Kadoor Date Created : 22/06/2009 Modifications : - ************************************************************************************/ HRESULT CMsgBufVVSE::ReadEntry(int& nType, BYTE* pbyMsg, int& nSize, int nEntry, BOOL bSetNextIndexStartPos) { // Lock the buffer EnterCriticalSection(&m_CritSectionForGB); HRESULT Result = CALL_SUCCESS; if ((nEntry < 0) || ((nEntry + m_nTmpMsgCount) > m_nMsgCount)) { Result = ERR_INVALID_INDEX; } else { int nTmpIndex = m_nReadIndexTmp; for (int i = 0; i < nEntry; i++) { nAdvanceReadIndex(nTmpIndex); } Result = ReadBuffer(nType, pbyMsg, nSize, nTmpIndex); if ((Result == CALL_SUCCESS) && (bSetNextIndexStartPos == TRUE)) { m_nReadIndexTmp = nTmpIndex; m_nTmpMsgCount += nEntry; } } // Unlock the buffer LeaveCriticalSection(&m_CritSectionForGB); return Result; }
/********************************************************************************** Function Name : nHandleStartIndex() Input(s) : - Output : CALL_SUCCESS for success. CALL_FAILURE for failure. Functionality : If buffer overrun criteria is met, start index is advanced until it solves buffer overrun. Member of : CMsgBufVVSE Friend of : - Authors : Pradeep Kadoor Date Created : 22/06/2009 Modifications : - ************************************************************************************/ int CMsgBufVVSE::nHandleStartIndex(int nSize) { int nResult = CALL_SUCCESS; if (m_nStartIndex < m_nIndexWrite) { if ((m_nStartIndex + m_nBufferSize) <= (m_nIndexWrite + nSize)) { TRACE("<start>%d<read>%d<write>%d<count>%d<tmpCount>%d\n",m_nStartIndex, m_nReadIndexTmp, m_nIndexWrite, m_nMsgCount, m_nTmpMsgCount); nAdvanceReadIndex(m_nStartIndex); m_nMsgCount--; m_nReadIndexTmp = m_nStartIndex; m_nTmpMsgCount = 0; TRACE("<start>%d<read>%d<write>%d<count>%d<tmpCount>%d\n",m_nStartIndex, m_nReadIndexTmp, m_nIndexWrite, m_nMsgCount, m_nTmpMsgCount); nHandleStartIndex(nSize); } } else if (m_nStartIndex > m_nIndexWrite) { if ((m_nStartIndex) <= (m_nIndexWrite + nSize)) { TRACE("<start>%d<read>%d<write>%d<count>%d<tmpCount>%d\n",m_nStartIndex, m_nReadIndexTmp, m_nIndexWrite, m_nMsgCount, m_nTmpMsgCount); nAdvanceReadIndex(m_nStartIndex); m_nMsgCount--; m_nReadIndexTmp = m_nStartIndex; m_nTmpMsgCount = 0; TRACE("<start>%d<read>%d<write>%d<count>%d<tmpCount>%d\n",m_nStartIndex, m_nReadIndexTmp, m_nIndexWrite, m_nMsgCount, m_nTmpMsgCount); nHandleStartIndex(nSize); } } else { if (m_nMsgCount != 0) { ASSERT(FALSE); } } TRACE("handled<start>%d<read>%d<write>%d<count>%d<tmpCount>%d\n",m_nStartIndex, m_nReadIndexTmp, m_nIndexWrite, m_nMsgCount, m_nTmpMsgCount); return nResult; }
/********************************************************************************** Function Name : AdvanceToNextMsg() Output : CALL_SUCCESS for success. CALL_FAILURE for failure. if allocated memory by the caller is not enough. Functionality : Interface function. Current msg is skipped. Caller can use this function when wishes to skip the current msg due to memory constraint. Member of : CMsgBufVSE Friend of : - Authors : Pradeep Kadoor Date Created : 22/06/2009 Modifications : - ************************************************************************************/ HRESULT CMsgBufVSE::AdvanceToNextMsg() { HRESULT Result = CALL_SUCCESS; if (m_nMsgCount == 0) { Result = EMPTY_APP_BUFFER; } else { nAdvanceReadIndex();//Helper function to advance the read index //to the next msg. } return Result; }
/********************************************************************************** Function Name : nSetStartPos Input : Entry Number - nIndex Output : CALL_SUCCESS for success. ERR_INVALID_INDEX for invalid index. Functionality : Interface function. Sets the start pos to the 'nIndex'th entry. Member of : CMsgBufVVSE Friend of : - Authors : Pradeep Kadoor Date Created : 10/11/2009 Modifications : - ************************************************************************************/ HRESULT CMsgBufVVSE::SetStartPos(int nEntry) { HRESULT Result = CALL_SUCCESS; if (nEntry < 0 || nEntry > m_nMsgCount) { Result = ERR_INVALID_INDEX; } else { m_nReadIndexTmp = m_nStartIndex; for (int i = 0; i < nEntry; i++) { nAdvanceReadIndex(m_nReadIndexTmp); m_nTmpMsgCount++; } } return Result; }