Transfers with filelength == 0 now behave exactly like transfers of every

other size (except streaming of course).
This commit is contained in:
irungentoo 2015-03-21 08:35:31 -04:00
parent f2a017c055
commit 8c2347222a
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
5 changed files with 101 additions and 61 deletions

View File

@ -145,16 +145,21 @@ void tox_file_receive(Tox *tox, uint32_t friend_number, uint32_t file_number, ui
}
file_size = filesize;
sending_pos = size_recv = 1337;
TOX_ERR_FILE_SEEK err_s;
if (filesize) {
sending_pos = size_recv = 1337;
if (!tox_file_send_seek(tox, friend_number, file_number, 1337, &err_s)) {
ck_abort_msg("tox_file_send_seek error");
TOX_ERR_FILE_SEEK err_s;
if (!tox_file_send_seek(tox, friend_number, file_number, 1337, &err_s)) {
ck_abort_msg("tox_file_send_seek error");
}
ck_assert_msg(err_s == TOX_ERR_FILE_SEEK_OK, "tox_file_send_seek wrong error");
} else {
sending_pos = size_recv = 0;
}
ck_assert_msg(err_s == TOX_ERR_FILE_SEEK_OK, "tox_file_send_seek wrong error");
TOX_ERR_FILE_CONTROL error;
if (tox_file_send_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME, &error)) {
@ -163,6 +168,8 @@ void tox_file_receive(Tox *tox, uint32_t friend_number, uint32_t file_number, ui
ck_abort_msg("tox_file_send_control failed. %i", error);
}
TOX_ERR_FILE_SEEK err_s;
if (tox_file_send_seek(tox, friend_number, file_number, 1234, &err_s)) {
ck_abort_msg("tox_file_send_seek no error");
}
@ -593,11 +600,13 @@ START_TEST(test_few_clients)
tox_iterate(tox3);
if (file_sending_done) {
if (sendf_ok && file_recv && totalf_size == file_size && size_recv == file_size && sending_pos == size_recv) {
if (sendf_ok && file_recv && totalf_size == file_size && size_recv == file_size && sending_pos == size_recv
&& file_accepted == 1) {
break;
} else {
ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %llu %llu %llu", sendf_ok, file_recv,
totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, totalf_size, size_recv, sending_pos);
ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %llu %llu %llu", sendf_ok, file_recv,
totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1, totalf_size, size_recv,
sending_pos);
}
}
@ -644,11 +653,12 @@ START_TEST(test_few_clients)
if (file_sending_done) {
if (sendf_ok && file_recv && m_send_reached && totalf_size == file_size && size_recv == max_sending
&& sending_pos == size_recv) {
&& sending_pos == size_recv && file_accepted == 1) {
break;
} else {
ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %llu %llu %llu %llu", sendf_ok, file_recv,
m_send_reached, totalf_size == file_size, size_recv == max_sending, sending_pos == size_recv, totalf_size, file_size,
ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %u %llu %llu %llu %llu", sendf_ok, file_recv,
m_send_reached, totalf_size == file_size, size_recv == max_sending, sending_pos == size_recv, file_accepted == 1,
totalf_size, file_size,
size_recv, sending_pos);
}
}
@ -664,6 +674,54 @@ START_TEST(test_few_clients)
}
}
printf("Starting file 0 transfer test.\n");
file_sending_done = file_accepted = file_size = file_recv = sendf_ok = size_recv = 0;
f_time = time(NULL);
tox_callback_file_recv_chunk(tox3, write_file, &to_compare);
tox_callback_file_recv_control(tox2, file_print_control, &to_compare);
tox_callback_file_chunk_request(tox2, tox_file_chunk_request, &to_compare);
tox_callback_file_recv_control(tox3, file_print_control, &to_compare);
tox_callback_file_recv(tox3, tox_file_receive, &to_compare);
totalf_size = 0;
fnum = tox_file_send(tox2, 0, TOX_FILE_KIND_DATA, totalf_size, 0, (uint8_t *)"Gentoo.exe", sizeof("Gentoo.exe"), 0);
ck_assert_msg(fnum != UINT32_MAX, "tox_new_file_sender fail");
ck_assert_msg(!tox_file_get_file_id(tox2, 1, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id didn't fail");
ck_assert_msg(gfierr == TOX_ERR_FILE_GET_FRIEND_NOT_FOUND, "wrong error");
ck_assert_msg(!tox_file_get_file_id(tox2, 0, fnum + 1, file_cmp_id, &gfierr), "tox_file_get_file_id didn't fail");
ck_assert_msg(gfierr == TOX_ERR_FILE_GET_NOT_FOUND, "wrong error");
ck_assert_msg(tox_file_get_file_id(tox2, 0, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id failed");
ck_assert_msg(gfierr == TOX_ERR_FILE_GET_OK, "wrong error");
while (1) {
tox_iterate(tox1);
tox_iterate(tox2);
tox_iterate(tox3);
if (file_sending_done) {
if (sendf_ok && file_recv && totalf_size == file_size && size_recv == file_size && sending_pos == size_recv
&& file_accepted == 1) {
break;
} else {
ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %llu %llu %llu", sendf_ok, file_recv,
totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1, totalf_size, size_recv,
sending_pos);
}
}
uint32_t tox1_interval = tox_iteration_interval(tox1);
uint32_t tox2_interval = tox_iteration_interval(tox2);
uint32_t tox3_interval = tox_iteration_interval(tox3);
if (tox2_interval > tox3_interval) {
c_sleep(tox3_interval);
} else {
c_sleep(tox2_interval);
}
}
printf("test_few_clients succeeded, took %llu seconds\n", time(NULL) - cur_time);
tox_kill(tox1);

View File

@ -1091,7 +1091,7 @@ static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t fi
* return -2 if filename length invalid.
* return -3 if no more file sending slots left.
* return -4 if could not send packet (friend offline).
* return -5 if succesfully sent file send request with filesize 0.
*
*/
long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_type, uint64_t filesize,
const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length)
@ -1115,22 +1115,18 @@ long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_
if (file_sendrequest(m, friendnumber, i, file_type, filesize, file_id, filename, filename_length) == 0)
return -4;
/* Only init file if filesize isn't 0. */
if (filesize) {
struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[i];
ft->status = FILESTATUS_NOT_ACCEPTED;
ft->size = filesize;
ft->transferred = 0;
ft->requested = 0;
ft->slots_allocated = 0;
ft->paused = FILE_PAUSE_NOT;
memcpy(ft->id, file_id, FILE_ID_LENGTH);
struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[i];
ft->status = FILESTATUS_NOT_ACCEPTED;
ft->size = filesize;
ft->transferred = 0;
ft->requested = 0;
ft->slots_allocated = 0;
ft->paused = FILE_PAUSE_NOT;
memcpy(ft->id, file_id, FILE_ID_LENGTH);
++m->friendlist[friendnumber].num_sending_files;
return i;
} else {
return -5;
}
++m->friendlist[friendnumber].num_sending_files;
return i;
}
int send_file_control_packet(const Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber,
@ -1473,6 +1469,12 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber)
uint16_t length = MAX_FILE_DATA_SIZE;
if (ft->size == 0) {
/* Send 0 data to friend if file is 0 length. */
file_data(m, friendnumber, i, 0, 0, 0);
break;
}
if (ft->size == ft->requested) {
break;
}
@ -2051,20 +2053,11 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
if (ft->status != FILESTATUS_NONE)
break;
uint32_t real_filenumber = UINT32_MAX;
if (filesize) {
/* Don't */
ft->status = FILESTATUS_NOT_ACCEPTED;
ft->size = filesize;
ft->transferred = 0;
ft->paused = FILE_PAUSE_NOT;
memcpy(ft->id, data + 1 + sizeof(uint32_t) + sizeof(uint64_t), FILE_ID_LENGTH);
real_filenumber = filenumber;
real_filenumber += 1;
real_filenumber <<= 16;
}
ft->status = FILESTATUS_NOT_ACCEPTED;
ft->size = filesize;
ft->transferred = 0;
ft->paused = FILE_PAUSE_NOT;
memcpy(ft->id, data + 1 + sizeof(uint32_t) + sizeof(uint64_t), FILE_ID_LENGTH);
uint8_t filename_terminated[filename_length + 1];
uint8_t *filename = NULL;
@ -2076,6 +2069,9 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
filename = filename_terminated;
}
uint32_t real_filenumber = filenumber;
real_filenumber += 1;
real_filenumber <<= 16;
if (m->file_sendrequest)
(*m->file_sendrequest)(m, i, real_filenumber, file_type, filesize, filename, filename_length,
@ -2099,13 +2095,13 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
}
case PACKET_ID_FILE_DATA: {
if (data_length <= 1)
if (data_length < 1)
break;
uint8_t filenumber = data[0];
struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber];
if (ft->status == FILESTATUS_NONE)
if (ft->status != FILESTATUS_TRANSFERRING)
break;
uint64_t position = ft->transferred;
@ -2126,7 +2122,7 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
ft->transferred += file_data_length;
if (ft->transferred >= ft->size || file_data_length != MAX_FILE_DATA_SIZE) {
if (file_data_length && (ft->transferred >= ft->size || file_data_length != MAX_FILE_DATA_SIZE)) {
file_data_length = 0;
file_data = NULL;
position = ft->transferred;

View File

@ -618,7 +618,7 @@ int file_get_id(const Messenger *m, int32_t friendnumber, uint32_t filenumber, u
* return -2 if filename length invalid.
* return -3 if no more file sending slots left.
* return -4 if could not send packet (friend offline).
* return -5 if succesfully sent file send request with filesize 0.
*
*/
long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_type, uint64_t filesize,
const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length);

View File

@ -1000,10 +1000,6 @@ uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t
case -4:
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_FRIEND_NOT_CONNECTED);
return UINT32_MAX;
case -5:
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_OK_ZERO_LENGTH);
return UINT32_MAX;
}
/* can't happen */

