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

simplify timeout function

This commit is contained in:
Jfreegman 2015-08-18 01:46:22 -04:00 committed by cnhenry
parent 271ca08eb2
commit ed0a4fb3b8
8 changed files with 21 additions and 27 deletions

View File

@ -80,13 +80,13 @@ void print_progress_bar(ToxWindow *self, double bps, double pct_done, uint32_t l
line_info_set(self, line_id, msg);
}
static void refresh_progress_helper(ToxWindow *self, struct FileTransfer *ft, uint64_t curtime)
static void refresh_progress_helper(ToxWindow *self, struct FileTransfer *ft)
{
if (ft->state == FILE_TRANSFER_INACTIVE)
return;
/* Timeout must be set to 1 second to show correct bytes per second */
if (!timed_out(ft->last_progress, curtime, 1))
if (!timed_out(ft->last_progress, 1))
return;
double remain = ft->file_size - ft->position;
@ -94,18 +94,17 @@ static void refresh_progress_helper(ToxWindow *self, struct FileTransfer *ft, ui
print_progress_bar(self, ft->bps, pct_done, ft->line_id);
ft->bps = 0;
ft->last_progress = curtime;
ft->last_progress = get_unix_time();
}
/* refreshes active file receiver status bars for friendnum */
void refresh_file_transfer_progress(ToxWindow *self, Tox *m, uint32_t friendnum)
{
uint64_t curtime = get_unix_time();
size_t i;
for (i = 0; i < MAX_FILES; ++i) {
refresh_progress_helper(self, &Friends.list[friendnum].file_receiver[i], curtime);
refresh_progress_helper(self, &Friends.list[friendnum].file_sender[i], curtime);
refresh_progress_helper(self, &Friends.list[friendnum].file_receiver[i]);
refresh_progress_helper(self, &Friends.list[friendnum].file_sender[i]);
}
}

View File

@ -323,7 +323,7 @@ static void groupchat_onGroupTitleChange(ToxWindow *self, Tox *m, int groupnum,
get_time_str(timefrmt, sizeof(timefrmt));
/* don't announce title when we join the room */
if (!timed_out(groupchats[self->num].start_time, get_unix_time(), GROUP_EVENT_WAIT))
if (!timed_out(groupchats[self->num].start_time, GROUP_EVENT_WAIT))
return;
char nick[TOX_MAX_NAME_LENGTH];
@ -402,7 +402,7 @@ void *group_add_wait(void *data)
pthread_mutex_lock(&Winthread.lock);
get_group_nick_truncate(m, peername, thrd->peernum, thrd->groupnum);
if (strcmp(peername, DEFAULT_TOX_NAME) || timed_out(thrd->timestamp, get_unix_time(), GROUP_EVENT_WAIT)) {
if (strcmp(peername, DEFAULT_TOX_NAME) || timed_out(thrd->timestamp, GROUP_EVENT_WAIT)) {
pthread_mutex_unlock(&Winthread.lock);
break;
}
@ -478,7 +478,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
switch (change) {
case TOX_CHAT_CHANGE_PEER_ADD:
if (!timed_out(groupchats[groupnum].start_time, get_unix_time(), GROUP_EVENT_WAIT))
if (!timed_out(groupchats[groupnum].start_time, GROUP_EVENT_WAIT))
break;
struct group_add_thrd *thrd = malloc(sizeof(struct group_add_thrd));
@ -518,7 +518,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
break;
case TOX_CHAT_CHANGE_PEER_NAME:
if (!timed_out(groupchats[self->num].start_time, get_unix_time(), GROUP_EVENT_WAIT))
if (!timed_out(groupchats[self->num].start_time, GROUP_EVENT_WAIT))
return;
/* ignore initial name change (TODO: this is a bad way to do this) */

View File

@ -332,7 +332,7 @@ void line_info_print(ToxWindow *self)
else if (line->msg[0] == '<')
wattroff(win, COLOR_PAIR(RED));
if (type == OUT_MSG && timed_out(line->timestamp, get_unix_time(), NOREAD_FLAG_TIMEOUT)) {
if (type == OUT_MSG && timed_out(line->timestamp, NOREAD_FLAG_TIMEOUT)) {
wattron(win, COLOR_PAIR(RED));
wprintw(win, " x", line->msg);
wattroff(win, COLOR_PAIR(RED));
@ -359,7 +359,7 @@ void line_info_print(ToxWindow *self)
wprintw(win, "%s %s %s", user_settings->line_normal, line->name1, line->msg);
wattroff(win, COLOR_PAIR(YELLOW));
if (type == OUT_ACTION && timed_out(line->timestamp, get_unix_time(), NOREAD_FLAG_TIMEOUT)) {
if (type == OUT_ACTION && timed_out(line->timestamp, NOREAD_FLAG_TIMEOUT)) {
wattron(win, COLOR_PAIR(RED));
wprintw(win, " x", line->msg);
wattroff(win, COLOR_PAIR(RED));

View File

@ -139,11 +139,9 @@ void write_to_log(const char *msg, const char *name, struct chatlog *log, bool e
strftime(s, MAX_STR_SIZE, t, get_time());
fprintf(log->file, "%s %s %s\n", s, name_frmt, msg);
uint64_t curtime = get_unix_time();
if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT)) {
if (timed_out(log->lastwrite, LOG_FLUSH_LIMIT)) {
fflush(log->file);
log->lastwrite = curtime;
log->lastwrite = get_unix_time();
}
}

View File

@ -140,9 +140,7 @@ void cqueue_try_send(ToxWindow *self, Tox *m)
if (!msg)
return;
uint64_t curtime = get_unix_time();
if (msg->receipt != 0 && !timed_out(msg->last_send_try, curtime, CQUEUE_TRY_SEND_INTERVAL))
if (msg->receipt != 0 && !timed_out(msg->last_send_try, CQUEUE_TRY_SEND_INTERVAL))
return;
uint32_t receipt = 0;
@ -150,7 +148,7 @@ void cqueue_try_send(ToxWindow *self, Tox *m)
TOX_MESSAGE_TYPE type = msg->type == OUT_MSG ? TOX_MESSAGE_TYPE_NORMAL : TOX_MESSAGE_TYPE_ACTION;
receipt = tox_friend_send_message(m, self->num, type, (uint8_t *) msg->message, msg->len, NULL);
msg->last_send_try = curtime;
msg->last_send_try = get_unix_time();
msg->receipt = receipt;
return;
}

View File

@ -65,9 +65,9 @@ uint64_t get_unix_time(void)
}
/* Returns 1 if connection has timed out, 0 otherwise */
int timed_out(uint64_t timestamp, uint64_t curtime, uint64_t timeout)
int timed_out(uint64_t timestamp, uint64_t timeout)
{
return timestamp + timeout <= curtime;
return timestamp + timeout <= get_unix_time();
}
/* Get the current local time */

View File

@ -75,7 +75,7 @@ int wcs_to_mbs_buf(char *buf, const wchar_t *string, size_t n);
int mbs_to_wcs_buf(wchar_t *buf, const char *string, size_t n);
/* Returns 1 if connection has timed out, 0 otherwise */
int timed_out(uint64_t timestamp, uint64_t timeout, uint64_t curtime);
int timed_out(uint64_t timestamp, uint64_t timeout);
/* Colours the window tab according to type. Beeps if is_beep is true */
void alert_window(ToxWindow *self, int type, bool is_beep);

View File

@ -763,9 +763,8 @@ static uint64_t last_bootstrap_time = 0;
static void do_bootstrap(Tox *m)
{
static int conn_err = 0;
uint64_t curtime = get_unix_time();
if (!timed_out(last_bootstrap_time, curtime, TRY_BOOTSTRAP_INTERVAL))
if (!timed_out(last_bootstrap_time, TRY_BOOTSTRAP_INTERVAL))
return;
if (tox_self_get_connection_status(m) != TOX_CONNECTION_NONE)
@ -774,7 +773,7 @@ static void do_bootstrap(Tox *m)
if (conn_err != 0)
return;
last_bootstrap_time = curtime;
last_bootstrap_time = get_unix_time();
conn_err = init_connection(m);
if (conn_err != 0)
@ -1202,7 +1201,7 @@ int main(int argc, char *argv[])
do_toxic(m, prompt);
uint64_t cur_time = get_unix_time();
if (timed_out(last_save, cur_time, AUTOSAVE_FREQ)) {
if (timed_out(last_save, AUTOSAVE_FREQ)) {
pthread_mutex_lock(&Winthread.lock);
if (store_data(m, DATA_FILE) != 0)
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, "WARNING: Failed to save to data file");