mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 00:53:03 +01:00
fixed file sending queueing and other bugs
This commit is contained in:
parent
34cc342010
commit
e8d2f9418b
50
src/chat.c
50
src/chat.c
@ -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,
|
||||
(long long unsigned int)filesize);
|
||||
|
||||
if (filenum >= MAX_FILENUMBER) {
|
||||
if (filenum >= MAX_FILES) {
|
||||
wprintw(ctx->history, "Too many pending file requests; discarding.\n");
|
||||
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);
|
||||
|
||||
friends[num].file_receiver.pending_file_transfers[filenum] = num;
|
||||
friends[num].file_receiver.pending[filenum] = true;
|
||||
strcpy(friends[num].file_receiver.filenames[filenum], filename);
|
||||
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
if (path_len > MAX_STR_SIZE) {
|
||||
@ -250,34 +255,53 @@ static void chat_sendfile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *pa
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(file_senders[num_file_senders].pathname, path, path_len + 1);
|
||||
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);
|
||||
int i;
|
||||
|
||||
wprintw(ctx->history, "Sending file '%s'...\n", path);
|
||||
++num_file_senders;
|
||||
for (i = 0; i < MAX_FILES; ++i) {
|
||||
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)
|
||||
{
|
||||
uint8_t filenum = atoi(num);
|
||||
|
||||
if (filenum < 0 || filenum >= MAX_FILENUMBER) {
|
||||
wprintw(ctx->history, "File transfer failed.\n");
|
||||
if (filenum < 0 || filenum >= MAX_FILES) {
|
||||
wprintw(ctx->history, "No pending file transfers with that number.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
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);
|
||||
else
|
||||
wprintw(ctx->history, "File transfer failed.\n");
|
||||
|
||||
friends[friendnum].file_receiver.pending[filenum] = false;
|
||||
}
|
||||
|
||||
static void print_chat_help(ChatContext *ctx)
|
||||
|
18
src/main.c
18
src/main.c
@ -351,7 +351,7 @@ void do_file_senders(Tox *m)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_FILE_SENDERS; ++i) {
|
||||
for (i = 0; i < MAX_FILES; ++i) {
|
||||
if (file_senders[i].file == NULL)
|
||||
continue;
|
||||
|
||||
@ -368,13 +368,21 @@ void do_file_senders(Tox *m)
|
||||
|
||||
if (file_senders[i].piecelen == 0) {
|
||||
fclose(file_senders[i].file);
|
||||
file_senders[i].file = NULL;
|
||||
tox_file_sendcontrol(m, friendnum, 0, filenum,
|
||||
TOX_FILECONTROL_FINISHED, 0, 0);
|
||||
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);
|
||||
|
||||
int i;
|
||||
|
||||
for (i = num_file_senders; i > 0; --i) {
|
||||
if (file_senders[i-1].active)
|
||||
break;
|
||||
}
|
||||
|
||||
num_file_senders = i;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -97,26 +97,26 @@ typedef struct {
|
||||
|
||||
/* Start file transfer code */
|
||||
|
||||
#define NUM_FILE_SENDERS 256
|
||||
#define MAX_FILENUMBER 100
|
||||
#define MAX_FILES 256
|
||||
#define FILE_PIECE_SIZE 1024
|
||||
|
||||
typedef struct {
|
||||
FILE *file;
|
||||
WINDOW *chatwin;
|
||||
int friendnum;
|
||||
bool active;
|
||||
uint8_t filenum;
|
||||
uint8_t nextpiece[FILE_PIECE_SIZE];
|
||||
uint16_t piecelen;
|
||||
uint8_t pathname[MAX_STR_SIZE];
|
||||
} FileSender;
|
||||
|
||||
FileSender file_senders[NUM_FILE_SENDERS];
|
||||
FileSender file_senders[MAX_FILES];
|
||||
uint8_t num_file_senders;
|
||||
|
||||
typedef struct {
|
||||
uint8_t pending_file_transfers[MAX_FILENUMBER];
|
||||
uint8_t filenames[MAX_FILENUMBER][MAX_STR_SIZE];
|
||||
uint8_t filenames[MAX_FILES][MAX_STR_SIZE];
|
||||
bool pending[MAX_FILES];
|
||||
} FileReceiver;
|
||||
|
||||
/* End file transfer code */
|
||||
|
Loading…
Reference in New Issue
Block a user