summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-03-12 16:05:18 -0400
committerirungentoo <irungentoo@gmail.com>2015-03-12 16:05:18 -0400
commit7e5ca487b5ad1b3482c731d7b26e3e8bfee619f4 (patch)
tree6e491259094abb175bfc9fea63978201687fd556
parent8dc2db2025815ed6924c0fc4a7d83377e209f6f8 (diff)
Don't assume tox_file_send_chunk() is called in the request chunk
callback.
-rw-r--r--toxcore/Messenger.c34
-rw-r--r--toxcore/Messenger.h2
2 files changed, 28 insertions, 8 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 03de5ea3..b062dcc9 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1078,10 +1078,14 @@ long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_
1078 if (file_sendrequest(m, friendnumber, i, file_type, filesize, filename, filename_length) == 0) 1078 if (file_sendrequest(m, friendnumber, i, file_type, filesize, filename, filename_length) == 0)
1079 return -4; 1079 return -4;
1080 1080
1081 m->friendlist[friendnumber].file_sending[i].status = FILESTATUS_NOT_ACCEPTED; 1081 struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[i];
1082 m->friendlist[friendnumber].file_sending[i].size = filesize; 1082 ft->status = FILESTATUS_NOT_ACCEPTED;
1083 m->friendlist[friendnumber].file_sending[i].transferred = 0; 1083 ft->size = filesize;
1084 m->friendlist[friendnumber].file_sending[i].paused = FILE_PAUSE_NOT; 1084 ft->transferred = 0;
1085 ft->requested = 0;
1086 ft->slots_allocated = 0;
1087 ft->paused = FILE_PAUSE_NOT;
1088
1085 ++m->friendlist[friendnumber].num_sending_files; 1089 ++m->friendlist[friendnumber].num_sending_files;
1086 1090
1087 return i; 1091 return i;
@@ -1254,6 +1258,10 @@ int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin
1254 //TODO record packet ids to check if other received complete file. 1258 //TODO record packet ids to check if other received complete file.
1255 ft->transferred += length; 1259 ft->transferred += length;
1256 1260
1261 if (ft->slots_allocated) {
1262 --ft->slots_allocated;
1263 }
1264
1257 if (length == 0 || ft->size == ft->transferred) { 1265 if (length == 0 || ft->size == ft->transferred) {
1258 ft->status = FILESTATUS_FINISHED; 1266 ft->status = FILESTATUS_FINISHED;
1259 ft->last_packet_number = ret; 1267 ft->last_packet_number = ret;
@@ -1324,6 +1332,13 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber)
1324 --m->friendlist[friendnumber].num_sending_files; 1332 --m->friendlist[friendnumber].num_sending_files;
1325 } 1333 }
1326 } 1334 }
1335
1336 /* TODO: if file is too slow, switch to the next. */
1337 if (ft->slots_allocated > free_slots) {
1338 free_slots = 0;
1339 } else {
1340 free_slots -= ft->slots_allocated;
1341 }
1327 } 1342 }
1328 1343
1329 while (ft->status == FILESTATUS_TRANSFERRING && (ft->paused == FILE_PAUSE_NOT)) { 1344 while (ft->status == FILESTATUS_TRANSFERRING && (ft->paused == FILE_PAUSE_NOT)) {
@@ -1333,18 +1348,21 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber)
1333 uint16_t length = MAX_CRYPTO_DATA_SIZE - 2; 1348 uint16_t length = MAX_CRYPTO_DATA_SIZE - 2;
1334 1349
1335 if (ft->size) { 1350 if (ft->size) {
1336 if (ft->size == ft->transferred) { 1351 if (ft->size == ft->requested) {
1337 break; 1352 break;
1338 } 1353 }
1339 1354
1340 if (ft->size - ft->transferred < length) { 1355 if (ft->size - ft->requested < length) {
1341 length = ft->size - ft->transferred; 1356 length = ft->size - ft->requested;
1342 } 1357 }
1343 } 1358 }
1344 1359
1345 if (m->file_reqchunk) 1360 if (m->file_reqchunk)
1346 (*m->file_reqchunk)(m, friendnumber, i, ft->transferred, length, m->file_reqchunk_userdata); 1361 (*m->file_reqchunk)(m, friendnumber, i, ft->requested, length, m->file_reqchunk_userdata);
1362
1363 ft->requested += length;
1347 1364
1365 ++ft->slots_allocated;
1348 --free_slots; 1366 --free_slots;
1349 } 1367 }
1350 1368
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index edb16cff..0cbef76d 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -131,6 +131,8 @@ struct File_Transfers {
131 uint8_t status; /* 0 == no transfer, 1 = not accepted, 3 = transferring, 4 = broken, 5 = finished */ 131 uint8_t status; /* 0 == no transfer, 1 = not accepted, 3 = transferring, 4 = broken, 5 = finished */
132 uint8_t paused; /* 0: not paused, 1 = paused by us, 2 = paused by other, 3 = paused by both. */ 132 uint8_t paused; /* 0: not paused, 1 = paused by us, 2 = paused by other, 3 = paused by both. */
133 uint32_t last_packet_number; /* number of the last packet sent. */ 133 uint32_t last_packet_number; /* number of the last packet sent. */
134 uint64_t requested; /* total data requested by the request chunk callback */
135 unsigned int slots_allocated; /* number of slots allocated to this transfer. */
134}; 136};
135enum { 137enum {
136 FILESTATUS_NONE, 138 FILESTATUS_NONE,