2019-12-05 08:32:33 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "storage/download_manager_mtproto.h"
|
|
|
|
|
|
|
|
#include "mtproto/facade.h"
|
|
|
|
#include "mtproto/mtproto_auth_key.h"
|
|
|
|
#include "mtproto/mtproto_rpc_sender.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "base/openssl_help.h"
|
|
|
|
|
|
|
|
namespace Storage {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kKillSessionTimeout = 15 * crl::time(1000);
|
2019-12-05 13:38:28 +00:00
|
|
|
constexpr auto kStartWaitedInSession = 4 * kDownloadPartSize;
|
|
|
|
constexpr auto kMaxWaitedInSession = 16 * kDownloadPartSize;
|
2019-12-05 08:32:33 +00:00
|
|
|
constexpr auto kStartSessionsCount = 1;
|
|
|
|
constexpr auto kMaxSessionsCount = 8;
|
2019-12-05 11:46:28 +00:00
|
|
|
constexpr auto kMaxTrackedSessionRemoves = 64;
|
|
|
|
constexpr auto kRetryAddSessionTimeout = 8 * crl::time(1000);
|
|
|
|
constexpr auto kRetryAddSessionSuccesses = 3;
|
|
|
|
constexpr auto kMaxTrackedSuccesses = kRetryAddSessionSuccesses
|
|
|
|
* kMaxTrackedSessionRemoves;
|
2019-12-06 07:05:38 +00:00
|
|
|
constexpr auto kRemoveSessionAfterTimeouts = 4;
|
2019-12-05 08:32:33 +00:00
|
|
|
constexpr auto kResetDownloadPrioritiesTimeout = crl::time(200);
|
2019-12-06 07:05:38 +00:00
|
|
|
constexpr auto kBadRequestDurationThreshold = 8 * crl::time(1000);
|
2019-12-05 08:32:33 +00:00
|
|
|
|
2019-12-05 13:38:28 +00:00
|
|
|
// Each (session remove by timeouts) we wait for time:
|
2019-12-05 11:46:28 +00:00
|
|
|
// kRetryAddSessionTimeout * max(removesCount, kMaxTrackedSessionRemoves)
|
2019-12-05 13:38:28 +00:00
|
|
|
// and for successes in all remaining sessions:
|
2019-12-05 11:46:28 +00:00
|
|
|
// kRetryAddSessionSuccesses * max(removesCount, kMaxTrackedSessionRemoves)
|
|
|
|
|
2019-12-05 08:32:33 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-12-23 09:37:03 +00:00
|
|
|
void DownloadManagerMtproto::Queue::enqueue(
|
|
|
|
not_null<Task*> task,
|
|
|
|
int priority) {
|
|
|
|
const auto position = ranges::find_if(_tasks, [&](const Enqueued &task) {
|
|
|
|
return task.priority <= priority;
|
|
|
|
}) - begin(_tasks);
|
|
|
|
const auto now = ranges::find(_tasks, task, &Enqueued::task);
|
|
|
|
const auto i = [&] {
|
|
|
|
if (now != end(_tasks)) {
|
|
|
|
(now->priority = priority);
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
_tasks.push_back({ task, priority });
|
|
|
|
return end(_tasks) - 1;
|
|
|
|
}();
|
|
|
|
const auto j = begin(_tasks) + position;
|
|
|
|
if (j < i) {
|
|
|
|
std::rotate(j, i, i + 1);
|
|
|
|
} else if (j > i + 1) {
|
|
|
|
std::rotate(i, i + 1, j);
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManagerMtproto::Queue::remove(not_null<Task*> task) {
|
2019-12-23 09:37:03 +00:00
|
|
|
_tasks.erase(ranges::remove(_tasks, task, &Enqueued::task), end(_tasks));
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManagerMtproto::Queue::resetGeneration() {
|
2019-12-23 09:37:03 +00:00
|
|
|
const auto from = ranges::find(_tasks, 0, &Enqueued::priority);
|
|
|
|
for (auto &task : ranges::make_subrange(from, end(_tasks))) {
|
|
|
|
if (task.priority) {
|
|
|
|
Assert(task.priority == -1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
task.priority = -1;
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadManagerMtproto::Queue::empty() const {
|
2019-12-23 09:37:03 +00:00
|
|
|
return _tasks.empty();
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 09:37:03 +00:00
|
|
|
auto DownloadManagerMtproto::Queue::nextTask(bool onlyHighestPriority) const
|
|
|
|
-> Task* {
|
|
|
|
if (_tasks.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const auto highestPriority = _tasks.front().priority;
|
|
|
|
const auto notHighestPriority = [&](const Enqueued &enqueued) {
|
|
|
|
return (enqueued.priority != highestPriority);
|
|
|
|
};
|
|
|
|
const auto till = (onlyHighestPriority && highestPriority > 0)
|
|
|
|
? ranges::find_if(_tasks, notHighestPriority)
|
|
|
|
: end(_tasks);
|
|
|
|
const auto readyToRequest = [&](const Enqueued &enqueued) {
|
|
|
|
return enqueued.task->readyToRequest();
|
|
|
|
};
|
|
|
|
const auto first = ranges::find_if(
|
|
|
|
ranges::make_subrange(begin(_tasks), till),
|
|
|
|
readyToRequest);
|
|
|
|
return (first != till) ? first->task.get() : nullptr;
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:38 +00:00
|
|
|
void DownloadManagerMtproto::Queue::removeSession(int index) {
|
2019-12-23 09:37:03 +00:00
|
|
|
for (const auto &enqueued : _tasks) {
|
|
|
|
enqueued.task->removeSession(index);
|
2019-12-06 07:05:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
DownloadManagerMtproto::DcSessionBalanceData::DcSessionBalanceData()
|
2019-12-05 13:38:28 +00:00
|
|
|
: maxWaitedAmount(kStartWaitedInSession) {
|
2019-12-05 11:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DownloadManagerMtproto::DcBalanceData::DcBalanceData()
|
|
|
|
: sessions(kStartSessionsCount) {
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:32:33 +00:00
|
|
|
DownloadManagerMtproto::DownloadManagerMtproto(not_null<ApiWrap*> api)
|
|
|
|
: _api(api)
|
|
|
|
, _resetGenerationTimer([=] { resetGeneration(); })
|
2019-12-05 11:46:28 +00:00
|
|
|
, _killSessionsTimer([=] { killSessions(); }) {
|
|
|
|
_api->instance()->restartsByTimeout(
|
|
|
|
) | rpl::filter([](MTP::ShiftedDcId shiftedDcId) {
|
|
|
|
return MTP::isDownloadDcId(shiftedDcId);
|
|
|
|
}) | rpl::start_with_next([=](MTP::ShiftedDcId shiftedDcId) {
|
|
|
|
sessionTimedOut(
|
|
|
|
MTP::BareDcId(shiftedDcId),
|
|
|
|
MTP::GetDcIdShift(shiftedDcId));
|
|
|
|
}, _lifetime);
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DownloadManagerMtproto::~DownloadManagerMtproto() {
|
2019-12-05 11:46:28 +00:00
|
|
|
killSessions();
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 09:37:03 +00:00
|
|
|
void DownloadManagerMtproto::enqueue(not_null<Task*> task, int priority) {
|
2019-12-05 08:32:33 +00:00
|
|
|
const auto dcId = task->dcId();
|
|
|
|
auto &queue = _queues[dcId];
|
2019-12-23 09:37:03 +00:00
|
|
|
queue.enqueue(task, priority);
|
2019-12-05 08:32:33 +00:00
|
|
|
if (!_resetGenerationTimer.isActive()) {
|
|
|
|
_resetGenerationTimer.callOnce(kResetDownloadPrioritiesTimeout);
|
|
|
|
}
|
|
|
|
checkSendNext(dcId, queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManagerMtproto::remove(not_null<Task*> task) {
|
|
|
|
const auto dcId = task->dcId();
|
|
|
|
auto &queue = _queues[dcId];
|
|
|
|
queue.remove(task);
|
2019-12-23 08:10:11 +00:00
|
|
|
checkSendNext(dcId, queue);
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManagerMtproto::resetGeneration() {
|
|
|
|
_resetGenerationTimer.cancel();
|
|
|
|
for (auto &[dcId, queue] : _queues) {
|
|
|
|
queue.resetGeneration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManagerMtproto::checkSendNext() {
|
|
|
|
for (auto &[dcId, queue] : _queues) {
|
|
|
|
if (queue.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
checkSendNext(dcId, queue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManagerMtproto::checkSendNext(MTP::DcId dcId, Queue &queue) {
|
2019-12-05 11:46:28 +00:00
|
|
|
while (trySendNextPart(dcId, queue)) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadManagerMtproto::trySendNextPart(MTP::DcId dcId, Queue &queue) {
|
2019-12-23 09:37:03 +00:00
|
|
|
auto &balanceData = _balanceData[dcId];
|
|
|
|
const auto &sessions = balanceData.sessions;
|
2019-12-05 08:32:33 +00:00
|
|
|
const auto bestIndex = [&] {
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto proj = [](const DcSessionBalanceData &data) {
|
|
|
|
return (data.requested < data.maxWaitedAmount)
|
|
|
|
? data.requested
|
|
|
|
: kMaxWaitedInSession;
|
|
|
|
};
|
|
|
|
const auto j = ranges::min_element(sessions, ranges::less(), proj);
|
|
|
|
return (j->requested + kDownloadPartSize <= j->maxWaitedAmount)
|
|
|
|
? (j - begin(sessions))
|
2019-12-05 08:32:33 +00:00
|
|
|
: -1;
|
|
|
|
}();
|
2019-12-05 11:46:28 +00:00
|
|
|
if (bestIndex < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-23 09:37:03 +00:00
|
|
|
const auto onlyHighestPriority = (balanceData.totalRequested > 0);
|
|
|
|
if (const auto task = queue.nextTask(onlyHighestPriority)) {
|
2019-12-05 11:46:28 +00:00
|
|
|
task->loadPart(bestIndex);
|
|
|
|
return true;
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
2019-12-05 11:46:28 +00:00
|
|
|
return false;
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:38 +00:00
|
|
|
int DownloadManagerMtproto::changeRequestedAmount(
|
2019-12-05 08:32:33 +00:00
|
|
|
MTP::DcId dcId,
|
|
|
|
int index,
|
2019-12-05 11:46:28 +00:00
|
|
|
int delta) {
|
|
|
|
const auto i = _balanceData.find(dcId);
|
|
|
|
Assert(i != _balanceData.end());
|
|
|
|
Assert(index < i->second.sessions.size());
|
2019-12-06 07:05:38 +00:00
|
|
|
const auto result = (i->second.sessions[index].requested += delta);
|
2019-12-23 09:37:03 +00:00
|
|
|
i->second.totalRequested += delta;
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto findNonEmptySession = [](const DcBalanceData &data) {
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
return ranges::find_if(
|
|
|
|
data.sessions,
|
|
|
|
_1 > 0,
|
|
|
|
&DcSessionBalanceData::requested);
|
|
|
|
};
|
|
|
|
if (delta > 0) {
|
|
|
|
killSessionsCancel(dcId);
|
|
|
|
} else if (findNonEmptySession(i->second) == end(i->second.sessions)) {
|
|
|
|
killSessionsSchedule(dcId);
|
|
|
|
}
|
2019-12-06 07:05:38 +00:00
|
|
|
return result;
|
2019-12-05 11:46:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 13:38:28 +00:00
|
|
|
void DownloadManagerMtproto::requestSucceeded(
|
|
|
|
MTP::DcId dcId,
|
|
|
|
int index,
|
2019-12-06 07:05:38 +00:00
|
|
|
int amountAtRequestStart,
|
|
|
|
crl::time timeAtRequestStart) {
|
2019-12-05 08:32:33 +00:00
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
2019-12-06 07:05:38 +00:00
|
|
|
const auto guard = gsl::finally([&] {
|
|
|
|
checkSendNext(dcId, _queues[dcId]);
|
|
|
|
});
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto i = _balanceData.find(dcId);
|
|
|
|
Assert(i != end(_balanceData));
|
|
|
|
auto &dc = i->second;
|
|
|
|
Assert(index < dc.sessions.size());
|
|
|
|
auto &data = dc.sessions[index];
|
2019-12-06 07:05:38 +00:00
|
|
|
const auto overloaded = (timeAtRequestStart <= dc.lastSessionRemove)
|
|
|
|
|| (amountAtRequestStart > data.maxWaitedAmount);
|
|
|
|
const auto parts = amountAtRequestStart / kDownloadPartSize;
|
|
|
|
const auto duration = (crl::now() - timeAtRequestStart);
|
|
|
|
DEBUG_LOG(("Download (%1,%2) request done, duration: %3, parts: %4%5"
|
|
|
|
).arg(dcId
|
|
|
|
).arg(index
|
|
|
|
).arg(duration
|
|
|
|
).arg(parts
|
|
|
|
).arg(overloaded ? " (overloaded)" : ""));
|
|
|
|
if (overloaded) {
|
|
|
|
return;
|
2019-12-05 13:38:28 +00:00
|
|
|
}
|
2019-12-06 07:05:38 +00:00
|
|
|
|
|
|
|
if (duration >= kBadRequestDurationThreshold) {
|
2019-12-05 13:38:28 +00:00
|
|
|
DEBUG_LOG(("Duration too large, signaling time out."));
|
|
|
|
sessionTimedOut(dcId, index);
|
|
|
|
return;
|
2019-12-06 07:05:38 +00:00
|
|
|
}
|
|
|
|
if (amountAtRequestStart == data.maxWaitedAmount
|
|
|
|
&& data.maxWaitedAmount < kMaxWaitedInSession) {
|
|
|
|
data.maxWaitedAmount = std::min(
|
|
|
|
data.maxWaitedAmount + kDownloadPartSize,
|
|
|
|
kMaxWaitedInSession);
|
|
|
|
DEBUG_LOG(("Download (%1,%2) increased max waited amount %3."
|
|
|
|
).arg(dcId
|
|
|
|
).arg(index
|
|
|
|
).arg(data.maxWaitedAmount));
|
2019-12-05 13:38:28 +00:00
|
|
|
}
|
2019-12-05 11:46:28 +00:00
|
|
|
data.successes = std::min(data.successes + 1, kMaxTrackedSuccesses);
|
|
|
|
const auto notEnough = ranges::find_if(
|
|
|
|
dc.sessions,
|
|
|
|
_1 < (dc.sessionRemoveTimes + 1) * kRetryAddSessionSuccesses,
|
|
|
|
&DcSessionBalanceData::successes);
|
|
|
|
if (notEnough != end(dc.sessions)) {
|
|
|
|
return;
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
2019-12-05 11:46:28 +00:00
|
|
|
for (auto &session : dc.sessions) {
|
|
|
|
session.successes = 0;
|
|
|
|
}
|
|
|
|
if (dc.timeouts > 0) {
|
|
|
|
--dc.timeouts;
|
|
|
|
return;
|
|
|
|
} else if (dc.sessions.size() == kMaxSessionsCount) {
|
|
|
|
return;
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto now = crl::now();
|
|
|
|
const auto delay = (dc.sessionRemoveTimes + 1) * kRetryAddSessionTimeout;
|
|
|
|
if (dc.lastSessionRemove && now < dc.lastSessionRemove + delay) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-06 07:05:38 +00:00
|
|
|
dc.sessions.emplace_back();
|
|
|
|
DEBUG_LOG(("Download (%1,%2) adding, now sessions: %3"
|
2019-12-05 11:46:28 +00:00
|
|
|
).arg(dcId
|
2019-12-06 07:05:38 +00:00
|
|
|
).arg(dc.sessions.size() - 1
|
2019-12-05 11:46:28 +00:00
|
|
|
).arg(dc.sessions.size()));
|
2019-12-06 07:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DownloadManagerMtproto::chooseSessionIndex(MTP::DcId dcId) const {
|
|
|
|
const auto i = _balanceData.find(dcId);
|
|
|
|
Assert(i != end(_balanceData));
|
|
|
|
const auto &sessions = i->second.sessions;
|
|
|
|
const auto j = ranges::min_element(
|
|
|
|
sessions,
|
|
|
|
ranges::less(),
|
|
|
|
&DcSessionBalanceData::requested);
|
|
|
|
return (j - begin(sessions));
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
void DownloadManagerMtproto::sessionTimedOut(MTP::DcId dcId, int index) {
|
|
|
|
const auto i = _balanceData.find(dcId);
|
|
|
|
if (i == end(_balanceData)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &dc = i->second;
|
|
|
|
if (index >= dc.sessions.size()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DEBUG_LOG(("Download (%1,%2) session timed-out.").arg(dcId).arg(index));
|
|
|
|
for (auto &session : dc.sessions) {
|
|
|
|
session.successes = 0;
|
|
|
|
}
|
|
|
|
if (dc.sessions.size() == kStartSessionsCount
|
|
|
|
|| ++dc.timeouts < kRemoveSessionAfterTimeouts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dc.timeouts = 0;
|
|
|
|
removeSession(dcId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManagerMtproto::removeSession(MTP::DcId dcId) {
|
|
|
|
auto &dc = _balanceData[dcId];
|
|
|
|
Assert(dc.sessions.size() > kStartSessionsCount);
|
|
|
|
const auto index = int(dc.sessions.size() - 1);
|
2019-12-06 07:05:38 +00:00
|
|
|
DEBUG_LOG(("Download (%1,%2) removing, now sessions: %3"
|
|
|
|
).arg(dcId
|
|
|
|
).arg(index
|
|
|
|
).arg(index));
|
2019-12-05 11:46:28 +00:00
|
|
|
auto &queue = _queues[dcId];
|
|
|
|
if (dc.sessionRemoveIndex == index) {
|
|
|
|
dc.sessionRemoveTimes = std::min(
|
|
|
|
dc.sessionRemoveTimes + 1,
|
|
|
|
kMaxTrackedSessionRemoves);
|
|
|
|
} else {
|
|
|
|
dc.sessionRemoveIndex = index;
|
|
|
|
dc.sessionRemoveTimes = 1;
|
|
|
|
}
|
2019-12-06 07:05:38 +00:00
|
|
|
auto &session = dc.sessions.back();
|
|
|
|
|
|
|
|
// Make sure we don't send anything to that session while redirecting.
|
|
|
|
session.requested += kMaxWaitedInSession * kMaxSessionsCount;
|
|
|
|
_queues[dcId].removeSession(index);
|
|
|
|
Assert(session.requested == kMaxWaitedInSession * kMaxSessionsCount);
|
|
|
|
|
|
|
|
dc.sessions.pop_back();
|
|
|
|
MTP::killSession(MTP::downloadDcId(dcId, index));
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
dc.lastSessionRemove = crl::now();
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
void DownloadManagerMtproto::killSessionsSchedule(MTP::DcId dcId) {
|
|
|
|
if (!_killSessionsWhen.contains(dcId)) {
|
|
|
|
_killSessionsWhen.emplace(dcId, crl::now() + kKillSessionTimeout);
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
2019-12-05 11:46:28 +00:00
|
|
|
if (!_killSessionsTimer.isActive()) {
|
|
|
|
_killSessionsTimer.callOnce(kKillSessionTimeout + 5);
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
void DownloadManagerMtproto::killSessionsCancel(MTP::DcId dcId) {
|
|
|
|
_killSessionsWhen.erase(dcId);
|
|
|
|
if (_killSessionsWhen.empty()) {
|
|
|
|
_killSessionsTimer.cancel();
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
void DownloadManagerMtproto::killSessions() {
|
2019-12-05 08:32:33 +00:00
|
|
|
const auto now = crl::now();
|
|
|
|
auto left = kKillSessionTimeout;
|
2019-12-05 11:46:28 +00:00
|
|
|
for (auto i = begin(_killSessionsWhen); i != end(_killSessionsWhen); ) {
|
2019-12-05 08:32:33 +00:00
|
|
|
if (i->second <= now) {
|
2019-12-05 11:46:28 +00:00
|
|
|
killSessions(i->first);
|
|
|
|
i = _killSessionsWhen.erase(i);
|
2019-12-05 08:32:33 +00:00
|
|
|
} else {
|
|
|
|
if (i->second - now < left) {
|
|
|
|
left = i->second - now;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 11:46:28 +00:00
|
|
|
if (!_killSessionsWhen.empty()) {
|
|
|
|
_killSessionsTimer.callOnce(left);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManagerMtproto::killSessions(MTP::DcId dcId) {
|
|
|
|
const auto i = _balanceData.find(dcId);
|
|
|
|
if (i != end(_balanceData)) {
|
|
|
|
auto &dc = i->second;
|
2019-12-23 09:37:03 +00:00
|
|
|
Assert(dc.totalRequested == 0);
|
2019-12-05 11:46:28 +00:00
|
|
|
auto sessions = base::take(dc.sessions);
|
|
|
|
dc = DcBalanceData();
|
|
|
|
for (auto j = 0; j != int(sessions.size()); ++j) {
|
|
|
|
Assert(sessions[j].requested == 0);
|
|
|
|
sessions[j] = DcSessionBalanceData();
|
|
|
|
MTP::stopSession(MTP::downloadDcId(dcId, j));
|
|
|
|
}
|
|
|
|
dc.sessions = base::take(sessions);
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadMtprotoTask::DownloadMtprotoTask(
|
|
|
|
not_null<DownloadManagerMtproto*> owner,
|
|
|
|
const StorageFileLocation &location,
|
|
|
|
Data::FileOrigin origin)
|
|
|
|
: _owner(owner)
|
|
|
|
, _dcId(location.dcId())
|
|
|
|
, _location({ location })
|
|
|
|
, _origin(origin) {
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadMtprotoTask::DownloadMtprotoTask(
|
|
|
|
not_null<DownloadManagerMtproto*> owner,
|
|
|
|
MTP::DcId dcId,
|
|
|
|
const Location &location)
|
|
|
|
: _owner(owner)
|
|
|
|
, _dcId(dcId)
|
|
|
|
, _location(location) {
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadMtprotoTask::~DownloadMtprotoTask() {
|
|
|
|
cancelAllRequests();
|
|
|
|
_owner->remove(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
MTP::DcId DownloadMtprotoTask::dcId() const {
|
|
|
|
return _dcId;
|
|
|
|
}
|
|
|
|
|
|
|
|
Data::FileOrigin DownloadMtprotoTask::fileOrigin() const {
|
|
|
|
return _origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 DownloadMtprotoTask::objectId() const {
|
|
|
|
if (const auto v = base::get_if<StorageFileLocation>(&_location.data)) {
|
|
|
|
return v->objectId();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DownloadMtprotoTask::Location &DownloadMtprotoTask::location() const {
|
|
|
|
return _location;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::refreshFileReferenceFrom(
|
|
|
|
const Data::UpdatedFileReferences &updates,
|
|
|
|
int requestId,
|
|
|
|
const QByteArray ¤t) {
|
|
|
|
if (const auto v = base::get_if<StorageFileLocation>(&_location.data)) {
|
|
|
|
v->refreshFileReference(updates);
|
|
|
|
if (v->fileReference() == current) {
|
|
|
|
cancelOnFail();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cancelOnFail();
|
|
|
|
return;
|
|
|
|
}
|
2019-12-05 11:46:28 +00:00
|
|
|
if (_sentRequests.contains(requestId)) {
|
|
|
|
makeRequest(finishSentRequest(
|
|
|
|
requestId,
|
|
|
|
FinishRequestReason::Redirect));
|
|
|
|
}
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:38 +00:00
|
|
|
void DownloadMtprotoTask::loadPart(int sessionIndex) {
|
|
|
|
makeRequest({ takeNextRequestOffset(), sessionIndex });
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:38 +00:00
|
|
|
void DownloadMtprotoTask::removeSession(int sessionIndex) {
|
|
|
|
struct Redirect {
|
|
|
|
mtpRequestId requestId = 0;
|
|
|
|
int offset = 0;
|
|
|
|
};
|
|
|
|
auto redirect = std::vector<Redirect>();
|
|
|
|
for (const auto &[requestId, requestData] : _sentRequests) {
|
|
|
|
if (requestData.sessionIndex == sessionIndex) {
|
|
|
|
redirect.reserve(_sentRequests.size());
|
|
|
|
redirect.push_back({ requestId, requestData.offset });
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 14:45:43 +00:00
|
|
|
for (auto &[requestData, bytes] : _cdnUncheckedParts) {
|
|
|
|
if (requestData.sessionIndex == sessionIndex) {
|
|
|
|
const auto newIndex = _owner->chooseSessionIndex(dcId());
|
|
|
|
Assert(newIndex < sessionIndex);
|
|
|
|
requestData.sessionIndex = newIndex;
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 07:05:38 +00:00
|
|
|
for (const auto &[requestId, offset] : redirect) {
|
2020-01-20 10:04:12 +00:00
|
|
|
const auto needMakeRequest = (requestId != _cdnHashesRequestId);
|
2019-12-06 07:05:38 +00:00
|
|
|
cancelRequest(requestId);
|
2020-01-20 10:04:12 +00:00
|
|
|
if (needMakeRequest) {
|
|
|
|
const auto newIndex = _owner->chooseSessionIndex(dcId());
|
|
|
|
Assert(newIndex < sessionIndex);
|
|
|
|
makeRequest({ offset, newIndex });
|
|
|
|
}
|
2019-12-06 07:05:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mtpRequestId DownloadMtprotoTask::sendRequest(
|
|
|
|
const RequestData &requestData) {
|
2019-12-05 08:32:33 +00:00
|
|
|
const auto offset = requestData.offset;
|
|
|
|
const auto limit = Storage::kDownloadPartSize;
|
|
|
|
const auto shiftedDcId = MTP::downloadDcId(
|
|
|
|
_cdnDcId ? _cdnDcId : dcId(),
|
2019-12-06 07:05:38 +00:00
|
|
|
requestData.sessionIndex);
|
2019-12-05 08:32:33 +00:00
|
|
|
if (_cdnDcId) {
|
|
|
|
return api().request(MTPupload_GetCdnFile(
|
|
|
|
MTP_bytes(_cdnToken),
|
|
|
|
MTP_int(offset),
|
|
|
|
MTP_int(limit)
|
|
|
|
)).done([=](const MTPupload_CdnFile &result, mtpRequestId id) {
|
|
|
|
cdnPartLoaded(result, id);
|
|
|
|
}).fail([=](const RPCError &error, mtpRequestId id) {
|
|
|
|
cdnPartFailed(error, id);
|
|
|
|
}).toDC(shiftedDcId).send();
|
|
|
|
}
|
|
|
|
return _location.data.match([&](const WebFileLocation &location) {
|
|
|
|
return api().request(MTPupload_GetWebFile(
|
|
|
|
MTP_inputWebFileLocation(
|
|
|
|
MTP_bytes(location.url()),
|
|
|
|
MTP_long(location.accessHash())),
|
|
|
|
MTP_int(offset),
|
|
|
|
MTP_int(limit)
|
|
|
|
)).done([=](const MTPupload_WebFile &result, mtpRequestId id) {
|
|
|
|
webPartLoaded(result, id);
|
|
|
|
}).fail([=](const RPCError &error, mtpRequestId id) {
|
|
|
|
partFailed(error, id);
|
|
|
|
}).toDC(shiftedDcId).send();
|
|
|
|
}, [&](const GeoPointLocation &location) {
|
|
|
|
return api().request(MTPupload_GetWebFile(
|
|
|
|
MTP_inputWebFileGeoPointLocation(
|
|
|
|
MTP_inputGeoPoint(
|
|
|
|
MTP_double(location.lat),
|
|
|
|
MTP_double(location.lon)),
|
|
|
|
MTP_long(location.access),
|
|
|
|
MTP_int(location.width),
|
|
|
|
MTP_int(location.height),
|
|
|
|
MTP_int(location.zoom),
|
|
|
|
MTP_int(location.scale)),
|
|
|
|
MTP_int(offset),
|
|
|
|
MTP_int(limit)
|
|
|
|
)).done([=](const MTPupload_WebFile &result, mtpRequestId id) {
|
|
|
|
webPartLoaded(result, id);
|
|
|
|
}).fail([=](const RPCError &error, mtpRequestId id) {
|
|
|
|
partFailed(error, id);
|
|
|
|
}).toDC(shiftedDcId).send();
|
|
|
|
}, [&](const StorageFileLocation &location) {
|
|
|
|
const auto reference = location.fileReference();
|
|
|
|
return api().request(MTPupload_GetFile(
|
2020-01-18 11:21:23 +00:00
|
|
|
MTP_flags(MTPupload_GetFile::Flag::f_cdn_supported),
|
2019-12-05 08:32:33 +00:00
|
|
|
location.tl(api().session().userId()),
|
|
|
|
MTP_int(offset),
|
|
|
|
MTP_int(limit)
|
|
|
|
)).done([=](const MTPupload_File &result, mtpRequestId id) {
|
|
|
|
normalPartLoaded(result, id);
|
|
|
|
}).fail([=](const RPCError &error, mtpRequestId id) {
|
|
|
|
normalPartFailed(reference, error, id);
|
|
|
|
}).toDC(shiftedDcId).send();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadMtprotoTask::setWebFileSizeHook(int size) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::makeRequest(const RequestData &requestData) {
|
|
|
|
placeSentRequest(sendRequest(requestData), requestData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::requestMoreCdnFileHashes() {
|
|
|
|
if (_cdnHashesRequestId || _cdnUncheckedParts.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto requestData = _cdnUncheckedParts.cbegin()->first;
|
|
|
|
const auto shiftedDcId = MTP::downloadDcId(
|
|
|
|
dcId(),
|
2019-12-06 07:05:38 +00:00
|
|
|
requestData.sessionIndex);
|
2019-12-05 08:32:33 +00:00
|
|
|
_cdnHashesRequestId = api().request(MTPupload_GetCdnFileHashes(
|
|
|
|
MTP_bytes(_cdnToken),
|
|
|
|
MTP_int(requestData.offset)
|
|
|
|
)).done([=](const MTPVector<MTPFileHash> &result, mtpRequestId id) {
|
|
|
|
getCdnFileHashesDone(result, id);
|
|
|
|
}).fail([=](const RPCError &error, mtpRequestId id) {
|
|
|
|
cdnPartFailed(error, id);
|
|
|
|
}).toDC(shiftedDcId).send();
|
|
|
|
placeSentRequest(_cdnHashesRequestId, requestData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::normalPartLoaded(
|
|
|
|
const MTPupload_File &result,
|
|
|
|
mtpRequestId requestId) {
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto requestData = finishSentRequest(
|
|
|
|
requestId,
|
|
|
|
FinishRequestReason::Success);
|
2019-12-05 08:32:33 +00:00
|
|
|
result.match([&](const MTPDupload_fileCdnRedirect &data) {
|
|
|
|
switchToCDN(requestData, data);
|
|
|
|
}, [&](const MTPDupload_file &data) {
|
|
|
|
partLoaded(requestData.offset, data.vbytes().v);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::webPartLoaded(
|
|
|
|
const MTPupload_WebFile &result,
|
|
|
|
mtpRequestId requestId) {
|
|
|
|
result.match([&](const MTPDupload_webFile &data) {
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto requestData = finishSentRequest(
|
|
|
|
requestId,
|
|
|
|
FinishRequestReason::Success);
|
2019-12-05 08:32:33 +00:00
|
|
|
if (setWebFileSizeHook(data.vsize().v)) {
|
|
|
|
partLoaded(requestData.offset, data.vbytes().v);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::cdnPartLoaded(const MTPupload_CdnFile &result, mtpRequestId requestId) {
|
|
|
|
result.match([&](const MTPDupload_cdnFileReuploadNeeded &data) {
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto requestData = finishSentRequest(
|
|
|
|
requestId,
|
|
|
|
FinishRequestReason::Redirect);
|
2019-12-05 08:32:33 +00:00
|
|
|
const auto shiftedDcId = MTP::downloadDcId(
|
|
|
|
dcId(),
|
2019-12-06 07:05:38 +00:00
|
|
|
requestData.sessionIndex);
|
2019-12-05 08:32:33 +00:00
|
|
|
const auto requestId = api().request(MTPupload_ReuploadCdnFile(
|
|
|
|
MTP_bytes(_cdnToken),
|
|
|
|
data.vrequest_token()
|
|
|
|
)).done([=](const MTPVector<MTPFileHash> &result, mtpRequestId id) {
|
|
|
|
reuploadDone(result, id);
|
|
|
|
}).fail([=](const RPCError &error, mtpRequestId id) {
|
|
|
|
cdnPartFailed(error, id);
|
|
|
|
}).toDC(shiftedDcId).send();
|
|
|
|
placeSentRequest(requestId, requestData);
|
|
|
|
}, [&](const MTPDupload_cdnFile &data) {
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto requestData = finishSentRequest(
|
|
|
|
requestId,
|
|
|
|
FinishRequestReason::Success);
|
2019-12-05 08:32:33 +00:00
|
|
|
auto key = bytes::make_span(_cdnEncryptionKey);
|
|
|
|
auto iv = bytes::make_span(_cdnEncryptionIV);
|
|
|
|
Expects(key.size() == MTP::CTRState::KeySize);
|
|
|
|
Expects(iv.size() == MTP::CTRState::IvecSize);
|
|
|
|
|
|
|
|
auto state = MTP::CTRState();
|
|
|
|
auto ivec = bytes::make_span(state.ivec);
|
|
|
|
std::copy(iv.begin(), iv.end(), ivec.begin());
|
|
|
|
|
|
|
|
auto counterOffset = static_cast<uint32>(requestData.offset) >> 4;
|
|
|
|
state.ivec[15] = static_cast<uchar>(counterOffset & 0xFF);
|
|
|
|
state.ivec[14] = static_cast<uchar>((counterOffset >> 8) & 0xFF);
|
|
|
|
state.ivec[13] = static_cast<uchar>((counterOffset >> 16) & 0xFF);
|
|
|
|
state.ivec[12] = static_cast<uchar>((counterOffset >> 24) & 0xFF);
|
|
|
|
|
|
|
|
auto decryptInPlace = data.vbytes().v;
|
|
|
|
auto buffer = bytes::make_detached_span(decryptInPlace);
|
|
|
|
MTP::aesCtrEncrypt(buffer, key.data(), &state);
|
|
|
|
|
|
|
|
switch (checkCdnFileHash(requestData.offset, buffer)) {
|
|
|
|
case CheckCdnHashResult::NoHash: {
|
|
|
|
_cdnUncheckedParts.emplace(requestData, decryptInPlace);
|
|
|
|
requestMoreCdnFileHashes();
|
|
|
|
} return;
|
|
|
|
|
|
|
|
case CheckCdnHashResult::Invalid: {
|
|
|
|
LOG(("API Error: Wrong cdnFileHash for offset %1."
|
|
|
|
).arg(requestData.offset));
|
|
|
|
cancelOnFail();
|
|
|
|
} return;
|
|
|
|
|
|
|
|
case CheckCdnHashResult::Good: {
|
|
|
|
partLoaded(requestData.offset, decryptInPlace);
|
|
|
|
} return;
|
|
|
|
}
|
|
|
|
Unexpected("Result of checkCdnFileHash()");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadMtprotoTask::CheckCdnHashResult DownloadMtprotoTask::checkCdnFileHash(
|
|
|
|
int offset,
|
|
|
|
bytes::const_span buffer) {
|
|
|
|
const auto cdnFileHashIt = _cdnFileHashes.find(offset);
|
|
|
|
if (cdnFileHashIt == _cdnFileHashes.cend()) {
|
|
|
|
return CheckCdnHashResult::NoHash;
|
|
|
|
}
|
|
|
|
const auto realHash = openssl::Sha256(buffer);
|
|
|
|
const auto receivedHash = bytes::make_span(cdnFileHashIt->second.hash);
|
|
|
|
if (bytes::compare(realHash, receivedHash)) {
|
|
|
|
return CheckCdnHashResult::Invalid;
|
|
|
|
}
|
|
|
|
return CheckCdnHashResult::Good;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::reuploadDone(
|
|
|
|
const MTPVector<MTPFileHash> &result,
|
|
|
|
mtpRequestId requestId) {
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto requestData = finishSentRequest(
|
|
|
|
requestId,
|
|
|
|
FinishRequestReason::Redirect);
|
2019-12-05 08:32:33 +00:00
|
|
|
addCdnHashes(result.v);
|
|
|
|
makeRequest(requestData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::getCdnFileHashesDone(
|
|
|
|
const MTPVector<MTPFileHash> &result,
|
|
|
|
mtpRequestId requestId) {
|
|
|
|
Expects(_cdnHashesRequestId == requestId);
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto requestData = finishSentRequest(
|
|
|
|
requestId,
|
|
|
|
FinishRequestReason::Redirect);
|
2019-12-05 08:32:33 +00:00
|
|
|
addCdnHashes(result.v);
|
|
|
|
auto someMoreChecked = false;
|
|
|
|
for (auto i = _cdnUncheckedParts.begin(); i != _cdnUncheckedParts.cend();) {
|
|
|
|
const auto uncheckedData = i->first;
|
|
|
|
const auto uncheckedBytes = bytes::make_span(i->second);
|
|
|
|
|
|
|
|
switch (checkCdnFileHash(uncheckedData.offset, uncheckedBytes)) {
|
|
|
|
case CheckCdnHashResult::NoHash: {
|
|
|
|
++i;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CheckCdnHashResult::Invalid: {
|
|
|
|
LOG(("API Error: Wrong cdnFileHash for offset %1."
|
|
|
|
).arg(uncheckedData.offset));
|
|
|
|
cancelOnFail();
|
|
|
|
return;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CheckCdnHashResult::Good: {
|
|
|
|
someMoreChecked = true;
|
|
|
|
const auto goodOffset = uncheckedData.offset;
|
|
|
|
const auto goodBytes = std::move(i->second);
|
|
|
|
const auto weak = base::make_weak(this);
|
|
|
|
i = _cdnUncheckedParts.erase(i);
|
|
|
|
if (!feedPart(goodOffset, goodBytes) || !weak) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: Unexpected("Result of checkCdnFileHash()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!someMoreChecked) {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"Could not find cdnFileHash for offset %1 "
|
|
|
|
"after getCdnFileHashes request."
|
|
|
|
).arg(requestData.offset));
|
|
|
|
cancelOnFail();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
requestMoreCdnFileHashes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::placeSentRequest(
|
|
|
|
mtpRequestId requestId,
|
|
|
|
const RequestData &requestData) {
|
2019-12-06 07:05:38 +00:00
|
|
|
const auto amount = _owner->changeRequestedAmount(
|
2019-12-05 08:32:33 +00:00
|
|
|
dcId(),
|
2019-12-06 07:05:38 +00:00
|
|
|
requestData.sessionIndex,
|
2019-12-05 08:32:33 +00:00
|
|
|
Storage::kDownloadPartSize);
|
|
|
|
const auto [i, ok1] = _sentRequests.emplace(requestId, requestData);
|
|
|
|
const auto [j, ok2] = _requestByOffset.emplace(
|
|
|
|
requestData.offset,
|
|
|
|
requestId);
|
|
|
|
|
2019-12-06 07:05:38 +00:00
|
|
|
i->second.requestedInSession = amount;
|
2019-12-05 13:38:28 +00:00
|
|
|
i->second.sent = crl::now();
|
|
|
|
|
2019-12-05 08:32:33 +00:00
|
|
|
Ensures(ok1 && ok2);
|
|
|
|
}
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
auto DownloadMtprotoTask::finishSentRequest(
|
|
|
|
mtpRequestId requestId,
|
|
|
|
FinishRequestReason reason)
|
2019-12-05 08:32:33 +00:00
|
|
|
-> RequestData {
|
|
|
|
auto it = _sentRequests.find(requestId);
|
|
|
|
Assert(it != _sentRequests.cend());
|
|
|
|
|
2019-12-05 13:38:28 +00:00
|
|
|
if (_cdnHashesRequestId == requestId) {
|
|
|
|
_cdnHashesRequestId = 0;
|
|
|
|
}
|
2019-12-05 08:32:33 +00:00
|
|
|
const auto result = it->second;
|
2019-12-05 11:46:28 +00:00
|
|
|
_owner->changeRequestedAmount(
|
2019-12-05 08:32:33 +00:00
|
|
|
dcId(),
|
2019-12-06 07:05:38 +00:00
|
|
|
result.sessionIndex,
|
2019-12-05 08:32:33 +00:00
|
|
|
-Storage::kDownloadPartSize);
|
|
|
|
_sentRequests.erase(it);
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto ok = _requestByOffset.remove(result.offset);
|
2019-12-05 08:32:33 +00:00
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
if (reason == FinishRequestReason::Success) {
|
2019-12-06 07:05:38 +00:00
|
|
|
_owner->requestSucceeded(
|
|
|
|
dcId(),
|
|
|
|
result.sessionIndex,
|
|
|
|
result.requestedInSession,
|
|
|
|
result.sent);
|
2019-12-05 11:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ensures(ok);
|
2019-12-05 08:32:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadMtprotoTask::haveSentRequests() const {
|
|
|
|
return !_sentRequests.empty() || !_cdnUncheckedParts.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadMtprotoTask::haveSentRequestForOffset(int offset) const {
|
|
|
|
return _requestByOffset.contains(offset)
|
|
|
|
|| _cdnUncheckedParts.contains({ offset, 0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::cancelAllRequests() {
|
|
|
|
while (!_sentRequests.empty()) {
|
|
|
|
cancelRequest(_sentRequests.begin()->first);
|
|
|
|
}
|
|
|
|
_cdnUncheckedParts.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::cancelRequestForOffset(int offset) {
|
|
|
|
const auto i = _requestByOffset.find(offset);
|
|
|
|
if (i != end(_requestByOffset)) {
|
|
|
|
cancelRequest(i->second);
|
|
|
|
}
|
|
|
|
_cdnUncheckedParts.remove({ offset, 0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::cancelRequest(mtpRequestId requestId) {
|
2019-12-06 07:05:38 +00:00
|
|
|
const auto hashes = (_cdnHashesRequestId == requestId);
|
2019-12-05 08:32:33 +00:00
|
|
|
api().request(requestId).cancel();
|
2019-12-05 11:46:28 +00:00
|
|
|
[[maybe_unused]] const auto data = finishSentRequest(
|
|
|
|
requestId,
|
|
|
|
FinishRequestReason::Cancel);
|
2019-12-06 07:05:38 +00:00
|
|
|
if (hashes && !_cdnUncheckedParts.empty()) {
|
|
|
|
crl::on_main(this, [=] {
|
|
|
|
requestMoreCdnFileHashes();
|
|
|
|
});
|
|
|
|
}
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 09:37:03 +00:00
|
|
|
void DownloadMtprotoTask::addToQueue(int priority) {
|
|
|
|
_owner->enqueue(this, priority);
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::removeFromQueue() {
|
|
|
|
_owner->remove(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::partLoaded(
|
|
|
|
int offset,
|
|
|
|
const QByteArray &bytes) {
|
|
|
|
feedPart(offset, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadMtprotoTask::normalPartFailed(
|
|
|
|
QByteArray fileReference,
|
|
|
|
const RPCError &error,
|
|
|
|
mtpRequestId requestId) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (error.code() == 400
|
|
|
|
&& error.type().startsWith(qstr("FILE_REFERENCE_"))) {
|
|
|
|
api().refreshFileReference(
|
|
|
|
_origin,
|
|
|
|
this,
|
|
|
|
requestId,
|
|
|
|
fileReference);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return partFailed(error, requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadMtprotoTask::partFailed(
|
|
|
|
const RPCError &error,
|
|
|
|
mtpRequestId requestId) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cancelOnFail();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadMtprotoTask::cdnPartFailed(
|
|
|
|
const RPCError &error,
|
|
|
|
mtpRequestId requestId) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.type() == qstr("FILE_TOKEN_INVALID")
|
|
|
|
|| error.type() == qstr("REQUEST_TOKEN_INVALID")) {
|
2019-12-05 11:46:28 +00:00
|
|
|
const auto requestData = finishSentRequest(
|
|
|
|
requestId,
|
|
|
|
FinishRequestReason::Redirect);
|
2019-12-05 08:32:33 +00:00
|
|
|
changeCDNParams(
|
|
|
|
requestData,
|
|
|
|
0,
|
|
|
|
QByteArray(),
|
|
|
|
QByteArray(),
|
|
|
|
QByteArray(),
|
|
|
|
QVector<MTPFileHash>());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return partFailed(error, requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::switchToCDN(
|
|
|
|
const RequestData &requestData,
|
|
|
|
const MTPDupload_fileCdnRedirect &redirect) {
|
|
|
|
changeCDNParams(
|
|
|
|
requestData,
|
|
|
|
redirect.vdc_id().v,
|
|
|
|
redirect.vfile_token().v,
|
|
|
|
redirect.vencryption_key().v,
|
|
|
|
redirect.vencryption_iv().v,
|
|
|
|
redirect.vfile_hashes().v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::addCdnHashes(
|
|
|
|
const QVector<MTPFileHash> &hashes) {
|
|
|
|
for (const auto &hash : hashes) {
|
|
|
|
hash.match([&](const MTPDfileHash &data) {
|
|
|
|
_cdnFileHashes.emplace(
|
|
|
|
data.voffset().v,
|
|
|
|
CdnFileHash{ data.vlimit().v, data.vhash().v });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadMtprotoTask::changeCDNParams(
|
|
|
|
const RequestData &requestData,
|
|
|
|
MTP::DcId dcId,
|
|
|
|
const QByteArray &token,
|
|
|
|
const QByteArray &encryptionKey,
|
|
|
|
const QByteArray &encryptionIV,
|
|
|
|
const QVector<MTPFileHash> &hashes) {
|
|
|
|
if (dcId != 0
|
|
|
|
&& (encryptionKey.size() != MTP::CTRState::KeySize
|
|
|
|
|| encryptionIV.size() != MTP::CTRState::IvecSize)) {
|
|
|
|
LOG(("Message Error: Wrong key (%1) / iv (%2) size in CDN params"
|
|
|
|
).arg(encryptionKey.size()
|
|
|
|
).arg(encryptionIV.size()));
|
|
|
|
cancelOnFail();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto resendAllRequests = (_cdnDcId != dcId
|
|
|
|
|| _cdnToken != token
|
|
|
|
|| _cdnEncryptionKey != encryptionKey
|
|
|
|
|| _cdnEncryptionIV != encryptionIV);
|
|
|
|
_cdnDcId = dcId;
|
|
|
|
_cdnToken = token;
|
|
|
|
_cdnEncryptionKey = encryptionKey;
|
|
|
|
_cdnEncryptionIV = encryptionIV;
|
|
|
|
addCdnHashes(hashes);
|
|
|
|
|
|
|
|
if (resendAllRequests && !_sentRequests.empty()) {
|
|
|
|
auto resendRequests = std::vector<RequestData>();
|
|
|
|
resendRequests.reserve(_sentRequests.size());
|
|
|
|
while (!_sentRequests.empty()) {
|
|
|
|
const auto requestId = _sentRequests.begin()->first;
|
|
|
|
api().request(requestId).cancel();
|
2019-12-05 11:46:28 +00:00
|
|
|
resendRequests.push_back(finishSentRequest(
|
|
|
|
requestId,
|
|
|
|
FinishRequestReason::Redirect));
|
2019-12-05 08:32:33 +00:00
|
|
|
}
|
|
|
|
for (const auto &requestData : resendRequests) {
|
|
|
|
makeRequest(requestData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
makeRequest(requestData);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Storage
|