static bool dbmerge_download (char *url, int fd, void(*progress_callback)(int, int), void(*event_callback)(int, char*), volatile bool *stop) { char host[256]; char port[6]; char page[256]; char *tmp = url; int pos = 0; if (event_callback) event_callback(8, url); // download message if (event_callback) event_callback(0, NULL); // turn on progress bar memset (host, 0, sizeof (host)); memset (port, 0, sizeof (port)); memset (page, 0, sizeof (page)); if (strlen (tmp) < 7) return false; if (memcmp (tmp, "http://", 7) != 0) return false; tmp += 7; while ((tmp[0+pos] != '\0') && (tmp[0+pos] != ':') && (tmp[0+pos] != '/')) pos++; if (pos > (sizeof (host) - 1)) return false; memcpy (host, tmp, pos); tmp += pos; if (tmp[0] == ':') { tmp++; pos = 0; while ((tmp[0+pos] != '\0') && (tmp[0+pos] != '/')) pos++; if (pos > (sizeof (port) - 1)) return false; memcpy (port, tmp, pos); tmp += pos; } else sprintf (port, "80"); tmp++; pos = 0; while ((tmp[0+pos] != '\0') && (tmp[0+pos] != '\n')) pos++; if (pos > (sizeof (page) - 1)) return false; memcpy (page, tmp, pos); char tmp_url[256]; sprintf (tmp_url, "http://%s/%s", host, page); if (!http_get (host, page, atoi (port), fd, progress_callback, stop)) { if (event_callback) event_callback(1, NULL); // turn off progress bar log_add ("Error downloading file %s", tmp_url); return false; } if (event_callback) event_callback(1, NULL); // turn off progress bar if (*stop) return false; return true; }
int connection_t::sync_close_i(const struct conn_id_t& conn_id_, bool is_del_from_hb_, conn_event_e close_type_) { LOGTRACE((CONNECTION_MODULE, "connection_t::sync_close_i args-[fd:%d] begin", conn_id_.socket)); work_service_t* service_ptr = conn_id_.service_ptr; if (NULL == service_ptr) { LOGWARN((CONNECTION_MODULE, "connection_t::sync_close_i error, return. args-[fd:%d]", conn_id_.socket)); return -1; } conn_sptr_t conn_sptr = service_ptr->get_conn(conn_id_); if (NULL == conn_sptr) { LOGWARN((CONNECTION_MODULE, "connection_t::sync_close_i connection not found, it maybe closed, return. args-[fd:%d]", conn_id_.socket)); return -1; } if (ST_CLOSED == conn_sptr->get_status()) { LOGTRACE((CONNECTION_MODULE, "connection_t::sync_close_i connection has closed, return. args-[fd:%d]", conn_id_.socket)); return 0; } //! yunjie: 关闭connection的socket fd, 从epoll中移除 conn_sptr->close_i(); if (is_del_from_hb_ && conn_sptr->is_enable_hb()) { //! yunjie: 从heart_beart中删除connection service_ptr->async_del_hb_element(conn_sptr->m_conn_id); } //! yunjie: connection的一些数据要先拷贝出来, //! 因为之后的async_del_connection会 //! delete connection对象 conn_status_e conn_status = conn_sptr->get_status(); on_conn_event_t event_callback = conn_sptr->m_conn_event_callback; void* user_data = conn_sptr->m_user_data; //! yunjie: 释放操作有可能是在本线程直接执行, 所以必须放最后 if (EV_DECONSTRUCT != close_type_) { //! yunjie: 从管理中移除connection并释放(delete) //! 注: 其实这里的async_del_connection是同步执行的, //! 因为sync_close_i肯定是在work线程上工作的, 所以 //! 不需要在投递消息到队列异步执行 service_ptr->async_del_connection(conn_id_); } //! yunjie: 在delete之后callback被调用, 否则可能会有许多运行时警告 if (NULL != event_callback) { //! yunjie: 调用用户注册的回调 event_callback(close_type_, conn_status, conn_id_, user_data); } LOGTRACE((CONNECTION_MODULE, "connection_t::sync_close_i args-[fd:%d] end", conn_id_.socket)); return 0; }
/* * create_automaton * * Allocates the memory required for a automaton. It then fills in the state * and event arrays but does not fill in the transition table or use the csv * file at all as this is done by a separate function. * * We pass in callbacks rather than arrays so that all memory allocation * regarding this object is done internally to the create function, thus all * freeing of memory can be done in the destroy function. * * Parameters: state_callback - A callback function pointer to a function which * will create all the state objects and fill them * in. * event_callback - A callback function pointer to a function which * will create all the event objects and fill them * in. * * Returns: A pointer to the newly created memory or NULL on failure. */ AUTOMATON *create_automaton(int (*state_callback)(AUTOMATON_STATE ***, int), int (*event_callback)(AUTOMATON_EVENT ***)) { /* * Local Variables */ AUTOMATON *automaton; /* * Allocate the required memory. Forcing everything to 0 means that we can * test against NULL when destroying the object even if it is only half * created. */ automaton = (AUTOMATON *) DT_MALLOC(sizeof(AUTOMATON)); memset(automaton, '\0', sizeof(AUTOMATON)); /* * Make the callback to get the array of events used in this automaton. */ automaton->num_events = event_callback(&(automaton->events)); /* * Make the callback to get the array of states used in this automaton. */ automaton->num_states = state_callback(&(automaton->states), automaton->num_events); return(automaton); }
static void event_loop(void *arg) { eventmanager *eventmgr = (eventmanager*)arg; ASSERT(eventmgr); int nsd; while(!eventmgr->stop) { if(sched_has_task()) { nsd = epoll_wait(eventmgr->epfd, eventmgr->events, eventmgr->nevent, 0); if(nsd < 0) { log_error("epoll_wait occur error, errno:%d", errno); break; } int i; for(i=0;i<nsd;i++) { struct epoll_event *epev = &eventmgr->events[i]; event_callback(epev->data.ptr, epev->events); } sched_run_once(); } nsd = epoll_wait(eventmgr->epfd, eventmgr->events, eventmgr->nevent, eventmgr->max_timeout); if(nsd < 0) { log_error("epoll_wait occur error, errno:%d", errno); break; } } log_warn("event loop exit."); }
static void event_callback_wrapper (const unsigned long signature, int event, const char *data_str, const unsigned long data_num) { struct em_event e; e.signature = signature; e.event = event; e.data_str = data_str; e.data_num = data_num; if (!rb_ivar_defined(EmModule, Intern_at_error_handler)) event_callback(&e); else rb_rescue((VALUE (*)(ANYARGS))event_callback, (VALUE)&e, (VALUE (*)(ANYARGS))event_error_handler, Qnil); }
static void event_callback_wrapper (const unsigned long a1, int a2, const char *a3, const unsigned long a4) { struct em_event e; e.a1 = a1; e.a2 = a2; e.a3 = a3; e.a4 = a4; if (!rb_ivar_defined(EmModule, Intern_at_error_handler)) event_callback(&e); else rb_rescue((VALUE (*)(ANYARGS))event_callback, (VALUE)&e, (VALUE (*)(ANYARGS))event_error_handler, Qnil); }
void test_horizontal_line_intersection(Vector2<double> old_position, Vector2<double> new_position, const ControlPoint& point, EventCallback&& event_callback) { auto y = static_cast<double>(point.start.y); if (old_position.y < y != new_position.y < y) { auto time_point = (y - old_position.y) / (new_position.y - old_position.y); auto intersect_x = static_cast<std::int32_t>(old_position.x + time_point * (new_position.x - old_position.x)); auto x = std::minmax(point.start.x, point.end.x); if (intersect_x >= x.first && intersect_x <= x.second) { event_callback(point, time_point); } } }
void test_vertical_line_intersection(Vector2<double> old_position, Vector2<double> new_position, const ControlPoint& point, EventCallback&& event_callback) { auto x = static_cast<double>(point.start.x); if (old_position.x < x != new_position.x < x) { auto time_point = (x - old_position.x) / (new_position.x - old_position.x); auto intersect_y = static_cast<std::int32_t>(old_position.y + time_point * (new_position.y - old_position.y)); auto y = std::minmax(point.start.y, point.end.y); if (intersect_y >= y.first && intersect_y <= y.second) { event_callback(point, time_point); } } }
void test_arbitrary_intersection(Vector2<double> old_position, Vector2<double> new_position, const ControlPoint& point, EventCallback&& event_callback) { auto intersection = find_line_segment_intersection(old_position, new_position, vector2_cast<double>(point.start), vector2_cast<double>(point.end)); if (intersection) { auto x_diff = new_position.x - old_position.x; auto y_diff = new_position.y - old_position.y; double time_point = std::abs(x_diff) < std::abs(y_diff) ? (intersection.point.y - old_position.y) / y_diff : (intersection.point.x - old_position.x) / x_diff; event_callback(point, time_point); } }
static void ip_event_dispatch (GList *dir_list, GList *pair_dir_list, ik_event_t *event) { GList *dirl; if (!event) return; /* TODO: * * Figure out how we will deliver move events */ for (dirl = dir_list; dirl; dirl = dirl->next) { GList *subl; ip_watched_dir_t *dir = dirl->data; for (subl = dir->subs; subl; subl = subl->next) { ih_sub_t *sub = subl->data; /* If the event and the subscription have a filename * they need to match before the event could be delivered. */ if (event->name && sub->filename) { if (strcmp (event->name, sub->filename)) continue; /* If the event doesn't have a filename, but the subscription does * we shouldn't deliever the event */ } else if (sub->filename) continue; event_callback (event, sub); } } if (!event->pair) return; for (dirl = pair_dir_list; dirl; dirl = dirl->next) { GList *subl; ip_watched_dir_t *dir = dirl->data; for (subl = dir->subs; subl; subl = subl->next) { ih_sub_t *sub = subl->data; /* If the event and the subscription have a filename * they need to match before the event could be delivered. */ if (event->pair->name && sub->filename) { if (strcmp (event->pair->name, sub->filename)) continue; /* If the event doesn't have a filename, but the subscription does * we shouldn't deliever the event */ } else if (sub->filename) continue; event_callback (event->pair, sub); } } }
static void xinput_process_event(XIRawEvent* revent) { GE_Event evt = {}; int i; //ignore events from master device if (revent->deviceid != revent->sourceid || revent->sourceid >= GE_MAX_DEVICES) { return; } int index = device_index[revent->sourceid]; switch (revent->evtype) { case XI_RawMotion: if(index >= 0) { evt.type = GE_MOUSEMOTION; evt.motion.which = devices[index].id; i = 0; evt.motion.xrel = XIMaskIsSet(revent->valuators.mask, 0) ? revent->raw_values[i++] : 0; evt.motion.yrel = XIMaskIsSet(revent->valuators.mask, 1) ? revent->raw_values[i++] : 0; } break; case XI_RawButtonPress: if(index >= 0) { evt.type = GE_MOUSEBUTTONDOWN; evt.button.which = devices[index].id; evt.button.button = get_button(revent->detail); } break; case XI_RawButtonRelease: if(index >= 0) { evt.type = GE_MOUSEBUTTONUP; evt.button.which = devices[index].id; evt.button.button = get_button(revent->detail); } break; case XI_RawKeyPress: { if(index >= 0) { evt.type = GE_KEYDOWN; evt.button.which = devices[index].id; evt.button.button = revent->detail - 8; } break; } case XI_RawKeyRelease: { if(index >= 0) { evt.type = GE_KEYUP; evt.button.which = devices[index].id; evt.button.button = revent->detail - 8; } break; } default: break; } /* * Process evt. */ if(evt.type != GE_NOEVENT) { event_callback(&evt); } }
INTERNAL void setprobe(char *spec) { char server[100]; char instname[100]; char *s, *t; int param; int id, nc; int clientid = 0; ECLIENT_LIST clients[MAX_CLIENT]; EVENT_EFI_CTL eprobe; s = spec; if (substr(PREFIX, s)) s += strlen(PREFIX); if (substr("//", s)) s += 2; t = server; while (*s && *s != '/') *t++ = *s++; *t = '\0'; if (*s++ != '/') probe_error(spec); if (*s == '/') ++s; t = instname; while (*s != '.') *t++ = *s++; *t = '\0'; if (*s++ != '.') probe_error(spec); if (isdigit(instname[0])) clientid = atoi(instname); param = atoi(s); if (!param) probe_error(spec); printf("setprobe: setting probe daffie://%s/%s.%d\n", server, instname, param); efi_init(); event_select_type(0, ET_MIN, ET_MAX); id = event_join(server, &nc); if (!id) { fprintf(stderr, "%s: couldn't join server \"%s\"\n", Pgm, server); exit(4); } printf("%s: joined %s as client id %d (%d clients)\n", Pgm, server, id, nc); event_register("earlab", "control", Pgm); event_callback(ET_EFI_WAVEDATA, receive_wavedata); event_callback(ET_EFI_SPIKEDATA, receive_spikedata); event_select_type(1, ET_EFI_WAVEDATA, ET_EFI_WAVEDATA); event_select_type(1, ET_EFI_SPIKEDATA, ET_EFI_SPIKEDATA); event_receive_enable(0); event_flush(0); eclient_monitor(); if (!clientid) nc = eclient_match(clients, "*", "earlab", "*", instname); else nc = eclient_match_id(clients, clientid); if (nc < 1) { fprintf(stderr, "%s: couldn't find match for %s\n", Pgm, instname); event_leave(); exit(1); } if (nc > 1) { fprintf(stderr, "%s: %s matched multiple (%d) clients\n", Pgm, instname, nc); event_leave(); exit(1); } eprobe.type = EFI_CTL_PROBE; eprobe.clientid = id; eprobe.tag = getpid(); eprobe.param = param - 1; eprobe.flag = 1; eprobe.value = 0; SEND(clients[0].id, EFI_CTL, eprobe); }
bool dbmerge_downloader (char *hashes_url, char *descriptors_url, char *dbroot, void(*progress_callback)(int, int), void(*event_callback)(int, char*), volatile bool *stop) { char sfn[256], sfn2[256]; int hashes_fd = -1, descriptors_fd = -1; FILE *fd_h, *fd_d; bool ret = false; sprintf (sfn, "%s/crossepg.tmp.XXXXXX", dbroot); sprintf (sfn2, "%s/crossepg.tmp.XXXXXX", dbroot); if ((hashes_fd = mkstemp (sfn)) == -1) { log_add ("Cannot get temp file (%s)", sfn); return false; } if ((descriptors_fd = mkstemp (sfn2)) == -1) { unlink (sfn); log_add ("Cannot get temp file (%s)", sfn2); return false; } if (!dbmerge_download(hashes_url, hashes_fd, progress_callback, event_callback, stop)) goto error; if (!dbmerge_download(descriptors_url, descriptors_fd, progress_callback, event_callback, stop)) goto error; if (dbmerge_extension_check (hashes_url, "gz")) { int fdtmp = -1; char sfntmp[256]; sprintf (sfntmp, "%s/crossepg.tmp.XXXXXX", dbroot); if ((fdtmp = mkstemp (sfntmp)) == -1) { log_add ("Cannot get temp file"); goto error; } else { if (event_callback) event_callback(10, hashes_url); // deflating message log_add ("Deflating %s", sfn); FILE *dest = fdopen (fdtmp, "w"); if (!gzip_inf (sfn, dest)) log_add ("Error deflating file"); else log_add ("File deflated"); fclose (dest); close (fdtmp); unlink (sfn); strcpy(sfn, sfntmp); } } if (dbmerge_extension_check (descriptors_url, "gz")) { int fdtmp = -1; char sfntmp[256]; sprintf (sfntmp, "%s/crossepg.tmp.XXXXXX", dbroot); if ((fdtmp = mkstemp (sfntmp)) == -1) { log_add ("Cannot get temp file"); goto error; } else { if (event_callback) event_callback(10, descriptors_url); // deflating message log_add ("Deflating %s", sfn2); FILE *dest = fdopen (fdtmp, "w"); if (!gzip_inf (sfn2, dest)) log_add ("Error deflating file"); else log_add ("File deflated"); fclose (dest); close (fdtmp); unlink (sfn2); strcpy(sfn2, sfntmp); } } fd_h = fopen (sfn, "r"); if (fd_h == NULL) { log_add ("Cannot open %s", sfn); goto error; } fd_d = fopen (sfn2, "r"); if (fd_d == NULL) { log_add ("Cannot open %s", sfn2); fclose (fd_h); goto error; } if (event_callback) event_callback(9, hashes_url); // parsing xepgdb if (event_callback) event_callback(0, NULL); // turn on progress bar ret = dbmerge_merge(fd_h, fd_d, progress_callback); if (event_callback) event_callback(1, NULL); // turn off progress bar fclose(fd_h); fclose(fd_d); error: unlink (sfn); unlink (sfn2); return ret; }
static void js_process_event(int device, struct js_event* je) { GE_Event evt = {}; if(je->type & JS_EVENT_INIT) { return; } if(je->type & JS_EVENT_BUTTON) { evt.type = je->value ? GE_JOYBUTTONDOWN : GE_JOYBUTTONUP; evt.jbutton.which = joystick_id[device]; evt.jbutton.button = je->number; } else if(je->type & JS_EVENT_AXIS) { int axis = joystick_ax_map[device][je->number]; if(axis >= ABS_HAT0X && axis <= ABS_HAT3Y) { evt.type = je->value ? GE_JOYBUTTONDOWN : GE_JOYBUTTONUP; int button; int value; axis -= ABS_HAT0X; if(!je->value) { value = joystick_hat_value[device][axis]; joystick_hat_value[device][axis] = 0; } else { value = je->value/32767; joystick_hat_value[device][axis] = value; } button = axis + value + 2*(axis/2); if(button < 4*(axis/2)) { button += 4; } evt.jbutton.which = joystick_id[device]; evt.jbutton.button = button + joystick_button_nb[device]; } else { evt.type = GE_JOYAXISMOTION; evt.jaxis.which = joystick_id[device]; evt.jaxis.axis = je->number; evt.jaxis.value = je->value; /* * Ugly patch for the sixaxis. */ if(GE_IsSixaxis(evt.jaxis.which) && evt.jaxis.axis > 3 && evt.jaxis.axis < 23) { evt.jaxis.value = (evt.jaxis.value + 32767) / 2; } } } /* * Process evt. */ if(evt.type != GE_NOEVENT) { eprintf("event from joystick: %s\n", joystick_name[device]); eprintf("type: %d number: %d value: %d\n", je->type, je->number, je->value); event_callback(&evt); } }