#include "stdafx.h"
#include "AV8Buffer.h"
#include "PlayView.h"
#include "winsock2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern WSABUF stWSABuf;
extern HWND hWnd;
//
// CAV8Buffer()
CAV8Buffer::CAV8Buffer(DWORD dwBlock, DWORD dwByte)
{
m_dwBlock = dwBlock;
m_dwByte = dwByte;
m_pdwIndex = (DWORD *)malloc(dwBlock * sizeof(DWORD));
m_pdwLength = (DWORD *)malloc(dwBlock * sizeof(DWORD));
m_pbFull = (BOOL *)malloc(dwBlock * sizeof(BOOL ));
m_plpArray = (LPSTR *)malloc(dwBlock * sizeof(LPSTR));
for (DWORD dwIndex = 0; dwIndex < dwBlock; dwIndex++)
{
m_pdwIndex [dwIndex] = dwIndex;
m_pdwLength[dwIndex] = 0;
m_pbFull [dwIndex] = FALSE;
m_plpArray [dwIndex] = (LPSTR)malloc(dwByte * sizeof(char));
}
m_lEmptyBlock = -1;
Flag = true;
}
//
// ~CAV8Buffer()
CAV8Buffer::~CAV8Buffer()
{
for (DWORD dwIndex = 0; dwIndex < m_dwBlock; dwIndex++)
free(m_plpArray[dwIndex]);
free(m_pdwIndex);
free(m_pdwLength);
free(m_pbFull);
free(m_plpArray);
}
//
// PushIn()
DWORD CAV8Buffer::PushIn(LPSTR lpData, DWORD dwLength)
{
//查询缓冲队列中空块的位置
if (m_lEmptyBlock < 0)
m_lEmptyBlock = QueryEmpty();
//空块的长度
DWORD dwOldLength = m_pdwLength[m_lEmptyBlock];
if (m_lEmptyBlock >= 0 && m_plpArray[m_lEmptyBlock])
{
memcpy((void *)(m_plpArray[m_lEmptyBlock] + dwOldLength), (const void *)lpData, min(dwLength, m_dwByte - dwOldLength));
m_pdwLength[m_lEmptyBlock] += min(dwLength, m_dwByte - dwOldLength);
if (m_pdwLength[m_lEmptyBlock] == m_dwByte)
{
m_pbFull[m_lEmptyBlock] = TRUE;
OrderAll(m_lEmptyBlock);
//缓冲区满,发送WM_READYSEND消息,启动数据发送线程
if(Flag)
::PostMessage(hWnd,WM_READYSEND,0,0);
m_lEmptyBlock = -1;
}
if (dwLength > m_dwByte - dwOldLength)
PushIn(lpData + (m_dwByte - dwOldLength), dwLength - (m_dwByte - dwOldLength));
return min(dwLength, m_dwByte - dwOldLength);
}
return 0;
}