View File

@ -1610,12 +1610,7 @@ typedef enum TOX_ERR_FILE_SEND {
* Too many ongoing transfers. The maximum number of concurrent file transfers
* is 256 per friend per direction (sending and receiving).
*/
TOX_ERR_FILE_SEND_TOO_MANY,
/**
* A file request packet was successfully sent to the friend however since it was zero
* length, no file number was allocated for the file transfer.
*/
TOX_ERR_FILE_SEND_OK_ZERO_LENGTH
TOX_ERR_FILE_SEND_TOO_MANY
} TOX_ERR_FILE_SEND;
/**
@ -1634,9 +1629,6 @@ typedef enum TOX_ERR_FILE_SEND {
* When a friend goes offline, all file transfers associated with the friend are
* purged from core.
*
* if file_size is 0, this function will not allocate a file transfer in core and so
* will not return a valid file number however it will send a file request packet.
*
* If the file contents change during a transfer, the behaviour is unspecified
* in general. What will actually happen depends on the mode in which the file
* was modified and how the client determines the file size.
@ -1787,8 +1779,6 @@ void tox_callback_file_chunk_request(Tox *tox, tox_file_chunk_request_cb *functi
* control command before any other control commands. It can be accepted by
* sending TOX_FILE_CONTROL_RESUME.
*
* If file_size is zero, the file_number is invalid and should be ignored.
*
* @param friend_number The friend number of the friend who is sending the file
* transfer request.
* @param file_number The friend-specific file number the data received is