diff --git a/Telegram/SourceFiles/core/kotato_settings.cpp b/Telegram/SourceFiles/core/kotato_settings.cpp index bf68f89ef..b18d88d92 100644 --- a/Telegram/SourceFiles/core/kotato_settings.cpp +++ b/Telegram/SourceFiles/core/kotato_settings.cpp @@ -17,6 +17,7 @@ https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL #include #include #include +#include namespace KotatoSettings { namespace { @@ -177,6 +178,46 @@ bool Manager::readCustomFile() { if (settingsShowChatIdIterator != settings.constEnd() && (*settingsShowChatIdIterator).isBool()) { cSetShowChatId((*settingsShowChatIdIterator).toBool()); } + + const auto settingsNetSpeedIterator = settings.constFind(qsl("net_speed_boost")); + if (settingsNetSpeedIterator != settings.constEnd()) { + if ((*settingsNetSpeedIterator).isString()) { + + const auto option = (*settingsNetSpeedIterator).toString(); + if (option == "high") { + cSetNetRequestsCount(8); + cSetNetDownloadSessionsCount(8); + cSetNetUploadSessionsCount(8); + cSetNetMaxFileQueries(64); + cSetNetUploadRequestInterval(200); + } else if (option == "medium") { + cSetNetRequestsCount(6); + cSetNetDownloadSessionsCount(6); + cSetNetUploadSessionsCount(6); + cSetNetMaxFileQueries(48); + cSetNetUploadRequestInterval(300); + } else if (option == "low") { + cSetNetRequestsCount(4); + cSetNetDownloadSessionsCount(4); + cSetNetUploadSessionsCount(4); + cSetNetMaxFileQueries(32); + cSetNetUploadRequestInterval(400); + } else { + cSetNetRequestsCount(2); + cSetNetDownloadSessionsCount(2); + cSetNetUploadSessionsCount(2); + cSetNetMaxFileQueries(16); + cSetNetUploadRequestInterval(500); + } + + } else if ((*settingsNetSpeedIterator).isNull()) { + cSetNetRequestsCount(2); + cSetNetDownloadSessionsCount(2); + cSetNetUploadSessionsCount(2); + cSetNetMaxFileQueries(16); + cSetNetUploadRequestInterval(500); + } + } return true; } @@ -208,6 +249,7 @@ void Manager::writeDefaultFile() { settings.insert(qsl("big_emoji_outline"), cBigEmojiOutline()); settings.insert(qsl("always_show_scheduled"), cAlwaysShowScheduled()); settings.insert(qsl("show_chat_id"), cShowChatId()); + settings.insert(qsl("net_speed_boost"), QJsonValue(QJsonValue::Null)); auto document = QJsonDocument(); document.setObject(settings); diff --git a/Telegram/SourceFiles/mtproto/connection.cpp b/Telegram/SourceFiles/mtproto/connection.cpp index 2ab74ede4..4810bc3ef 100644 --- a/Telegram/SourceFiles/mtproto/connection.cpp +++ b/Telegram/SourceFiles/mtproto/connection.cpp @@ -1273,9 +1273,9 @@ void ConnectionPrivate::onSentSome(uint64 size) { } } if (isUploadDcId(_shiftedDcId)) { - remain *= kUploadSessionsCount; + remain *= cNetUploadSessionsCount(); } else if (isDownloadDcId(_shiftedDcId)) { - remain *= kDownloadSessionsCount; + remain *= cNetDownloadSessionsCount(); } _waitForReceivedTimer.callOnce(remain); } diff --git a/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp b/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp index b66eb6f44..9dfaadba2 100644 --- a/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp +++ b/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp @@ -304,7 +304,7 @@ void DedicatedLoader::startLoading() { } void DedicatedLoader::sendRequest() { - if (_requests.size() >= kRequestsCount || _offset >= _size) { + if (_requests.size() >= cNetRequestsCount() || _offset >= _size) { return; } const auto offset = _offset; @@ -320,7 +320,7 @@ void DedicatedLoader::sendRequest() { MTP::updaterDcId(_dcId)); _offset += kChunkSize; - if (_requests.size() < kRequestsCount) { + if (_requests.size() < cNetRequestsCount()) { App::CallDelayed(kNextRequestDelay, this, [=] { sendRequest(); }); } } diff --git a/Telegram/SourceFiles/mtproto/facade.h b/Telegram/SourceFiles/mtproto/facade.h index cffb33584..dfa3dab83 100644 --- a/Telegram/SourceFiles/mtproto/facade.h +++ b/Telegram/SourceFiles/mtproto/facade.h @@ -37,10 +37,13 @@ constexpr ShiftedDcId updaterDcId(DcId dcId) { constexpr auto kDownloadSessionsCount = 2; constexpr auto kUploadSessionsCount = 2; +constexpr auto kDownloadSessionsCountMax = 8; +constexpr auto kUploadSessionsCountMax = 8; + namespace internal { constexpr ShiftedDcId downloadDcId(DcId dcId, int index) { - static_assert(kDownloadSessionsCount < kMaxMediaDcCount, "Too large MTPDownloadSessionsCount!"); + static_assert(kDownloadSessionsCountMax < kMaxMediaDcCount, "Too large MTPDownloadSessionsCount!"); return ShiftDcId(dcId, kBaseDownloadDcShift + index); }; @@ -48,12 +51,12 @@ constexpr ShiftedDcId downloadDcId(DcId dcId, int index) { // send(req, callbacks, MTP::downloadDcId(dc, index)) - for download shifted dc id inline ShiftedDcId downloadDcId(DcId dcId, int index) { - Expects(index >= 0 && index < kDownloadSessionsCount); + Expects(index >= 0 && index < cNetDownloadSessionsCount()); return internal::downloadDcId(dcId, index); } inline constexpr bool isDownloadDcId(ShiftedDcId shiftedDcId) { - return (shiftedDcId >= internal::downloadDcId(0, 0)) && (shiftedDcId < internal::downloadDcId(0, kDownloadSessionsCount - 1) + kDcShift); + return (shiftedDcId >= internal::downloadDcId(0, 0)) && (shiftedDcId < internal::downloadDcId(0, kDownloadSessionsCountMax - 1) + kDcShift); } inline bool isCdnDc(MTPDdcOption::Flags flags) { @@ -78,7 +81,7 @@ inline DcId getTemporaryIdFromRealDcId(ShiftedDcId shiftedDcId) { namespace internal { constexpr ShiftedDcId uploadDcId(DcId dcId, int index) { - static_assert(kUploadSessionsCount < kMaxMediaDcCount, "Too large MTPUploadSessionsCount!"); + static_assert(kUploadSessionsCountMax < kMaxMediaDcCount, "Too large MTPUploadSessionsCount!"); return ShiftDcId(dcId, kBaseUploadDcShift + index); }; @@ -87,13 +90,13 @@ constexpr ShiftedDcId uploadDcId(DcId dcId, int index) { // send(req, callbacks, MTP::uploadDcId(index)) - for upload shifted dc id // uploading always to the main dc so BareDcId(result) == 0 inline ShiftedDcId uploadDcId(int index) { - Expects(index >= 0 && index < kUploadSessionsCount); + Expects(index >= 0 && index < cNetUploadSessionsCount()); return internal::uploadDcId(0, index); }; constexpr bool isUploadDcId(ShiftedDcId shiftedDcId) { - return (shiftedDcId >= internal::uploadDcId(0, 0)) && (shiftedDcId < internal::uploadDcId(0, kUploadSessionsCount - 1) + kDcShift); + return (shiftedDcId >= internal::uploadDcId(0, 0)) && (shiftedDcId < internal::uploadDcId(0, kUploadSessionsCountMax - 1) + kDcShift); } inline ShiftedDcId destroyKeyNextDcId(ShiftedDcId shiftedDcId) { diff --git a/Telegram/SourceFiles/settings.cpp b/Telegram/SourceFiles/settings.cpp index 24b89affe..222d87be2 100644 --- a/Telegram/SourceFiles/settings.cpp +++ b/Telegram/SourceFiles/settings.cpp @@ -214,3 +214,9 @@ int gStickerHeight = 128; bool gBigEmojiOutline = false; bool gAlwaysShowScheduled = true; bool gShowChatId = true; + +int gNetRequestsCount = 2; +int gNetDownloadSessionsCount = 2; +int gNetUploadSessionsCount = 2; +int gNetMaxFileQueries = 16; +int gNetUploadRequestInterval = 500; diff --git a/Telegram/SourceFiles/settings.h b/Telegram/SourceFiles/settings.h index 786828332..c58061e2b 100644 --- a/Telegram/SourceFiles/settings.h +++ b/Telegram/SourceFiles/settings.h @@ -188,3 +188,9 @@ DeclareSetting(int, StickerHeight); DeclareSetting(bool, BigEmojiOutline); DeclareSetting(bool, AlwaysShowScheduled); DeclareSetting(bool, ShowChatId); + +DeclareSetting(int, NetRequestsCount); +DeclareSetting(int, NetDownloadSessionsCount); +DeclareSetting(int, NetUploadSessionsCount); +DeclareSetting(int, NetMaxFileQueries); +DeclareSetting(int, NetUploadRequestInterval); diff --git a/Telegram/SourceFiles/storage/file_download.cpp b/Telegram/SourceFiles/storage/file_download.cpp index ffac1874e..d76c4b180 100644 --- a/Telegram/SourceFiles/storage/file_download.cpp +++ b/Telegram/SourceFiles/storage/file_download.cpp @@ -55,7 +55,7 @@ void Downloader::clearPriorities() { } void Downloader::requestedAmountIncrement(MTP::DcId dcId, int index, int amount) { - Expects(index >= 0 && index < MTP::kDownloadSessionsCount); + Expects(index >= 0 && index < cNetDownloadSessionsCount()); using namespace rpl::mappers; @@ -95,7 +95,7 @@ void Downloader::killDownloadSessions() { auto ms = crl::now(), left = MTP::kAckSendWaiting + kKillSessionTimeout; for (auto i = _killDownloadSessionTimes.begin(); i != _killDownloadSessionTimes.end(); ) { if (i->second <= ms) { - for (int j = 0; j < MTP::kDownloadSessionsCount; ++j) { + for (int j = 0; j < cNetDownloadSessionsCount(); ++j) { MTP::stopSession(MTP::downloadDcId(i->first, j)); } i = _killDownloadSessionTimes.erase(i); @@ -115,7 +115,7 @@ int Downloader::chooseDcIndexForRequest(MTP::DcId dcId) const { auto result = 0; auto it = _requestedBytesAmount.find(dcId); if (it != _requestedBytesAmount.cend()) { - for (auto i = 1; i != MTP::kDownloadSessionsCount; ++i) { + for (auto i = 1; i != cNetDownloadSessionsCount(); ++i) { if (it->second[i] < it->second[result]) { result = i; } @@ -128,7 +128,7 @@ not_null Downloader::queueForDc(MTP::DcId dcId) { const auto i = _queuesForDc.find(dcId); const auto result = (i != end(_queuesForDc)) ? i - : _queuesForDc.emplace(dcId, Queue(kMaxFileQueries)).first; + : _queuesForDc.emplace(dcId, Queue(cNetMaxFileQueries())).first; return &result->second; } diff --git a/Telegram/SourceFiles/storage/file_download.h b/Telegram/SourceFiles/storage/file_download.h index 558a89bb4..ce492fc51 100644 --- a/Telegram/SourceFiles/storage/file_download.h +++ b/Telegram/SourceFiles/storage/file_download.h @@ -77,7 +77,7 @@ private: base::Observable _taskFinishedObservable; int _priority = 1; - using RequestedInDc = std::array; + using RequestedInDc = std::array; std::map _requestedBytesAmount; base::flat_map _killDownloadSessionTimes; diff --git a/Telegram/SourceFiles/storage/file_upload.cpp b/Telegram/SourceFiles/storage/file_upload.cpp index 6d852cdb9..066edd39c 100644 --- a/Telegram/SourceFiles/storage/file_upload.cpp +++ b/Telegram/SourceFiles/storage/file_upload.cpp @@ -237,7 +237,7 @@ void Uploader::currentFailed() { dcMap.clear(); uploadingId = FullMsgId(); sentSize = 0; - for (int i = 0; i < MTP::kUploadSessionsCount; ++i) { + for (int i = 0; i < cNetUploadSessionsCount(); ++i) { sentSizes[i] = 0; } @@ -245,13 +245,13 @@ void Uploader::currentFailed() { } void Uploader::stopSessions() { - for (int i = 0; i < MTP::kUploadSessionsCount; ++i) { + for (int i = 0; i < cNetUploadSessionsCount(); ++i) { MTP::stopSession(MTP::uploadDcId(i)); } } void Uploader::sendNext() { - if (sentSize >= kMaxUploadFileParallelSize || _pausedId.msg) return; + if (sentSize >= (cNetUploadSessionsCount() * 512 * 1024) || _pausedId.msg) return; bool stopping = stopSessionsTimer.isActive(); if (queue.empty()) { @@ -275,7 +275,7 @@ void Uploader::sendNext() { auto &uploadingData = i->second; auto todc = 0; - for (auto dc = 1; dc != MTP::kUploadSessionsCount; ++dc) { + for (auto dc = 1; dc != cNetUploadSessionsCount(); ++dc) { if (sentSizes[dc] < sentSizes[todc]) { todc = dc; } @@ -453,7 +453,7 @@ void Uploader::sendNext() { parts.erase(part); } - nextTimer.start(kUploadRequestInterval); + nextTimer.start(crl::time(cNetUploadRequestInterval())); } void Uploader::cancel(const FullMsgId &msgId) { @@ -490,7 +490,7 @@ void Uploader::clear() { docRequestsSent.clear(); dcMap.clear(); sentSize = 0; - for (int i = 0; i < MTP::kUploadSessionsCount; ++i) { + for (int i = 0; i < cNetUploadSessionsCount(); ++i) { MTP::stopSession(MTP::uploadDcId(i)); sentSizes[i] = 0; } diff --git a/Telegram/SourceFiles/storage/file_upload.h b/Telegram/SourceFiles/storage/file_upload.h index df6f3bc2c..7e1f63d5c 100644 --- a/Telegram/SourceFiles/storage/file_upload.h +++ b/Telegram/SourceFiles/storage/file_upload.h @@ -121,7 +121,7 @@ private: base::flat_map docRequestsSent; base::flat_map dcMap; uint32 sentSize = 0; - uint32 sentSizes[MTP::kUploadSessionsCount] = { 0 }; + uint32 sentSizes[MTP::kUploadSessionsCountMax] = { 0 }; FullMsgId uploadingId; FullMsgId _pausedId;