⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpdownload.cpp

📁 模拟迅雷的下载工具软件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
}

void CalueTimertoSZ(int ihh,int imm,int iss,char sz[])
{
		char hh[10],mm[10],ss[10];

		if(ihh<10)
		{
			_itoa(ihh,hh,10);
			hh[1]=hh[0];
			hh[0]='0';
			hh[2]='\0';
		}
		else
			_itoa(ihh,hh,10);

		if(imm<10)
		{
			_itoa(imm,mm,10);
			mm[1]=mm[0];
			mm[0]='0';
			mm[2]='\0';

		}
		else
			_itoa(imm,mm,10);

		if(iss<10)
		{
			_itoa(iss,ss,10);
			ss[1]=ss[0];
			ss[0]='0';
			ss[2]='\0';

		}
		else
			_itoa(iss,ss,10);
	
		//sz.Format("%s:%s:%s",hh,mm,ss);
		sz[0]=hh[0];
		sz[1]=hh[1];
		sz[2]=':';
		sz[3]=mm[0];
		sz[4]=mm[1];
		sz[5]=':';
		sz[6]=ss[0];
		sz[7]=ss[1];
		sz[8]='\0';

}
void CalueSpareTimer(long &hh,long &mm,long &ss,long lComp,long lbps)
{
		CString f1,b2,sTemp;
		float fComp,fbps;
		fComp=(float)lComp;
		if(lbps>1024L)
			fComp/=1024.0f;
		fbps=(float)lbps;
		if(lbps>1024L)
			fbps/=1024.0f;
		fComp/=fbps;
		float fss=fComp;
		fss/=60.0f;
		//Calue "ss"
		sTemp="";
		sTemp.Format("%.2f",fss);
		CalueFloat(f1,b2,sTemp);
		fss=(float)_ttol((LPCTSTR)b2);
		float fmm=(float)_ttol((LPCTSTR)f1);
		fss*=0.01f;
		fss*=60.0f;       //%.0f  12s
		
		//Calue "mm"
		fmm/=60.0f;
		sTemp="";
		f1="";
		b2="";
		sTemp.Format("%.2f",fmm);
		CalueFloat(f1,b2,sTemp);
		fmm=(float)_ttol((LPCTSTR)b2);
		float fhh=(float)_ttol((LPCTSTR)f1);
		fmm*=0.01f;
		fmm*=60.0f;   //%.0f    23m

		hh=(long)fhh;
		mm=(long)fmm;
		ss=(long)fss;

}
void CalueFloat(CString &f1,CString &b2,CString ss)
{
	f1=ss.Left(ss.GetLength()-3);
	b2=ss.Right(2);
}



