1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 21:53:27 +01:00

fixed file sending queueing and other bugs

This commit is contained in:
Jfreegman 2013-10-18 01:35:08 -04:00
parent 34cc342010
commit e8d2f9418b
3 changed files with 55 additions and 23 deletions

View File

@ -123,7 +123,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil
wprintw(ctx->history, "File transfer request for '%s' (%llu bytes).\n", filename, wprintw(ctx->history, "File transfer request for '%s' (%llu bytes).\n", filename,
(long long unsigned int)filesize); (long long unsigned int)filesize);
if (filenum >= MAX_FILENUMBER) { if (filenum >= MAX_FILES) {
wprintw(ctx->history, "Too many pending file requests; discarding.\n"); wprintw(ctx->history, "Too many pending file requests; discarding.\n");
return; return;
} }
@ -142,7 +142,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil
wprintw(ctx->history, "Type '/savefile %d' to accept the file transfer.\n", filenum); wprintw(ctx->history, "Type '/savefile %d' to accept the file transfer.\n", filenum);
friends[num].file_receiver.pending_file_transfers[filenum] = num; friends[num].file_receiver.pending[filenum] = true;
strcpy(friends[num].file_receiver.filenames[filenum], filename); strcpy(friends[num].file_receiver.filenames[filenum], filename);
self->blink = true; self->blink = true;
@ -221,6 +221,11 @@ static void chat_groupinvite(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t
static void chat_sendfile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *path) static void chat_sendfile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *path)
{ {
if (num_file_senders >= MAX_FILES) {
wprintw(ctx->history,"Please wait for some of your outgoing file transfers to complete.\n");
return;
}
int path_len = strlen(path); int path_len = strlen(path);
if (path_len > MAX_STR_SIZE) { if (path_len > MAX_STR_SIZE) {
@ -250,34 +255,53 @@ static void chat_sendfile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *pa
return; return;
} }
memcpy(file_senders[num_file_senders].pathname, path, path_len + 1); int i;
file_senders[num_file_senders].chatwin = ctx->history;
file_senders[num_file_senders].file = file_to_send;
file_senders[num_file_senders].filenum = (uint8_t) filenum;
file_senders[num_file_senders].friendnum = friendnum;
file_senders[num_file_senders].piecelen = fread(file_senders[num_file_senders].nextpiece, 1,
tox_filedata_size(m, friendnum), file_to_send);
wprintw(ctx->history, "Sending file '%s'...\n", path); for (i = 0; i < MAX_FILES; ++i) {
++num_file_senders; if (!file_senders[i].active) {
memcpy(file_senders[i].pathname, path, path_len + 1);
file_senders[i].active = true;
file_senders[i].chatwin = ctx->history;
file_senders[i].file = file_to_send;
file_senders[i].filenum = (uint8_t) filenum;
file_senders[i].friendnum = friendnum;
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
tox_filedata_size(m, friendnum), file_to_send);
wprintw(ctx->history, "Sending file: '%s'\n", path);
if (i == num_file_senders)
++num_file_senders;
return;
}
}
} }
static void chat_savefile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *num) static void chat_savefile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *num)
{ {
uint8_t filenum = atoi(num); uint8_t filenum = atoi(num);
if (filenum < 0 || filenum >= MAX_FILENUMBER) { if (filenum < 0 || filenum >= MAX_FILES) {
wprintw(ctx->history, "File transfer failed.\n"); wprintw(ctx->history, "No pending file transfers with that number.\n");
return; return;
} }
int friendnum = self->num; int friendnum = self->num;
if (!friends[friendnum].file_receiver.pending[filenum]) {
wprintw(ctx->history, "No pending file transfers with that number.\n");
return;
}
uint8_t *filename = friends[friendnum].file_receiver.filenames[filenum]; uint8_t *filename = friends[friendnum].file_receiver.filenames[filenum];
if (tox_file_sendcontrol(m, friendnum, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0)) if (tox_file_sendcontrol(m, friendnum, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0))
wprintw(ctx->history, "Accepted file transfer %u. Saving file as: '%s'\n", filenum, filename); wprintw(ctx->history, "Accepted file transfer %u. Saving file as: '%s'\n", filenum, filename);
else else
wprintw(ctx->history, "File transfer failed.\n"); wprintw(ctx->history, "File transfer failed.\n");
friends[friendnum].file_receiver.pending[filenum] = false;
} }
static void print_chat_help(ChatContext *ctx) static void print_chat_help(ChatContext *ctx)

View File

@ -351,7 +351,7 @@ void do_file_senders(Tox *m)
{ {
int i; int i;
for (i = 0; i < NUM_FILE_SENDERS; ++i) { for (i = 0; i < MAX_FILES; ++i) {
if (file_senders[i].file == NULL) if (file_senders[i].file == NULL)
continue; continue;
@ -368,13 +368,21 @@ void do_file_senders(Tox *m)
if (file_senders[i].piecelen == 0) { if (file_senders[i].piecelen == 0) {
fclose(file_senders[i].file); fclose(file_senders[i].file);
file_senders[i].file = NULL; memset(&file_senders[i], 0, sizeof(FileSender));
tox_file_sendcontrol(m, friendnum, 0, filenum,
TOX_FILECONTROL_FINISHED, 0, 0); tox_file_sendcontrol(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0);
uint8_t *pathname = file_senders[i].pathname; 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);
int i;
for (i = num_file_senders; i > 0; --i) {
if (file_senders[i-1].active)
break;
}
num_file_senders = i;
return; return;
} }
} }

View File

@ -97,26 +97,26 @@ typedef struct {
/* Start file transfer code */ /* Start file transfer code */
#define NUM_FILE_SENDERS 256 #define MAX_FILES 256
#define MAX_FILENUMBER 100
#define FILE_PIECE_SIZE 1024 #define FILE_PIECE_SIZE 1024
typedef struct { typedef struct {
FILE *file; FILE *file;
WINDOW *chatwin; WINDOW *chatwin;
int friendnum; int friendnum;
bool active;
uint8_t filenum; uint8_t filenum;
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];
} FileSender; } FileSender;
FileSender file_senders[NUM_FILE_SENDERS]; FileSender file_senders[MAX_FILES];
uint8_t num_file_senders; uint8_t num_file_senders;
typedef struct { typedef struct {
uint8_t pending_file_transfers[MAX_FILENUMBER]; uint8_t filenames[MAX_FILES][MAX_STR_SIZE];
uint8_t filenames[MAX_FILENUMBER][MAX_STR_SIZE]; bool pending[MAX_FILES];
} FileReceiver; } FileReceiver;
/* End file transfer code */ /* End file transfer code */