Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional aria2 build. #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "libs/QArchive"]
path = libs/QArchive
url = https://github.com/antony-jr/QArchive
[submodule "libs/aria2"]
path = libs/aria2
url = https://github.com/aria2/aria2.git
114 changes: 114 additions & 0 deletions launcher/aria2client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "aria2client.h"
#include <aria2api.h>
#include <QCoreApplication>
#include <QDebug>

Aria2Client::Aria2Client(QObject *parent) : QObject(parent)
{
aria2::libraryInit();
session.reset(aria2::sessionNew(aria2::KeyVals(), config));
connect(this, &Aria2Client::downloadLoop,
this, &Aria2Client::downloadLoopBody, Qt::QueuedConnection);
}

Aria2Client::~Aria2Client()
{
aria2::sessionFinal(session.get());
aria2::libraryDeinit();
}

void Aria2Client::download(const QString &uri, const QString &checksum,
const QString &outFile)
{
aria2::KeyVals options {
{ "max-connection-per-server", "3" },
{ "file-allocation", "falloc" },
{ "check-integrity", "true" },
{ "out", outFile.toStdString() }
};

if (!checksum.isEmpty())
options.push_back({ "checksum",
QStringLiteral("sha1=%1").arg(checksum).toStdString() });

aria2::addUri(session.get(), nullptr, { uri.toStdString() }, options);

auto start = std::chrono::steady_clock::now();
emit downloadLoop(start);
}

void Aria2Client::downloadLoopBody(sys_time start)
{
if (aria2::run(session.get(), aria2::RUN_ONCE) < 1)
return;

auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>
(now - start).count();
if (elapsed >= 100) {
start = now;
std::vector<aria2::A2Gid> gids = aria2::getActiveDownload(session.get());
for (const auto &gid : gids) {
aria2::DownloadHandle *dh = aria2::getDownloadHandle(session.get(), gid);
if (!dh) continue;
emit progress(dh->getCompletedLength(), dh->getTotalLength(),
dh->getDownloadSpeed());
aria2::deleteDownloadHandle(dh);
}
}

// This shouldn't cause an infinite loop as long as the signal delivery is
// queued and not direct, which will force Qt to handle other events in its
// own event loop before getting to the aria2 event loop.
emit downloadLoop(start);
}

int Aria2Client::downloadCallback(aria2::Session *session,
aria2::DownloadEvent event, aria2::A2Gid gid,
[[gnu::unused]] void *userData)
{
switch (event) {
case aria2::EVENT_ON_DOWNLOAD_COMPLETE:
case aria2::EVENT_ON_BT_DOWNLOAD_COMPLETE:
emit finished(NO_ERROR);
break;
case aria2::EVENT_ON_DOWNLOAD_ERROR:
case aria2::EVENT_ON_DOWNLOAD_STOP:

emit finished(CONNECTION_FAILED);
break;
default:
qWarning() << "unknown aria2 event" << event;
return 0;
}

QDebug debug = qDebug().nospace().noquote();
debug << " ["
<< QString::fromStdString(aria2::gidToHex(gid))
<< "] ";

aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid);

if (!dh) {
qWarning() << "no aria2::DownloadHandle provided in event!";
return 0;
}

if (dh->getNumFiles() > 0) {
aria2::FileData f = dh->getFile(1);
// Path may be empty if the file name has not been determined yet.
if (f.path.empty() && !f.uris.empty()) {
debug << QString::fromStdString(f.uris[0].uri);
} else {
debug << QString::fromStdString(f.path);
}
}

// (Newline is printed on object destruction)
debug << " finished";

aria2::deleteDownloadHandle(dh);

return 0;
}

40 changes: 40 additions & 0 deletions launcher/aria2client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef ARIA2CLIENT_H
#define ARIA2CLIENT_H

#include <memory>

#include <QObject>

#include <aria2/aria2.h>

enum DownloadError {
NO_ERROR,
CONNECTION_FAILED
};

using sys_time = std::chrono::time_point<std::chrono::steady_clock,
std::chrono::nanoseconds>;

class Aria2Client : public QObject
{
Q_OBJECT
public:
explicit Aria2Client(QObject *parent = nullptr);
~Aria2Client();

void download(const QString &uri, const QString &checksum, const QString &outFile);
private:
std::unique_ptr<aria2::Session> session;
aria2::SessionConfig config;

int downloadCallback(aria2::Session* session, aria2::DownloadEvent event,
aria2::A2Gid gid, void* userData);
signals:
void downloadLoop(sys_time from);
void progress(uint64_t current, uint64_t max, uint64_t speed);
void finished(DownloadError error);
private slots:
void downloadLoopBody(sys_time from);
};

#endif // ARIA2CLIENT_H
10 changes: 8 additions & 2 deletions launcher/launcher.pro
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ SOURCES += \
updater.cpp \
runtimeerror.cpp \
installnotice.cpp \
installprogress.cpp
installprogress.cpp \
aria2client.cpp

HEADERS += \
mainwindow.h \
Expand All @@ -45,7 +46,8 @@ HEADERS += \
runtimeerror.h \
task.h \
installnotice.h \
installprogress.h
installprogress.h \
aria2client.h

FORMS += \
mainwindow.ui \
Expand All @@ -61,5 +63,9 @@ QT += concurrent

LIBS += ../libs/QArchive/libQArchive.a -larchive -lbz2 -llzma -llz4 -lz -lzstd -lnettle -lexpat -lxml2
win32:LIBS += -lbcrypt -liconv
LIBS += -L../libs/aria2/release/lib -laria2

INCLUDEPATH += ../libs/QArchive ../libs/QArchive/include
INCLUDEPATH += ../libs/aria2/src/includes/ ../libs/aria2/src/


1 change: 1 addition & 0 deletions launcher/res/install_notice.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ <h2>Attorney Online client</h2>
<pre>
<p>
Copyright (c) 2018 David Skoland, oldmud0
Aria2 Build fixes by ytobi and raidensnake
</p>
<p>
Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
13 changes: 8 additions & 5 deletions launcher/updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,15 @@ void Updater::taskDownload(QDir &installDir, const QUrl &url, const QString &has
// (Yeah, we're gonna do this crappy trick again...)
eventLoop.reset(new QEventLoop());
QArchive::DiskExtractor extractor(filename, installDir.path(), this, false);
// Note. The parmeters of the signal QArchive::DiskExtractor::error do not match the slot.
// QArchive::DiskExtractor::error emits short code only
QObject::connect(&extractor, &QArchive::DiskExtractor::error,
[&](short code, const QString &file) {
error = true;
errorMsg = file;
errorCode = code;
qCritical() << "Error extracting" << file << "- code" << code;
[&](short code) {
const QString file("Unknown file");
error = true;
errorMsg = file;
errorCode = code;
qCritical() << "Error extracting" << file << "- code" << code;
emit eventLoop->quit();
});
QObject::connect(&extractor, &QArchive::DiskExtractor::progress,
Expand Down
1 change: 1 addition & 0 deletions libs/aria2
Submodule aria2 added at 15cad9