diff --git a/testing/tox_sync.c b/testing/tox_sync.c index 77f75c36..785fffeb 100644 --- a/testing/tox_sync.c +++ b/testing/tox_sync.c @@ -46,43 +46,39 @@ #define NUM_FILE_SENDERS 256 typedef struct { FILE *file; - uint16_t friendnum; - uint8_t filenumber; - uint8_t nextpiece[1400]; - uint16_t piecelength; + uint32_t friendnum; + uint32_t filenumber; } File_t; File_t file_senders[NUM_FILE_SENDERS]; File_t file_recv[NUM_FILE_SENDERS]; uint8_t numfilesenders; -void send_filesenders(Tox *m) +void tox_file_request_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, size_t length, + void *user_data) { - uint32_t i; + unsigned int i; for (i = 0; i < NUM_FILE_SENDERS; ++i) { - if (file_senders[i].file == 0) - continue; - - while (1) { - if (tox_file_send_data(m, file_senders[i].friendnum, file_senders[i].filenumber, file_senders[i].nextpiece, - file_senders[i].piecelength) != 0) - break; - - file_senders[i].piecelength = fread(file_senders[i].nextpiece, 1, tox_file_data_size(m, file_senders[i].friendnum), - file_senders[i].file); - - if (file_senders[i].piecelength == 0) { + /* This is slow */ + if (file_senders[i].file && file_senders[i].friendnum == friend_number && file_senders[i].filenumber == file_number) { + if (length == 0) { fclose(file_senders[i].file); file_senders[i].file = 0; - - printf("[t] %u file transfer: %u completed %i\n", file_senders[i].friendnum, file_senders[i].filenumber, - tox_file_send_control(m, file_senders[i].friendnum, 0, file_senders[i].filenumber, TOX_FILECONTROL_FINISHED, 0, 0)); + printf("[t] %u file transfer: %u completed\n", file_senders[i].friendnum, file_senders[i].filenumber); break; } + + fseek(file_senders[i].file, position, SEEK_SET); + uint8_t data[length]; + int len = fread(data, 1, length, file_senders[i].file); + tox_file_send_chunk(tox, friend_number, file_number, data, len, 0); + break; } } } -int add_filesender(Tox *m, uint16_t friendnum, char *filename) + + +uint32_t add_filesender(Tox *m, uint16_t friendnum, char *filename) { FILE *tempfile = fopen(filename, "rb"); @@ -92,22 +88,20 @@ int add_filesender(Tox *m, uint16_t friendnum, char *filename) fseek(tempfile, 0, SEEK_END); uint64_t filesize = ftell(tempfile); fseek(tempfile, 0, SEEK_SET); - int filenum = tox_new_file_sender(m, friendnum, filesize, (uint8_t *)filename, strlen(filename) + 1); + uint32_t filenum = tox_file_send(m, friendnum, TOX_FILE_KIND_DATA, filesize, (uint8_t *)filename, strlen(filename) + 1, + 0); if (filenum == -1) return -1; file_senders[numfilesenders].file = tempfile; - file_senders[numfilesenders].piecelength = fread(file_senders[numfilesenders].nextpiece, 1, tox_file_data_size(m, - file_senders[numfilesenders].friendnum), - file_senders[numfilesenders].file); file_senders[numfilesenders].friendnum = friendnum; file_senders[numfilesenders].filenumber = filenum; ++numfilesenders; return filenum; } -void kill_filesender(Tox *m, uint8_t filenum) +void kill_filesender(Tox *m, uint32_t filenum) { uint32_t i; @@ -130,9 +124,15 @@ int not_sending() static char path[1024]; -void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, const uint8_t *filename, - uint16_t filename_length, void *userdata) +void file_request_accept(Tox *tox, uint32_t friend_number, uint32_t file_number, TOX_FILE_KIND type, + uint64_t file_size, const uint8_t *filename, size_t filename_length, void *user_data) { + if (type != TOX_FILE_KIND_DATA) { + printf("Refused invalid file type."); + tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0); + return; + } + char fullpath[1024]; uint32_t i; uint16_t rm = 0; @@ -151,48 +151,62 @@ void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t if (tempfile != 0) { fclose(tempfile); - tox_file_send_control(m, friendnumber, 1, filenumber, TOX_FILECONTROL_KILL, 0, 0); + tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0); return; } - file_recv[filenumber].file = fopen(fullpath, "wb"); + uint8_t file_index = (file_number >> 16) - 1; + file_recv[file_index].file = fopen(fullpath, "wb"); - if (file_recv[filenumber].file == 0) { - tox_file_send_control(m, friendnumber, 1, filenumber, TOX_FILECONTROL_KILL, 0, 0); + if (file_recv[file_index].file == 0) { + tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0); return; } - if (tox_file_send_control(m, friendnumber, 1, filenumber, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) { + if (tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME, 0)) { printf("Accepted file transfer. (file: %s)\n", fullpath); } } -void file_print_control(Tox *m, int friendnumber, uint8_t recieve_send, uint8_t filenumber, uint8_t control_type, - const uint8_t *data, - uint16_t length, void *userdata) +void file_print_control(Tox *tox, uint32_t friend_number, uint32_t file_number, TOX_FILE_CONTROL control, + void *user_data) { - if (recieve_send == 1 && (control_type == TOX_FILECONTROL_KILL || control_type == TOX_FILECONTROL_FINISHED)) { - kill_filesender(m, filenumber); + if (file_number < (1 << 15) && (control == TOX_FILE_CONTROL_CANCEL)) { + kill_filesender(tox, file_number); return; } - if (recieve_send == 0 && (control_type == TOX_FILECONTROL_KILL || control_type == TOX_FILECONTROL_FINISHED)) { - fclose(file_recv[filenumber].file); + if (file_number > (1 << 15) && (control == TOX_FILE_CONTROL_CANCEL)) { + uint8_t file_index = (file_number >> 16) - 1; + fclose(file_recv[file_index].file); printf("File closed\n"); - file_recv[filenumber].file = 0; + file_recv[file_index].file = 0; return; } } -void write_file(Tox *m, int friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length, void *userdata) +void write_file(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, const uint8_t *data, + size_t length, void *user_data) { - if (file_recv[filenumber].file != 0) - if (fwrite(data, length, 1, file_recv[filenumber].file) != 1) + uint8_t file_index = (filenumber >> 16) - 1; + if (length == 0) { + fclose(file_recv[file_index].file); + printf("File closed\n"); + file_recv[file_index].file = 0; + printf("%u file transfer: %u completed\n", friendnumber, filenumber); + return; + } + + if (file_recv[file_index].file != 0) { + fseek(file_recv[file_index].file, position, SEEK_SET); + + if (fwrite(data, length, 1, file_recv[file_index].file) != 1) printf("Error writing data\n"); + } } -void print_online(Tox *tox, uint32_t friendnumber, uint8_t status, void *userdata) +void print_online(Tox *tox, uint32_t friendnumber, TOX_CONNECTION status, void *userdata) { if (status) printf("\nOther went online.\n"); @@ -215,9 +229,10 @@ int main(int argc, char *argv[]) } Tox *tox = tox_new(0, 0, 0, 0); - tox_callback_file_data(tox, write_file, NULL); + tox_callback_file_receive_chunk(tox, write_file, NULL); tox_callback_file_control(tox, file_print_control, NULL); - tox_callback_file_send_request(tox, file_request_accept, NULL); + tox_callback_file_receive(tox, file_request_accept, NULL); + tox_callback_file_request_chunk(tox, tox_file_request_chunk, NULL); tox_callback_friend_connection_status(tox, print_online, NULL); uint16_t port = atoi(argv[argvoffset + 2]); @@ -290,7 +305,6 @@ int main(int argc, char *argv[]) } } - send_filesenders(tox); tox_iteration(tox); c_sleep(1); }