-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patharia2_c.cc
More file actions
281 lines (242 loc) · 7.16 KB
/
aria2_c.cc
File metadata and controls
281 lines (242 loc) · 7.16 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "aria2_c.h"
#include "_cgo_export.h"
#include <aria2/aria2.h>
#include <iostream>
#include <sstream>
#include <string.h>
std::vector<std::string> splitBySemicolon(std::string in) {
std::string val;
std::stringstream is(in);
std::vector<std::string> out;
while (getline(is, val, ';')) {
out.push_back(val);
}
return out;
}
const char *toCStr(std::string in) {
int len = in.length();
char *val = new char[len + 1];
memcpy(val, in.data(), len);
val[len] = '\0';
return val;
}
const char *getFileName(std::string dir, std::string path) {
if (path.find(dir, 0) == std::string::npos) {
return toCStr(path);
}
int index = dir.size();
std::string name = path.substr(index + 1);
return toCStr(name);
}
const char *toAria2goOptions(aria2::KeyVals options) {
std::vector<std::pair<std::string, std::string>>::iterator it;
std::string cOptions;
for (it = options.begin(); it != options.end(); it++) {
std::pair<std::string, std::string> p = *it;
cOptions += p.first + ";" + p.second + ";";
}
return toCStr(cOptions);
}
aria2::KeyVals toAria2Options(const char *options) {
aria2::KeyVals aria2Options;
if (options == nullptr) {
return aria2Options;
}
std::vector<std::string> o = splitBySemicolon(std::string(options));
/* key and val should be pair */
if (o.size() % 2 != 0) {
return aria2Options;
}
for (int i = 0; i < o.size(); i += 2) {
std::string key = o[i];
std::string val = o[i + 1];
aria2Options.push_back(std::make_pair(key, val));
}
return aria2Options;
}
struct FileInfo *parseFileData(aria2::DownloadHandle *dh) {
std::string dir = dh->getDir();
std::vector<aria2::FileData> files = dh->getFiles();
int numFiles = dh->getNumFiles();
struct FileInfo *allFiles = new FileInfo[numFiles];
for (int i = 0; i < numFiles; i++) {
aria2::FileData file = files[i];
struct FileInfo *fi = new FileInfo();
fi->index = file.index;
fi->name = getFileName(dir, file.path);
fi->length = file.length;
fi->completedLength = file.completedLength;
fi->selected = file.selected;
allFiles[i] = *fi;
}
return allFiles;
}
/* retrieve all BitTorrent meta information */
struct MetaInfo *parseMetaInfo(aria2::BtMetaInfoData btMetaInfo) {
struct MetaInfo *mi = new MetaInfo();
mi->name = toCStr(btMetaInfo.name);
mi->comment = toCStr(btMetaInfo.comment);
mi->creationUnix = btMetaInfo.creationDate;
std::vector<std::vector<std::string>> announceList = btMetaInfo.announceList;
std::vector<std::vector<std::string>>::iterator it;
std::string cAnnounceList;
for (it = announceList.begin(); it != announceList.end(); it++) {
std::vector<std::string>::iterator cit;
std::vector<std::string> childList = *it;
for (cit = childList.begin(); cit != childList.end(); cit++) {
cAnnounceList += *cit;
if (it != announceList.end() - 1 || cit != childList.end() - 1) {
cAnnounceList += ";";
}
}
}
mi->announceList = toCStr(cAnnounceList);
return mi;
}
/**
* Global aria2 session.
*/
aria2::Session *session;
/**
* Global aria2go go pointer.
*/
uint64_t aria2goPointer;
/**
* Download event callback for aria2.
*/
int downloadEventCallback(aria2::Session *session, aria2::DownloadEvent event,
const aria2::A2Gid gid, void *userData) {
notifyEvent(aria2goPointer, gid, event);
return 0;
}
/**
* Initial aria2 library.
*/
int init(uint64_t pointer, const char *options) {
aria2goPointer = pointer;
int ret = aria2::libraryInit();
aria2::SessionConfig config;
config.keepRunning = true;
/* do not use signal handler, cause c will block go */
config.useSignalHandler = false;
config.downloadEventCallback = downloadEventCallback;
session = aria2::sessionNew(toAria2Options(options), config);
return ret;
}
/**
* Shutdown schedules. This will cause run finished.
*/
int shutdownSchedules(bool force) { return aria2::shutdown(session, force); }
/**
* Deinit aria2 library, this must be invoked when process exit(signal handler
* is not used), so aria2 will be able to save session config.
*/
int deinit() {
int ret = aria2::sessionFinal(session);
session = nullptr;
aria2::libraryDeinit();
return ret;
}
/**
* Adds new HTTP(S)/FTP/BitTorrent Magnet URI. See `addUri` in aria2.
*
* @param uri uri to add
*/
uint64_t addUri(char *uri, const char *options) {
std::vector<std::string> uris = {uri};
aria2::A2Gid gid;
int ret = aria2::addUri(session, &gid, uris, toAria2Options(options));
if (ret < 0) {
return 0;
}
return gid;
}
/**
* Add bit torrent file. See `addTorrent` in aria2.
*/
uint64_t addTorrent(char *fp, const char *options) {
aria2::A2Gid gid;
int ret = aria2::addTorrent(session, &gid, fp, toAria2Options(options));
if (ret < 0) {
return 0;
}
return gid;
}
/**
* Change aria2 options. See `changeOption` in aria2.
*/
bool changeOptions(uint64_t gid, const char *options) {
return aria2::changeOption(session, gid, toAria2Options(options)) == 0;
}
/**
* Get options for given gid. see `getOptions` in aria2.
*/
const char *getOptions(uint64_t gid) {
aria2::DownloadHandle *dh = aria2::getDownloadHandle(session, gid);
if (!dh) {
return nullptr;
}
return toAria2goOptions(dh->getOptions());
}
/**
* Change global options. See `changeGlobalOption` in aria2.
*/
bool changeGlobalOptions(const char *options) {
return aria2::changeGlobalOption(session, toAria2Options(options));
}
/**
* Get global options. see `getGlobalOptions` in aria2.
*/
const char *getGlobalOptions() {
aria2::KeyVals options = aria2::getGlobalOptions(session);
return toAria2goOptions(options);
}
/**
* Performs event polling and actions for them.
*/
int run() { return aria2::run(session, aria2::RUN_DEFAULT); }
/**
* Pause an active download with given gid. This will mark the download to
* `DOWNLOAD_PAUSED`. See `resume`.
*/
bool pause(uint64_t gid) { return aria2::pauseDownload(session, gid) == 0; }
/**
* Resume a paused download with given gid. See `pause`.
*/
bool resume(uint64_t gid) { return aria2::unpauseDownload(session, gid) == 0; }
/**
* Remove a download in queue with given gid. This will stop downloading and
* seeding(for torrent).
*/
bool removeDownload(uint64_t gid) {
return aria2::removeDownload(session, gid) == 0;
}
/**
* Get download information for current download with given gid.
*/
struct DownloadInfo *getDownloadInfo(uint64_t gid) {
if (session == nullptr) {
return nullptr;
}
aria2::DownloadHandle *dh = aria2::getDownloadHandle(session, gid);
if (!dh) {
return nullptr;
}
struct DownloadInfo *di = new DownloadInfo();
di->status = dh->getStatus();
di->totalLength = dh->getTotalLength();
di->bytesCompleted = dh->getCompletedLength();
di->uploadLength = dh->getUploadLength();
di->downloadSpeed = dh->getDownloadSpeed();
di->uploadSpeed = dh->getUploadSpeed();
di->pieceLength = dh->getPieceLength();
di->numPieces = dh->getNumPieces();
di->connections = dh->getConnections();
di->numFiles = dh->getNumFiles();
di->infoHash = toCStr(dh->getInfoHash());
di->metaInfo = parseMetaInfo(dh->getBtMetaInfo());
di->files = parseFileData(dh);
/* delete download handle */
aria2::deleteDownloadHandle(dh);
return di;
}