-
Notifications
You must be signed in to change notification settings - Fork 2
/
DownLoadFile.cpp
131 lines (110 loc) · 2.9 KB
/
DownLoadFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "DownLoadFile.h"
#include "GameBaseInfo.h"
#include "cocos2d.h"
USING_NS_CC;
#include<stdio.h>
#ifdef _WIN32
#include <io.h>
#else
#include<unistd.h>
#endif
DownLoadFile::DownLoadFile()
{
}
DownLoadFile::~DownLoadFile()
{
}
DownLoadFile* DownLoadFile::instance()
{
static DownLoadFile *pInstance = NULL;
if (pInstance==NULL)
{
pInstance = new DownLoadFile();
}
return pInstance;
}
void DownLoadFile::downLoadFile(string url, string fileFullName)
{
m_fileFullName = fileFullName;
if (!dlFile(url))
{
//下载失败
remove(getDownFileTempName(m_fileFullName).c_str());
remove(m_fileFullName.c_str());
fireEvent(0, gameDownLoadFailed, url.c_str());
}
else
{
fireEvent(0, gameDownLoadFinish, url.c_str());
}
}
int saveFile(void *buffer, size_t size, size_t nmemb, void *stream);
int onPercent(void* ptr, double fileSize, double downloadSize, double rUlTotal, double rUlNow);
bool DownLoadFile::dlFile(string url)
{
CURLcode return_code;
return_code = curl_global_init(CURL_GLOBAL_ALL);
if (CURLE_OK != return_code)
{
printf("init libcurl failed.");
return false;
}
// 获取easy handle
CURL *easy_handle = curl_easy_init();
if (NULL == easy_handle)
{
printf( "get a easy handle failed.") ;
curl_global_cleanup();
return false;
}
FILE *fp = fopen(getDownFileTempName(m_fileFullName).c_str(), "wb");
//设置下载URL
return_code = curl_easy_setopt(easy_handle,CURLOPT_URL, url.c_str());
//设置重定向URL
return_code = curl_easy_setopt(easy_handle,CURLOPT_FOLLOWLOCATION, url.c_str());
//设置回调函数
return_code = curl_easy_setopt(easy_handle,CURLOPT_WRITEFUNCTION, saveFile);
//设置回调数据
return_code = curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, fp);
if (return_code != CURLE_OK)
{
return false;
}
//curl的进度条声明
curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, 0);
//回调进度条函数
curl_easy_setopt(easy_handle, CURLOPT_PROGRESSFUNCTION, onPercent);
// 执行数据请求
if (curl_easy_perform(easy_handle) != CURLE_OK) {
return false;
}
// 释放资源
fclose(fp);
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
rename(getDownFileTempName(m_fileFullName).c_str(), m_fileFullName.c_str());
return true;
}
void DownLoadFile::onProgressbar( double maxsize, double downloadsize )
{
structDownLoad pDownLoad;
pDownLoad.cur = downloadsize;
pDownLoad.max = maxsize;
pDownLoad.fileFullName = m_fileFullName;
fireEvent(0, gameDownLoading, (void *)&pDownLoad);
}
int saveFile(void *buffer, size_t size, size_t nmemb, void *stream)
{
FILE *fp = (FILE *)stream;
size_t return_size = fwrite(buffer, size, nmemb, fp);
return return_size;
}
int onPercent(void* ptr, double fileSize, double downloadSize, double rUlTotal, double rUlNow)
{
DownLoadFile::instance()->onProgressbar(fileSize , downloadSize);
return 0;
}
string DownLoadFile::getDownFileTempName(string fileName)
{
return fileName+".tmp";
}