File sending added to public api.

This commit is contained in:
irungentoo 2013-10-02 20:53:24 -04:00
parent 8a77211b43
commit fbd494a8b4
4 changed files with 184 additions and 14 deletions

View File

@ -87,8 +87,8 @@ void send_filesenders(Tox *m)
continue;
while (1) {
if (!file_data(m, file_senders[i].friendnum, file_senders[i].filenumber, file_senders[i].nextpiece,
file_senders[i].piecelength))
if (!tox_file_senddata(m, file_senders[i].friendnum, file_senders[i].filenumber, file_senders[i].nextpiece,
file_senders[i].piecelength))
break;
file_senders[i].piecelength = fread(file_senders[i].nextpiece, 1, 1000, file_senders[i].file);
@ -96,7 +96,7 @@ void send_filesenders(Tox *m)
if (file_senders[i].piecelength == 0) {
fclose(file_senders[i].file);
file_senders[i].file = 0;
file_control(m, file_senders[i].friendnum, 0, file_senders[i].filenumber, 3, 0, 0);
tox_file_sendcontrol(m, file_senders[i].friendnum, 0, file_senders[i].filenumber, 3, 0, 0);
char msg[512];
sprintf(msg, "[t] %u file transfer: %u completed", file_senders[i].friendnum, file_senders[i].filenumber);
new_lines(msg);
@ -115,7 +115,7 @@ 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 = new_filesender(m, friendnum, filesize, filename, strlen(filename) + 1);
int filenum = tox_new_filesender(m, friendnum, filesize, (uint8_t *)filename, strlen(filename) + 1);
if (filenum == -1)
return -1;
@ -665,7 +665,7 @@ void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t
sprintf(msg, "[t] %u is sending us: %s of size %llu", friendnumber, filename, filesize);
new_lines(msg);
if (file_control(m, friendnumber, 1, filenumber, 0, 0, 0)) {
if (tox_file_sendcontrol(m, friendnumber, 1, filenumber, 0, 0, 0)) {
sprintf(msg, "Accepted file transfer. (saving file as: %u.%u.bin)", friendnumber, filenumber);
new_lines(msg);
} else
@ -679,7 +679,7 @@ void file_print_control(Tox *m, int friendnumber, uint8_t send_recieve, uint8_t
char msg[512] = {0};
if (control_type == 0)
sprintf(msg, "[t] %u accepted file transfer: %u", friendnumber, filenumber, friendnumber, filenumber);
sprintf(msg, "[t] %u accepted file transfer: %u", friendnumber, filenumber);
else if (control_type == 3)
sprintf(msg, "[t] %u file transfer: %u completed", friendnumber, filenumber);
else
@ -694,7 +694,7 @@ void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uin
sprintf(filename, "%u.%u.bin", friendnumber, filenumber);
FILE *pFile = fopen(filename, "a");
if (file_dataremaining(m, friendnumber, filenumber, 1) == 0) {
if (tox_file_dataremaining(m, friendnumber, filenumber, 1) == 0) {
//file_control(m, friendnumber, 1, filenumber, 3, 0, 0);
char msg[512];
sprintf(msg, "[t] %u file transfer: %u completed", friendnumber, filenumber);
@ -752,9 +752,9 @@ int main(int argc, char *argv[])
tox_callback_statusmessage(m, print_statuschange, NULL);
tox_callback_group_invite(m, print_invite, NULL);
tox_callback_group_message(m, print_groupmessage, NULL);
callback_file_data(m, write_file, NULL);
callback_file_control(m, file_print_control, NULL);
callback_file_sendrequest(m, file_request_accept, NULL);
tox_callback_file_data(m, write_file, NULL);
tox_callback_file_control(m, file_print_control, NULL);
tox_callback_file_sendrequest(m, file_request_accept, NULL);
initscr();
noecho();

View File

@ -484,8 +484,7 @@ void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int,
*
*/
void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *,
uint16_t,
void *), void *userdata);
uint16_t, void *), void *userdata);
/* Set the callback for file data.
*

View File

@ -473,7 +473,90 @@ int tox_group_message_send(void *tox, int groupnumber, uint8_t *message, uint32_
/******************END OF GROUP CHAT FUNCTIONS************************/
/****************FILE SENDING FUNCTIONS*****************/
/* Set the callback for file send requests.
*
* Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
*/
void tox_callback_file_sendrequest(void *tox, void (*function)(Messenger *tox, int, uint8_t, uint64_t, uint8_t *,
uint16_t,
void *), void *userdata)
{
Messenger *m = tox;
callback_file_sendrequest(m, function, userdata);
}
/* Set the callback for file control requests.
*
* Function(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
*
*/
void tox_callback_file_control(void *tox, void (*function)(Messenger *tox, int, uint8_t, uint8_t, uint8_t, uint8_t *,
uint16_t, void *), void *userdata)
{
Messenger *m = tox;
callback_file_control(m, function, userdata);
}
/* Set the callback for file data.
*
* Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
*
*/
void tox_callback_file_data(void *tox, void (*function)(Messenger *tox, int, uint8_t, uint8_t *, uint16_t length,
void *),
void *userdata)
{
Messenger *m = tox;
callback_file_data(m, function, userdata);
}
/* Send a file send request.
* Maximum filename length is 255 bytes.
* return file number on success
* return -1 on failure
*/
int tox_new_filesender(void *tox, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length)
{
Messenger *m = tox;
return new_filesender(m, friendnumber, filesize, filename, filename_length);
}
/* Send a file control request.
* send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file.
*
* return 1 on success
* return 0 on failure
*/
int tox_file_sendcontrol(void *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
uint8_t *data, uint16_t length)
{
Messenger *m = tox;
return file_control(m, friendnumber, send_receive, filenumber, message_id, data, length);
}
/* Send file data.
*
* return 1 on success
* return 0 on failure
*/
int tox_file_senddata(void *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length)
{
Messenger *m = tox;
return file_data(m, friendnumber, filenumber, data, length);
}
/* Give the number of bytes left to be sent/received.
*
* send_receive is 0 if we want the sending files, 1 if we want the receiving.
*
* return number of bytes remaining to be sent/received on success
* return 0 on failure
*/
uint64_t tox_file_dataremaining(void *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive)
{
Messenger *m = tox;
return file_dataremaining(m, friendnumber, filenumber, send_receive);
}
/***************END OF FILE SENDING FUNCTIONS******************/
/* Use these functions to bootstrap the client.
* Sends a get nodes request to the given node with ip port and public_key.

View File

@ -426,8 +426,96 @@ int tox_join_groupchat(Tox *tox, int friendnumber, uint8_t *friend_group_public_
int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t length);
/****************FILE SENDING FUNCTIONS*****************/
/* NOTE: This how to will be updated.
*
* HOW TO SEND FILES CORRECTLY:
* 1. Use tox_new_filesender(...) to create a new file sender.
* 2. Wait for the callback set with tox_callback_file_control(...) to be called with receive_send == 1 and control_type == TOX_FILECONTROL_ACCEPT
* 3. Send the data with tox_file_senddata(...)
* 4. When sending is done, send a tox_file_sendcontrol(...) with send_receive = 0 and message_id = TOX_FILECONTROL_FINISHED
*
* HOW TO RECEIVE FILES CORRECTLY:
* 1. wait for the callback set with tox_callback_file_sendrequest(...)
* 2. accept or refuse the connection with tox_file_sendcontrol(...) with send_receive = 1 and message_id = TOX_FILECONTROL_ACCEPT or TOX_FILECONTROL_KILL
* 3. save all the data received with the callback set with tox_callback_file_data(...) to a file.
* 4. when the callback set with tox_callback_file_control(...) is called with receive_send == 0 and control_type == TOX_FILECONTROL_FINISHED
* the file is done transferring.
*
* tox_file_dataremaining(...) can be used to know how many bytes are left to send/receive.
*
* More to come...
*/
enum {
TOX_FILECONTROL_ACCEPT,
TOX_FILECONTROL_PAUSE,
TOX_FILECONTROL_KILL,
TOX_FILECONTROL_FINISHED
};
/* Set the callback for file send requests.
*
* Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
*/
void tox_callback_file_sendrequest(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint64_t, uint8_t *, uint16_t,
void *), void *userdata);
/* Set the callback for file control requests.
*
* receive_send is 1 if the message is for a slot on which we are currently sending a file and 0 if the message
* is for a slot on which we are receiving the file
*
* Function(Tox *tox, int friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
*
*/
void tox_callback_file_control(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint8_t, uint8_t, uint8_t *,
uint16_t, void *), void *userdata);
/* Set the callback for file data.
*
* Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
*
*/
void tox_callback_file_data(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint8_t *, uint16_t length, void *),
void *userdata);
/* Send a file send request.
* Maximum filename length is 255 bytes.
* return file number on success
* return -1 on failure
*/
int tox_new_filesender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length);
/* Send a file control request.
*
* send_receive is 0 if we want the control packet to target a file we are currently sending,
* 1 if it targets a file we are currently receiving.
*
* return 1 on success
* return 0 on failure
*/
int tox_file_sendcontrol(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
uint8_t *data, uint16_t length);
/* Send file data.
*
* return 1 on success
* return 0 on failure
*/
int tox_file_senddata(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length);
/* Give the number of bytes left to be sent/received.
*
* send_receive is 0 if we want the sending files, 1 if we want the receiving.
*
* return number of bytes remaining to be sent/received on success
* return 0 on failure
*/
uint64_t tox_file_dataremaining(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive);
/***************END OF FILE SENDING FUNCTIONS******************/
/******************END OF GROUP CHAT FUNCTIONS************************/
/*
* Use these two functions to bootstrap the client.