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

📄 geohttpdown.cpp

📁 类似flashget下载工具代码 本来想在完善了上传的
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// GeoHttpDown.cpp : implementation file
//

#include "stdafx.h"

#include "MyInsaneBT.h"
#include "GeoHttpDown.h"
#include "HttpSocket.h"
#include "MainFrm.h"
#include "BreakPointDown.h"
#include "SureDlg.h"
#include "Buffer.h"
#include "SetupRegister.h"
//#include <afxmt.h>
//CCriticalSection lock;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//void TestProxy(CString proxyaddr,int port,const char* sendchar);

void FormatTime(int ihh,int imm,int iss,char sz[]);
CString GetHostName(const char* url,const char* strObject);
CString getRefererName(const char* url);
//void WriteDownLoadLog(int item,int subitem,CString log);
void GetDownLoadLog(int item,int subitem,CString& log);
CFile* CreateTempFile(CString sPath,int TID);
/////////////////////////////////////////////////////////////////////////////
long  CGeoHttpDown::m_ID = 0;
HANDLE CGeoHttpDown::m_handle;
// CGeoHttpDown
AFTrace CGeoHttpDown::t("CGeoHttpDown",AFTrace::console|AFTrace::file);
CGeoHttpDown::CGeoHttpDown()
{  
   //创建一个事件,无信号状态,人工复位
   m_handle = CreateEvent(NULL, TRUE, FALSE, NULL);
   m_DownInfo = new DOWN_INFO();
   //m_ID++;
}

CGeoHttpDown::~CGeoHttpDown()
{
	m_ID--;
	if(m_DownInfo!=NULL) delete m_DownInfo;
	::CloseHandle(m_handle);
}

CGeoHttpDown::CGeoHttpDown(PVOID lParam)

{  
	   m_DownInfo = new DOWN_INFO();
	   m_handle = CreateEvent(NULL, TRUE, FALSE, NULL);
	   DOWN_INFO* Info = (LPDOWN_INFO)lParam;
	   m_DownInfo->bIsFileDown = Info->bIsFileDown; //是否下载完成
	   m_DownInfo->strLocationUrl = Info->strLocationUrl;//URL
	   m_DownInfo->priLocationUrl = Info->priLocationUrl;
	   m_DownInfo->pMainFrame = Info->pMainFrame; //主框架
	   m_DownInfo->strFilePath = Info->strFilePath;//文件保存路径
	   m_DownInfo->nSocketLinkNum = Info->nSocketLinkNum;//服务器测试次数
	   m_DownInfo->lFileLength = 0; //文件大小
	   m_DownInfo->bIsFileDown = false; //断点下载
	   m_DownInfo->nBreakRev = 0;
	  
	   //让PID 等于 list行号
	   //m_DownInfo->nPID = m_ID; //PID
       CMyInsaneBTView* pView = NULL;
	   CMainFrame* pFrmMain = (CMainFrame*)m_DownInfo->pMainFrame;
	   pView = (CMyInsaneBTView*)pFrmMain->GetActiveView();
       m_DownInfo->nPID =  pView->m_downlisctr.GetItemCount();


	   m_usetime.nPID = m_DownInfo->nPID; 
	   m_usetime.nHH = 0;   //下载说用时间
	   m_usetime.nMM = 0;
	   m_usetime.nSS = 0;
   


	   CMainFrame* pFrm = (CMainFrame*)Info->pMainFrame;


	   CMainFrame* pMainFrm = (CMainFrame*)m_DownInfo->pMainFrame;
	   m_pView = (CMyInsaneBTView*)pMainFrm->GetActiveView();

	   m_stHttpDlInfo.CFG = new CBreakPointDown;
	   m_stHttpDlInfo.m_HandleStop = CreateEvent(NULL, TRUE, TRUE, NULL);
	   m_stHttpDlInfo.m_Stop =true;
	   m_stHttpDlInfo.m_HTTPDOWNCLS = this;
	   pFrm->m_http_info.push_back(&m_stHttpDlInfo);
	   //m_stHttpDlInfo.sGUID = CreateGUID();
	   //m_ID++; //线程ID
}

