mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-26 15:03:27 +01:00
give file senders their own threads
This commit is contained in:
parent
e8a39e1722
commit
da65ba4e8d
@ -42,7 +42,7 @@
|
||||
|
||||
extern char *DATA_FILE;
|
||||
|
||||
extern FileSender file_senders[MAX_FILES];
|
||||
extern FSenderThread file_senders[MAX_FILES];
|
||||
extern ToxicFriend friends[MAX_FRIENDS_NUM];
|
||||
|
||||
extern struct _Winthread Winthread;
|
||||
@ -394,10 +394,7 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu
|
||||
uint8_t msg[MAX_STR_SIZE];
|
||||
uint64_t size = friends[num].file_receiver.size[filenum];
|
||||
long double remain = (long double) tox_file_data_remaining(m, num, filenum, 1);
|
||||
long double pct_remain = 100;
|
||||
|
||||
if (remain)
|
||||
pct_remain = (1 - (remain / size)) * 100;
|
||||
long double pct_remain = remain ? (1 - (remain / size)) * 100 : 100;
|
||||
|
||||
const uint8_t *name = friends[num].file_receiver.filenames[filenum];
|
||||
snprintf(msg, sizeof(msg), "Saving file as: '%s' (%.1Lf%%)", name, pct_remain);
|
||||
|
@ -30,14 +30,12 @@
|
||||
#include "execute.h"
|
||||
#include "line_info.h"
|
||||
#include "groupchat.h"
|
||||
#include "file_senders.h"
|
||||
|
||||
extern ToxWindow *prompt;
|
||||
|
||||
extern int max_file_senders_index;
|
||||
extern ToxicFriend friends[MAX_FRIENDS_NUM];
|
||||
|
||||
extern FileSender file_senders[MAX_FILES];
|
||||
extern uint8_t max_file_senders_index;
|
||||
|
||||
void cmd_chat_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
||||
{
|
||||
struct history *hst = self->chatwin->hst;
|
||||
@ -256,29 +254,5 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
|
||||
return;
|
||||
}
|
||||
|
||||
int i;
|
||||
|
||||
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].toxwin = self;
|
||||
file_senders[i].file = file_to_send;
|
||||
file_senders[i].filenum = filenum;
|
||||
file_senders[i].friendnum = self->num;
|
||||
file_senders[i].timestamp = get_unix_time();
|
||||
file_senders[i].size = filesize;
|
||||
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
|
||||
tox_file_data_size(m, self->num), file_to_send);
|
||||
|
||||
uint8_t msg[MAX_STR_SIZE];
|
||||
snprintf(msg, sizeof(msg), "Sending file: '%s'", path);
|
||||
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
||||
|
||||
if (i == max_file_senders_index)
|
||||
++max_file_senders_index;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
new_filesender_thread(self, m, path, path_len, file_to_send, filenum, filesize);
|
||||
}
|
||||
|
@ -20,10 +20,10 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "toxic.h"
|
||||
#include "windows.h"
|
||||
@ -31,13 +31,17 @@
|
||||
#include "line_info.h"
|
||||
#include "misc_tools.h"
|
||||
|
||||
FileSender file_senders[MAX_FILES];
|
||||
uint8_t max_file_senders_index;
|
||||
extern struct _Winthread Winthread;
|
||||
|
||||
static void close_file_sender(int i)
|
||||
#define TIMEOUT_FILESENDER 120
|
||||
|
||||
FSenderThread file_senders[MAX_FILES];
|
||||
int max_file_senders_index;
|
||||
|
||||
static void exit_file_sender(int i)
|
||||
{
|
||||
pthread_mutex_lock(&file_senders[i].lock);
|
||||
fclose(file_senders[i].file);
|
||||
memset(&file_senders[i], 0, sizeof(FileSender));
|
||||
|
||||
int j;
|
||||
|
||||
@ -47,6 +51,10 @@ static void close_file_sender(int i)
|
||||
}
|
||||
|
||||
max_file_senders_index = j;
|
||||
pthread_mutex_unlock(&file_senders[i].lock);
|
||||
memset(&file_senders[i], 0, sizeof(FSenderThread));
|
||||
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
/* Should only be called on exit */
|
||||
@ -60,24 +68,26 @@ void close_all_file_senders(void)
|
||||
}
|
||||
}
|
||||
|
||||
void do_file_senders(Tox *m)
|
||||
void *do_file_sender(void *data)
|
||||
{
|
||||
int i = *(int *) data;
|
||||
|
||||
uint8_t msg[MAX_STR_SIZE];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < max_file_senders_index; ++i) {
|
||||
if (!file_senders[i].active)
|
||||
continue;
|
||||
ToxWindow *self = file_senders[i].toxwin;
|
||||
Tox *m = file_senders[i].m;
|
||||
uint8_t *pathname = file_senders[i].pathname;
|
||||
int filenum = file_senders[i].filenum;
|
||||
int32_t friendnum = file_senders[i].friendnum;
|
||||
FILE *fp = file_senders[i].file;
|
||||
|
||||
ToxWindow *self = file_senders[i].toxwin;
|
||||
uint8_t *pathname = file_senders[i].pathname;
|
||||
int filenum = file_senders[i].filenum;
|
||||
int32_t friendnum = file_senders[i].friendnum;
|
||||
FILE *fp = file_senders[i].file;
|
||||
while (file_senders[i].piecelen > 0) {
|
||||
uint64_t current_time = get_unix_time();
|
||||
|
||||
/* If file transfer has timed out kill transfer and send kill control */
|
||||
if (timed_out(file_senders[i].timestamp, current_time, TIMEOUT_FILESENDER)) {
|
||||
pthread_mutex_lock(&file_senders[i].lock);
|
||||
|
||||
if (self->chatwin != NULL) {
|
||||
snprintf(msg, sizeof(msg), "File transfer for '%s' timed out.", pathname);
|
||||
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
||||
@ -85,15 +95,15 @@ void do_file_senders(Tox *m)
|
||||
}
|
||||
|
||||
tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
||||
close_file_sender(i);
|
||||
continue;
|
||||
pthread_mutex_unlock(&file_senders[i].lock);
|
||||
|
||||
exit_file_sender(i);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
if (tox_file_send_data(m, friendnum, filenum, file_senders[i].nextpiece,
|
||||
file_senders[i].piecelen) == -1)
|
||||
break;
|
||||
pthread_mutex_lock(&file_senders[i].lock);
|
||||
|
||||
while (tox_file_send_data(m, friendnum, filenum, file_senders[i].nextpiece,
|
||||
file_senders[i].piecelen) != -1) {
|
||||
file_senders[i].timestamp = current_time;
|
||||
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
|
||||
tox_file_data_size(m, friendnum), fp);
|
||||
@ -102,27 +112,70 @@ void do_file_senders(Tox *m)
|
||||
if (self->chatwin != NULL) {
|
||||
uint64_t size = file_senders[i].size;
|
||||
long double remain = (long double) tox_file_data_remaining(m, friendnum, filenum, 0);
|
||||
long double pct_remain = 100;
|
||||
|
||||
if (remain)
|
||||
pct_remain = (1 - (remain / size)) * 100;
|
||||
long double pct_remain = remain ? (1 - (remain / size)) * 100 : 100;
|
||||
|
||||
const uint8_t *name = file_senders[i].pathname;
|
||||
snprintf(msg, sizeof(msg), "File transfer for '%s' accepted (%.1Lf%%)", name, pct_remain);
|
||||
line_info_set(self, file_senders[i].line_id, msg);
|
||||
}
|
||||
|
||||
if (file_senders[i].piecelen == 0) {
|
||||
if (self->chatwin != NULL) {
|
||||
snprintf(msg, sizeof(msg), "File '%s' successfuly sent.", pathname);
|
||||
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
||||
alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true);
|
||||
}
|
||||
|
||||
tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0);
|
||||
close_file_sender(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&file_senders[i].lock);
|
||||
usleep(40000); /* Probably not optimal */
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&file_senders[i].lock);
|
||||
|
||||
if (self->chatwin != NULL) {
|
||||
snprintf(msg, sizeof(msg), "File '%s' successfuly sent.", pathname);
|
||||
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
||||
alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true);
|
||||
}
|
||||
|
||||
tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0);
|
||||
pthread_mutex_unlock(&file_senders[i].lock);
|
||||
|
||||
exit_file_sender(i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void new_filesender_thread(ToxWindow *self, Tox *m, uint8_t *path, int path_len, FILE *file_to_send,
|
||||
int filenum, uint64_t filesize)
|
||||
{
|
||||
pthread_t tid;
|
||||
pthread_mutex_t lock;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_FILES; ++i) {
|
||||
if (file_senders[i].active)
|
||||
continue;
|
||||
|
||||
if (i == max_file_senders_index)
|
||||
++max_file_senders_index;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
memcpy(file_senders[i].pathname, path, path_len + 1);
|
||||
file_senders[i].tid = tid;
|
||||
file_senders[i].lock = lock;
|
||||
file_senders[i].active = true;
|
||||
file_senders[i].toxwin = self;
|
||||
file_senders[i].m = m;
|
||||
file_senders[i].file = file_to_send;
|
||||
file_senders[i].filenum = filenum;
|
||||
file_senders[i].friendnum = self->num;
|
||||
file_senders[i].timestamp = get_unix_time();
|
||||
file_senders[i].size = filesize;
|
||||
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
|
||||
tox_file_data_size(m, self->num), file_to_send);
|
||||
uint8_t msg[MAX_STR_SIZE];
|
||||
snprintf(msg, sizeof(msg), "Sending file: '%s'", path);
|
||||
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
||||
|
||||
if (pthread_create(&file_senders[i].tid, NULL, do_file_sender, (void *) &i) != 0)
|
||||
exit_toxic_err("failed in new_filesenders_thread", FATALERR_THREAD_CREATE);
|
||||
|
||||
if (pthread_mutex_init(&file_senders[i].lock, NULL) != 0)
|
||||
exit_toxic_err("failed in new_filesende", FATALERR_MUTEX_INIT);
|
||||
}
|
@ -23,16 +23,17 @@
|
||||
#ifndef _filesenders_h
|
||||
#define _filesenders_h
|
||||
|
||||
#include "toxic.h"
|
||||
#include "windows.h"
|
||||
|
||||
#define FILE_PIECE_SIZE 2048 /* must be >= (MAX_CRYPTO_DATA_SIZE - 2) in toxcore/net_crypto.h */
|
||||
#define MAX_FILES 256
|
||||
#define TIMEOUT_FILESENDER 300
|
||||
|
||||
typedef struct {
|
||||
pthread_t tid;
|
||||
pthread_mutex_t lock;
|
||||
|
||||
FILE *file;
|
||||
ToxWindow *toxwin;
|
||||
Tox *m;
|
||||
|
||||
int32_t friendnum;
|
||||
bool active;
|
||||
int filenum;
|
||||
@ -42,11 +43,12 @@ typedef struct {
|
||||
uint64_t timestamp;
|
||||
uint64_t size;
|
||||
uint32_t line_id;
|
||||
} FileSender;
|
||||
} FSenderThread;
|
||||
|
||||
void new_filesender_thread(ToxWindow *self, Tox *m, uint8_t *path, int path_len, FILE *file_to_send,
|
||||
int filenum, uint64_t filesize);
|
||||
|
||||
/* Should only be called on exit */
|
||||
void close_all_file_senders(void);
|
||||
|
||||
void do_file_senders(Tox *m);
|
||||
|
||||
#endif /* #define _filesenders_h */
|
||||
|
@ -54,7 +54,6 @@
|
||||
#include "friendlist.h"
|
||||
#include "prompt.h"
|
||||
#include "misc_tools.h"
|
||||
#include "file_senders.h"
|
||||
#include "line_info.h"
|
||||
#include "settings.h"
|
||||
#include "log.h"
|
||||
@ -460,7 +459,6 @@ static void do_toxic(Tox *m, ToxWindow *prompt)
|
||||
pthread_mutex_lock(&Winthread.lock);
|
||||
|
||||
do_connection(m, prompt);
|
||||
do_file_senders(m);
|
||||
tox_do(m); /* main tox-core loop */
|
||||
|
||||
pthread_mutex_unlock(&Winthread.lock);
|
||||
|
Loading…
Reference in New Issue
Block a user