1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 06:13:03 +01:00

add file sender timeouts

This commit is contained in:
Jfreegman 2013-11-14 19:14:54 -05:00
parent be88c89e09
commit ee509a7cec
7 changed files with 57 additions and 26 deletions

View File

@ -181,6 +181,7 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive
break; break;
case TOX_FILECONTROL_KILL: case TOX_FILECONTROL_KILL:
wprintw(ctx->history, "File transfer for '%s' failed.\n", filename); wprintw(ctx->history, "File transfer for '%s' failed.\n", filename);
friends[num].file_receiver.pending[filenum] = false;
break; break;
case TOX_FILECONTROL_FINISHED: case TOX_FILECONTROL_FINISHED:
wprintw(ctx->history, "File transfer for '%s' complete.\n", filename); wprintw(ctx->history, "File transfer for '%s' complete.\n", filename);

View File

@ -168,6 +168,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *prompt, Tox *m, int num, int argc,
file_senders[i].file = file_to_send; file_senders[i].file = file_to_send;
file_senders[i].filenum = (uint8_t) filenum; file_senders[i].filenum = (uint8_t) filenum;
file_senders[i].friendnum = num; file_senders[i].friendnum = num;
file_senders[i].timestamp = (uint64_t)time(NULL);
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
tox_filedata_size(m, num), file_to_send); tox_filedata_size(m, num), file_to_send);

View File

@ -34,13 +34,14 @@ void sort_friendlist_index(void)
/* split friends into online and offline groups */ /* split friends into online and offline groups */
for (i = 0; i < max_friends_index; ++i) { for (i = 0; i < max_friends_index; ++i) {
if (friends[i].active) { if (!friends[i].active)
continue;
if (friends[i].online) if (friends[i].online)
on_friends[on_cnt++] = friends[i].num; on_friends[on_cnt++] = friends[i].num;
else else
off_friends[off_cnt++] = friends[i].num; off_friends[off_cnt++] = friends[i].num;
} }
}
/* update friendlist_index, putting online friends before offline friends */ /* update friendlist_index, putting online friends before offline friends */
for (i = 0; i < on_cnt; ++i) for (i = 0; i < on_cnt; ++i)

View File

@ -19,6 +19,7 @@
#include <signal.h> #include <signal.h>
#include <locale.h> #include <locale.h>
#include <string.h> #include <string.h>
#include <time.h>
#ifdef _WIN32 #ifdef _WIN32
#include <direct.h> #include <direct.h>
@ -347,7 +348,23 @@ static void load_data(Tox *m, char *path)
} }
} }
void do_file_senders(Tox *m) static void close_file_sender(Tox *m, int i)
{
fclose(file_senders[i].file);
memset(&file_senders[i], 0, sizeof(FileSender));
int j;
for (j = num_file_senders; j > 0; --j) {
if (file_senders[j-1].active)
break;
}
num_file_senders = j;
return;
}
static void do_file_senders(Tox *m)
{ {
int i; int i;
@ -356,33 +373,33 @@ void do_file_senders(Tox *m)
continue; continue;
while (true) { while (true) {
uint8_t *pathname = file_senders[i].pathname;
uint8_t filenum = file_senders[i].filenum; uint8_t filenum = file_senders[i].filenum;
uint64_t current_time = (uint64_t)time(NULL);
int friendnum = file_senders[i].friendnum; int friendnum = file_senders[i].friendnum;
if (!tox_file_senddata(m, friendnum, filenum, file_senders[i].nextpiece, if (!tox_file_senddata(m, friendnum, filenum, file_senders[i].nextpiece,
file_senders[i].piecelen)) file_senders[i].piecelen)) {
return;
/* If file transfer has timed out kill transfer and send kill control */
if (timed_out(file_senders[i].timestamp, current_time, TIMEOUT_FILESENDER)) {
wprintw(file_senders[i].chatwin, "File transfer for '%s' timed out.\n",
pathname);
tox_file_sendcontrol(m, friendnum, 0, filenum, TOX_FILECONTROL_KILL, 0, 0);
close_file_sender(m, i);
}
return;
}
file_senders[i].timestamp = current_time;
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, tox_filedata_size(m, file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, tox_filedata_size(m,
friendnum), file_senders[i].file); friendnum), file_senders[i].file);
if (file_senders[i].piecelen == 0) { if (file_senders[i].piecelen == 0) {
fclose(file_senders[i].file);
memset(&file_senders[i], 0, sizeof(FileSender));
tox_file_sendcontrol(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0);
uint8_t *pathname = file_senders[i].pathname;
wprintw(file_senders[i].chatwin, "File '%s' successfuly sent.\n", pathname); wprintw(file_senders[i].chatwin, "File '%s' successfuly sent.\n", pathname);
tox_file_sendcontrol(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0);
int i; close_file_sender(m, i);
for (i = num_file_senders; i > 0; --i) {
if (file_senders[i-1].active)
break;
}
num_file_senders = i;
return; return;
} }
} }

View File

@ -100,3 +100,9 @@ char *wc_to_char(wchar_t ch)
return ret; return ret;
} }
/* Returns true if connection has timed out, false otherwise */
bool timed_out(uint64_t timestamp, uint64_t curtime, uint64_t timeout)
{
return timestamp + timeout <= curtime;
}

View File

@ -19,3 +19,6 @@ uint8_t *wcs_to_char(wchar_t *string);
/* convert a wide char to null terminated string */ /* convert a wide char to null terminated string */
char *wc_to_char(wchar_t ch); char *wc_to_char(wchar_t ch);
/* Returns true if connection has timed out, false otherwise */
bool timed_out(uint64_t timestamp, uint64_t timeout, uint64_t curtime);

View File

@ -101,6 +101,7 @@ typedef struct {
#define MAX_FILES 256 #define MAX_FILES 256
#define FILE_PIECE_SIZE 1024 #define FILE_PIECE_SIZE 1024
#define TIMEOUT_FILESENDER 300
typedef struct { typedef struct {
FILE *file; FILE *file;
@ -111,6 +112,7 @@ typedef struct {
uint8_t nextpiece[FILE_PIECE_SIZE]; uint8_t nextpiece[FILE_PIECE_SIZE];
uint16_t piecelen; uint16_t piecelen;
uint8_t pathname[MAX_STR_SIZE]; uint8_t pathname[MAX_STR_SIZE];
uint64_t timestamp;
} FileSender; } FileSender;
FileSender file_senders[MAX_FILES]; FileSender file_senders[MAX_FILES];