CGeoHttpDown::CGeoHttpDown(CString Cfg_Path)
{
    m_stHttpDlInfo.CFG = new CBreakPointDown;
    m_stHttpDlInfo.CFG->open_cfg_file(Cfg_Path.GetBuffer(1));

	m_stHttpDlInfo.CFG->m_filepath = Cfg_Path;
    create_DownInfo(m_stHttpDlInfo.CFG->GetCFGMap());

}
BEGIN_MESSAGE_MAP(CGeoHttpDown, CWnd)
	//{{AFX_MSG_MAP(CGeoHttpDown)
	    ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_STARTCLOCK,StartClock)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGeoHttpDown message handlers


//DWORD CALLBACK CGeoHttpDown::StartDownLoad(LPVOID lParam)
UINT StartDownLoad(LPVOID lParam)
{     
      //一个CGeoHttpDown实例保存和管理一个下载任务
	  CGeoHttpDown* phttp = (CGeoHttpDown*)lParam;
	  ::WaitForSingleObject(phttp->m_stHttpDlInfo.m_Break_handle,INFINITE);

      int test = 0;
	  long fileLength; //文件长度
      CString sState;
	  BOOL Success = FALSE;
	  DWORD Wait;
	  HANDLE hThreadTest;
	  //每个线程下载长度
	  long agaveLength = 0;
	  //读写注册表
	  //测试下载服务器,并获得文件长度
	  CWinThread *MulpWinThread[5];
	  DOWN_INFO Mulm_DownInfo[5]; 
	  CString Name;
      CString FilePath;
     
      CMainFrame* pMainFrm = (CMainFrame*)phttp->m_DownInfo->pMainFrame;
	 
      CMyInsaneBTView* pView = NULL;
      pView = (CMyInsaneBTView*)pMainFrm->GetActiveView();
	  ASSERT(pView!=NULL);
     
	
	  
      while(test<phttp->m_DownInfo->nSocketLinkNum)
	  {
        
   
	     AfxBeginThread(TestServerState,(LPVOID)phttp);
	     //等待测试线程
		 Wait = ::WaitForSingleObject(CGeoHttpDown::m_handle,4000);
	     //Wait = ::WaitForSingleObject(CGeoHttpDown::m_handle,INFINITE);
         sState.Format("%d",phttp->m_DownInfo->nSvrState);
	     sState = sState.Left(1);
		 //获得文件大小
	     fileLength = phttp->m_DownInfo->lFileLength;
	    if(Wait==WAIT_TIMEOUT)
		{
		   TerminateThread(hThreadTest,0);
		   
		   if(sState=="2" && fileLength>0) //测试成功
		   {   
			   CGeoHttpDown::t.out(phttp->m_DownInfo->strLocationUrl);
		       Success = TRUE;
			   break;
		   }
	       else if(sState=="5")
		           break;
		
		
		}
		else if(Wait==WAIT_OBJECT_0)
		{
		     if(sState=="2" && fileLength>0) //测试成功
			 {   
				 CGeoHttpDown::t.out(phttp->m_DownInfo->strLocationUrl);
				 Name = phttp->GetFileName(phttp->m_DownInfo->strLocationUrl);
				 Success = TRUE;
			     //判断是否为新任务 0新任务 1断点下载
		     
			  //判断文件是否已经存在
			 CString sPath =  phttp->m_DownInfo->strFilePath +_T("\\")+Name;
	         if(!FindFile(sPath.GetBuffer(1)))
			 {
	             return 0;
			 }	 
			if(phttp->m_DownInfo->nBreakRev!=1)
			 {
			    //CString sPath =  phttp->m_DownInfo->strFilePath +_T("\\")+Name;
			    phttp->m_stHttpDlInfo.m_Filename = Name;
				//创建CFG文件
			    phttp->m_stHttpDlInfo.CFG->create_cfg_file(sPath.GetBuffer(1),Name.GetBuffer(1),phttp->m_DownInfo->priLocationUrl.GetBuffer(1));
			     
			}
			
			   
			 
			
             if(!phttp->m_DownInfo->nBreakRev)
			 {
				  pView->m_downlisctr.InsertItem(phttp->m_DownInfo->nPID,Name);
				 //文件名
				 
		         //状态
                 WriteDownLoadLog(phttp->m_DownInfo->nPID,1,"正在中..");
 				 //文件大小
				 CString sSize;
				 double MK = (float)fileLength/(1024*1024);
				 //写文件大小信息
				 CString sFilesize;
				 sFilesize.Format("%d",fileLength);
                 //写文件大小信息
				 phttp->m_stHttpDlInfo.CFG->CFG_SetInt("FILESIZE",fileLength);
				 
				 LPDownLoadIndo load = pMainFrm->GetDownLoadInfo(phttp->m_DownInfo->nPID);
				 //load->filesize = fileLength;
				 //保存文件大小
				 sSize.Format("%.2f",MK);
                 WriteDownLoadLog(phttp->m_DownInfo->nPID,3,sSize + _T("M"));
				 
				 //下在完成率
				 WriteDownLoadLog(phttp->m_DownInfo->nPID,2,"0");
				 Success = TRUE;
				 }
                 break;
			 }
	         else if(sState=="5")
		          break;
		
		}

        test++;
	   }
       
	  
      //开始下载
	  if(Success)
	  {
	    
        //URL
        WriteDownLoadLog(phttp->m_DownInfo->nPID,7,phttp->m_DownInfo->strLocationUrl);
	    for(int i=0;i<5;i++)
		{
		   MulpWinThread[i]=NULL;
		}
	    
		//取整
		long modlength = phttp->m_DownInfo->lFileLength % 10; 
	    long nLength = (phttp->m_DownInfo->lFileLength-modlength)/5;
		long length = phttp->m_DownInfo->lFileLength;
	    //test 
	    //phttp->m_DownInfo->nBreakRev = 0;
		
		//新下载,创建文件
		if(!phttp->m_DownInfo->nBreakRev)
		{   
			//未下载完成文件,扩展名为*.mtd
			//下载信息保存在*.cfg文件中
			CString sPath =  phttp->m_DownInfo->strFilePath +_T("\\")+Name+".utd";
		  	CFile pFile;
		    if(!pFile.Open((LPCTSTR)(sPath),
				CFile::modeCreate|CFile::modeWrite|CFile::shareDenyNone))
			{
			    return -1;
			}
		    pFile.SetLength(length); //设置新文件长度
		    pFile.Close();
		    phttp->m_DownInfo->strFilePath = sPath;
		    FilePath = sPath;
		    //保存文件名字,和路径信息
            phttp->m_stHttpDlInfo.CFG->CFG_SetString("FULLPATH",sPath);
            phttp->m_stHttpDlInfo.CFG->CFG_SetString("FILENAME",Name+".utd");
			//线程个数
			phttp->m_stHttpDlInfo.CFG->CFG_SetInt("THREADNUM",5);
			//PID
            phttp->m_stHttpDlInfo.CFG->CFG_SetString("PID",phttp->m_DownInfo->nPID);
			//URL
            phttp->m_stHttpDlInfo.CFG->CFG_SetString("URL",phttp->m_DownInfo->strLocationUrl);
		    phttp->m_stHttpDlInfo.CFG->Flush();
		}
      
	  //新下载,设置线程下载指针
      if(!phttp->m_DownInfo->nBreakRev)
	  {
		//设置每个线程下载文件长度    
		Mulm_DownInfo[0].lFromeByte=0;
		Mulm_DownInfo[0].lToByte=nLength;
		Mulm_DownInfo[0].lBreakPointByte=0;
        
		Mulm_DownInfo[1].lFromeByte=nLength;
		Mulm_DownInfo[1].lToByte=nLength*2;
		Mulm_DownInfo[1].lBreakPointByte=nLength;

		Mulm_DownInfo[2].lFromeByte=nLength*2;
		Mulm_DownInfo[2].lToByte=nLength*3;
		Mulm_DownInfo[2].lBreakPointByte=nLength*2;

		Mulm_DownInfo[3].lFromeByte=nLength*3;
		Mulm_DownInfo[3].lToByte=nLength*4;
		Mulm_DownInfo[3].lBreakPointByte=nLength*3;

		Mulm_DownInfo[4].lFromeByte=nLength*4;
		Mulm_DownInfo[4].lToByte=length;
		Mulm_DownInfo[4].lBreakPointByte=nLength*4;
		 
		 
		 
	  }
	  else
	  {
	    //设置每个线程下载文件长度  断点下载  
		Mulm_DownInfo[0].lFromeByte=phttp->m_stHttpDlInfo.BreakInfo0.lFromeByte;
		Mulm_DownInfo[0].lToByte=phttp->m_stHttpDlInfo.BreakInfo0.lToByte;
		Mulm_DownInfo[0].lBreakPointByte=phttp->m_stHttpDlInfo.BreakInfo0.BreakLength0;
	  
	    Mulm_DownInfo[1].lFromeByte=phttp->m_stHttpDlInfo.BreakInfo1.lFromeByte;
		Mulm_DownInfo[1].lToByte=phttp->m_stHttpDlInfo.BreakInfo1.lToByte;
		Mulm_DownInfo[1].lBreakPointByte=phttp->m_stHttpDlInfo.BreakInfo1.BreakLength1;

	    Mulm_DownInfo[2].lFromeByte=phttp->m_stHttpDlInfo.BreakInfo2.lFromeByte;
		Mulm_DownInfo[2].lToByte=phttp->m_stHttpDlInfo.BreakInfo2.lToByte;
		Mulm_DownInfo[2].lBreakPointByte=phttp->m_stHttpDlInfo.BreakInfo2.BreakLength2;
        
        Mulm_DownInfo[3].lFromeByte=phttp->m_stHttpDlInfo.BreakInfo3.lFromeByte;
		Mulm_DownInfo[3].lToByte=phttp->m_stHttpDlInfo.BreakInfo3.lToByte;
		Mulm_DownInfo[3].lBreakPointByte=phttp->m_stHttpDlInfo.BreakInfo3.BreakLength3;

        Mulm_DownInfo[4].lFromeByte=phttp->m_stHttpDlInfo.BreakInfo4.lFromeByte;
		Mulm_DownInfo[4].lToByte=phttp->m_stHttpDlInfo.BreakInfo4.lToByte;
		Mulm_DownInfo[4].lBreakPointByte=phttp->m_stHttpDlInfo.BreakInfo4.BreakLength4;


	  }
  for(i=0;i<5;i++)
	{
		Mulm_DownInfo[i].fPercent=0.0f;
		Mulm_DownInfo[i].fSpeed=0.0f;
		Mulm_DownInfo[i].lFileLength=length;
		Mulm_DownInfo[i].nSvrState=2;
		Mulm_DownInfo[i].pMainFrame=phttp->m_DownInfo->pMainFrame;
		Mulm_DownInfo[i].strFilePath=phttp->m_DownInfo->strFilePath ;
		Mulm_DownInfo[i].strLocationUrl=phttp->m_DownInfo->strLocationUrl;
		Mulm_DownInfo[i].nSocketLinkNum=phttp->m_DownInfo->nSocketLinkNum;
		Mulm_DownInfo[i].nPID=phttp->m_DownInfo->nPID; //组ID
		Mulm_DownInfo[i].bIsFileDown=TRUE; //为真,线程正在下载文件
		Mulm_DownInfo[i].strRegSubPath=phttp->m_DownInfo->strRegSubPath;
		Mulm_DownInfo[i].nCID=i; //确定是第几个线程
		Mulm_DownInfo[i].nBreakRev=phttp->m_DownInfo->nBreakRev;
		Mulm_DownInfo[i].stSocketType.strAcceptType=phttp->m_DownInfo->stSocketType.strAcceptType;
		Mulm_DownInfo[i].the=(CWnd*)phttp;
		
		
   }

	}//if
	  
     else
	 {
	   AfxMessageBox("服务器连接失败");
	   return -1;
	 }
	  

	        
	  CMainFrame* pFrm = (CMainFrame*)phttp->m_DownInfo->pMainFrame;
	  pFrm->m_Logworkspace.SendMessage(WM_CREATETAB,0,0);
	  ::WaitForSingleObject(pFrm->m_Logworkspace.m_Event,INFINITE);
      
      if(!phttp->m_DownInfo->nBreakRev)
      {   
		  //打开第一个计时器,用于计算下载时间
		  phttp->BeginTimer(1);
	      //打开第二个计时器,用于计算剩余时间
	      phttp->BeginTimer(2);
	  }


	  //创建下载线程
	  AfxBeginThread(HttpDown,(LPVOID)&Mulm_DownInfo[0]);
	  AfxBeginThread(HttpDown,(LPVOID)&Mulm_DownInfo[1]);
      AfxBeginThread(HttpDown,(LPVOID)&Mulm_DownInfo[2]);
	  AfxBeginThread(HttpDown,(LPVOID)&Mulm_DownInfo[3]);
	  AfxBeginThread(HttpDown,(LPVOID)&Mulm_DownInfo[4]);



      HANDLE WaitDown;
	  WaitDown = CreateEvent(NULL, TRUE, FALSE, NULL);
   
	  //等待下载线程
	 while(Mulm_DownInfo[0].bIsFileDown || Mulm_DownInfo[1].bIsFileDown ||
		  Mulm_DownInfo[2].bIsFileDown || Mulm_DownInfo[3].bIsFileDown ||
		  Mulm_DownInfo[4].bIsFileDown)
	 {
	
        ::WaitForSingleObject(WaitDown,3000);         

	 }
    
	//判断是否播放音乐
	 if(CSetupRegister::Instance()->m_music)
        pFrm->SendMessage(WM_PALYSOUND,0,0);
    //停止1秒,让定时器写完下载信息
    Sleep(1000);
    
    //修改文件扩展名字,删除CFG文件
    if(!phttp->m_DownInfo->nBreakRev)
	{   
        phttp->StopTimer(1);
        phttp->StopTimer(2);
		ChangeFileName(FilePath);
		CFile::Remove(phttp->m_stHttpDlInfo.CFG->m_filepath);
		phttp->m_stHttpDlInfo.CFG->Delete_Record(phttp->m_stHttpDlInfo.CFG->m_filepath.GetBuffer(1));
    }
	else
    {
		ChangeFileName(phttp->m_DownInfo->strFilePath);
		CFile::Remove(phttp->m_stHttpDlInfo.CFG->m_filepath);
		phttp->StopTimer(1);
        phttp->StopTimer(2);
		phttp->m_stHttpDlInfo.CFG->Delete_Record(phttp->m_stHttpDlInfo.CFG->m_filepath.GetBuffer(1));
	    
	    CString sTemp="100";
	    sTemp+="%";
	    WriteDownLoadLog(phttp->m_DownInfo->nPID,2,sTemp);
			  
	}
   
   //发送下载提示信息
   if(CSetupRegister::Instance()->m_tiptool)
   {
	   LPDownLoadFinish DownFinish = new DownLoadFinish;
       DownFinish->caption = "易下载提醒您";
       DownFinish->info = Name + "下载完毕";
       pMainFrm->SendMessage(WM_DOWNLOAD_FINISH,0,(LPARAM)DownFinish);
   }
   return 0;
	 
}

