int get_frame(int fd, char *device, char **frame) { static char buf[1024]; int size = 0; int ms_idle = 0; while(1) { int c = read(fd, buf, 1); if(timed_out(c, &ms_idle)) { buf[0] = '\0'; return 0; } if(c == -1) { printf("error: read(%s): %s\n", device, strerror(errno)); return c; } if(c == 0) { continue; } if(buf[0] == '[') break; } while(1) { int i; int c = read(fd, buf + size, sizeof(buf) - size); if(timed_out(c, &ms_idle)) { printf("timeout\n"); buf[0] = '\0'; return 0; } if(c == -1) { printf("error: read(%s): %s\n", device, strerror(errno)); return c; } if(c == 0) { continue; } int const new_size = size + c; for(i = size; i < new_size; i++) { if(buf[i] == ']') { buf[i] = '\0'; *frame = buf; return i; } } size = new_size; } return -1; }
Main_window::Main_window(int argc, char**argv) :timer(this) , data1(0) , data2(0) , video(this) , file1(argv[1], atoi(argv[2]), atoi(argv[3]), Media::I420) , file2(argv[4], atoi(argv[5]), atoi(argv[6]), Media::I420) , stereo_mode(7) , disp_mode(Video_widget::RGB) { format[0] = Media::I420; format[1] = Media::I420; vwidth[0] = atoi(argv[2]); vheight[0] = atoi(argv[3]); vwidth[1] = atoi(argv[5]); vheight[1] = atoi(argv[6]); file1.open(); file2.open(); data1 = new uint8_t[file1.frame_size()]; data2 = new uint8_t[file2.frame_size()]; video.set_display_mode(disp_mode); video.set_stereo_mode(stereo_mode); connect(&timer, SIGNAL(timeout()), this, SLOT(timed_out())); timer.setInterval(40); timer.start(); }
/* * Sends a getnodes request to up to MAX_GETNODES_REQUESTS nodes in the nodes list that have not been queried. * Returns the number of requests sent. */ static size_t send_node_requests(Crawler *cwl) { if (!timed_out(cwl->last_getnodes_request, GETNODES_REQUEST_INTERVAL)) { return 0; } size_t count = 0; uint32_t i; for (i = cwl->send_ptr; count < MAX_GETNODES_REQUESTS && i < cwl->num_nodes; ++i) { DHT_getnodes(cwl->dht, &cwl->nodes_list[i].ip_port, cwl->nodes_list[i].public_key, cwl->nodes_list[i].public_key); for (size_t j = 0; j < NUM_RAND_GETNODE_REQUESTS; ++j) { int r = rand() % cwl->num_nodes; DHT_getnodes(cwl->dht, &cwl->nodes_list[i].ip_port, cwl->nodes_list[i].public_key, cwl->nodes_list[r].public_key); } ++count; } cwl->send_ptr = i; cwl->last_getnodes_request = get_time(); return count; }
void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event) { if (!log->log_on) return; if (log->file == NULL) { log->log_on = false; return; } uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3]; if (event) snprintf(name_frmt, sizeof(name_frmt), "* %s", name); else snprintf(name_frmt, sizeof(name_frmt), "%s:", name); struct tm *tminfo = get_time(); fprintf(log->file,"%04d/%02d/%02d [%02d:%02d:%02d] %s %s\n", tminfo->tm_year+1900, tminfo->tm_mon+1, tminfo->tm_mday, tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name_frmt, msg); uint64_t curtime = (uint64_t) time(NULL); if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT)) { fflush(log->file); log->lastwrite = curtime; } }
static void do_thread_control(void) { LOCK; if (threads.num_active >= MAX_CRAWLERS || !timed_out(threads.last_created, NEW_CRAWLER_INTERVAL)) { UNLOCK; return; } UNLOCK; Crawler *cwl = crawler_new(); if (cwl == NULL) { return; } int ret = init_crawler_thread(cwl); if (ret != 0) { fprintf(stderr, "init_crawler_thread() failed with error: %d\n", ret); return; } threads.last_created = get_time(); LOCK; ++threads.num_active; UNLOCK; }
static void send_file_data(ToxWindow *self, Tox *m, int i, int32_t friendnum, int filenum, const char *filename) { FILE *fp = file_senders[i].file; while (true) { if (tox_file_send_data(m, friendnum, filenum, (uint8_t *) file_senders[i].nextpiece, file_senders[i].piecelen) == -1) return; uint64_t curtime = get_unix_time(); file_senders[i].timestamp = curtime; file_senders[i].bps += file_senders[i].piecelen; file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, tox_file_data_size(m, friendnum), fp); double remain = (double) tox_file_data_remaining(m, friendnum, filenum, 0); /* refresh line with percentage complete and transfer speed (must be called once per second) */ if (timed_out(file_senders[i].last_progress, curtime, 1) || (!remain && !file_senders[i].finished)) { file_senders[i].last_progress = curtime; double pct_done = remain > 0 ? (1 - (remain / file_senders[i].size)) * 100 : 100; print_progress_bar(self, i, -1, pct_done); file_senders[i].bps = 0; } /* file sender is closed in chat_onFileControl callback after receiving reply */ if (file_senders[i].piecelen == 0 && !file_senders[i].finished) { tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0); file_senders[i].finished = true; } } }
static void groupchat_onGroupTitleChange(ToxWindow *self, Tox *m, int groupnum, int peernum, const char *title, uint8_t length) { ChatContext *ctx = self->chatwin; if (self->num != groupnum) return; set_window_title(self, title, length); char timefrmt[TIME_STR_SIZE]; get_time_str(timefrmt, sizeof(timefrmt)); /* don't announce title when we join the room */ if (!timed_out(groupchats[self->num].start_time, GROUP_EVENT_WAIT)) return; char nick[TOX_MAX_NAME_LENGTH]; get_group_nick_truncate(m, nick, peernum, groupnum); line_info_add(self, timefrmt, nick, NULL, NAME_CHANGE, 0, 0, " set the group title to: %s", title); char tmp_event[MAX_STR_SIZE]; snprintf(tmp_event, sizeof(tmp_event), "set title to %s", title); write_to_log(tmp_event, nick, ctx->log, true); }
void write_to_log(const uint8_t *msg, uint8_t *name, struct chatlog *log, bool event) { if (!log->log_on) return; if (log->file == NULL) { log->log_on = false; return; } uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3]; if (event) snprintf(name_frmt, sizeof(name_frmt), "* %s", name); else snprintf(name_frmt, sizeof(name_frmt), "%s:", name); uint8_t s[MAX_STR_SIZE]; strftime(s, MAX_STR_SIZE, "%Y/%m/%d [%H:%M:%S]", 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)) { fflush(log->file); log->lastwrite = curtime; } }
/* * pthread routine: Any timers that have timed out get their "timedout" value set to true */ void* poll_timers() { while(1) { //printf("PTHREAD: polling\n"); //sleep(1); struct timespec time, time2; time.tv_sec = 0; time.tv_nsec = 10000000L; // 1sec = 1,000,000,000 nanoseconds. 100th of a second nanosleep(&time, &time2); pthread_mutex_lock(&timers_mutex); int i; for(i=0; timer_list[i] != 0; i++) { struct packet_timer* timer = timer_list[i]; // make sure that the timer is running and it's data packet has been sent //printf("PTHREAD: checking if timer %d has timed out with current time: %ld \n",timer->pckt_ackno, clock()); if(timed_out(timer->pckt_ackno, timer_list, clock())) { //printf("PTHREAD: timer %d has timedout\n",timer->pckt_ackno); timer->timedout = true; } } pthread_mutex_unlock(&timers_mutex); } // return for compiler return NULL; }
/* Waits GROUP_EVENT_WAIT seconds for a new peer to set their name before announcing them */ void *group_add_wait(void *data) { struct group_add_thrd *thrd = (struct group_add_thrd *) data; ToxWindow *self = thrd->self; Tox *m = thrd->m; char peername[TOX_MAX_NAME_LENGTH]; /* keep polling for a name that differs from the default until we run out of time */ while (true) { usleep(100000); 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, GROUP_EVENT_WAIT)) { pthread_mutex_unlock(&Winthread.lock); break; } pthread_mutex_unlock(&Winthread.lock); } const char *event = "has joined the room"; char timefrmt[TIME_STR_SIZE]; get_time_str(timefrmt, sizeof(timefrmt)); pthread_mutex_lock(&Winthread.lock); line_info_add(self, timefrmt, (char *) peername, NULL, CONNECTION, 0, GREEN, event); write_to_log(event, (char *) peername, self->chatwin->log, true); pthread_mutex_unlock(&Winthread.lock); pthread_attr_destroy(&thrd->attr); free(thrd); pthread_exit(NULL); }
static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenum, const char *data, uint16_t length) { if (self->num != num) return; FILE *fp = friends[num].file_receiver.files[filenum]; if (fp) { if (fwrite(data, length, 1, fp) != 1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Error writing to file."); tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); chat_close_file_receiver(num, filenum); } } friends[num].file_receiver.bps[filenum] += length; double remain = (double) tox_file_data_remaining(m, num, filenum, 1); uint64_t curtime = get_unix_time(); /* refresh line with percentage complete and transfer speed (must be called once per second) */ if (!remain || timed_out(friends[num].file_receiver.last_progress[filenum], curtime, 1)) { friends[num].file_receiver.last_progress[filenum] = curtime; uint64_t size = friends[num].file_receiver.size[filenum]; double pct_remain = remain > 0 ? (1 - (remain / size)) * 100 : 100; print_progress_bar(self, filenum, num, pct_remain); friends[num].file_receiver.bps[filenum] = 0; } }
static void do_connection(Tox *m, ToxWindow *prompt) { char msg[MAX_STR_SIZE] = {0}; static int conn_err = 0; static bool was_connected = false; static uint64_t last_conn_try = 0; uint64_t curtime = get_unix_time(); bool is_connected = tox_isconnected(m); if (was_connected && is_connected) return; if (!was_connected && is_connected) { was_connected = true; prompt_update_connectionstatus(prompt, was_connected); snprintf(msg, sizeof(msg), "DHT connected."); } else if (was_connected && !is_connected) { was_connected = false; prompt_update_connectionstatus(prompt, was_connected); 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]) line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); }
void write_to_log(const char *msg, const char *name, struct chatlog *log, bool event) { if (!log->log_on) return; if (log->file == NULL) { log->log_on = false; return; } char name_frmt[TOXIC_MAX_NAME_LENGTH + 3]; if (event) snprintf(name_frmt, sizeof(name_frmt), "* %s", name); else snprintf(name_frmt, sizeof(name_frmt), "%s:", name); const char *t = user_settings->log_timestamp_format; char s[MAX_STR_SIZE]; strftime(s, MAX_STR_SIZE, t, get_time()); fprintf(log->file, "%s %s %s\n", s, name_frmt, msg); if (timed_out(log->lastwrite, LOG_FLUSH_LIMIT)) { fflush(log->file); log->lastwrite = get_unix_time(); } }
uint8_t readBytes(uint8_t address, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout) { #ifdef I2C_DEBUG printf("I2C (0x"); printf("%02X", address); printf(") reading "); printf("%d", length); printf(" byte(s) from 0x"); printf("%02X", regAddr); printf("...\r\n"); #endif uint8_t count = 0; set_timeout(timeout); for (uint8_t i=0; i< length; i+= min(length, I2C_BUFFER_LENGTH)) { i2c_begin_transmission(address); // i2c_write_byte(regAddr); // i2c_write(®Addr, 1); i2c_write_byte(regAddr); i2c_end_transmission(1); i2c_request_from(address, (uint8_t) min(length - i, I2C_BUFFER_LENGTH), 0, 0, 1); while(i2c_available() > 0) { data[count] = i2c_read(); #ifdef I2C_DEBUG printf("%02X", data[count]); if(count + 1 < length) printf(" "); #endif count++; // break on timeout if(timed_out()) { #ifdef I2C_DEBUG printf("\r\nreadBytes: timeout occured: (%d)ms\r\n", timeout); #endif break; } } } #ifdef I2C_DEBUG printf(" done. ("); printf("%d", count); printf(" read)\r\n"); #endif return count; }
int main(int argc, char **argv) { signal(SIGINT, catch_SIGINT); umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); Tox *m = init_tox(); if (m == NULL) exit(EXIT_FAILURE); init_toxbot_state(); print_profile_info(m); bootstrap_DHT(m); uint64_t looptimer = (uint64_t) time(NULL); uint64_t last_friend_purge = 0; uint64_t last_group_purge = 0; useconds_t msleepval = 40000; uint64_t loopcount = 0; while (!FLAG_EXIT) { uint64_t cur_time = (uint64_t) time(NULL); if (timed_out(last_friend_purge, cur_time, FRIEND_PURGE_INTERVAL)) { purge_inactive_friends(m); save_data(m, DATA_FILE); last_friend_purge = cur_time; } if (timed_out(last_group_purge, cur_time, GROUP_PURGE_INTERVAL)) { purge_empty_groups(m); last_group_purge = cur_time; } tox_iterate(m); msleepval = optimal_msleepval(&looptimer, &loopcount, cur_time, msleepval); usleep(msleepval); } exit_toxbot(m); return 0; }
/* Returns true if the crawler is unable to find new nodes in the DHT or the exit flag has been triggered */ static bool crawler_finished(Crawler *cwl) { LOCK; if (FLAG_EXIT || (cwl->send_ptr == cwl->num_nodes && timed_out(cwl->last_new_node, CRAWLER_TIMEOUT))) { UNLOCK; return true; } UNLOCK; return false; }
void check_file_transfer_timeouts(Tox *m) { char msg[MAX_STR_SIZE]; static uint64_t last_check = 0; if (!timed_out(last_check, CHECK_FILE_TIMEOUT_INTERAVAL)) return; last_check = get_unix_time(); size_t i, j; for (i = 0; i < Friends.max_idx; ++i) { if (!Friends.list[i].active) continue; for (j = 0; j < MAX_FILES; ++j) { struct FileTransfer *ft_send = &Friends.list[i].file_sender[j]; if (ft_send->state > FILE_TRANSFER_PAUSED) { if (timed_out(ft_send->last_keep_alive, TIMEOUT_FILESENDER)) { snprintf(msg, sizeof(msg), "File transfer for '%s' timed out.", ft_send->file_name); close_file_transfer(ft_send->window, m, ft_send, TOX_FILE_CONTROL_CANCEL, msg, notif_error); } } struct FileTransfer *ft_recv = &Friends.list[i].file_receiver[j]; if (ft_recv->state > FILE_TRANSFER_PAUSED) { if (timed_out(ft_recv->last_keep_alive, TIMEOUT_FILESENDER)) { snprintf(msg, sizeof(msg), "File transfer for '%s' timed out.", ft_recv->file_name); close_file_transfer(ft_recv->window, m, ft_recv, TOX_FILE_CONTROL_CANCEL, msg, notif_error); } } } } }
void pdu_send(struct pdu *pdu) { struct connection *conn; ssize_t ret, total_len; size_t padding; uint32_t zero = 0; struct iovec iov[3]; int iovcnt; conn = pdu->pdu_connection; #ifdef ICL_KERNEL_PROXY if (conn->conn_conf.isc_iser != 0) return (pdu_send_proxy(pdu)); #endif assert(conn->conn_conf.isc_iser == 0); pdu_set_data_segment_length(pdu, pdu->pdu_data_len); iov[0].iov_base = pdu->pdu_bhs; iov[0].iov_len = sizeof(*pdu->pdu_bhs); total_len = iov[0].iov_len; iovcnt = 1; if (pdu->pdu_data_len > 0) { iov[1].iov_base = pdu->pdu_data; iov[1].iov_len = pdu->pdu_data_len; total_len += iov[1].iov_len; iovcnt = 2; padding = pdu_padding(pdu); if (padding > 0) { assert(padding < sizeof(zero)); iov[2].iov_base = &zero; iov[2].iov_len = padding; total_len += iov[2].iov_len; iovcnt = 3; } } ret = writev(conn->conn_socket, iov, iovcnt); if (ret < 0) { if (timed_out()) log_errx(1, "exiting due to timeout"); log_err(1, "writev"); } if (ret != total_len) log_errx(1, "short write"); }
static gboolean timer_handler(gpointer data) { os_hnd_timer_id_t *timer_data = (os_hnd_timer_id_t *) data; /* Make a copy of this, because the handler may delete the timer data. */ void *cb_data; os_timed_out_t timed_out; timed_out = timer_data->timed_out; cb_data = timer_data->cb_data; timer_data->running = 0; timed_out(cb_data, timer_data); return FALSE; }
static void refresh_progress_helper(ToxWindow *self, Tox *m, 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_line_progress, 1)) return; double remain = ft->file_size - ft->position; double pct_done = remain > 0 ? (1 - (remain / ft->file_size)) * 100 : 100; print_progress_bar(self, ft->bps, pct_done, ft->line_id); ft->bps = 0; ft->last_line_progress = get_unix_time(); }
static void timer_handler(selector_t *sel, sel_timer_t *timer, void *data) { os_hnd_timer_id_t *timer_data = (os_hnd_timer_id_t *) data; /* Make a copy of this, because the handler may delete the timer data. */ void *cb_data; os_timed_out_t timed_out; timed_out = timer_data->timed_out; cb_data = timer_data->cb_data; timer_data->running = 0; timed_out(cb_data, timer_data); }
static int one_touch_play_req_active_source(struct node *node, unsigned me, unsigned la, bool interactive) { struct cec_msg msg = {}; cec_msg_init(&msg, me, la); cec_msg_active_source(&msg, node->phys_addr); fail_on_test(!transmit_timeout(node, &msg)); /* We have now said that we are active source, so receiving a reply to Request Active Source should fail the test. */ cec_msg_init(&msg, me, la); cec_msg_request_active_source(&msg, true); fail_on_test(!transmit_timeout(node, &msg)); fail_on_test(!timed_out(&msg)); return 0; }
static void pdu_read(int fd, char *data, size_t len) { ssize_t ret; while (len > 0) { ret = read(fd, data, len); if (ret < 0) { if (timed_out()) log_errx(1, "exiting due to timeout"); log_err(1, "read"); } else if (ret == 0) log_errx(1, "read: connection lost"); len -= ret; data += ret; } }
GUI_Spectrum::GUI_Spectrum(QWidget *parent) : QWidget(parent) //, ui(new Ui::GUI_Spectrum) { highlightedColumn = -1; _cur_style_idx = 0;//CSettingsStorage::getInstance()->getSpectrumStyle(); ui = new Ui::GUI_Spectrum(); ui->setupUi(this); _timer = new QTimer(); _timer->setInterval(30); _timer_stopped = true; connect(_timer, SIGNAL(timeout()), this, SLOT(timed_out())); _ecsc = new EngineColorStyleChooser(this->minimumWidth(), this->minimumHeight()); _cur_style = _ecsc->get_color_scheme_spectrum(_cur_style_idx); _update_running = false; for(int i=0; i<N_BINS; i++){ _spec << 0.0f; } for(int i=0; i<1100; i++){ log_lu[i] = log( (i * 1.0f) / 10.0f ); } _steps = new int*[N_BINS]; for(int i=0; i<N_BINS; i++){ _steps[i] = new int[_cur_style.n_rects]; for(int j=0; j<_cur_style.n_rects; j++){ _steps[i][j] = 0; } } //ui->lab->setText("fdfsdfs"); ui->lab->setMouseTracking(true); this->setMouseTracking(true); parent->setMouseTracking(true); //this->installEventFilter(this); this->update(); }
static void timer_handler(selector_t *sel, sel_timer_t *timer, void *data) { os_hnd_timer_id_t *timer_data = (os_hnd_timer_id_t *) data; /* Make a copy of this, because the handler may delete the timer data. */ void *cb_data; os_timed_out_t timed_out; /* Lock will create a memory barrier, which we really need here. */ pthread_mutex_lock(&timer_data->lock); timed_out = timer_data->timed_out; cb_data = timer_data->cb_data; timer_data->running = 0; pthread_mutex_unlock(&timer_data->lock); timed_out(cb_data, timer_data); }
static void do_bootstrap(Tox *m) { static int conn_err = 0; if (!timed_out(last_bootstrap_time, TRY_BOOTSTRAP_INTERVAL)) return; if (tox_self_get_connection_status(m) != TOX_CONNECTION_NONE) return; if (conn_err != 0) return; last_bootstrap_time = get_unix_time(); conn_err = init_connection(m); if (conn_err != 0) line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Auto-connect failed with error code %d", conn_err); }
void do_file_senders(Tox *m) { int i; for (i = 0; i < max_file_senders_index; ++i) { if (!file_senders[i].active) continue; if (file_senders[i].queue_pos > 0) { --file_senders[i].queue_pos; continue; } ToxWindow *self = file_senders[i].toxwin; char *filename = file_senders[i].filename; int filenum = file_senders[i].filenum; int32_t friendnum = file_senders[i].friendnum; /* kill file transfer if chatwindow is closed */ if (self->chatwin == NULL) { close_file_sender(self, m, i, NULL, TOX_FILECONTROL_KILL, filenum, friendnum); continue; } /* If file transfer has timed out kill transfer and send kill control */ if (timed_out(file_senders[i].timestamp, get_unix_time(), TIMEOUT_FILESENDER)) { char msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "File transfer for '%s' timed out.", filename); close_file_sender(self, m, i, msg, TOX_FILECONTROL_KILL, filenum, friendnum); sound_notify(self, error, NT_NOFOCUS | NT_WNDALERT_2, NULL); if (self->active_box != -1) box_notify2(self, error, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "%s", msg); else box_notify(self, error, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, self->name, "%s", msg); continue; } send_file_data(self, m, i, friendnum, filenum, filename); file_senders[i].queue_pos = num_active_file_senders - 1; } }
static int power_status_give(struct node *node, unsigned me, unsigned la, bool interactive) { struct cec_msg msg = { }; cec_msg_init(&msg, me, la); cec_msg_give_device_power_status(&msg, true); fail_on_test(!transmit_timeout(node, &msg)); fail_on_test(timed_out(&msg)); fail_on_test(unrecognized_op(&msg)); if (refused(&msg)) return REFUSED; if (cec_msg_status_is_abort(&msg)) return PRESUMED_OK; __u8 power_status; cec_ops_report_power_status(&msg, &power_status); fail_on_test(power_status >= 4); return 0; }
static void pdu_read(const struct connection *conn, char *data, size_t len) { ssize_t ret; while (len > 0) { ret = read(conn->conn_socket, data, len); if (ret < 0) { if (timed_out()) { fail(conn, "Login Phase timeout"); log_errx(1, "exiting due to timeout"); } fail(conn, strerror(errno)); log_err(1, "read"); } else if (ret == 0) { fail(conn, "connection lost"); log_errx(1, "read: connection lost"); } len -= ret; data += ret; } }
int main(int argc, char *argv[]) { char *user_config_dir = get_user_config_dir(); int config_err = 0; parse_args(argc, argv); /* Make sure all written files are read/writeable only by the current user. */ umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); signal(SIGINT, catch_SIGINT); config_err = create_user_config_dir(user_config_dir); if (DATA_FILE == NULL ) { if (config_err) { DATA_FILE = strdup("data"); if (DATA_FILE == NULL) exit_toxic_err("failed in main", FATALERR_MEMORY); } else { DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); if (DATA_FILE == NULL) exit_toxic_err("failed in main", FATALERR_MEMORY); strcpy(DATA_FILE, user_config_dir); strcat(DATA_FILE, CONFIGDIR); strcat(DATA_FILE, "data"); } } free(user_config_dir); /* init user_settings struct and load settings from conf file */ user_settings_ = calloc(1, sizeof(struct user_settings)); if (user_settings_ == NULL) exit_toxic_err("failed in main", FATALERR_MEMORY); char *p = arg_opts.config_path[0] ? arg_opts.config_path : NULL; int settings_err = settings_load(user_settings_, p); Tox *m = init_tox(arg_opts.use_ipv4); init_term(); if (m == NULL) exit_toxic_err("failed in main", FATALERR_NETWORKINIT); if (!arg_opts.ignore_data_file) load_data(m, DATA_FILE); prompt = init_windows(m); prompt_init_statusbar(prompt, m); /* 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); #ifdef _AUDIO av = init_audio(prompt, m); set_primary_device(input, user_settings_->audio_in_dev); set_primary_device(output, user_settings_->audio_out_dev); #elif _SOUND_NOTIFY if ( init_devices() == de_InternalError ) line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to init devices"); #endif /* _AUDIO */ init_notify(60, 3000); #ifdef _SOUND_NOTIFY notify(prompt, self_log_in, 0); #endif /* _SOUND_NOTIFY */ const char *msg; if (config_err) { msg = "Unable to determine configuration directory. Defaulting to 'data' for a keyfile..."; line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); } if (settings_err == -1) { msg = "Failed to load user settings"; line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); } sort_friendlist_index(); uint64_t last_save = (uint64_t) time(NULL); while (true) { update_unix_time(); do_toxic(m, prompt); uint64_t cur_time = get_unix_time(); if (timed_out(last_save, cur_time, AUTOSAVE_FREQ)) { pthread_mutex_lock(&Winthread.lock); store_data(m, DATA_FILE); pthread_mutex_unlock(&Winthread.lock); last_save = cur_time; } usleep(40000); } return 0; }