//
CHttpDownLoad::CHttpDownLoad()
{
	m_pMainFrame=NULL;

	m_bIsEndThe=FALSE;
	m_bIsAfxBegin5Thread0=FALSE;
	m_bIsAfxBegin5Thread1=FALSE;
	m_bIsAfxBegin5Thread2=FALSE;
	m_bIsAfxBegin5Thread3=FALSE;
	m_bIsAfxBegin5Thread4=FALSE;


}
CHttpDownLoad::CHttpDownLoad(void *lParam)
{
	FILEDIALOG_INFO *stFileDlgInfo=(FILEDIALOG_INFO*)lParam;

	m_stLeoDownInfo.bIsFileDown=TRUE;
	m_stLeoDownInfo.fPercent=0.0f;
	m_stLeoDownInfo.fSpeed=0.0f;
	m_stLeoDownInfo.lBreakPointByte=0L;
	m_stLeoDownInfo.lFileLength=0L;
	m_stLeoDownInfo.lFromeByte=0L;
	m_stLeoDownInfo.lToByte=0L;
	m_stLeoDownInfo.nBreakRev=stFileDlgInfo->nBreakRev;
	m_stLeoDownInfo.nImage=stFileDlgInfo->nImage;
	m_stLeoDownInfo.nPID=stFileDlgInfo->nPID;
	m_stLeoDownInfo.nCID=0;
	m_stLeoDownInfo.nSocketLinkNum=stFileDlgInfo->nSocketLinkNum;
	m_stLeoDownInfo.nSvrState=0;
	m_stLeoDownInfo.strComment=stFileDlgInfo->strComment;
	m_stLeoDownInfo.strFilePath=stFileDlgInfo->strFilePath;
	m_stLeoDownInfo.strLocationUrl=stFileDlgInfo->strLocationUrl;
	m_stLeoDownInfo.strRegSubPath=stFileDlgInfo->strRegSubPath;
	m_stLeoDownInfo.bRename=stFileDlgInfo->bRename;
	m_stLeoDownInfo.the=this;
	m_stLeoDownInfo.stSocketType.strAcceptType="";
	m_stLeoDownInfo.stSocketType.nTimeOut=5000;
	m_stLeoDownInfo.stSocketType.strServer="";

	
	m_stHttpDlInfo.BreakInfo0.BreakLength0=0;
	m_stHttpDlInfo.BreakInfo0.lFromeByte=0;
	m_stHttpDlInfo.BreakInfo0.lToByte=0;
	m_stHttpDlInfo.BreakInfo0.lSpeed0=0.0f;
	m_stHttpDlInfo.BreakInfo0.bRevData0=FALSE;
	m_stHttpDlInfo.BreakInfo0.bSpeed=TRUE;
	
	m_stHttpDlInfo.BreakInfo1.BreakLength1=0;
	m_stHttpDlInfo.BreakInfo1.lFromeByte=0;
	m_stHttpDlInfo.BreakInfo1.lToByte=0;
	m_stHttpDlInfo.BreakInfo1.lSpeed1=0.0f;
	m_stHttpDlInfo.BreakInfo1.bRevData1=FALSE;
	m_stHttpDlInfo.BreakInfo1.bSpeed=TRUE;

	m_stHttpDlInfo.BreakInfo2.BreakLength2=0;
	m_stHttpDlInfo.BreakInfo2.lFromeByte=0;
	m_stHttpDlInfo.BreakInfo2.lToByte=0;
	m_stHttpDlInfo.BreakInfo2.lSpeed2=0.0f;
	m_stHttpDlInfo.BreakInfo2.bRevData2=FALSE;
	m_stHttpDlInfo.BreakInfo2.bSpeed=TRUE;

	m_stHttpDlInfo.BreakInfo3.BreakLength3=0;
	m_stHttpDlInfo.BreakInfo3.lFromeByte=0;
	m_stHttpDlInfo.BreakInfo3.lToByte=0;
	m_stHttpDlInfo.BreakInfo3.lSpeed3=0.0f;
	m_stHttpDlInfo.BreakInfo3.bRevData3=FALSE;
	m_stHttpDlInfo.BreakInfo3.bSpeed=TRUE;

	m_stHttpDlInfo.BreakInfo4.BreakLength4=0;
	m_stHttpDlInfo.BreakInfo4.lFromeByte=0;
	m_stHttpDlInfo.BreakInfo4.lToByte=0;
	m_stHttpDlInfo.BreakInfo4.lSpeed4=0.0f;
	m_stHttpDlInfo.BreakInfo4.bRevData4=FALSE;
	m_stHttpDlInfo.BreakInfo4.bSpeed=TRUE;

	m_stHttpDlInfo.lFileLength=0;
	m_stHttpDlInfo.bSpeed=FALSE;
	m_stHttpDlInfo.fSpeed=0.0f;
	m_stHttpDlInfo.strRegPath=stFileDlgInfo->strRegSubPath;

	m_stTimeBegin.nHH=0;
	m_stTimeBegin.nMM=0;
	m_stTimeBegin.nSS=0;
	m_stTimeBegin.nPID=stFileDlgInfo->nPID;
	m_stTimeBegin.nItem=stFileDlgInfo->nPID-1;
	m_stTimeBegin.nStartDownLoadNum=0;
	m_stTimeBegin.strRegPath=stFileDlgInfo->strRegSubPath;
	m_stTimeBegin.the=this;

	m_stTimeList.eHH=0;
	m_stTimeList.eMM=0;
	m_stTimeList.eSS=0;
	m_stTimeList.nPID=stFileDlgInfo->nPID;
	m_stTimeList.nItem=stFileDlgInfo->nPID-1;
	m_stTimeList.strRegPath=stFileDlgInfo->strRegSubPath;
	m_stTimeList.the=this;
	

	m_pMainFrame=stFileDlgInfo->pMainFrame;

	m_bIsEndThe=FALSE;
	m_bIsAfxBegin5Thread0=FALSE;
	m_bIsAfxBegin5Thread1=FALSE;
	m_bIsAfxBegin5Thread2=FALSE;
	m_bIsAfxBegin5Thread3=FALSE;
	m_bIsAfxBegin5Thread4=FALSE;



}

