mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-27 01:03:26 +01:00
code cleanup, improve do_connection
This commit is contained in:
parent
eb7d910683
commit
034a8f5d8b
@ -79,6 +79,7 @@ static struct _dns_thread {
|
|||||||
pthread_mutex_t lock;
|
pthread_mutex_t lock;
|
||||||
} dns_thread;
|
} dns_thread;
|
||||||
|
|
||||||
|
|
||||||
static int dns_error(ToxWindow *self, uint8_t *errmsg)
|
static int dns_error(ToxWindow *self, uint8_t *errmsg)
|
||||||
{
|
{
|
||||||
uint8_t msg[MAX_STR_SIZE];
|
uint8_t msg[MAX_STR_SIZE];
|
||||||
@ -289,10 +290,10 @@ void dns3_lookup(ToxWindow *self, Tox *m, uint8_t *id_bin, uint8_t *addr, uint8_
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
t_data.self = self;
|
|
||||||
snprintf(t_data.id_bin, sizeof(t_data.id_bin), "%s", id_bin);
|
snprintf(t_data.id_bin, sizeof(t_data.id_bin), "%s", id_bin);
|
||||||
snprintf(t_data.addr, sizeof(t_data.addr), "%s", addr);
|
snprintf(t_data.addr, sizeof(t_data.addr), "%s", addr);
|
||||||
snprintf(t_data.msg, sizeof(t_data.msg), "%s", msg);
|
snprintf(t_data.msg, sizeof(t_data.msg), "%s", msg);
|
||||||
|
t_data.self = self;
|
||||||
t_data.m = m;
|
t_data.m = m;
|
||||||
t_data.busy = 1;
|
t_data.busy = 1;
|
||||||
|
|
||||||
|
@ -297,7 +297,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
|
|||||||
if (self->num != groupnum)
|
if (self->num != groupnum)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (groupnum > MAX_GROUPCHAT_NUM)
|
if (groupnum > max_groupchat_index)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
groupchats[groupnum].num_peers = tox_group_number_peers(m, groupnum);
|
groupchats[groupnum].num_peers = tox_group_number_peers(m, groupnum);
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
#define SIDEBAR_WIDTH 16
|
#define SIDEBAR_WIDTH 16
|
||||||
#define SDBAR_OFST 2 /* Offset for the peer number box at the top of the statusbar */
|
#define SDBAR_OFST 2 /* Offset for the peer number box at the top of the statusbar */
|
||||||
#define MAX_GROUPCHAT_NUM MAX_WINDOWS_NUM
|
#define MAX_GROUPCHAT_NUM MAX_WINDOWS_NUM - 2
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int chatwin;
|
int chatwin;
|
||||||
|
@ -86,7 +86,7 @@ void mv_curs_end(WINDOW *w, size_t len, int max_y, int max_x);
|
|||||||
/* gets base file name from path or original file name if no path is supplied */
|
/* gets base file name from path or original file name if no path is supplied */
|
||||||
void get_file_name(uint8_t *namebuf, uint8_t *pathname);
|
void get_file_name(uint8_t *namebuf, uint8_t *pathname);
|
||||||
|
|
||||||
/* converts str to all uppercase */
|
/* converts str to all lowercase */
|
||||||
void str_to_lower(uint8_t *str);
|
void str_to_lower(uint8_t *str);
|
||||||
|
|
||||||
#endif /* #define _misc_tools_h */
|
#endif /* #define _misc_tools_h */
|
||||||
|
79
src/toxic.c
79
src/toxic.c
@ -128,7 +128,7 @@ static void init_term(void)
|
|||||||
#if HAVE_WIDECHAR
|
#if HAVE_WIDECHAR
|
||||||
|
|
||||||
if (setlocale(LC_ALL, "") == NULL)
|
if (setlocale(LC_ALL, "") == NULL)
|
||||||
exit_toxic_err("Could not set your locale, plese check your locale settings or"
|
exit_toxic_err("Could not set your locale, please check your locale settings or"
|
||||||
"disable wide char support", FATALERR_LOCALE_SET);
|
"disable wide char support", FATALERR_LOCALE_SET);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -217,10 +217,12 @@ static Tox *init_tox(int ipv4)
|
|||||||
#define MAXNODES 50
|
#define MAXNODES 50
|
||||||
#define NODELEN (MAXLINE - TOX_CLIENT_ID_SIZE - 7)
|
#define NODELEN (MAXLINE - TOX_CLIENT_ID_SIZE - 7)
|
||||||
|
|
||||||
static int linecnt = 0;
|
static struct _toxNodes {
|
||||||
static char nodes[MAXNODES][NODELEN];
|
int lines;
|
||||||
static uint16_t ports[MAXNODES];
|
char nodes[MAXNODES][NODELEN];
|
||||||
static uint8_t keys[MAXNODES][TOX_CLIENT_ID_SIZE];
|
uint16_t ports[MAXNODES];
|
||||||
|
uint8_t keys[MAXNODES][TOX_CLIENT_ID_SIZE];
|
||||||
|
} toxNodes;
|
||||||
|
|
||||||
static int nodelist_load(char *filename)
|
static int nodelist_load(char *filename)
|
||||||
{
|
{
|
||||||
@ -234,7 +236,7 @@ static int nodelist_load(char *filename)
|
|||||||
|
|
||||||
char line[MAXLINE];
|
char line[MAXLINE];
|
||||||
|
|
||||||
while (fgets(line, sizeof(line), fp) && linecnt < MAXNODES) {
|
while (fgets(line, sizeof(line), fp) && toxNodes.lines < MAXNODES) {
|
||||||
if (strlen(line) > MINLINE) {
|
if (strlen(line) > MINLINE) {
|
||||||
const char *name = strtok(line, " ");
|
const char *name = strtok(line, " ");
|
||||||
const char *port = strtok(NULL, " ");
|
const char *port = strtok(NULL, " ");
|
||||||
@ -244,19 +246,19 @@ static int nodelist_load(char *filename)
|
|||||||
if (name == NULL || port == NULL || key_ascii == NULL)
|
if (name == NULL || port == NULL || key_ascii == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
snprintf(nodes[linecnt], sizeof(nodes[linecnt]), "%s", name);
|
snprintf(toxNodes.nodes[toxNodes.lines], sizeof(toxNodes.nodes[toxNodes.lines]), "%s", name);
|
||||||
nodes[linecnt][NODELEN - 1] = 0;
|
toxNodes.nodes[toxNodes.lines][NODELEN - 1] = 0;
|
||||||
ports[linecnt] = htons(atoi(port));
|
toxNodes.ports[toxNodes.lines] = htons(atoi(port));
|
||||||
|
|
||||||
uint8_t *key_binary = hex_string_to_bin(key_ascii);
|
uint8_t *key_binary = hex_string_to_bin(key_ascii);
|
||||||
memcpy(keys[linecnt], key_binary, TOX_CLIENT_ID_SIZE);
|
memcpy(toxNodes.keys[toxNodes.lines], key_binary, TOX_CLIENT_ID_SIZE);
|
||||||
free(key_binary);
|
free(key_binary);
|
||||||
|
|
||||||
linecnt++;
|
toxNodes.lines++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (linecnt < 1) {
|
if (toxNodes.lines < 1) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
@ -267,8 +269,8 @@ static int nodelist_load(char *filename)
|
|||||||
|
|
||||||
int init_connection_helper(Tox *m, int line)
|
int init_connection_helper(Tox *m, int line)
|
||||||
{
|
{
|
||||||
return tox_bootstrap_from_address(m, nodes[line], TOX_ENABLE_IPV6_DEFAULT,
|
return tox_bootstrap_from_address(m, toxNodes.nodes[line], TOX_ENABLE_IPV6_DEFAULT,
|
||||||
ports[line], keys[line]);
|
toxNodes.ports[line], toxNodes.keys[line]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Connects to a random DHT node listed in the DHTnodes file
|
/* Connects to a random DHT node listed in the DHTnodes file
|
||||||
@ -285,8 +287,8 @@ static bool srvlist_loaded = false;
|
|||||||
|
|
||||||
int init_connection(Tox *m)
|
int init_connection(Tox *m)
|
||||||
{
|
{
|
||||||
if (linecnt > 0) /* already loaded nodelist */
|
if (toxNodes.lines > 0) /* already loaded nodelist */
|
||||||
return init_connection_helper(m, rand() % linecnt) ? 0 : 3;
|
return init_connection_helper(m, rand() % toxNodes.lines) ? 0 : 3;
|
||||||
|
|
||||||
/* only once:
|
/* only once:
|
||||||
* - load the nodelist
|
* - load the nodelist
|
||||||
@ -301,15 +303,15 @@ int init_connection(Tox *m)
|
|||||||
else
|
else
|
||||||
res = nodelist_load(arg_opts.nodes_path);
|
res = nodelist_load(arg_opts.nodes_path);
|
||||||
|
|
||||||
if (linecnt < 1)
|
if (toxNodes.lines < 1)
|
||||||
return res;
|
return res;
|
||||||
|
|
||||||
res = 3;
|
res = 3;
|
||||||
int i;
|
int i;
|
||||||
int n = MIN(NUM_INIT_NODES, linecnt);
|
int n = MIN(NUM_INIT_NODES, toxNodes.lines);
|
||||||
|
|
||||||
for (i = 0; i < n; ++i) {
|
for (i = 0; i < n; ++i) {
|
||||||
if (init_connection_helper(m, rand() % linecnt))
|
if (init_connection_helper(m, rand() % toxNodes.lines))
|
||||||
res = 0;
|
res = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,30 +322,37 @@ int init_connection(Tox *m)
|
|||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define TRY_CONNECT 10
|
||||||
|
|
||||||
static void do_connection(Tox *m, ToxWindow *prompt)
|
static void do_connection(Tox *m, ToxWindow *prompt)
|
||||||
{
|
{
|
||||||
uint8_t msg[MAX_STR_SIZE] = {0};
|
uint8_t msg[MAX_STR_SIZE] = {0};
|
||||||
|
|
||||||
static int conn_try = 0;
|
|
||||||
static int conn_err = 0;
|
static int conn_err = 0;
|
||||||
static bool dht_on = false;
|
static bool was_connected = false;
|
||||||
|
static uint64_t last_conn_try = 0;
|
||||||
|
uint64_t curtime = get_unix_time();
|
||||||
bool is_connected = tox_isconnected(m);
|
bool is_connected = tox_isconnected(m);
|
||||||
|
|
||||||
if (!dht_on && !is_connected && !(conn_try++ % 100)) {
|
if (was_connected && is_connected)
|
||||||
if (!conn_err) {
|
return;
|
||||||
if ((conn_err = init_connection(m))) {
|
|
||||||
snprintf(msg, sizeof(msg), "\nAuto-connect failed with error code %d", conn_err);
|
if (!was_connected && is_connected) {
|
||||||
}
|
was_connected = true;
|
||||||
}
|
prompt_update_connectionstatus(prompt, was_connected);
|
||||||
} else if (!dht_on && is_connected) {
|
|
||||||
dht_on = true;
|
|
||||||
prompt_update_connectionstatus(prompt, dht_on);
|
|
||||||
snprintf(msg, sizeof(msg), "DHT connected.");
|
snprintf(msg, sizeof(msg), "DHT connected.");
|
||||||
} else if (dht_on && !is_connected) {
|
} else if (was_connected && !is_connected) {
|
||||||
dht_on = false;
|
was_connected = false;
|
||||||
prompt_update_connectionstatus(prompt, dht_on);
|
prompt_update_connectionstatus(prompt, was_connected);
|
||||||
snprintf(msg, sizeof(msg), "\nDHT disconnected. Attempting to reconnect.");
|
snprintf(msg, sizeof(msg), "DHT disconnected. Attempting to reconnect.");
|
||||||
|
} else if (!was_connected && !is_connected && timed_out(last_conn_try, curtime, TRY_CONNECT)) {
|
||||||
|
/* if autoconnect has already failed there's no point in trying again */
|
||||||
|
if (conn_err == 0) {
|
||||||
|
last_conn_try = curtime;
|
||||||
|
|
||||||
|
if ((conn_err = init_connection(m)) != 0)
|
||||||
|
snprintf(msg, sizeof(msg), "Auto-connect failed with error code %d", conn_err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg[0])
|
if (msg[0])
|
||||||
|
Loading…
Reference in New Issue
Block a user