void CGeoHttpDown::BeginDownLoad()
{}

UINT TestServerState(LPVOID lParam)
{ 
   
	CGeoHttpDown* the = (CGeoHttpDown*)lParam;
    
	CMainFrame* pFrm = (CMainFrame*)the->m_DownInfo->pMainFrame;
    //获得代理信息
    LPProxyInfo Proxy = pFrm->m_default_proxy;

	bool is_proxy = false;
	if(Proxy!=NULL)
		is_proxy = true; //使用代理下载


    
	CHttpSocket HttpSocket;
	CString strServer,strObject;
	DWORD dwServiceType;
	unsigned short nPort;
	long nLength;
	const char *pRequestHeader = NULL;
	char *pResponseHeader=NULL;
	char *pAcceptType=NULL;
	static BOOL first = FALSE;
	int testnum = 1;
	CString url;
	char* curl;
	//int nTimeout=the->m_stLeoDownInfo.stSocketType.nTimeOut;
	//解析地址
 
    
	url = the->m_DownInfo->strLocationUrl;
	curl =new char[url.GetLength()+1];
	strcpy(curl,url.GetBuffer(1));
	url.ReleaseBuffer();
	curl[url.GetLength()+1] ='\0';


    AfxParseURL((LPCTSTR)(the->m_DownInfo->strLocationUrl),dwServiceType,strServer,strObject,nPort);
	//保存服务器名 www.xxx.xx 172.17.0.2
    the->m_DownInfo->stSocketType.strServer=strServer;  
    

⌨️ 快捷键说明

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