mirror of
https://github.com/Tha14/toxic.git
synced 2024-12-23 12:53:26 +01:00
put file senders in one thread due to weird issues & a few fixes
This commit is contained in:
parent
134e5873a9
commit
33e98fd720
24
src/chat.c
24
src/chat.c
@ -42,7 +42,7 @@
|
||||
|
||||
extern char *DATA_FILE;
|
||||
|
||||
extern FSenderThread file_senders[MAX_FILES];
|
||||
extern FileSender file_senders[MAX_FILES];
|
||||
extern ToxicFriend friends[MAX_FRIENDS_NUM];
|
||||
|
||||
extern struct _Winthread Winthread;
|
||||
@ -315,14 +315,14 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t
|
||||
alert_window(self, WINDOW_ALERT_2, true);
|
||||
}
|
||||
|
||||
static void chat_close_file_receiver(int num, uint8_t filenum)
|
||||
static void chat_close_file_receiver(int32_t num, uint8_t filenum)
|
||||
{
|
||||
friends[num].file_receiver.pending[filenum] = false;
|
||||
friends[num].file_receiver.size[filenum] = 0;
|
||||
FILE *file = friends[num].file_receiver.files[filenum];
|
||||
|
||||
if (file != NULL)
|
||||
if (file != NULL) {
|
||||
fclose(file);
|
||||
friends[num].file_receiver.files[filenum] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t receive_send,
|
||||
@ -382,12 +382,16 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu
|
||||
if (self->num != num)
|
||||
return;
|
||||
|
||||
if (fwrite(data, length, 1, friends[num].file_receiver.files[filenum]) != 1) {
|
||||
uint8_t *msg = " * Error writing to file.";
|
||||
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, RED);
|
||||
FILE *fp = friends[num].file_receiver.files[filenum];
|
||||
|
||||
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
||||
chat_close_file_receiver(num, filenum);
|
||||
if (fp) {
|
||||
if (fwrite(data, length, 1, friends[num].file_receiver.files[filenum]) != 1) {
|
||||
uint8_t *msg = " * Error writing to file.";
|
||||
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, RED);
|
||||
|
||||
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
||||
chat_close_file_receiver(num, filenum);
|
||||
}
|
||||
}
|
||||
|
||||
/* refresh line with percentage complete */
|
||||
|
@ -30,12 +30,14 @@
|
||||
#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;
|
||||
@ -254,5 +256,29 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
|
||||
return;
|
||||
}
|
||||
|
||||
new_filesender_thread(self, m, path, path_len, file_to_send, filenum, filesize);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
@ -31,17 +32,19 @@
|
||||
#include "line_info.h"
|
||||
#include "misc_tools.h"
|
||||
|
||||
extern struct _Winthread Winthread;
|
||||
FileSender file_senders[MAX_FILES];
|
||||
uint8_t max_file_senders_index;
|
||||
|
||||
#define TIMEOUT_FILESENDER 120
|
||||
|
||||
FSenderThread file_senders[MAX_FILES];
|
||||
int max_file_senders_index;
|
||||
|
||||
static void exit_file_sender(int i)
|
||||
static void close_file_sender(ToxWindow *self, Tox *m, int i, uint8_t *msg, int CTRL, int filenum, int32_t friendnum)
|
||||
{
|
||||
pthread_mutex_lock(&file_senders[i].lock);
|
||||
if (self->chatwin != NULL) {
|
||||
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, CTRL, 0, 0);
|
||||
fclose(file_senders[i].file);
|
||||
memset(&file_senders[i], 0, sizeof(FileSender));
|
||||
|
||||
int j;
|
||||
|
||||
@ -51,10 +54,6 @@ static void exit_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 */
|
||||
@ -68,43 +67,34 @@ void close_all_file_senders(void)
|
||||
}
|
||||
}
|
||||
|
||||
void *do_file_sender(void *data)
|
||||
void do_file_senders(Tox *m)
|
||||
{
|
||||
int i = *(int *) data;
|
||||
|
||||
uint8_t msg[MAX_STR_SIZE];
|
||||
int i;
|
||||
|
||||
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;
|
||||
for (i = 0; i < max_file_senders_index; ++i) {
|
||||
if (!file_senders[i].active)
|
||||
continue;
|
||||
|
||||
while (file_senders[i].piecelen > 0) {
|
||||
uint64_t current_time = get_unix_time();
|
||||
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;
|
||||
|
||||
/* 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);
|
||||
alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true);
|
||||
}
|
||||
|
||||
tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
||||
pthread_mutex_unlock(&file_senders[i].lock);
|
||||
|
||||
exit_file_sender(i);
|
||||
if (timed_out(file_senders[i].timestamp, get_unix_time(), TIMEOUT_FILESENDER)) {
|
||||
snprintf(msg, sizeof(msg), "File transfer for '%s' timed out.", pathname);
|
||||
close_file_sender(self, m, i, msg, TOX_FILECONTROL_KILL, filenum, friendnum);
|
||||
continue;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&file_senders[i].lock);
|
||||
while (true) {
|
||||
if (tox_file_send_data(m, friendnum, filenum, file_senders[i].nextpiece,
|
||||
file_senders[i].piecelen) == -1)
|
||||
break;
|
||||
|
||||
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].timestamp = get_unix_time();
|
||||
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
|
||||
tox_file_data_size(m, friendnum), fp);
|
||||
|
||||
@ -114,68 +104,15 @@ void *do_file_sender(void *data)
|
||||
long double remain = (long double) tox_file_data_remaining(m, friendnum, filenum, 0);
|
||||
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);
|
||||
snprintf(msg, sizeof(msg), "File transfer for '%s' accepted (%.1Lf%%)", pathname, pct_remain);
|
||||
line_info_set(self, file_senders[i].line_id, msg);
|
||||
}
|
||||
|
||||
if (file_senders[i].piecelen == 0) {
|
||||
snprintf(msg, sizeof(msg), "File '%s' successfuly sent.", pathname);
|
||||
close_file_sender(self, m, i, msg, TOX_FILECONTROL_FINISHED, filenum, friendnum);
|
||||
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,17 +23,16 @@
|
||||
#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 120
|
||||
|
||||
typedef struct {
|
||||
pthread_t tid;
|
||||
pthread_mutex_t lock;
|
||||
|
||||
FILE *file;
|
||||
ToxWindow *toxwin;
|
||||
Tox *m;
|
||||
|
||||
int32_t friendnum;
|
||||
bool active;
|
||||
int filenum;
|
||||
@ -43,12 +42,11 @@ typedef struct {
|
||||
uint64_t timestamp;
|
||||
uint64_t size;
|
||||
uint32_t line_id;
|
||||
} FSenderThread;
|
||||
|
||||
void new_filesender_thread(ToxWindow *self, Tox *m, uint8_t *path, int path_len, FILE *file_to_send,
|
||||
int filenum, uint64_t filesize);
|
||||
} FileSender;
|
||||
|
||||
/* Should only be called on exit */
|
||||
void close_all_file_senders(void);
|
||||
|
||||
void do_file_senders(Tox *m);
|
||||
|
||||
#endif /* #define _filesenders_h */
|
||||
|
32
src/toxic.c
32
src/toxic.c
@ -54,6 +54,7 @@
|
||||
#include "friendlist.h"
|
||||
#include "prompt.h"
|
||||
#include "misc_tools.h"
|
||||
#include "file_senders.h"
|
||||
#include "line_info.h"
|
||||
#include "settings.h"
|
||||
#include "log.h"
|
||||
@ -82,6 +83,8 @@ struct arg_opts {
|
||||
} arg_opts;
|
||||
|
||||
struct _Winthread Winthread;
|
||||
struct _FSenderThread FSenderThread;
|
||||
|
||||
struct user_settings *user_settings = NULL;
|
||||
|
||||
static void ignore_SIGINT(int sig)
|
||||
@ -457,11 +460,10 @@ static void load_data(Tox *m, char *path)
|
||||
static void do_toxic(Tox *m, ToxWindow *prompt)
|
||||
{
|
||||
pthread_mutex_lock(&Winthread.lock);
|
||||
|
||||
do_connection(m, prompt);
|
||||
tox_do(m); /* main tox-core loop */
|
||||
|
||||
pthread_mutex_unlock(&Winthread.lock);
|
||||
|
||||
tox_do(m); /* main tox-core loop */
|
||||
}
|
||||
|
||||
void *thread_winref(void *data)
|
||||
@ -474,6 +476,19 @@ void *thread_winref(void *data)
|
||||
}
|
||||
}
|
||||
|
||||
void *thread_filesenders(void *data)
|
||||
{
|
||||
Tox *m = (Tox *) data;
|
||||
|
||||
while (true) {
|
||||
pthread_mutex_lock(&FSenderThread.lock);
|
||||
do_file_senders(m);
|
||||
pthread_mutex_unlock(&FSenderThread.lock);
|
||||
|
||||
usleep(60000);
|
||||
}
|
||||
}
|
||||
|
||||
static void print_usage(void)
|
||||
{
|
||||
fprintf(stderr, "usage: toxic [OPTION] [FILE ...]\n");
|
||||
@ -590,13 +605,19 @@ int main(int argc, char *argv[])
|
||||
|
||||
prompt = init_windows(m);
|
||||
|
||||
/* create new thread for ncurses stuff */
|
||||
/* thread for ncurses stuff */
|
||||
if (pthread_mutex_init(&Winthread.lock, NULL) != 0)
|
||||
exit_toxic_err("failed in main", FATALERR_MUTEX_INIT);
|
||||
|
||||
if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0)
|
||||
exit_toxic_err("failed in main", FATALERR_THREAD_CREATE);
|
||||
|
||||
|
||||
/* thread for filesenders */
|
||||
if (pthread_mutex_init(&FSenderThread.lock, NULL) != 0)
|
||||
exit_toxic_err("failed in main", FATALERR_MUTEX_INIT);
|
||||
|
||||
if (pthread_create(&FSenderThread.tid, NULL, thread_filesenders, (void *) m) != 0)
|
||||
exit_toxic_err("failed in main", FATALERR_THREAD_CREATE);
|
||||
|
||||
uint8_t *msg;
|
||||
|
||||
@ -615,7 +636,6 @@ int main(int argc, char *argv[])
|
||||
line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
if (settings_err == -1) {
|
||||
msg = "Failed to load user settings";
|
||||
line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
||||
|
@ -67,6 +67,11 @@ struct _Winthread {
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
struct _FSenderThread {
|
||||
pthread_t tid;
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
typedef struct ToxWindow ToxWindow;
|
||||
typedef struct StatusBar StatusBar;
|
||||
typedef struct PromptBuf PromptBuf;
|
||||
|
Loading…
Reference in New Issue
Block a user