// get buffer address for socket
FishinoSockBuf *FishinoSockBuffers::GetBuf(uint8_t sock)
{
	// search for socket
	uint8_t iBuf = FindBuf(sock);

	// return NULL if not found
	if(iBuf == 0xff)
		return NULL;
	
	// return found buffer
	return buffers + iBuf;
}
// create buffer for socket
// (or increase usage count if already there)
FishinoSockBuf *FishinoSockBuffers::AllocBuf(uint8_t sock)
{
	// do NOT allocate for invalid socket
	if(sock == 0xff)
		return 0;

	// search for socket
	uint8_t iBuf = FindBuf(sock);

	// if not found, allocate a new buffer extending buffer count if needed
	if(iBuf == 0xff)
	{
		// first try to find a free slot
		for(uint8_t i = 0; i < bufAlloc; i++)
			if(buffers[i].sock == 0xff)
			{
				iBuf = i;
				break;
			}
			
		// if no free slot is found (unlikely....) just increase number of buffers
		if(iBuf == 0xff)
		{
			FishinoSockBuf *newBufs = (FishinoSockBuf *)FISHINO_MALLOC((bufAlloc + 1) * sizeof(FishinoSockBuf));
			memcpy(newBufs, buffers, bufAlloc * sizeof(FishinoSockBuf));
			free(buffers);
			buffers = newBufs;
			iBuf = bufAlloc++;
			buffers[iBuf].sock = 0xff;
			buffers[iBuf].buf = NULL;
			buffers[iBuf].count = 0;
			buffers[iBuf].pos = 0;
			buffers[iBuf].clientCount = 0;
		}
	}
	
	FishinoSockBuf *buf = buffers + iBuf;
	if(buf->sock == 0xff)
	{
		buf->sock = sock;
		buf->buf = (uint8_t *)FISHINO_MALLOC(FISHINO_SOCKET_BUF_SIZE);
		buf->count = 0;
		buf->pos = 0;
		buf->clientCount = 0;
	}
	buf->clientCount++;
	return buf;
}
// remove buffer for socket (or decrease its usage counter if more than one)
void FishinoSockBuffers::KillBuf(uint8_t sock)
{
	// search for socket
	uint8_t iBuf = FindBuf(sock);
	if(iBuf == 0xff)
		return;

	FishinoSockBuf *buf = buffers + iBuf;
	if(!buf->clientCount || !--buf->clientCount)
	{
		if(buf->buf)
			free(buf->buf);
		buf->buf = NULL;
		buf->count = 0;
		buf->pos = 0;
		buf->sock = 0xff;
		buf->clientCount = 0;
	}
}
BOOL CIVPlaybackDataBuf::ChannelTarget::ReadSome(FILETIME& CurrentFrameTime)
{
    IVFileDataHead DataHead;
    PlayBackTargetQueue* pTemp = NULL;
    m_Reader.read(
        (char*)&DataHead, 
        sizeof(IVFileDataHead));
    SYSTEMTIME syt;
    BOOL bRc = FileTimeToSystemTime(&DataHead.t, &syt);
    if ( bRc )
    {
        bRc = IsValidSystemTime(syt);
    }
    if ( !bRc ||
         DataHead.wTargetNum > TARGET_MAX_NUM ||
         DataHead.dwPrePos > DataHead.dwNextPos )
    {
        ASSERT(FALSE);
        return FALSE;
    }
    
    /**
    *@note 1. 找是否有目标数据缓冲(TargetQueue)
    */
    int nBufPos = FindBuf();
    if ( nBufPos == NO_BUF_Ramain )
    {
        DebugOut("CIVDataBuf::OnIVDataSend No Find Buf!\n");
        assert(false);
        return FALSE;
    }

    CurrentFrameTime = DataHead.t;
    pTemp = &m_pTargetBuf[nBufPos];
    pTemp->AddRef();
    pTemp->nCount = DataHead.wTargetNum;
    m_Reader.read(
        (char*)pTemp->Tar,
        sizeof(WPG_Target)*DataHead.wTargetNum);
    m_IVSomeData.insert( PlayBackGroupTarget(DataHead.t, pTemp) );
    return TRUE;
}