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

put file senders in one thread due to weird issues & a few fixes

This commit is contained in:
Jfreegman 2014-06-28 12:14:43 -04:00
parent 134e5873a9
commit 33e98fd720
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
6 changed files with 118 additions and 128 deletions

View File

@ -42,7 +42,7 @@
extern char *DATA_FILE; extern char *DATA_FILE;
extern FSenderThread file_senders[MAX_FILES]; extern FileSender file_senders[MAX_FILES];
extern ToxicFriend friends[MAX_FRIENDS_NUM]; extern ToxicFriend friends[MAX_FRIENDS_NUM];
extern struct _Winthread Winthread; 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); 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]; FILE *file = friends[num].file_receiver.files[filenum];
if (file != NULL) if (file != NULL) {
fclose(file); fclose(file);
friends[num].file_receiver.files[filenum] = NULL;
}
} }
static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t receive_send, static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t receive_send,
@ -382,6 +382,9 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu
if (self->num != num) if (self->num != num)
return; return;
FILE *fp = friends[num].file_receiver.files[filenum];
if (fp) {
if (fwrite(data, length, 1, friends[num].file_receiver.files[filenum]) != 1) { if (fwrite(data, length, 1, friends[num].file_receiver.files[filenum]) != 1) {
uint8_t *msg = " * Error writing to file."; uint8_t *msg = " * Error writing to file.";
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, RED); line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, RED);
@ -389,6 +392,7 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
chat_close_file_receiver(num, filenum); chat_close_file_receiver(num, filenum);
} }
}
/* refresh line with percentage complete */ /* refresh line with percentage complete */
uint8_t msg[MAX_STR_SIZE]; uint8_t msg[MAX_STR_SIZE];

View File

@ -30,12 +30,14 @@
#include "execute.h" #include "execute.h"
#include "line_info.h" #include "line_info.h"
#include "groupchat.h" #include "groupchat.h"
#include "file_senders.h"
extern ToxWindow *prompt; extern ToxWindow *prompt;
extern int max_file_senders_index;
extern ToxicFriend friends[MAX_FRIENDS_NUM]; 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]) void cmd_chat_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
struct history *hst = self->chatwin->hst; struct history *hst = self->chatwin->hst;
@ -254,5 +256,29 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
return; 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;
}
}
} }

View File

@ -20,6 +20,7 @@
* *
*/ */
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
@ -31,17 +32,19 @@
#include "line_info.h" #include "line_info.h"
#include "misc_tools.h" #include "misc_tools.h"
extern struct _Winthread Winthread; FileSender file_senders[MAX_FILES];
uint8_t max_file_senders_index;
#define TIMEOUT_FILESENDER 120 static void close_file_sender(ToxWindow *self, Tox *m, int i, uint8_t *msg, int CTRL, int filenum, int32_t friendnum)
FSenderThread file_senders[MAX_FILES];
int max_file_senders_index;
static void exit_file_sender(int i)
{ {
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); fclose(file_senders[i].file);
memset(&file_senders[i], 0, sizeof(FileSender));
int j; int j;
@ -51,10 +54,6 @@ static void exit_file_sender(int i)
} }
max_file_senders_index = j; 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 */ /* 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]; 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; ToxWindow *self = file_senders[i].toxwin;
Tox *m = file_senders[i].m;
uint8_t *pathname = file_senders[i].pathname; uint8_t *pathname = file_senders[i].pathname;
int filenum = file_senders[i].filenum; int filenum = file_senders[i].filenum;
int32_t friendnum = file_senders[i].friendnum; int32_t friendnum = file_senders[i].friendnum;
FILE *fp = file_senders[i].file; 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 file transfer has timed out kill transfer and send kill control */
if (timed_out(file_senders[i].timestamp, current_time, TIMEOUT_FILESENDER)) { if (timed_out(file_senders[i].timestamp, get_unix_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); snprintf(msg, sizeof(msg), "File transfer for '%s' timed out.", pathname);
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); close_file_sender(self, m, i, msg, TOX_FILECONTROL_KILL, filenum, friendnum);
alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true); continue;
} }
tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_KILL, 0, 0); while (true) {
pthread_mutex_unlock(&file_senders[i].lock); if (tox_file_send_data(m, friendnum, filenum, file_senders[i].nextpiece,
file_senders[i].piecelen) == -1)
break;
exit_file_sender(i); file_senders[i].timestamp = get_unix_time();
}
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, file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
tox_file_data_size(m, friendnum), fp); 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 remain = (long double) tox_file_data_remaining(m, friendnum, filenum, 0);
long double pct_remain = remain ? (1 - (remain / size)) * 100 : 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%%)", pathname, pct_remain);
snprintf(msg, sizeof(msg), "File transfer for '%s' accepted (%.1Lf%%)", name, pct_remain);
line_info_set(self, file_senders[i].line_id, msg); line_info_set(self, file_senders[i].line_id, msg);
} }
}
pthread_mutex_unlock(&file_senders[i].lock); if (file_senders[i].piecelen == 0) {
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); snprintf(msg, sizeof(msg), "File '%s' successfuly sent.", pathname);
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); close_file_sender(self, m, i, msg, TOX_FILECONTROL_FINISHED, filenum, friendnum);
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; 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);
} }

