資源簡介
基于libcurl/pthreads的多線程下載器,支持暫停、還原、中止,斷點續(xù)傳,支持HTTP,HTTPS協(xié)議鏈接,經(jīng)過上千萬級別的下載量測試,測試成功率99.5%,http://blog.csdn.net/u012532305/article/details/46709495也是這篇文章的源碼
參考后,移植到了 linux平臺

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?“CurlDownloader.h“
//下載臨時文件后綴
#define?DOWNLOAD_TMP_FILE_EXT?“.dltmp“
//檢測是否支持多線程分片傳輸?shù)淖址?br/>#define?RANGE_TEST_FLAG?“RangeTest“
//用于檢測是否支持多線程下載接收大小
#define?RANGE_TEST_RECV_SIZE?1024
CURLSH*?CurlDownloader::sharedns_handle?=?NULL;?
CurlDownConfig?CurlDownloader::g_curlDownCfg;
//線程信息
typedef?struct?_tThreadInfo{
unsigned?long?ulBegPos;??//下載起始位置
unsigned?long?ulBlockSize;?//本線程負(fù)責(zé)下載的數(shù)據(jù)大小
unsigned?long?ulRecvSize;?//本線程已經(jīng)接收到的數(shù)據(jù)大小
CURL*?pCurl;
pthread_t?thrdId;
int?iTryTimes;??//失敗已經(jīng)重試次數(shù)
_tThreadInfo()?//?默認(rèn)構(gòu)造函數(shù)
{
ulBegPos?=?0;
ulBlockSize?=?0;
ulRecvSize?=?0;
pCurl?=?NULL;
// thrdId.p?=?NULL;
// thrdId.x?=?0;
thrdId?=?0;
iTryTimes?=?0;
}
}ThreadInfo;
void?myprint(const?std::string&?strLog)
{
printf(“%s\n“?strLog.c_str());
}
void?FFlushEx(FILE*?stream)
{
int?duphandle;
fflush(stream);
duphandle?=?dup(fileno(stream));
close(duphandle);
}
void?ReplaceStr(std::string?&src?const?std::string?&target?const?std::string?&replacement)
{
if?(target?==?replacement)
{
return;
}
std::string::size_type?startpos?=?0;??
while?(startpos?!=?std::string::npos)??
{
startpos?=?src.find(target?startpos);??????
if(startpos?!=?std::string::npos)??
{
src.replace(startpos?target.length()?replacement);?
startpos?+=?replacement.length();
}
}
}
void?SplitStr(const?std::string&?s?const?std::string?&delim?std::vector?*ret)?
{??
if?(!ret)?
{
return;
}
ret->clear();
size_t?last?=?0;??
size_t?index=s.find_first_of(delimlast);??
while?(index!=std::string::npos)??
{??
ret->push_back(s.substr(lastindex-last));??
last=index+delim.length();??
index=s.find_first_of(delimlast);??
}??
if?(index-last>0)??
{??
ret->push_back(s.substr(lastindex-last));??
}??
}??
static?bool?checkConnect(const?char?*dst?int?cnt)???
{???
FILE?*stream;???????
char?recvBuf[16]?=?{0};???????
char?cmdBuf[256]?=?{0};?????????
if?(NULL?==?dst?||?cnt?<=?0)???????
{
return?false;???
}
sprintf(cmdBuf?“ping?%s?-c?%d?-i?0.2?|?grep?time=?|?wc?-l“?dst?cnt);???????
stream?=?popen(cmdBuf?“r“);???????
fread(recvBuf?sizeof(char)?sizeof(recvBuf)-1?stream);??????
pclose(stream);??
if?(atoi(recvBuf)?>?0)
{
return?true;???
}
return?false;
}???
unsigned?long?GetTickCount()
{
????struct?timespec?ts;
????clock_gettime(CLOCK_MONOTONIC?&ts);
????return?(ts.tv_sec?*?1000?+?ts.tv_nsec?/?1000000);
}
/**?
?*?功能:拷貝文件函數(shù)?
?*?參數(shù):?
?*??????sourceFileNameWithPath:源文件名(帶路徑)?
?*??????targetFileNameWithPath:目標(biāo)文件名(帶路徑)?
?*?返回值:?
?*??????true:?拷貝成功?
?*??????false:拷貝失敗?
?*?author:wangchangshuai?jlu?
?*/??
static?bool?copyFile(const?char?*sourceFileNameWithPath?const?char?*targetFileNameWithPath)??
{??
????FILE?*fpR?*fpW;??
????char?bu
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-03-08?18:00??include\
?????文件????????4709??2016-03-16?19:34??include\CurlDownloader.h
?????目錄???????????0??2016-03-08?18:00??src\
?????文件???????54534??2016-03-17?11:21??src\CurlDownloader.cpp
?????文件????????1350??2016-03-17?11:24??src\main.cpp
?????文件?????????674??2016-03-14?15:10??Makefile
- 上一篇:各種USB封裝,官方版本
- 下一篇:計算機(jī)組成原理實驗報告8份
評論
共有 條評論