Ejemplo n.º 1
0
    int CommSockImp::OnRecv() {
        SetLastRecvTime();
        AllocPack* pack = pMemPool_->Alloc(1024);
        if (pack) {
            int len = Recv((void*) pack->getbuffer(), pack->getsize());

            if (len > 0) {
                pack->SetLength(len);
                OnRecvPack(pack);
            }
            pack->release();

            return len;
        }
        return -1;
    }
Ejemplo n.º 2
0
bool CConnectionLayer::PushRB(const char *pBuf, int iLen)
{
	SRecvBuf *pRB;

	if (m_conRecvBufQueue.empty() || m_conRecvBufQueue.back()->iTotalLen == m_conRecvBufQueue.back()->iRecvLen)
	{
		if (iLen < RPH_SIZE)
		{
			pRB = CConnectionManager::GetInstance()->AllocRB();//new SRecvBuf;
			if (pRB == NULL)
			{
				return false;
			}
			pRB->iRecvLen = iLen;
			pRB->iTotalLen = -1;
			pRB->pBuf = CConnectionManager::GetInstance()->AllocBuf(pRB->iRecvLen);//new char[pRB->iRecvLen];
			memcpy(pRB->pBuf, pBuf, iLen);
			m_conRecvBufQueue.push(pRB);
		}
		else
		{
			SReliabilityPacketHeader *pRPH;
			int iOffset = 0;
			while (iOffset < iLen)
			{
				pRPH = (SReliabilityPacketHeader *)(pBuf+iOffset);
				pRB = CConnectionManager::GetInstance()->AllocRB();//new SRecvBuf;
				if (pRB == NULL)
				{
					return false;
				}
				pRB->iTotalLen = pRPH->iSingleSize;
				pRB->iRecvLen = pRB->iTotalLen + iOffset > iLen ? iLen - iOffset : pRB->iTotalLen;
				pRB->pBuf = CConnectionManager::GetInstance()->AllocBuf(pRB->iTotalLen);//new char[pRB->iTotalLen];
				if (pRB->pBuf == NULL)
				{
					return false;
				}
				memcpy(pRB->pBuf, pBuf+iOffset, pRB->iRecvLen);
				iOffset += pRB->iRecvLen;
				m_conRecvBufQueue.push(pRB);
			}
		}
	}
	else
	{
		pRB = m_conRecvBufQueue.back();
		if (pRB->iRecvLen + iLen > pRB->iTotalLen)
		{
			SReliabilityPacketHeader *pRPH;
			int iOffset = pRB->iTotalLen-pRB->iRecvLen;
			memcpy(pRB->pBuf+pRB->iRecvLen, pBuf, iOffset); 
			pRB->iRecvLen = pRB->iTotalLen;
			while (iOffset < iLen)
			{
				pRPH = (SReliabilityPacketHeader *)(pBuf+iOffset);
				pRB = CConnectionManager::GetInstance()->AllocRB();//new SRecvBuf;
				if (pRB == NULL)
				{
					return false;
				}
				pRB->iTotalLen = pRPH->iSingleSize;
				pRB->iRecvLen = pRB->iTotalLen + iOffset > iLen ? iLen - iOffset : pRB->iTotalLen;
				pRB->pBuf = CConnectionManager::GetInstance()->AllocBuf(pRB->iTotalLen);//new char[pRB->iTotalLen];
				if (pRB->pBuf == NULL)
				{
					return false;
				}
				memcpy(pRB->pBuf, pBuf+iOffset, pRB->iRecvLen);
				iOffset += pRB->iRecvLen;
				m_conRecvBufQueue.push(pRB);
			}
		}
		else
		{
			memcpy(pRB->pBuf+pRB->iRecvLen, pBuf, iLen); 
			pRB->iRecvLen += iLen;
		}
	}

	SetLastRecvTime(GetTickCount());
	return true;
}