diff options
-rw-r--r-- | testing/nTox.c | 20 | ||||
-rw-r--r-- | toxcore/Messenger.h | 3 | ||||
-rw-r--r-- | toxcore/tox.c | 85 | ||||
-rw-r--r-- | toxcore/tox.h | 90 |
4 files changed, 184 insertions, 14 deletions
diff --git a/testing/nTox.c b/testing/nTox.c index 1051155f..f82c0fba 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -87,8 +87,8 @@ void send_filesenders(Tox *m) | |||
87 | continue; | 87 | continue; |
88 | 88 | ||
89 | while (1) { | 89 | while (1) { |
90 | if (!file_data(m, file_senders[i].friendnum, file_senders[i].filenumber, file_senders[i].nextpiece, | 90 | if (!tox_file_senddata(m, file_senders[i].friendnum, file_senders[i].filenumber, file_senders[i].nextpiece, |
91 | file_senders[i].piecelength)) | 91 | file_senders[i].piecelength)) |
92 | break; | 92 | break; |
93 | 93 | ||
94 | file_senders[i].piecelength = fread(file_senders[i].nextpiece, 1, 1000, file_senders[i].file); | 94 | file_senders[i].piecelength = fread(file_senders[i].nextpiece, 1, 1000, file_senders[i].file); |
@@ -96,7 +96,7 @@ void send_filesenders(Tox *m) | |||
96 | if (file_senders[i].piecelength == 0) { | 96 | if (file_senders[i].piecelength == 0) { |
97 | fclose(file_senders[i].file); | 97 | fclose(file_senders[i].file); |
98 | file_senders[i].file = 0; | 98 | file_senders[i].file = 0; |
99 | file_control(m, file_senders[i].friendnum, 0, file_senders[i].filenumber, 3, 0, 0); | 99 | tox_file_sendcontrol(m, file_senders[i].friendnum, 0, file_senders[i].filenumber, 3, 0, 0); |
100 | char msg[512]; | 100 | char msg[512]; |
101 | sprintf(msg, "[t] %u file transfer: %u completed", file_senders[i].friendnum, file_senders[i].filenumber); | 101 | sprintf(msg, "[t] %u file transfer: %u completed", file_senders[i].friendnum, file_senders[i].filenumber); |
102 | new_lines(msg); | 102 | new_lines(msg); |
@@ -115,7 +115,7 @@ int add_filesender(Tox *m, uint16_t friendnum, char *filename) | |||
115 | fseek(tempfile, 0, SEEK_END); | 115 | fseek(tempfile, 0, SEEK_END); |
116 | uint64_t filesize = ftell(tempfile); | 116 | uint64_t filesize = ftell(tempfile); |
117 | fseek(tempfile, 0, SEEK_SET); | 117 | fseek(tempfile, 0, SEEK_SET); |
118 | int filenum = new_filesender(m, friendnum, filesize, filename, strlen(filename) + 1); | 118 | int filenum = tox_new_filesender(m, friendnum, filesize, (uint8_t *)filename, strlen(filename) + 1); |
119 | 119 | ||
120 | if (filenum == -1) | 120 | if (filenum == -1) |
121 | return -1; | 121 | return -1; |
@@ -665,7 +665,7 @@ void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t | |||
665 | sprintf(msg, "[t] %u is sending us: %s of size %llu", friendnumber, filename, filesize); | 665 | sprintf(msg, "[t] %u is sending us: %s of size %llu", friendnumber, filename, filesize); |
666 | new_lines(msg); | 666 | new_lines(msg); |
667 | 667 | ||
668 | if (file_control(m, friendnumber, 1, filenumber, 0, 0, 0)) { | 668 | if (tox_file_sendcontrol(m, friendnumber, 1, filenumber, 0, 0, 0)) { |
669 | sprintf(msg, "Accepted file transfer. (saving file as: %u.%u.bin)", friendnumber, filenumber); | 669 | sprintf(msg, "Accepted file transfer. (saving file as: %u.%u.bin)", friendnumber, filenumber); |
670 | new_lines(msg); | 670 | new_lines(msg); |
671 | } else | 671 | } else |
@@ -679,7 +679,7 @@ void file_print_control(Tox *m, int friendnumber, uint8_t send_recieve, uint8_t | |||
679 | char msg[512] = {0}; | 679 | char msg[512] = {0}; |
680 | 680 | ||
681 | if (control_type == 0) | 681 | if (control_type == 0) |
682 | sprintf(msg, "[t] %u accepted file transfer: %u", friendnumber, filenumber, friendnumber, filenumber); | 682 | sprintf(msg, "[t] %u accepted file transfer: %u", friendnumber, filenumber); |
683 | else if (control_type == 3) | 683 | else if (control_type == 3) |
684 | sprintf(msg, "[t] %u file transfer: %u completed", friendnumber, filenumber); | 684 | sprintf(msg, "[t] %u file transfer: %u completed", friendnumber, filenumber); |
685 | else | 685 | else |
@@ -694,7 +694,7 @@ void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uin | |||
694 | sprintf(filename, "%u.%u.bin", friendnumber, filenumber); | 694 | sprintf(filename, "%u.%u.bin", friendnumber, filenumber); |
695 | FILE *pFile = fopen(filename, "a"); | 695 | FILE *pFile = fopen(filename, "a"); |
696 | 696 | ||
697 | if (file_dataremaining(m, friendnumber, filenumber, 1) == 0) { | 697 | if (tox_file_dataremaining(m, friendnumber, filenumber, 1) == 0) { |
698 | //file_control(m, friendnumber, 1, filenumber, 3, 0, 0); | 698 | //file_control(m, friendnumber, 1, filenumber, 3, 0, 0); |
699 | char msg[512]; | 699 | char msg[512]; |
700 | sprintf(msg, "[t] %u file transfer: %u completed", friendnumber, filenumber); | 700 | sprintf(msg, "[t] %u file transfer: %u completed", friendnumber, filenumber); |
@@ -752,9 +752,9 @@ int main(int argc, char *argv[]) | |||
752 | tox_callback_statusmessage(m, print_statuschange, NULL); | 752 | tox_callback_statusmessage(m, print_statuschange, NULL); |
753 | tox_callback_group_invite(m, print_invite, NULL); | 753 | tox_callback_group_invite(m, print_invite, NULL); |
754 | tox_callback_group_message(m, print_groupmessage, NULL); | 754 | tox_callback_group_message(m, print_groupmessage, NULL); |
755 | callback_file_data(m, write_file, NULL); | 755 | tox_callback_file_data(m, write_file, NULL); |
756 | callback_file_control(m, file_print_control, NULL); | 756 | tox_callback_file_control(m, file_print_control, NULL); |
757 | callback_file_sendrequest(m, file_request_accept, NULL); | 757 | tox_callback_file_sendrequest(m, file_request_accept, NULL); |
758 | 758 | ||
759 | initscr(); | 759 | initscr(); |
760 | noecho(); | 760 | noecho(); |
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 40b2fac2..5d31e15b 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h | |||
@@ -484,8 +484,7 @@ void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int, | |||
484 | * | 484 | * |
485 | */ | 485 | */ |
486 | void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, | 486 | void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, |
487 | uint16_t, | 487 | uint16_t, void *), void *userdata); |
488 | void *), void *userdata); | ||
489 | 488 | ||
490 | /* Set the callback for file data. | 489 | /* Set the callback for file data. |
491 | * | 490 | * |
diff --git a/toxcore/tox.c b/toxcore/tox.c index 1d7118be..21e8bf75 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -473,7 +473,90 @@ int tox_group_message_send(void *tox, int groupnumber, uint8_t *message, uint32_ | |||
473 | 473 | ||
474 | 474 | ||
475 | 475 | ||
476 | /******************END OF GROUP CHAT FUNCTIONS************************/ | 476 | /****************FILE SENDING FUNCTIONS*****************/ |
477 | |||
478 | |||
479 | /* Set the callback for file send requests. | ||
480 | * | ||
481 | * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) | ||
482 | */ | ||
483 | void tox_callback_file_sendrequest(void *tox, void (*function)(Messenger *tox, int, uint8_t, uint64_t, uint8_t *, | ||
484 | uint16_t, | ||
485 | void *), void *userdata) | ||
486 | { | ||
487 | Messenger *m = tox; | ||
488 | callback_file_sendrequest(m, function, userdata); | ||
489 | } | ||
490 | /* Set the callback for file control requests. | ||
491 | * | ||
492 | * Function(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) | ||
493 | * | ||
494 | */ | ||
495 | void tox_callback_file_control(void *tox, void (*function)(Messenger *tox, int, uint8_t, uint8_t, uint8_t, uint8_t *, | ||
496 | uint16_t, void *), void *userdata) | ||
497 | { | ||
498 | Messenger *m = tox; | ||
499 | callback_file_control(m, function, userdata); | ||
500 | } | ||
501 | /* Set the callback for file data. | ||
502 | * | ||
503 | * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) | ||
504 | * | ||
505 | */ | ||
506 | void tox_callback_file_data(void *tox, void (*function)(Messenger *tox, int, uint8_t, uint8_t *, uint16_t length, | ||
507 | void *), | ||
508 | void *userdata) | ||
509 | |||
510 | { | ||
511 | Messenger *m = tox; | ||
512 | callback_file_data(m, function, userdata); | ||
513 | } | ||
514 | /* Send a file send request. | ||
515 | * Maximum filename length is 255 bytes. | ||
516 | * return file number on success | ||
517 | * return -1 on failure | ||
518 | */ | ||
519 | int tox_new_filesender(void *tox, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length) | ||
520 | { | ||
521 | Messenger *m = tox; | ||
522 | return new_filesender(m, friendnumber, filesize, filename, filename_length); | ||
523 | } | ||
524 | /* Send a file control request. | ||
525 | * send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file. | ||
526 | * | ||
527 | * return 1 on success | ||
528 | * return 0 on failure | ||
529 | */ | ||
530 | int tox_file_sendcontrol(void *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, | ||
531 | uint8_t *data, uint16_t length) | ||
532 | { | ||
533 | Messenger *m = tox; | ||
534 | return file_control(m, friendnumber, send_receive, filenumber, message_id, data, length); | ||
535 | } | ||
536 | /* Send file data. | ||
537 | * | ||
538 | * return 1 on success | ||
539 | * return 0 on failure | ||
540 | */ | ||
541 | int tox_file_senddata(void *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) | ||
542 | { | ||
543 | Messenger *m = tox; | ||
544 | return file_data(m, friendnumber, filenumber, data, length); | ||
545 | } | ||
546 | /* Give the number of bytes left to be sent/received. | ||
547 | * | ||
548 | * send_receive is 0 if we want the sending files, 1 if we want the receiving. | ||
549 | * | ||
550 | * return number of bytes remaining to be sent/received on success | ||
551 | * return 0 on failure | ||
552 | */ | ||
553 | uint64_t tox_file_dataremaining(void *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive) | ||
554 | { | ||
555 | Messenger *m = tox; | ||
556 | return file_dataremaining(m, friendnumber, filenumber, send_receive); | ||
557 | } | ||
558 | |||
559 | /***************END OF FILE SENDING FUNCTIONS******************/ | ||
477 | 560 | ||
478 | /* Use these functions to bootstrap the client. | 561 | /* Use these functions to bootstrap the client. |
479 | * Sends a get nodes request to the given node with ip port and public_key. | 562 | * Sends a get nodes request to the given node with ip port and public_key. |
diff --git a/toxcore/tox.h b/toxcore/tox.h index eabbb1af..b1f18515 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h | |||
@@ -426,8 +426,96 @@ int tox_join_groupchat(Tox *tox, int friendnumber, uint8_t *friend_group_public_ | |||
426 | int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t length); | 426 | int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t length); |
427 | 427 | ||
428 | 428 | ||
429 | /****************FILE SENDING FUNCTIONS*****************/ | ||
430 | /* NOTE: This how to will be updated. | ||
431 | * | ||
432 | * HOW TO SEND FILES CORRECTLY: | ||
433 | * 1. Use tox_new_filesender(...) to create a new file sender. | ||
434 | * 2. Wait for the callback set with tox_callback_file_control(...) to be called with receive_send == 1 and control_type == TOX_FILECONTROL_ACCEPT | ||
435 | * 3. Send the data with tox_file_senddata(...) | ||
436 | * 4. When sending is done, send a tox_file_sendcontrol(...) with send_receive = 0 and message_id = TOX_FILECONTROL_FINISHED | ||
437 | * | ||
438 | * HOW TO RECEIVE FILES CORRECTLY: | ||
439 | * 1. wait for the callback set with tox_callback_file_sendrequest(...) | ||
440 | * 2. accept or refuse the connection with tox_file_sendcontrol(...) with send_receive = 1 and message_id = TOX_FILECONTROL_ACCEPT or TOX_FILECONTROL_KILL | ||
441 | * 3. save all the data received with the callback set with tox_callback_file_data(...) to a file. | ||
442 | * 4. when the callback set with tox_callback_file_control(...) is called with receive_send == 0 and control_type == TOX_FILECONTROL_FINISHED | ||
443 | * the file is done transferring. | ||
444 | * | ||
445 | * tox_file_dataremaining(...) can be used to know how many bytes are left to send/receive. | ||
446 | * | ||
447 | * More to come... | ||
448 | */ | ||
449 | |||
450 | enum { | ||
451 | TOX_FILECONTROL_ACCEPT, | ||
452 | TOX_FILECONTROL_PAUSE, | ||
453 | TOX_FILECONTROL_KILL, | ||
454 | TOX_FILECONTROL_FINISHED | ||
455 | }; | ||
456 | /* Set the callback for file send requests. | ||
457 | * | ||
458 | * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) | ||
459 | */ | ||
460 | void tox_callback_file_sendrequest(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, | ||
461 | void *), void *userdata); | ||
462 | |||
463 | /* Set the callback for file control requests. | ||
464 | * | ||
465 | * receive_send is 1 if the message is for a slot on which we are currently sending a file and 0 if the message | ||
466 | * is for a slot on which we are receiving the file | ||
467 | * | ||
468 | * Function(Tox *tox, int friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) | ||
469 | * | ||
470 | */ | ||
471 | void tox_callback_file_control(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, | ||
472 | uint16_t, void *), void *userdata); | ||
473 | |||
474 | /* Set the callback for file data. | ||
475 | * | ||
476 | * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) | ||
477 | * | ||
478 | */ | ||
479 | void tox_callback_file_data(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint8_t *, uint16_t length, void *), | ||
480 | void *userdata); | ||
481 | |||
482 | |||
483 | /* Send a file send request. | ||
484 | * Maximum filename length is 255 bytes. | ||
485 | * return file number on success | ||
486 | * return -1 on failure | ||
487 | */ | ||
488 | int tox_new_filesender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length); | ||
489 | |||
490 | /* Send a file control request. | ||
491 | * | ||
492 | * send_receive is 0 if we want the control packet to target a file we are currently sending, | ||
493 | * 1 if it targets a file we are currently receiving. | ||
494 | * | ||
495 | * return 1 on success | ||
496 | * return 0 on failure | ||
497 | */ | ||
498 | int tox_file_sendcontrol(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, | ||
499 | uint8_t *data, uint16_t length); | ||
500 | |||
501 | /* Send file data. | ||
502 | * | ||
503 | * return 1 on success | ||
504 | * return 0 on failure | ||
505 | */ | ||
506 | int tox_file_senddata(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length); | ||
507 | |||
508 | /* Give the number of bytes left to be sent/received. | ||
509 | * | ||
510 | * send_receive is 0 if we want the sending files, 1 if we want the receiving. | ||
511 | * | ||
512 | * return number of bytes remaining to be sent/received on success | ||
513 | * return 0 on failure | ||
514 | */ | ||
515 | uint64_t tox_file_dataremaining(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive); | ||
516 | |||
517 | /***************END OF FILE SENDING FUNCTIONS******************/ | ||
429 | 518 | ||
430 | /******************END OF GROUP CHAT FUNCTIONS************************/ | ||
431 | 519 | ||
432 | /* | 520 | /* |
433 | * Use these two functions to bootstrap the client. | 521 | * Use these two functions to bootstrap the client. |