Пример #1
0
	unsigned long CloHttp::HttpStart(void)
	{
		CloHttpTask * pHttpTask =  m_pAssoc->m_pHttpTask;
		if( !pHttpTask )
		{//
			assert(0);
			return -1;
		}

		// 开始下载文件
		SAFE_CALLBACK( this , E_HTTPSTATUS_START, 0 );

		Lock();
		SAFE_DELETE(m_pAssoc->m_pHttpResponse);
		if( m_pAssoc->m_bCancel ) 
		{
			// It's being canceled!
			Unlock();
			SAFE_CALLBACK( this , E_HTTPSTATUS_CANCEL, 0 );
			return StopThread();
		}
		Unlock();

		try
		{
			//add by dongzhong 2009-05-26
			m_pAssoc->Start();

			// It's being canceled!
			if( m_pAssoc->m_bCancel ){
				pHttpTask->Cancel();
				SAFE_CALLBACK( this , E_HTTPSTATUS_CANCEL, 0 );
				return StopThread();
			}

			if(!m_pAssoc->m_pHttpResponse)
			{
				pHttpTask->Cancel();
				SAFE_CALLBACK( this , E_HTTPSTATUS_ERROR , GetLastError() );
				return StopThread();
			}

			// header 
			RecvHeader();

			// body
			RecvBody();

		}
		catch (...)
		{
			pHttpTask->Cancel() ;

			SAFE_CALLBACK( this , E_HTTPSTATUS_ERROR, GetLastError() );
			return StopThread();
		}	
		return StopThread();
	}
void CListener::ProcessRead(SOCKET sock)
{
	MSG_HEADER	hdr;
	int			iError = 0;

	if (!RecvHeader(sock, (char*)&hdr, sizeof (MSG_HEADER), 0, &iError))
	{
		if (iError)
		{
			// check existence of the connection and close	
			CloseConnection(sock);
		}
		else	// nonfatal error -> don't close connection
		{
		}
		return;
	}
	else
	{
		hdr.ConvertNTOH();
	}

	if (hdr.Command < VCM_MIN || hdr.Command > VCM_MAX)
	{
		// invalid command
		// check existence of the connection and close
		CloseConnection(sock);
		return;
	}

	if (hdr.DataLen < 0)
	{
		// check existence of the connection and close
		CloseConnection(sock);
		return;
	}

#ifdef UNICODE
	if (hdr.DataLen % 2 != 0)
	{
		// invalid data (because data is a widechar string, number of bytes must be even)		
		// check existence of the connection and close
		CloseConnection(sock);
	}
#endif

	if (hdr.DataLen > 0)
	{
		int		iNumTChar = hdr.DataLen/sizeof(TCHAR);
		TCHAR	*pData;

		pData = new TCHAR[iNumTChar + 1];
		if (!pData)
		{
			CloseConnection(sock);
			return;
		}

		if (!RecvData(sock, (char*)pData, hdr.DataLen, 0, &iError))
		{
			delete []pData;
			if (iError)
			{
				// check existence of the connection and close
				CloseConnection(sock);
			}
			return;
		}

		pData[iNumTChar] = 0;
		ProcessCmd(hdr, pData, sock);
		delete []pData;
	}
	else
	{
		ProcessCmd(hdr, _T(""), sock);
	}
}