CHttpDownLoad::~CHttpDownLoad()
{
	//CloseHandle(m_hEndMutex);
	//CloseHandle(m_hFileDownMutex);
	//CloseHandle(m_hHttpDLMutex);
	//CloseHandle(m_glhListCtrlMutex);
	//CloseHandle(m_glhListMutex);

}


BEGIN_MESSAGE_MAP(CHttpDownLoad, CWnd)
	//{{AFX_MSG_MAP(CHttpDownLoad)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CHttpDownLoad message handlers
void CHttpDownLoad::SetThreadEnd(BOOL nEnd)
{
	//WaitForSingleObject(m_hEndMutex,INFINITE);
	m_bIsEndThe=nEnd;
	//ReleaseMutex(m_hEndMutex);

}
void CHttpDownLoad::SetTimerBegin(UINT nEvent)
{
	SetTimer(nEvent,1000,NULL);
}
void CHttpDownLoad::KillTimerEnd(int nEvent)
{
	KillTimer(nEvent);
}
LEODOWN_INFO CHttpDownLoad::GetLeoDownInfo()
{
	return m_stLeoDownInfo;
}
void CHttpDownLoad::SetLeoDownInfo(LEODOWN_INFO lParam,CWnd *pMainFrame)
{
	m_stLeoDownInfo.bIsFileDown=lParam.bIsFileDown;
	m_stLeoDownInfo.fPercent=lParam.fPercent;
	m_stLeoDownInfo.fSpeed=lParam.fSpeed;
	m_stLeoDownInfo.lBreakPointByte=lParam.lBreakPointByte;
	m_stLeoDownInfo.lFileLength=lParam.lFileLength;
	m_stLeoDownInfo.lFromeByte=lParam.lFromeByte;
	m_stLeoDownInfo.lToByte=lParam.lToByte;
	m_stLeoDownInfo.nBreakRev=lParam.nBreakRev;
	m_stLeoDownInfo.nImage=lParam.nImage;
	m_stLeoDownInfo.nPID=lParam.nPID;
	m_stLeoDownInfo.nSocketLinkNum=lParam.nSocketLinkNum;
	m_stLeoDownInfo.nSvrState=lParam.nSvrState;
	m_stLeoDownInfo.strComment=lParam.strComment;
	m_stLeoDownInfo.strFilePath=lParam.strFilePath;
	m_stLeoDownInfo.strLocationUrl=lParam.strLocationUrl;
	m_stLeoDownInfo.strRegSubPath=lParam.strRegSubPath;
	m_stLeoDownInfo.the=this;
	m_stLeoDownInfo.stSocketType.strAcceptType="";

	m_stTimeBegin.nHH=0;
	m_stTimeBegin.nMM=0;
	m_stTimeBegin.nSS=0;
	m_stTimeBegin.nPID=lParam.nPID;
	m_stTimeBegin.nItem=lParam.nPID-1;
	m_stTimeBegin.nStartDownLoadNum=0;
	m_stTimeBegin.strRegPath=lParam.strRegSubPath;
	m_stTimeBegin.the=this;

	m_stTimeList.eHH=0;
	m_stTimeList.eMM=0;
	m_stTimeList.eSS=0;
	m_stTimeList.nPID=lParam.nPID;
	m_stTimeList.nItem=lParam.nPID-1;
	m_stTimeList.strRegPath=lParam.strRegSubPath;
	m_stTimeList.the=this;


	m_pMainFrame=pMainFrame;

}


