File control packets can now be used by the person sending the file.

This commit is contained in:
irungentoo 2013-10-02 20:08:14 -04:00
parent 6182af8449
commit 6425cf3b53
3 changed files with 115 additions and 47 deletions

View File

@ -96,6 +96,10 @@ 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);
char msg[512];
sprintf(msg, "[t] %u file transfer: %u completed", file_senders[i].friendnum, file_senders[i].filenumber);
new_lines(msg);
break;
}
}
@ -661,14 +665,15 @@ 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, filenumber, 0, 0, 0)) {
if (file_control(m, friendnumber, 1, filenumber, 0, 0, 0)) {
sprintf(msg, "Accepted file transfer. (saving file as: %u.%u.bin)", friendnumber, filenumber);
new_lines(msg);
} else
new_lines("Could not accept file transfer.");
}
void file_print_control(Tox *m, int friendnumber, uint8_t filenumber, uint8_t control_type, uint8_t *data,
void file_print_control(Tox *m, int friendnumber, uint8_t send_recieve, uint8_t filenumber, uint8_t control_type,
uint8_t *data,
uint16_t length, void *userdata)
{
char msg[512] = {0};
@ -690,7 +695,7 @@ void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uin
FILE *pFile = fopen(filename, "a");
if (file_dataremaining(m, friendnumber, filenumber, 1) == 0) {
file_control(m, friendnumber, filenumber, 3, 0, 0);
//file_control(m, friendnumber, 1, filenumber, 3, 0, 0);
char msg[512];
sprintf(msg, "[t] %u file transfer: %u completed", friendnumber, filenumber);
new_lines(msg);

View File

@ -948,10 +948,11 @@ void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int,
/* Set the callback for file control requests.
*
* Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
* 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 callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t *, uint16_t,
void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *,
uint16_t,
void *), void *userdata)
{
m->file_filecontrol = function;
@ -1026,13 +1027,15 @@ int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *f
}
/* 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 file_control(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t message_id, uint8_t *data, uint16_t length)
int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
uint8_t *data, uint16_t length)
{
if (length > MAX_DATA_SIZE - 2)
if (length > MAX_DATA_SIZE - 3)
return 0;
if (friend_not_valid(m, friendnumber))
@ -1041,26 +1044,46 @@ int file_control(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t mes
if (m->friendlist[friendnumber].file_receiving[filenumber].status == 0)
return 0;
if (send_receive > 1)
return 0;
uint8_t packet[MAX_DATA_SIZE];
packet[0] = filenumber;
packet[1] = message_id;
memcpy(packet + 2, data, length);
packet[0] = send_receive;
packet[1] = filenumber;
packet[2] = message_id;
memcpy(packet + 3, data, length);
if (write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, length + 2)) {
switch (message_id) {
case FILECONTROL_ACCEPT:
m->friendlist[friendnumber].file_receiving[filenumber].status = 3;
break;
if (write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, length + 3)) {
if (send_receive == 1)
switch (message_id) {
case FILECONTROL_ACCEPT:
m->friendlist[friendnumber].file_receiving[filenumber].status = 3;
break;
case FILECONTROL_PAUSE:
m->friendlist[friendnumber].file_receiving[filenumber].status = 2;
break;
case FILECONTROL_PAUSE:
m->friendlist[friendnumber].file_receiving[filenumber].status = 5;
break;
case FILECONTROL_KILL:
case FILECONTROL_FINISHED:
m->friendlist[friendnumber].file_receiving[filenumber].status = 0;
break;
}
case FILECONTROL_KILL:
case FILECONTROL_FINISHED:
m->friendlist[friendnumber].file_receiving[filenumber].status = 0;
break;
}
else
switch (message_id) {
case FILECONTROL_ACCEPT:
m->friendlist[friendnumber].file_sending[filenumber].status = 3;
break;
case FILECONTROL_PAUSE:
m->friendlist[friendnumber].file_sending[filenumber].status = 5;
break;
case FILECONTROL_KILL:
case FILECONTROL_FINISHED:
m->friendlist[friendnumber].file_sending[filenumber].status = 0;
break;
}
return 1;
} else {
@ -1141,25 +1164,61 @@ static void break_files(Messenger *m, int friendnumber)
}
}
static int handle_filecontrol(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t message_id, uint8_t *data,
static int handle_filecontrol(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t filenumber,
uint8_t message_id, uint8_t *data,
uint16_t length)
{
if (m->friendlist[friendnumber].file_sending[filenumber].status == 0)
if (send_receive > 1)
return -1;
switch (message_id) {
case FILECONTROL_ACCEPT:
m->friendlist[friendnumber].file_sending[filenumber].status = 3;
return 0;
if (send_receive == 0) {
if (m->friendlist[friendnumber].file_receiving[filenumber].status == 0)
return -1;
case FILECONTROL_PAUSE:
m->friendlist[friendnumber].file_sending[filenumber].status = 2;
return 0;
switch (message_id) {
case FILECONTROL_ACCEPT:
if (m->friendlist[friendnumber].file_receiving[filenumber].status != 5) {
m->friendlist[friendnumber].file_receiving[filenumber].status = 3;
return 0;
}
case FILECONTROL_KILL:
case FILECONTROL_FINISHED:
m->friendlist[friendnumber].file_sending[filenumber].status = 0;
return 0;
return -1;
case FILECONTROL_PAUSE:
if (m->friendlist[friendnumber].file_receiving[filenumber].status != 5) {
m->friendlist[friendnumber].file_receiving[filenumber].status = 2;
return 0;
}
return -1;
case FILECONTROL_KILL:
case FILECONTROL_FINISHED:
m->friendlist[friendnumber].file_receiving[filenumber].status = 0;
return 0;
}
} else {
if (m->friendlist[friendnumber].file_sending[filenumber].status == 0)
return -1;
switch (message_id) {
case FILECONTROL_ACCEPT:
if (m->friendlist[friendnumber].file_sending[filenumber].status != 5) {
m->friendlist[friendnumber].file_sending[filenumber].status = 3;
return 0;
}
return -1;
case FILECONTROL_PAUSE:
m->friendlist[friendnumber].file_sending[filenumber].status = 2;
return 0;
case FILECONTROL_KILL:
case FILECONTROL_FINISHED:
m->friendlist[friendnumber].file_sending[filenumber].status = 0;
return 0;
}
}
return -1;
@ -1468,17 +1527,19 @@ void doFriends(Messenger *m)
}
case PACKET_ID_FILE_CONTROL: {
if (data_length < 2)
if (data_length < 3)
break;
uint8_t filenumber = data[0];
uint8_t control_type = data[1];
uint8_t send_receive = data[0];
uint8_t filenumber = data[1];
uint8_t control_type = data[2];
if (handle_filecontrol(m, i, filenumber, control_type, data + 2, data_length - 2) == -1)
if (handle_filecontrol(m, i, send_receive, filenumber, control_type, data + 3, data_length - 3) == -1)
break;
if (m->file_filecontrol)
(*m->file_filecontrol)(m, i, filenumber, control_type, data + 2, data_length - 2, m->file_filecontrol_userdata);
(*m->file_filecontrol)(m, i, send_receive, filenumber, control_type, data + 3, data_length - 3,
m->file_filecontrol_userdata);
break;
}

View File

@ -102,7 +102,7 @@ USERSTATUS;
struct File_Transfers {
uint64_t size;
uint64_t transferred;
uint8_t status; /* 0 == no transfer, 1 = not accepted, 2 = paused, 3 = transferring, 4 = broken*/
uint8_t status; /* 0 == no transfer, 1 = not accepted, 2 = paused by the other, 3 = transferring, 4 = broken, 5 = paused by us */
};
/* This cannot be bigger than 256 */
@ -186,7 +186,7 @@ typedef struct Messenger {
void (*file_sendrequest)(struct Messenger *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, void *);
void *file_sendrequest_userdata;
void (*file_filecontrol)(struct Messenger *m, int, uint8_t, uint8_t, uint8_t *, uint16_t, void *);
void (*file_filecontrol)(struct Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t, void *);
void *file_filecontrol_userdata;
void (*file_filedata)(struct Messenger *m, int, uint8_t, uint8_t *, uint16_t length, void *);
void *file_filedata_userdata;
@ -480,10 +480,11 @@ void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int,
/* Set the callback for file control requests.
*
* Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
* 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 callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t *, uint16_t,
void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *,
uint16_t,
void *), void *userdata);
/* Set the callback for file data.
@ -510,12 +511,13 @@ int file_sendrequest(Messenger *m, int friendnumber, uint8_t filenumber, uint64_
int new_filesender(Messenger *m, 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 sending file, 1 if it targets a receiving file.
*
* return 1 on success
* return 0 on failure
*/
int file_control(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t message_id, uint8_t *data,
uint16_t length);
int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
uint8_t *data, uint16_t length);
/* Send file data.
*