static void thread_deferred_cb_skew(void *arg) { struct basic_test_data *data = arg; struct timeval tv_timer = {4, 0}; struct deferred_cb_queue *queue; time_t elapsed; int i; queue = event_base_get_deferred_cb_queue(data->base); tt_assert(queue); for (i = 0; i < QUEUE_THREAD_COUNT; ++i) deferred_data[i].queue = queue; timer_start = time(NULL); event_base_once(data->base, -1, EV_TIMEOUT, timer_callback, NULL, &tv_timer); event_base_once(data->base, -1, EV_TIMEOUT, start_threads_callback, NULL, NULL); event_base_dispatch(data->base); elapsed = timer_end - timer_start; TT_BLATHER(("callback count, %u", callback_count)); TT_BLATHER(("elapsed time, %u", (unsigned)elapsed)); /* XXX be more intelligent here. just make sure skew is * within 2 seconds for now. */ tt_assert(elapsed >= 4 && elapsed <= 6); end: for (i = 0; i < QUEUE_THREAD_COUNT; ++i) THREAD_JOIN(load_threads[i]); }
int main() { pipe(pp); struct event_base *base = event_base_new(); event_base_once(base, pp[0], EV_READ, read_cb, NULL, NULL); /* must not specify EV_PERSIST */ event_base_once(base, pp[1], EV_WRITE, write_cb, NULL, NULL); event_base_dispatch(base); return 0; }
void kismet_conn_eventcb(struct bufferevent *bev, short event, void* args) { DEBUG(255, "kismet_conn_eventcb(%p, 0x%02x, %p);", bev, event, args); if (!(event & BEV_EVENT_CONNECTED)) { struct event_base* base = bufferevent_get_base(bev); struct server* node = (struct server*) args; bufferevent_free(node->conn); node->conn = NULL; struct timeval tv = {node->timeout, 0}; struct reconnect_struct* reconnect_struct = malloc(sizeof(struct reconnect_struct)); reconnect_struct->server = node; reconnect_struct->base = base; struct inserter* inserter = node->inserters; while (inserter) { if (inserter->capabilities) { unsigned int next_token = 0; while (inserter->capabilities[next_token]) free(inserter->capabilities[next_token++]); free(inserter->capabilities); inserter->capabilities = NULL; } inserter = inserter->next; }; event_base_once(base, -1, EV_TIMEOUT, reconnectServer, reconnect_struct, &tv); } };
static void mosq_reconnect(struct _squash *st) { mosq_cleanup(st); /* now schedule a reconnection for 5 seconds away. */ struct timeval later = {5, 0}; event_base_once(st->base, -1, EV_TIMEOUT, mosq_reconnect_handler, st, &later); }
/** Initialize the software. This should be called FIRST */ void napaInit(void *event2_base) { eventbase = event2_base; #if 0 /* Won't need this for normal operation */ struct timeval t = { 1000*1000*1000, 0 }; event_base_once(eventbase, -1, EV_TIMEOUT, never_callback, NULL, &t); #endif }
static void thread_deferred_cb_skew(void *arg) { struct timeval tv_timer = {1, 0}; struct event_base *base = NULL; struct event_config *cfg = NULL; struct timeval elapsed; int elapsed_usec; int i; cfg = event_config_new(); tt_assert(cfg); event_config_set_max_dispatch_interval(cfg, NULL, 16, 0); base = event_base_new_with_config(cfg); tt_assert(base); for (i = 0; i < QUEUE_THREAD_COUNT; ++i) deferred_data[i].queue = base; evutil_gettimeofday(&timer_start, NULL); event_base_once(base, -1, EV_TIMEOUT, timer_callback, NULL, &tv_timer); event_base_once(base, -1, EV_TIMEOUT, start_threads_callback, NULL, NULL); event_base_dispatch(base); evutil_timersub(&timer_end, &timer_start, &elapsed); TT_BLATHER(("callback count, %u", callback_count)); elapsed_usec = (unsigned)(elapsed.tv_sec*1000000 + elapsed.tv_usec); TT_BLATHER(("elapsed time, %u usec", elapsed_usec)); /* XXX be more intelligent here. just make sure skew is * within .4 seconds for now. */ tt_assert(elapsed_usec >= 600000 && elapsed_usec <= 1400000); end: for (i = 0; i < QUEUE_THREAD_COUNT; ++i) THREAD_JOIN(load_threads[i]); if (base) event_base_free(base); if (cfg) event_config_free(cfg); }
static void mosq_reconnect_handler(evutil_socket_t fd, short what, void *arg) { (void)fd; (void)what; struct _squash *st = arg; if (mosq_setup(st)) { /* this will handle restarting... */ /* now schedule a reconnection for 5 seconds away. */ struct timeval later = {5, 0}; event_base_once(st->base, -1, EV_TIMEOUT, mosq_reconnect_handler, st, &later); } }
HANDLE napaSchedulePeriodic(const struct timeval *start, double frequency, void(*cb)(HANDLE handle, void *arg), void *cbarg) { struct timeval t = { 0,0 }; if (start) t = *start; struct periodic *h = malloc(sizeof(struct periodic)); if (!h) return NULL; h->cb = cb; h->cbarg = cbarg; h->frequency = frequency; event_base_once(eventbase, -1, EV_TIMEOUT, schedule_periodic_callback, h, &t); return h; }
void schedule_periodic_callback(evutil_socket_t fd, short what, void *arg) { struct periodic *p = arg; /* This indicates STOP */ if (p == NULL || p->frequency == 0.0) { if (arg) free(arg); return; } double next = 1.0 / p->frequency; struct timeval t = { floor(next), fmod(next,1.0) * 1000000.0 }; event_base_once(eventbase, -1, EV_TIMEOUT, schedule_periodic_callback, arg, &t); if (p->cb) p->cb(arg, p->cbarg); }
void ClientImpl::reconnectEventCallback() { if (m_pendingConnectionSize.load(boost::memory_order_consume) <= 0) return; boost::mutex::scoped_lock lock(m_pendingConnectionLock); const int64_t now = get_sec_time(); BOOST_FOREACH( PendingConnectionSPtr& pc, m_pendingConnectionList ) { if ((now - pc->m_startPending) > RECONNECT_INTERVAL){ //pc->m_startPending = now; initiateConnection(pc); } } struct timeval tv; tv.tv_sec = RECONNECT_INTERVAL; tv.tv_usec = 0; event_base_once(m_base, -1, EV_TIMEOUT, reconnectCallback, this, &tv); }
void ClientImpl::createPendingConnection(const std::string &hostname, const unsigned short port, int64_t time){ logMessage(ClientLogger::DEBUG, "ClientImpl::createPendingConnection"); PendingConnectionSPtr pc(new PendingConnection(hostname, port, m_base, this)); pc->m_startPending = time; { boost::mutex::scoped_lock lock(m_pendingConnectionLock); m_pendingConnectionList.push_back(pc); m_pendingConnectionSize.store(m_pendingConnectionList.size(), boost::memory_order_release); } struct timeval tv; tv.tv_sec = (time > 0)? RECONNECT_INTERVAL: 0; tv.tv_usec = 0; event_base_once(m_base, -1, EV_TIMEOUT, reconnectCallback, this, &tv); }
/** * Called by the application to send data to a remote peer * @param from * @param to * @param buffer_ptr * @param buffer_size * @return The dimension of the buffer or -1 if a connection error occurred. */ int send_to_peer(const struct nodeID *from, struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size) { msgData_cb *p; int current; send_params params = {0,0,0,0}; if (buffer_size <= 0) { fprintf(stderr,"Net-helper: message size problematic: %d\n", buffer_size); return buffer_size; } // if buffer is full, discard the message and return an error flag int index = next_S(); if (index<0) { // free(buffer_ptr); fprintf(stderr,"Net-helper: send buffer full\n "); return -1; } sendingBuffer[index] = malloc(buffer_size); if (! sendingBuffer[index]){ fprintf(stderr,"Net-helper: memory full, can't send!\n "); return -1; } memset(sendingBuffer[index],0,buffer_size); memcpy(sendingBuffer[index],buffer_ptr,buffer_size); // free(buffer_ptr); p = malloc(sizeof(msgData_cb)); p->bIdx = index; p->mSize = buffer_size; p->msgType = (unsigned char)buffer_ptr[0]; p->conn_cb_called = false; p->cancelled = false; current = p->bIdx; to->connID = mlOpenConnection(to->addr,&connReady_cb,p, params); if (to->connID<0) { free_sending_buffer(current); fprintf(stderr,"Net-helper: Couldn't get a connection ID to send msg %d.\n ", p->bIdx); free(p); return -1; } else { struct timeval timeout = NH_PACKET_TIMEOUT; event_base_once(base, -1, EV_TIMEOUT, send_to_peer_cb, (void *) p, &timeout); return buffer_size; //p->mSize; } }
/** Initializer for this "SOM" */ void som_init(cfg_t *som_config) { if (!som_config) return; const char *protocol = cfg_getstr(som_config, "protocol"); channel = cfg_getstr(som_config, "channel"); info("Scheduler is initializing protocol [ \"%s\" ] on channel %s", protocol,channel); chunkbuffer = chbInit("size=256,time=now"); if (!chunkbuffer) fatal("Error initialising the Chunk Buffer"); chbRegisterNotifier(chunkbuffer, chunkbuffer_notifier, NULL); int publishPeerIDPeriod = cfg_getint(som_config, "publishPeerIDPeriod"); if (publishPeerIDPeriod) napaSchedulePeriodic(NULL, 1.0/(float)publishPeerIDPeriod, publish_PeerID, NULL); /* If we are passive, we need to figure out who is the server, and send * a message to it for ML to be able to work... Sigh... */ if (!strcmp(protocol, "passive")) { struct timeval t = { 0, 0 }; event_base_once(eventbase, -1, EV_TIMEOUT, &findServer, NULL, &t); } /* If the string "neighborlist" is present in the protocol name, we * launch a neighborlist instance */ if (strstr(protocol, "neighborlist")) { cfg_t *nlist_cfg = cfg_getsec(som_config, "neighborlist"); neighbors_size = cfg_getint(nlist_cfg, "size"); neighbors = calloc(sizeof(NeighborListEntry), neighbors_size); neighborlist = neighborlist_init(repository, neighbors_size, cfg_getint(nlist_cfg, "refreshPeriod"), channel, neighborlist_cb, NULL); } }
/** This function gets called when the "source locator" finishes. * Each "client" tries to find the PeerID of the source (a Peer with the * SrcLag measurement 0.0) and open a Connection towards it. * This function is needed only because a ML deficiency (sometimes a * Connection can be opened from A to B only after B has initiated to * connection to A). */ void findServer_cb(HANDLE rep, HANDLE id, void *cbarg, char **result, int nResults) { if (nResults != 1) { /* We couldn't find the server properly, try again in 30 secs. */ struct timeval t = { 30, 0 }; event_base_once(eventbase, -1, EV_TIMEOUT, &findServer, NULL, &t); return; } info("The server is at %s", result[0]); static char server[64]; strcpy(server,result[0]); socketID_handle remsocketID = malloc(SOCKETID_SIZE); mlStringToSocketID(server, remsocketID); mlOpenConnection(remsocketID, connection_cb, server, sendParams); free(remsocketID); free(result[0]); free(result); }
int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) { return event_base_once (ev_x_cur, fd, events, cb, arg, tv); }
tr_watchdir_backend * tr_watchdir_win32_new (tr_watchdir_t handle) { const char * const path = tr_watchdir_get_path (handle); wchar_t * wide_path; tr_watchdir_win32 * backend; backend = tr_new0 (tr_watchdir_win32, 1); backend->base.free_func = &tr_watchdir_win32_free; backend->fd = INVALID_HANDLE_VALUE; backend->notify_pipe[0] = backend->notify_pipe[1] = TR_BAD_SOCKET; if ((wide_path = tr_win32_utf8_to_native (path, -1)) == NULL) { log_error ("Failed to convert \"%s\" to native path", path); goto fail; } if ((backend->fd = CreateFileW (wide_path, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL)) == INVALID_HANDLE_VALUE) { log_error ("Failed to open directory \"%s\"", path); goto fail; } tr_free (wide_path); wide_path = NULL; backend->overlapped.Pointer = handle; if (!ReadDirectoryChangesW (backend->fd, backend->buffer, sizeof (backend->buffer), FALSE, WIN32_WATCH_MASK, NULL, &backend->overlapped, NULL)) { log_error ("Failed to read directory changes"); goto fail; } if (evutil_socketpair (AF_INET, SOCK_STREAM, 0, backend->notify_pipe) == -1) { log_error ("Failed to create notify pipe: %s", tr_strerror (errno)); goto fail; } if ((backend->event = bufferevent_socket_new (tr_watchdir_get_event_base (handle), backend->notify_pipe[0], 0)) == NULL) { log_error ("Failed to create event buffer: %s", tr_strerror (errno)); goto fail; } bufferevent_setwatermark (backend->event, EV_READ, sizeof (FILE_NOTIFY_INFORMATION), 0); bufferevent_setcb (backend->event, &tr_watchdir_win32_on_event, NULL, NULL, handle); bufferevent_enable (backend->event, EV_READ); if ((backend->thread = (HANDLE) _beginthreadex (NULL, 0, &tr_watchdir_win32_thread, handle, 0, NULL)) == NULL) { log_error ("Failed to create thread"); goto fail; } /* Perform an initial scan on the directory */ if (event_base_once (tr_watchdir_get_event_base (handle), -1, EV_TIMEOUT, &tr_watchdir_win32_on_first_scan, handle, NULL) == -1) log_error ("Failed to perform initial scan: %s", tr_strerror (errno)); return BACKEND_DOWNCAST (backend); fail: tr_watchdir_win32_free (BACKEND_DOWNCAST (backend)); tr_free (wide_path); return NULL; }
void ClientImpl::interrupt() { event_base_once(m_base, -1, EV_TIMEOUT, interrupt_callback, this, NULL); }
int main (int argc, char** argv) { int fd; struct sockaddr_un saddr; struct event_base* event; struct timeval timeout; struct fake_message fm; struct silent_data sd; struct event ev; /* This holds the filename of the socket to watch */ char* filename = NULL; /* This does two things. First, it temporarily holds the timeout to determine a terminal is silent. */ /* Second, if it is anything by NULL we use silence mode, rather than activity mode */ char* silence = NULL; int i; timeout.tv_sec = 0; timeout.tv_usec = 0; if (argc == 1) { fputs("Must give path to socket as argument\n", stderr); return(EXIT_FAILURE); } for (i=1; i < argc; i++) { if (strncmp(argv[i], "-s", 2) == 0) { if (strlen(argv[i]) == 2) { /* This flag is only -s, next argument will be the timeout */ silence = argv[++i]; } else { /* This flag contains the timeout jammed against it (-s16) */ silence = argv[i] + 2; } } else if (strncmp(argv[i], "-h", 2) == 0) { /* Print usage */ fputs("dtach-watch: This command watches a dtach socket for activity or inactivity.\n", stderr); fputs(" The default mode watches the given socket and returns with an exit\n", stderr); fputs(" code of 0 when activity is detected on the socket. If the -s option\n", stderr); fputs(" is given with a time, then instead we use silence mode. In this\n", stderr); fputs(" mode the the socket is watched and dtach-watch will exit with an\n", stderr); fputs(" exit code of 0 when there are the requested number of seconds\n", stderr); fputs(" without activity.\n", stderr); fputs("Usage: dtach-watch [-s TIME] /path/to/socket\n", stderr); fputs(" /path/to/socket This is the path to the dtach socket to watch\n", stderr); fputs(" -s TIME Use 'silence' mode with a timeout of TIME seconds\n", stderr); return(EXIT_FAILURE); } else { /* Filename */ filename = argv[i]; } } if (filename == NULL) { fputs("A path to the dtach socket must be given.\n", stderr); return(EXIT_FAILURE); } if (silence != NULL) { char *endptr; timeout.tv_sec = strtol(silence, &endptr, 10); if (silence == endptr) { fprintf(stderr, "Time '%s' not a valid integer\n", silence); exit(EXIT_FAILURE); } else if (timeout.tv_sec <= 0) { fputs("Time must be greater than 0.\n", stderr); exit(EXIT_FAILURE); } } fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd < 0) { fputs("Call to socket failed.\n", stderr); return(EXIT_FAILURE); } saddr.sun_family = AF_UNIX; strcpy(saddr.sun_path, filename); if (connect(fd, (struct sockaddr*)&saddr, sizeof(struct sockaddr_un)) < 0) { close(fd); fputs("Connect failed.\n", stderr); return(EXIT_FAILURE); } event = event_base_new(); fm.type = 1; fm.len= 0; /* This should write an empty packet asking to attach */ write(fd, (char*)&fm, sizeof(struct fake_message)); if (silence) { sd.eb = event; sd.ev = &ev; sd.tv = &timeout; event_set(&ev, fd, EV_READ, &silent, (void*)&sd); event_base_set(event, &ev); event_add(&ev, &timeout); } else { event_base_once(event, fd, EV_READ, &activity, (void*)event, NULL); } event_base_dispatch(event); /* Inform that we're detaching */ fm.type = 2; write(fd, (char*)&fm, sizeof(struct fake_message)); event_base_free(event); return EXIT_SUCCESS; }