int main(int argc, char **argv) { if (3 > argc) { fprintf(stderr, "Usage: %s host port\n", argv[0]); return -1; } addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr(argv[1]); addr.sin_port = htons(atoi(argv[2])); number_of_clients = 0; times_sum = 0.0; times_count = 0; times_max = 0.0; times_min = 100.0; loop = ev_default_loop(0); ev_timer_init(&wev_exit, wcb_exit, 0, EXECUTION_TIME); ev_timer_again(loop, &wev_exit); ev_timer_init(&wev_silent, wcb_silent, 0, SILENT_TIMEOUT); ev_timer_again(loop, &wev_silent); ev_run(loop, 0); fprintf(stderr, "times_sum=%f times_count=%u avg=%f min=%f max=%f\n", times_sum, (unsigned) times_count, times_sum / times_count, times_min, times_max); return 0; }
/** * enter_event_loop -- begin execution of the game loop * * After initialization and set-up, this is where the flow of control will * reside for the vast majority of the process lifetime. It will only be * exited in order to handle a termination signal, or as a response to a * user's "quit" command. */ void enter_event_loop(void) { #define REPEAT_PERIOD 0.025f #define ANIMATE_PERIOD 3.5f #define FLOW_PERIOD 0.08f struct ev_loop *readloop = EV_DEFAULT; struct ev_loop *execloop = EV_DEFAULT; ev_io read; // stdin is readable ev_timer render; // things which must occur once per tick ("atomic") ev_timer flow; // water and weather effects, some physics ev_timer animate; // long-period animations of the map window ev_io_init(&read, &iolisten_cb, 0, EV_READ); ev_init(&render, &render_cb); ev_init(&animate, &animate_cb); ev_init(&flow, &flow_cb); render.repeat = REPEAT_PERIOD; animate.repeat = ANIMATE_PERIOD; flow.repeat = FLOW_PERIOD; ev_timer_again(execloop, &render); ev_timer_again(execloop, &animate); ev_timer_again(execloop, &flow); loop_test(true); ev_io_start(readloop, &read); ev_run(execloop, 0); }
static void _prepare_cb(UNUSED EV_P_ UNUSED ev_prepare *w, UNUSED int revents) { mnbtrie_node_t *node; mrkthr_ctx_t *ctx = NULL; if (!(mrkthr_flags & CO_FLAG_SHUTDOWN)) { timecounter_now = (uint64_t)(ev_now(the_loop) * 1000000000.); #ifdef TRACE_VERBOSE CTRACE(FRED("Sifting sleepq ...")); #endif /* this will make sure there are no expired ctxes in the sleepq */ poller_sift_sleepq(); /* get the first to wake sleeping mrkthr */ if ((node = BTRIE_MIN(&the_sleepq)) != NULL) { ev_tstamp secs; ctx = node->value; assert(ctx != NULL); if (ctx->expire_ticks > timecounter_now) { secs = (ev_tstamp)(ctx->expire_ticks - timecounter_now) / 1000000000.; } else { /* * some time has elapsed after the call to * sift_sleepq() that made an event expire. */ secs = 0.00000095367431640625; } #ifdef TRACE_VERBOSE CTRACE("wait %f", secs); #endif etimer.repeat = secs; ev_timer_again(the_loop, &etimer); } else { #ifdef TRACE_VERBOSE CTRACE("no wait"); #endif //etimer.repeat = 1.00000095367431640625; //etimer.repeat = INFINITY; etimer.repeat = 59.0; /* <MAX_BLOCKTIME */ ev_timer_again(the_loop, &etimer); //ev_timer_stop(the_loop, &etimer); //ev_unref(the_loop); } } else { CTRACE("breaking the loop"); ev_break(the_loop, EVBREAK_ALL); } //CTRACE("revents=%08x", revents); }
void start_watchers(struct ev_loop *loop) { ev_io_start(loop, &bspwm_watcher); ev_timer_again(loop, &wifi_timer); ev_timer_again(loop, &battery_watcher); ev_timer_again(loop, &sound_watcher); ev_io_start(loop, &packages_io_watcher); ev_periodic_start(loop, &clock_watcher); ev_signal_start(loop, &signal_watcher); ev_prepare_start(loop, &prepare_display); }
void evcom_stream_read_resume (evcom_stream *stream) { stream->flags &= ~EVCOM_PAUSED; ev_timer_again(D_LOOP_(stream) &stream->timeout_watcher); if (stream->recv_action == stream_recv__wait_for_resume) { stream->recv_action = stream_recv__data; } if (ATTACHED(stream)) { ev_timer_again(D_LOOP_(stream) &stream->timeout_watcher); if (READABLE(stream)) ev_io_start(D_LOOP_(stream) &stream->read_watcher); } }
static void dns_timeout_cb (EV_P_ ev_timer *w, int revents) { ev_now_update(EV_A); ev_tstamp now = ev_now(EV_A ); int next = dns_timeouts(NULL, -1, now); if (next > 0) { w->repeat = next; ev_timer_again (EV_A_ w); } else { w->repeat = 1; ev_timer_again (EV_A_ w); } }
/* Internal callback. called by stream->timeout_watcher */ static void on_timeout (EV_P_ ev_timer *watcher, int revents) { evcom_stream *stream = watcher->data; #if EV_MULTIPLICITY assert(stream->loop == loop); #endif assert(revents == EV_TIMEOUT); assert(watcher == &stream->timeout_watcher); if (PAUSED(stream)) { ev_timer_again(D_LOOP_(stream) &stream->timeout_watcher); return; } if (stream->on_timeout) stream->on_timeout(stream); // Hack to get error in Node on 'close' event. // should probably be made into a proper error code. stream->errorno = 1; ev_timer_stop(EV_A_ watcher); evcom_stream_force_close(stream); if (stream->on_close) stream->on_close(stream); }
void http_transport_start(struct c2_transport *t) { struct http_ctx *ctx = c2_transport_get_ctx(t); ctx->running = 1; ctx->poll_timer.repeat = 0.01; ev_timer_again(c2_transport_loop(t), &ctx->poll_timer); }
/* Internal callback * Called by server->connection_watcher. */ static void on_connection(struct ev_loop *loop, ev_io *watcher, int revents) { ebb_server *server = watcher->data; //printf("on connection!\n"); assert(server->listening); assert(server->loop == loop); assert(&server->connection_watcher == watcher); if(EV_ERROR & revents) { error("on_connection() got error event, closing server."); ebb_server_unlisten(server); return; } struct sockaddr_in addr; // connector's address information socklen_t addr_len = sizeof(addr); int fd = accept( server->fd , (struct sockaddr*) & addr , & addr_len ); if(fd < 0) { if(errno != EAGAIN && errno != EWOULDBLOCK) { perror("accept()"); } return; } ebb_connection *connection = NULL; if(server->new_connection) connection = server->new_connection(server, &addr); if(connection == NULL) { close(fd); return; } set_nonblock(fd); connection->fd = fd; connection->open = TRUE; connection->server = server; memcpy(&connection->sockaddr, &addr, addr_len); if(server->port[0] != '\0') connection->ip = inet_ntoa(connection->sockaddr.sin_addr); #ifdef SO_NOSIGPIPE int arg = 1; setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &arg, sizeof(int)); #endif /* Note: not starting the write watcher until there is data to be written */ ev_io_set(&connection->write_watcher, connection->fd, EV_WRITE); ev_io_set(&connection->read_watcher, connection->fd, EV_READ); /* XXX: seperate error watcher? */ ev_timer_again(loop, &connection->timeout_watcher); ev_io_start(loop, &connection->read_watcher); }
/** This would get called when idel time watcher goes timeout */ static void wr_wkr_wait_cb(struct ev_loop *loop, ev_timer *w, int revents) { LOG_FUNCTION wr_wkr_t *worker = (wr_wkr_t*)w->data; //ev_timer_stop(loop, &worker->t_idle); ev_timer_stop(loop, &worker->t_wait); if(worker->app == NULL) { LOG_INFO("wr_wkr_wait_cb: Worker removed with pid %d", worker->pid); wr_wkr_free(worker); } else if(worker->trials_done < WR_PING_TRIALS) { worker->trials_done++; LOG_INFO("Worker %d with pid %u ping for trial no %d", worker->id, worker->pid, worker->trials_done); worker->t_wait.repeat = WR_PING_WAIT_TIME; worker->state |= (WR_WKR_HANG | WR_WKR_PING_SENT); if(worker->state & WR_WKR_PING_REPLIED) { worker->state ^= WR_WKR_PING_REPLIED; } scgi_t *scgi = scgi_new(); scgi_header_add(scgi, "COMPONENT", strlen("COMPONENT"), "WORKER", strlen("WORKER")); scgi_header_add(scgi, "METHOD", strlen("METHOD"), "PING", strlen("PING")); if(scgi_build(scgi)!=0) { LOG_ERROR(WARN,"SCGI request build failed."); } worker->ctl->scgi = scgi; ev_io_start(loop, &worker->ctl->w_write); //ev_timer_again(loop, &worker->t_ping_wait); ev_timer_again(loop, &worker->t_wait); } else { //render 500 response to Request, kill the worker wr_wkr_recreate(worker); } }
/* * animate_cb * * Probably an ambiguous name, this is a long-term callback which steps * through the WINDOWs in a multiwin linked list, drawing the newly active * WINDOW to the screen. It is responsible for the edge effects along the * shoreline right now. * Repeat: 3.5 seconds */ void animate_cb(EV_P_ ev_timer *w, int revents) { spin_animate_loop(); NEXT(ACTIVE->L[RIM]); ev_timer_again(EV_DEFAULT, w); }
static void connection_readable_cb(EV_P_ struct ev_io *watcher, int revents) { connection_t *c = get_ev_data(connection, watcher, read); char buf[BUFFER_CHUNK_SIZE]; if (EV_ERROR & revents) { connection_error(c, "Error reading on connection socket"); return; } size_t received = recv(c->fd, buf, BUFFER_CHUNK_SIZE, 0); ev_timer_again(c->loop, &c->timeout_watcher); if (received == -1) { /* error, closing connection */ connection_errno(c); return; } if (received == 0) { /* received 0 byte, read again next loop */ return; } trace(c->backend, buf, received); request_parse(c, buf, received); }
void read_cb(int fd, short which, void *arg) { io_block * block = arg; int idx = block->idx, widx = idx + 1; u_char ch; if (timers) { if (native) { #if NATIVE block->timer.repeat = 10. + drand48 (); ev_timer_again (EV_DEFAULT_UC_ &block->timer); #else abort (); #endif } else { struct timeval tv; event_del (&io_blocks[idx].event); tv.tv_sec = 10; tv.tv_usec = drand48() * 1e6; event_add(&io_blocks[idx].event, &tv); } } count += read(fd, &ch, sizeof(ch)); if (writes) { if (widx >= num_pipes) widx -= num_pipes; write(pipes[2 * widx + 1], "e", 1); writes--; fired++; } }
static inline void wr_wait_watcher_start(wr_wkr_t *w) { w->trials_done = 0; // Clear WR_WKR_PING_SENT, WR_WKR_PING_REPLIED and WR_WKR_HANG state. w->state &= (~224); w->t_wait.repeat = WR_WKR_IDLE_TIME; ev_timer_again(w->loop, &w->t_wait); }
void evcom_stream_reset_timeout (evcom_stream *stream, float timeout) { stream->timeout_watcher.repeat = timeout; if (ATTACHED(stream)) { ev_timer_again(D_LOOP_(stream) &stream->timeout_watcher); } }
/* * flow_cb * * This callback calculates the next iteration of the surface flow and renders * them on the screen. * Repeat: 0.1 seconds */ void flow_cb(EV_P_ ev_timer *w, int revents) { spin_flow_loop(); do_flow(MAPBOOK->map[WORLD]); ev_timer_again(EV_DEFAULT, w); }
int uv_timer_again(uv_timer_t* timer) { if (!ev_is_active(&timer->timer_watcher)) { uv__set_sys_error(timer->loop, EINVAL); return -1; } ev_timer_again(timer->loop->ev, &timer->timer_watcher); return 0; }
static void client_ev_timeout(struct ev_loop *loop, ev_timer *w, ATTR_UNUSED int revents) { RTSP_Client *rtsp = w->data; if(rtsp->session && rtsp->session->rtp_sessions) g_slist_foreach(rtsp->session->rtp_sessions, check_if_any_rtp_session_timedout, NULL); ev_timer_again (loop, w); }
int uv_timer_again(uv_handle_t* handle) { if (!ev_is_active(&handle->timer_watcher)) { uv_err_new(handle, EINVAL); return -1; } ev_timer_again(EV_DEFAULT_UC_ &handle->timer_watcher); return 0; }
//---------------------------------------------------------------------------- void dir_watcher_t::restart_dir_timeout() { // restart timeout (with possibly new value) if( p_loop_) { ev_w_dir_timeout_.repeat = d_dir_timeout_sec_; ev_timer_again( p_loop_, &ev_w_dir_timeout_); } }
static void uv__ares_io(EV_P_ struct ev_io* watcher, int revents) { /* Reset the idle timer */ ev_timer_again(EV_A_ &ares_data.timer); /* Process DNS responses */ ares_process_fd(ares_data.channel, revents & EV_READ ? watcher->fd : ARES_SOCKET_BAD, revents & EV_WRITE ? watcher->fd : ARES_SOCKET_BAD); }
int uv_timer_again(uv_timer_t* timer) { if (!ev_is_active(&timer->timer_watcher)) { uv_err_new((uv_handle_t*)timer, EINVAL); return -1; } ev_timer_again(EV_DEFAULT_UC_ &timer->timer_watcher); return 0; }
static int fdevent_libev_poll(fdevents *ev, int timeout_ms) { timeout_watcher.repeat = (timeout_ms > 0) ? timeout_ms/1000.0 : 0.001; ev_timer_again(ev->libev_loop, &timeout_watcher); ev_run(ev->libev_loop, EVRUN_ONCE); fdevent_sched_run(ev->srv, ev); return 0; }
int uv_timer_again(uv_timer_t* timer) { if (!uv__timer_active(timer)) { uv__set_sys_error(timer->loop, EINVAL); return -1; } assert(uv__timer_repeating(timer)); ev_timer_again(timer->loop->ev, &timer->timer_watcher); return 0; }
/** * register timer callbacks * first execution of the callback after timeout */ ev_watcher* event_register_timer_w(EV_P_ watcher_cb_t cb, double to) { ev_timer* ev_handle = (ev_timer*) malloc(sizeof(ev_timer)); // ev_init does not case to ev_watcher, while setting callback ev_init( ev_handle, (timer_cb_t)cb); ev_timer_set(ev_handle, 0, to); ev_timer_again(EV_A_ ev_handle); return (ev_watcher*) ev_handle; }
static void mqtt_write_cb(EV_P_ ev_io *w, int revents) { EV_ALL *cev = (EV_ALL *)(((char *)w) - offsetof (EV_ALL, mt_writew)); int rc = FAILURE, sent = 0; unsigned char *buf_ptr_item; send_queue_item_st *item; item = llqueue_poll(cev->client.send_queue); buf_ptr_item = item->sendbuf; int length = item->buflen; while (sent < length) { rc = linux_write_ev(cev->client.ipstack, &buf_ptr_item[sent], length); if (rc < 0) // there was an error writing the data break; sent += rc; } if (sent == length) { //record the fact that we have successfully sent the packet if(cev->client.keepAliveInterval >0) { // cev->mt_pingw.repeat = cev->client.keepAliveInterval-1; //可以重新设超时时间 ev_timer_again(cev->mainloop, &cev->mt_pingw); //重新计时发ping包 log_printf(LOG_NOTICE, "reset mqtt ping timer,interval is %d\n", PING_INTERVAL); } log_printf(LOG_NOTICE, "[Send2Mqtt]: %s, %d\n", buf_ptr_item, length); rc = SUCCESS; } else { log_printf(LOG_ERROR, "mqtt_write_cb failed\n"); rc = FAILURE; } //队列数据取出发送之后,需要释放 free(buf_ptr_item); //must free free(item); //must free int q_count = llqueue_count(cev->client.send_queue); log_printf(LOG_NOTICE, "[MqttSendQueueCount]=%d\n", q_count); if(q_count <=0) { log_printf(LOG_NOTICE, "[StopMqttSendWatcher]send queue empty!\n"); ev_io_stop(cev->mainloop, &cev->mt_writew); } // if(rc != SUCCESS) { log_printf(LOG_ERROR, "mqtt_write_cb failed,[ReConnectMqtt]\n"); MQTTReConnect(&cev->client); } }
static void zg_ping_cb(EV_P_ ev_timer *w, int revents) { char *temp = "pingtest"; EV_ALL *cev = (EV_ALL *)(((char *)w) - offsetof (EV_ALL, zg_pingw)); ev_send_msg_2_zigbees(cev, (unsigned char *)temp, strlen(temp)+1); log_printf(LOG_NOTICE, "[SendPing2ZW] %d\n", cev->zigbee_client.keepAliveInterval); if(cev->zigbee_client.keepAliveInterval >0) ev_timer_again(cev->mainloop, &cev->zg_pingw); //重新计时发ping包 }
static void reset_timer() { struct timeval tvout; struct timeval *tv = ares_timeout(default_ctx.channel, NULL, &tvout); if (tv == NULL) { return; } float repeat = tv->tv_sec + tv->tv_usec / 1000000. + 1e-9; ev_timer_set(&default_ctx.tw, repeat, repeat); ev_timer_again(default_loop, &default_ctx.tw); }
static void packages_io_cb(struct ev_loop *loop, ev_io *w, int revents) { (void) revents; ev_io_stop(loop, w); FILE *packages_file = fdopen(w->fd, "r"); PackagesInfo *info = (PackagesInfo *) w->data; packages_update(info, packages_file); pclose(packages_file); ev_timer_again(loop, &packages_timer); }
/** Check load balance to add the worker */ int wr_app_chk_load_to_add_wkr(wr_app_t *app) { if(TOTAL_WORKER_COUNT(app) < app->conf->max_worker) { if(app->msg_que->q_count > app->high_ratio) { if(!ev_is_active(&app->t_add)) { LOG_DEBUG(DEBUG,"%s() Timer set", __FUNCTION__); ev_timer_again(app->svr->ebb_svr.loop, &app->t_add); } } else if(ev_is_active(&app->t_add)) { LOG_DEBUG(DEBUG,"%s() Timer stop", __FUNCTION__); ev_timer_stop(app->svr->ebb_svr.loop, &app->t_add); } } }