void CHttpDownLoad::OnTimer(UINT nIDEvent) 
{
	//------nIDEvent为1时总计时器开起,为2时剩余计时器开起

	if(nIDEvent==1)
	{
		//计时器格式为hh:mm:ss(01:20:35)
		m_stTimeBegin.nSS=m_stTimeBegin.nSS+1;
		if(m_stTimeBegin.nSS>=59)
		{
			m_stTimeBegin.nSS=0;
			m_stTimeBegin.nMM+=1;
			if(m_stTimeBegin.nMM>=59)
			{
				m_stTimeBegin.nMM=0;
				m_stTimeBegin.nHH+=1;
			}
		}
		char sz[9];
		CalueTimertoSZ(m_stTimeBegin.nHH,m_stTimeBegin.nMM,m_stTimeBegin.nSS,sz); //排列计时器格式	
		CAULUETIMER_DATA stCaul;
		stCaul.nItem=m_stTimeBegin.nItem;
		int index=0;
		while(sz[index]!='\0')
		{
			stCaul.szTimer[index]=sz[index++];
		}
		stCaul.szTimer[index]='\0';

		::SendMessage(m_pMainFrame->m_hWnd,WM_CAULUETIMER,0,(LPARAM)&stCaul);
	}
	else if(nIDEvent==2)
	{
		int i=m_stTimeList.nPID;
		CString sTemp1;
		HKEY nhKeyRoot=HKEY_CURRENT_USER;
		CLRegistry mDownRegistry;
		sTemp1=m_stTimeList.strRegPath;
		mDownRegistry.Open(nhKeyRoot,(LPCTSTR)sTemp1);

		LIST_VIEWDATA pListViewData;

		//设置下载百分比和速率
		long FileLength=m_stHttpDlInfo.lFileLength;   //文件总长度
		long BreakLength0,BreakLength1,BreakLength2,BreakLength3,BreakLength4;  //5个子线程已经下载完的字节数
		long nCompletedSize;  //已经下载完的总字节数
		long dwPercent,dwCompleted;  //已完的百分比,已经下载完的总字节数
		long mmSpeed;  //每秒下载的字节数(xxkb/s)
		CString sTemp;

		if(FileLength>0L)
		{
			long LOffet=FileLength/5L;
			
			BreakLength0=m_stHttpDlInfo.BreakInfo0.BreakLength0;
			BreakLength1=m_stHttpDlInfo.BreakInfo1.BreakLength1;
			BreakLength1-=LOffet;  //这样计算可以得到子线程实际下载的字节数
			BreakLength2=m_stHttpDlInfo.BreakInfo2.BreakLength2;
			BreakLength2-=LOffet*2;
			BreakLength3=m_stHttpDlInfo.BreakInfo3.BreakLength3;
			BreakLength3-=LOffet*3;
			BreakLength4=m_stHttpDlInfo.BreakInfo4.BreakLength4;
			BreakLength4-=LOffet*4;

			nCompletedSize=BreakLength0+BreakLength1+BreakLength2+BreakLength3+BreakLength4;
			if(nCompletedSize<=FileLength && nCompletedSize>0)
			{
				//---计算已完成的百分比---
				dwCompleted=nCompletedSize;
				dwCompleted/=1024;
				dwPercent=FileLength;
				dwPercent/=1024;
				dwPercent=dwCompleted*100/dwPercent;

				sTemp="";
				sTemp.Format("%ld",dwPercent);
				sTemp+="%";
				mDownRegistry.Write("Percent",(LPCTSTR)sTemp);  //将百分比写入注册表
				mDownRegistry.Close();
				pListViewData.nItem=m_stTimeList.nItem;
				pListViewData.nColumn=3;
				pListViewData.lpcszText=(LPCTSTR)sTemp;
					//向CMainFrame发送WM_WRITELISTDATA消息,用于向列表框写入百分比数
				::SendMessage(m_pMainFrame->m_hWnd,WM_WRITELISTDATA,1,(LPARAM)&pListViewData);

				//---得到下载速率-----
					if(m_stHttpDlInfo.BreakInfo0.bSpeed && m_stHttpDlInfo.BreakInfo0.lSpeed0>0)
					{
						m_stHttpDlInfo.fSpeed=(m_stHttpDlInfo.BreakInfo0.lSpeed0);
						m_stHttpDlInfo.BreakInfo1.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo2.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo3.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo4.bSpeed=FALSE;
					}
					else if(m_stHttpDlInfo.BreakInfo1.bSpeed && m_stHttpDlInfo.BreakInfo1.lSpeed1>0)
					{
						m_stHttpDlInfo.fSpeed=(m_stHttpDlInfo.BreakInfo1.lSpeed1);
						m_stHttpDlInfo.BreakInfo0.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo2.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo3.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo4.bSpeed=FALSE;

					}
					else if(m_stHttpDlInfo.BreakInfo2.bSpeed && m_stHttpDlInfo.BreakInfo2.lSpeed2>0)
					{
						m_stHttpDlInfo.fSpeed=(m_stHttpDlInfo.BreakInfo2.lSpeed2);
						m_stHttpDlInfo.BreakInfo1.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo0.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo3.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo4.bSpeed=FALSE;
					}
					else if(m_stHttpDlInfo.BreakInfo3.bSpeed && m_stHttpDlInfo.BreakInfo3.lSpeed3>0)
					{
						m_stHttpDlInfo.fSpeed=(m_stHttpDlInfo.BreakInfo3.lSpeed3);
						m_stHttpDlInfo.BreakInfo1.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo2.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo0.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo4.bSpeed=FALSE;

					}
					else if(m_stHttpDlInfo.BreakInfo4.bSpeed && m_stHttpDlInfo.BreakInfo4.lSpeed4>0)
					{
						m_stHttpDlInfo.fSpeed=(m_stHttpDlInfo.BreakInfo4.lSpeed4);
						m_stHttpDlInfo.BreakInfo1.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo2.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo3.bSpeed=FALSE;
						m_stHttpDlInfo.BreakInfo0.bSpeed=FALSE;

					}
				mmSpeed=(long)(m_stHttpDlInfo.fSpeed);
				sTemp="";
				long lSpeed=mmSpeed;
				if(lSpeed>=1024L)
				{
					lSpeed/=1024L;
					sTemp="";
					sTemp.Format("%ldkb/s",lSpeed);
				}
				else
				{
					sTemp="";
					sTemp.Format("%ldb/s",lSpeed);
				}
			
				pListViewData.nItem=m_stTimeList.nItem;
				pListViewData.nColumn=4;
				pListViewData.lpcszText=(LPCTSTR)sTemp;
				::SendMessage(m_pMainFrame->m_hWnd,WM_WRITELISTDATA,0,(LPARAM)&pListViewData);


				//---计算剩余时间---
				long lbps=mmSpeed; //xxbyte/s
				if(lbps>0L)
				{
					long lComp=FileLength-nCompletedSize;         //lComp xxbps
					long elhh,elmm,elss;
					CalueSpareTimer(elhh,elmm,elss,lComp,lbps);
					m_stTimeList.eHH=(int)elhh;
					m_stTimeList.eMM=(int)elmm;
					m_stTimeList.eSS=(int)elss;
					sTemp="";
					char sz[9];
					CalueTimertoSZ(m_stTimeList.eHH,m_stTimeList.eMM,m_stTimeList.eSS,sz);	
					pListViewData.nItem=m_stTimeList.nItem;
					pListViewData.nColumn=6;
					pListViewData.lpcszText=sz;
					::SendMessage(m_pMainFrame->m_hWnd,WM_WRITELISTDATA,0,(LPARAM)&pListViewData);
					sTemp="";
				}
			}
		}
	}
	
	CWnd::OnTimer(nIDEvent);
}

UINT CHttpDownLoad::EndSelf()
{
	return 0;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -