Skip to content

Commit 2073d02

Browse files
sudden6iphydf
authored andcommitted
fix: don't count filetransfer as sending until accepted
This fixes high CPU load in c-toxcore due to started but not accepted file transfers causing lots of iterations in do_all_filetransfers(...). Additionally this skips expensive calls max_speed_reached(...).
1 parent 4a2cb37 commit 2073d02

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

toxcore/Messenger.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,8 +1148,6 @@ long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_
11481148

11491149
memcpy(ft->id, file_id, FILE_ID_LENGTH);
11501150

1151-
++m->friendlist[friendnumber].num_sending_files;
1152-
11531151
return i;
11541152
}
11551153

@@ -1255,11 +1253,12 @@ int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber,
12551253

12561254
if (send_file_control_packet(m, friendnumber, send_receive, file_number, control, nullptr, 0)) {
12571255
if (control == FILECONTROL_KILL) {
1258-
ft->status = FILESTATUS_NONE;
1259-
1260-
if (send_receive == 0) {
1256+
if (send_receive == 0 && (ft->status == FILESTATUS_TRANSFERRING || ft->status == FILESTATUS_FINISHED)) {
1257+
// We are actively sending that file, remove from list
12611258
--m->friendlist[friendnumber].num_sending_files;
12621259
}
1260+
1261+
ft->status = FILESTATUS_NONE;
12631262
} else if (control == FILECONTROL_PAUSE) {
12641263
ft->paused |= FILE_PAUSE_US;
12651264
} else if (control == FILECONTROL_ACCEPT) {
@@ -1465,15 +1464,13 @@ static bool do_all_filetransfers(Messenger *m, int32_t friendnumber, void *userd
14651464
return false;
14661465
}
14671466

1468-
if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(
1469-
m->fr_c, friendcon->friendcon_id))) {
1470-
LOGGER_TRACE(m->log, "Maximum connection speed reached");
1471-
// connection doesn't support any more data
1472-
return false;
1473-
}
1474-
14751467
struct File_Transfers *const ft = &friendcon->file_sending[i];
14761468

1469+
if (ft->status == FILESTATUS_NONE || ft->status == FILESTATUS_NOT_ACCEPTED) {
1470+
// Filetransfers not actively sending, nothing to do
1471+
continue;
1472+
}
1473+
14771474
// If the file transfer is complete, we request a chunk of size 0.
14781475
if (ft->status == FILESTATUS_FINISHED && friend_received_packet(m, friendnumber, ft->last_packet_number) == 0) {
14791476
if (m->file_reqchunk) {
@@ -1483,9 +1480,7 @@ static bool do_all_filetransfers(Messenger *m, int32_t friendnumber, void *userd
14831480
// Now it's inactive, we're no longer sending this.
14841481
ft->status = FILESTATUS_NONE;
14851482
--friendcon->num_sending_files;
1486-
}
1487-
1488-
if (ft->status == FILESTATUS_TRANSFERRING && ft->paused == FILE_PAUSE_NOT) {
1483+
} else if (ft->status == FILESTATUS_TRANSFERRING && ft->paused == FILE_PAUSE_NOT) {
14891484
if (ft->size == 0) {
14901485
/* Send 0 data to friend if file is 0 length. */
14911486
file_data(m, friendnumber, i, 0, nullptr, 0);
@@ -1508,6 +1503,14 @@ static bool do_all_filetransfers(Messenger *m, int32_t friendnumber, void *userd
15081503
// The allocated slot is no longer free.
15091504
--*free_slots;
15101505
}
1506+
1507+
// Must be last to allow finishing file transfers
1508+
if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(
1509+
m->fr_c, friendcon->friendcon_id))) {
1510+
LOGGER_TRACE(m->log, "Maximum connection speed reached");
1511+
// connection doesn't support any more data
1512+
return false;
1513+
}
15111514
}
15121515

15131516
return true;
@@ -1614,6 +1617,7 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receiv
16141617
case FILECONTROL_ACCEPT: {
16151618
if (receive_send && ft->status == FILESTATUS_NOT_ACCEPTED) {
16161619
ft->status = FILESTATUS_TRANSFERRING;
1620+
++m->friendlist[friendnumber].num_sending_files;
16171621
} else {
16181622
if (ft->paused & FILE_PAUSE_OTHER) {
16191623
ft->paused ^= FILE_PAUSE_OTHER;
@@ -1652,12 +1656,12 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receiv
16521656
m->file_filecontrol(m, friendnumber, real_filenumber, control_type, userdata);
16531657
}
16541658

1655-
ft->status = FILESTATUS_NONE;
1656-
1657-
if (receive_send) {
1659+
if (receive_send && (ft->status == FILESTATUS_TRANSFERRING || ft->status == FILESTATUS_FINISHED)) {
16581660
--m->friendlist[friendnumber].num_sending_files;
16591661
}
16601662

1663+
ft->status = FILESTATUS_NONE;
1664+
16611665
return 0;
16621666
}
16631667

0 commit comments

Comments
 (0)