View File

@ -23,17 +23,16 @@
#ifndef _filesenders_h #ifndef _filesenders_h
#define _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 FILE_PIECE_SIZE 2048 /* must be >= (MAX_CRYPTO_DATA_SIZE - 2) in toxcore/net_crypto.h */
#define MAX_FILES 256 #define MAX_FILES 256
#define TIMEOUT_FILESENDER 120
typedef struct { typedef struct {
pthread_t tid;
pthread_mutex_t lock;
FILE *file; FILE *file;
ToxWindow *toxwin; ToxWindow *toxwin;
Tox *m;
int32_t friendnum; int32_t friendnum;
bool active; bool active;
int filenum; int filenum;
@ -43,12 +42,11 @@ typedef struct {
uint64_t timestamp; uint64_t timestamp;
uint64_t size; uint64_t size;
uint32_t line_id; uint32_t line_id;
} FSenderThread; } FileSender;
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 */ /* Should only be called on exit */
void close_all_file_senders(void); void close_all_file_senders(void);
void do_file_senders(Tox *m);
#endif /* #define _filesenders_h */ #endif /* #define _filesenders_h */

View File

@ -54,6 +54,7 @@
#include "friendlist.h" #include "friendlist.h"
#include "prompt.h" #include "prompt.h"
#include "misc_tools.h" #include "misc_tools.h"
#include "file_senders.h"
#include "line_info.h" #include "line_info.h"
#include "settings.h" #include "settings.h"
#include "log.h" #include "log.h"
@ -82,6 +83,8 @@ struct arg_opts {
} arg_opts; } arg_opts;
struct _Winthread Winthread; struct _Winthread Winthread;
struct _FSenderThread FSenderThread;
struct user_settings *user_settings = NULL; struct user_settings *user_settings = NULL;
static void ignore_SIGINT(int sig) 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) static void do_toxic(Tox *m, ToxWindow *prompt)
{ {
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
do_connection(m, prompt); do_connection(m, prompt);
tox_do(m); /* main tox-core loop */
pthread_mutex_unlock(&Winthread.lock); pthread_mutex_unlock(&Winthread.lock);
tox_do(m); /* main tox-core loop */
} }
void *thread_winref(void *data) 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) static void print_usage(void)
{ {
fprintf(stderr, "usage: toxic [OPTION] [FILE ...]\n"); fprintf(stderr, "usage: toxic [OPTION] [FILE ...]\n");
@ -590,13 +605,19 @@ int main(int argc, char *argv[])
prompt = init_windows(m); prompt = init_windows(m);
/* create new thread for ncurses stuff */ /* thread for ncurses stuff */
if (pthread_mutex_init(&Winthread.lock, NULL) != 0) if (pthread_mutex_init(&Winthread.lock, NULL) != 0)
exit_toxic_err("failed in main", FATALERR_MUTEX_INIT); exit_toxic_err("failed in main", FATALERR_MUTEX_INIT);
if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0) if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0)
exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); 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; 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); line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
} }
if (settings_err == -1) { if (settings_err == -1) {
msg = "Failed to load user settings"; msg = "Failed to load user settings";
line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);

View File

@ -67,6 +67,11 @@ struct _Winthread {
pthread_mutex_t lock; pthread_mutex_t lock;
}; };
struct _FSenderThread {
pthread_t tid;
pthread_mutex_t lock;
};
typedef struct ToxWindow ToxWindow; typedef struct ToxWindow ToxWindow;
typedef struct StatusBar StatusBar; typedef struct StatusBar StatusBar;
typedef struct PromptBuf PromptBuf; typedef struct PromptBuf PromptBuf;