Exemple #1
0
void Http::Read_Request()
{
    int read_len = 0;
    while (already_read_len < buf_size)
    {
        read_len = Read(buf_data + already_read_len, buf_size - already_read_len);
        if (read_len <= 0)
            break;
        else
        {
            already_read_len += read_len;
            header_len = GetHeaderLen(buf_data, already_read_len);
            if (header_len > 0)
                break;
        }
    }
    //buf_data[already_read_len] = '\0';
}
Exemple #2
0
BOOL BSNetPack::SplitPack(BYTE* recvBuffer, UINT nRecvLen, UINT& nSavedLen)
{
	BSAutoLock lock(&m_mutexBuffer);
	//TRACE("IntoSplitPack:%X len:%X\n:", *recvBuffer, nRecvLen);
	// 遍历RecvBuffer
	for (UINT i=0;i<nRecvLen;)
	{
		// 未取得当前包长度时
		if (!IsHeaderFinished())
		{
			// 计算包头长度
			UINT nRealHeaderLen = GetHeaderLen();
			// 当未收够包头长度时
			if (ReceivedLen <  nRealHeaderLen)
			{
				int tempcopylen = nRealHeaderLen - ReceivedLen;
				if (nRecvLen-i<tempcopylen)
				{
					tempcopylen=nRecvLen-i;
					SaveToBuffer((recvBuffer+i), tempcopylen);
					i+=tempcopylen;
					continue;
				}
				SaveToBuffer((recvBuffer+i), tempcopylen);
				i+=tempcopylen;
			}				
			// 获取包长度(包括包头和包内容)
			ParsePackLen();
			//TRACE("TotalLen:%d\n", nLen);
		}
		if ( GetPackLen() < GetHeaderLen())
		{
			// pack len Error Handling 
			ASSERT(FALSE);
			TRACE("ErrorPackLen:%d%X, len:%d\n", *(BYTE*)(Buffer), GetPackLen());
			BSLogger::SysLog(LOG_ERROR, "报文长度不正确 %u", GetPackLen());
			ClearBuffer();
			return FALSE;
			break;
		}

		// 本次分包报文在当前recv到的buffer里可以读取的长度
		int nPackThisRead = (nRecvLen-i) < (PackLen-ReceivedLen) ? (nRecvLen-i) : (PackLen-ReceivedLen);

		//TRACE("PackThisRead:%d\n", nPackThisRead);

		if(PackLen>MaxBufferLen || ReceivedLen+nPackThisRead>MaxBufferLen)
		{
			// pack len too big, drop it.
			ASSERT(FALSE);
			i += nPackThisRead;
			TRACE("DropTooBigPack:%X, len:%d\n", *(BYTE*)(Buffer), PackLen);
			BSLogger::SysLog(LOG_ERROR, "报文长度过大 %u", PackLen);
			ClearBuffer();
			return FALSE;
			break;
		}

		SaveToBuffer(recvBuffer+i, nPackThisRead);
		i += nPackThisRead;
		nSavedLen = i;

		// 收到一个完整报文
		if (IsPackFinished())
		{
			return TRUE;
		}
	}
	return TRUE;
};