From 541bc0e174535b682a7bcb1a6894a753ab2d941c Mon Sep 17 00:00:00 2001 From: Zetok Zalbavar Date: Tue, 20 Dec 2016 09:21:45 +0000 Subject: [PATCH] refactor(core): simplify code for tox interval during file transfer Also some style changes. --- src/core/corefile.cpp | 18 +++++++------ src/core/corefile.h | 63 ++++++++++++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/src/core/corefile.cpp b/src/core/corefile.cpp index 2abefb677..df635a4b9 100644 --- a/src/core/corefile.cpp +++ b/src/core/corefile.cpp @@ -49,21 +49,23 @@ 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; + /* + Sleep at most 1000ms if we have no FT, 10 for user FTs + There is no real difference between 10ms sleep and 50ms sleep when it + comes to CPU usage – just keep the CPU usage low when there are no file + transfers, and speed things up when there is an ongoing file transfer. + */ + constexpr unsigned fileInterval = 10, + idleInterval = 1000; for (ToxFile& file : fileMap) { if (file.status == ToxFile::TRANSMITTING) { - if (file.fileKind == TOX_FILE_KIND_DATA) - return fastFileInterval; - else - interval = slowFileInterval; + return fileInterval; } } - return interval; + return idleInterval; } void CoreFile::sendAvatarFile(Core* core, uint32_t friendId, const QByteArray& data) diff --git a/src/core/corefile.h b/src/core/corefile.h index a9f6221ec..c96f4416c 100644 --- a/src/core/corefile.h +++ b/src/core/corefile.h @@ -44,14 +44,33 @@ private: // 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); - static void pauseResumeFileSend(Core* core, uint32_t friendId, uint32_t fileId); - static void pauseResumeFileRecv(Core* core, uint32_t friendId, uint32_t fileId); - static void cancelFileSend(Core* core, uint32_t friendId, uint32_t fileId); - static void cancelFileRecv(Core* core, uint32_t friendId, uint32_t fileId); - static void rejectFileRecvRequest(Core* core, uint32_t friendId, uint32_t fileId); - static void acceptFileRecvRequest(Core* core, uint32_t friendId, uint32_t fileId, QString path); + 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); + static void pauseResumeFileSend(Core* core, + uint32_t friendId, + uint32_t fileId); + static void pauseResumeFileRecv(Core* core, + uint32_t friendId, + uint32_t fileId); + static void cancelFileSend(Core* core, + uint32_t friendId, + uint32_t fileId); + static void cancelFileRecv(Core* core, + uint32_t friendId, + uint32_t fileId); + static void rejectFileRecvRequest(Core* core, + uint32_t friendId, + uint32_t fileId); + static void acceptFileRecvRequest(Core* core, + uint32_t friendId, + uint32_t fileId, + QString path); 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); @@ -62,20 +81,28 @@ private: } private: - static void onFileReceiveCallback(Tox*, uint32_t friendId, uint32_t fileId, - uint32_t kind, uint64_t filesize, - const uint8_t* fname, size_t fnameLen, + static void onFileReceiveCallback(Tox*, + uint32_t friendId, + uint32_t fileId, + uint32_t kind, + uint64_t filesize, + const uint8_t* fname, + size_t fnameLen, void *vCore); - static void onFileControlCallback(Tox *tox, uint32_t friendId, - uint32_t fileId, TOX_FILE_CONTROL control, - void *core); + static void onFileControlCallback(Tox *tox, uint32_t friendId, uint32_t fileId, + TOX_FILE_CONTROL control, void *core); static void onFileDataCallback(Tox *tox, uint32_t friendId, uint32_t fileId, uint64_t pos, size_t length, void *core); - static void onFileRecvChunkCallback(Tox *tox, uint32_t friendId, - uint32_t fileId, uint64_t position, - const uint8_t* data, size_t length, + static void onFileRecvChunkCallback(Tox *tox, + uint32_t friendId, + uint32_t fileId, + uint64_t position, + const uint8_t* data, + size_t length, void *vCore); - static void onConnectionStatusChanged(Core* core, uint32_t friendId, bool online); + static void onConnectionStatusChanged(Core* core, + uint32_t friendId, + bool online); private: static QMutex fileSendMutex;