1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(core): simplify code for tox interval during file transfer

Also some style changes.
This commit is contained in:
Zetok Zalbavar 2016-12-20 09:21:45 +00:00
parent dccef4d49f
commit 541bc0e174
No known key found for this signature in database
GPG Key ID: C953D3880212068A
2 changed files with 55 additions and 26 deletions

View File

@ -49,21 +49,23 @@ using namespace std;
*/ */
unsigned CoreFile::corefileIterationInterval() 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; Sleep at most 1000ms if we have no FT, 10 for user FTs
unsigned interval = idleInterval; 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) for (ToxFile& file : fileMap)
{ {
if (file.status == ToxFile::TRANSMITTING) if (file.status == ToxFile::TRANSMITTING)
{ {
if (file.fileKind == TOX_FILE_KIND_DATA) return fileInterval;
return fastFileInterval;
else
interval = slowFileInterval;
} }
} }
return interval; return idleInterval;
} }
void CoreFile::sendAvatarFile(Core* core, uint32_t friendId, const QByteArray& data) void CoreFile::sendAvatarFile(Core* core, uint32_t friendId, const QByteArray& data)

View File

@ -44,14 +44,33 @@ private:
// Internal file sending APIs, used by Core. Public API in core.h // Internal file sending APIs, used by Core. Public API in core.h
private: private:
static void sendFile(Core *core, uint32_t friendId, QString filename, QString filePath, long long filesize); static void sendFile(Core *core,
static void sendAvatarFile(Core* core, uint32_t friendId, const QByteArray& data); uint32_t friendId,
static void pauseResumeFileSend(Core* core, uint32_t friendId, uint32_t fileId); QString filename,
static void pauseResumeFileRecv(Core* core, uint32_t friendId, uint32_t fileId); QString filePath,
static void cancelFileSend(Core* core, uint32_t friendId, uint32_t fileId); long long filesize);
static void cancelFileRecv(Core* core, uint32_t friendId, uint32_t fileId); static void sendAvatarFile(Core* core,
static void rejectFileRecvRequest(Core* core, uint32_t friendId, uint32_t fileId); uint32_t friendId,
static void acceptFileRecvRequest(Core* core, uint32_t friendId, uint32_t fileId, QString path); 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 ToxFile *findFile(uint32_t friendId, uint32_t fileId);
static void addFile(uint32_t friendId, uint32_t fileId, const ToxFile& file); static void addFile(uint32_t friendId, uint32_t fileId, const ToxFile& file);
static void removeFile(uint32_t friendId, uint32_t fileId); static void removeFile(uint32_t friendId, uint32_t fileId);
@ -62,20 +81,28 @@ private:
} }
private: private:
static void onFileReceiveCallback(Tox*, uint32_t friendId, uint32_t fileId, static void onFileReceiveCallback(Tox*,
uint32_t kind, uint64_t filesize, uint32_t friendId,
const uint8_t* fname, size_t fnameLen, uint32_t fileId,
uint32_t kind,
uint64_t filesize,
const uint8_t* fname,
size_t fnameLen,
void *vCore); void *vCore);
static void onFileControlCallback(Tox *tox, uint32_t friendId, static void onFileControlCallback(Tox *tox, uint32_t friendId, uint32_t fileId,
uint32_t fileId, TOX_FILE_CONTROL control, TOX_FILE_CONTROL control, void *core);
void *core);
static void onFileDataCallback(Tox *tox, uint32_t friendId, uint32_t fileId, static void onFileDataCallback(Tox *tox, uint32_t friendId, uint32_t fileId,
uint64_t pos, size_t length, void *core); uint64_t pos, size_t length, void *core);
static void onFileRecvChunkCallback(Tox *tox, uint32_t friendId, static void onFileRecvChunkCallback(Tox *tox,
uint32_t fileId, uint64_t position, uint32_t friendId,
const uint8_t* data, size_t length, uint32_t fileId,
uint64_t position,
const uint8_t* data,
size_t length,
void *vCore); void *vCore);
static void onConnectionStatusChanged(Core* core, uint32_t friendId, bool online); static void onConnectionStatusChanged(Core* core,
uint32_t friendId,
bool online);
private: private:
static QMutex fileSendMutex; static QMutex fileSendMutex;