From 7cb98b90c7b7754cf9c876228ab62fac79d906d0 Mon Sep 17 00:00:00 2001 From: tux3 Date: Mon, 11 May 2015 20:41:19 +0200 Subject: [PATCH] Faster file transfers (up to 160ms/s) By reducing the maximum interval between two tox_iterate calls during file transfers to 10ms. This results in reasonnable amounts of extra CPU% used. If the only file transfers are avatars, CoreFile allows sleeping for up to 50ms. If there are currently no FT, up to 1000ms. We now take the minimum sleeping interval asked by toxcore, toxav, and CoreFile --- src/core/core.cpp | 4 +++- src/core/corefile.cpp | 19 +++++++++++++++++++ src/core/corefile.h | 4 ++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 39eff2ce8..bca66d2dc 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -363,7 +363,9 @@ void Core::process() tolerance = 3*CORE_DISCONNECT_TOLERANCE; } - toxTimer->start(qMin(tox_iteration_interval(tox), toxav_do_interval(toxav))); + unsigned sleeptime = qMin(tox_iteration_interval(tox), toxav_do_interval(toxav)); + sleeptime = qMin(sleeptime, CoreFile::corefileIterationInterval()); + toxTimer->start(sleeptime); } bool Core::checkConnection() diff --git a/src/core/corefile.cpp b/src/core/corefile.cpp index 744318f2f..41f805a3e 100644 --- a/src/core/corefile.cpp +++ b/src/core/corefile.cpp @@ -13,6 +13,25 @@ QMutex CoreFile::fileSendMutex; QHash CoreFile::fileMap; using namespace std; +unsigned CoreFile::corefileIterationInterval() +{ + /// Sleep at most 1000ms if we have no FT, 10 for user FTs, 50 for the rest (avatars, ...) + constexpr unsigned fastFileInterval=10, slowFileInterval=50, idleInterval=1000; + unsigned interval = idleInterval; + + for (ToxFile& file : fileMap) + { + if (file.status == ToxFile::TRANSMITTING) + { + if (file.fileKind == TOX_FILE_KIND_DATA) + return fastFileInterval; + else + interval = slowFileInterval; + } + } + return interval; +} + void CoreFile::sendAvatarFile(Core* core, uint32_t friendId, const QByteArray& data) { QMutexLocker mlocker(&fileSendMutex); diff --git a/src/core/corefile.h b/src/core/corefile.h index 9588bb545..40539e8d3 100644 --- a/src/core/corefile.h +++ b/src/core/corefile.h @@ -24,6 +24,7 @@ class CoreFile private: CoreFile()=delete; + // Internal file sending APIs, used by Core. Public API in core.h private: static void sendFile(Core *core, uint32_t friendId, QString Filename, QString FilePath, long long filesize); static void sendAvatarFile(Core* core, uint32_t friendId, const QByteArray& data); @@ -36,6 +37,9 @@ private: static ToxFile *findFile(uint32_t friendId, uint32_t fileId); static void addFile(uint32_t friendId, uint32_t fileId, const ToxFile& file); static void removeFile(uint32_t friendId, uint32_t fileId); + /// Returns the maximum amount of time in ms that Core should wait between two + /// tox_iterate calls to get good file transfer performances + static unsigned corefileIterationInterval(); private: static void onFileReceiveCallback(Tox*, uint32_t friendnumber, uint32_t fileId, uint32_t kind,