void net_ep_cb(EV_P_ ev_io *w, int revents) { net_ep_t ep; int old_events; ep = (net_ep_t)w->data; assert(ep); old_events = net_ep_calc_ev_events(ep); if (revents & EV_READ) { int recv_size = ep->m_chanel_r->m_type->read_from_net(ep->m_chanel_r, ep->m_fd); if (recv_size < 0) { CPE_ERROR( ep->m_mgr->m_em, "net_mgr: ep %d: read data error, errno=%d (%s)", ep->m_id, cpe_sock_errno(), cpe_sock_errstr(cpe_sock_errno())); net_ep_close_i(ep, net_ep_event_close_by_error); return; } else if (recv_size == 0) { if (ep->m_mgr->m_debug) { CPE_INFO(ep->m_mgr->m_em, "net_mgr: ep %d: socket close by peer!", ep->m_id); } net_ep_close_i(ep, net_ep_event_close_by_peer); return; } else { if (ep->m_mgr->m_debug) { CPE_INFO(ep->m_mgr->m_em, "net_mgr: ep %d: receive %d types data!", ep->m_id, (int)recv_size); } if (ep->m_process_fun) { ep->m_process_fun(ep, ep->m_process_ctx, net_ep_event_read); } } } if (revents & EV_WRITE) { ssize_t send_size = ep->m_chanel_w->m_type->write_to_net(ep->m_chanel_w, ep->m_fd); if (send_size < 0) { CPE_ERROR( ep->m_mgr->m_em, "net_mgr: ep %d: write data error, errno=%d (%s)", ep->m_id, cpe_sock_errno(), cpe_sock_errstr(cpe_sock_errno())); net_ep_close_i(ep, net_ep_event_close_by_error); return; } else { if (ep->m_mgr->m_debug) { CPE_INFO(ep->m_mgr->m_em, "net_mgr: ep %d: send %d bytes data!", ep->m_id, (int)send_size); } } } net_ep_update_events(ep, old_events); }
dr_cvt_result_t dr_cvt_fun_copy_decode( LPDRMETA meta, void * output, size_t * output_capacity, const void * input, size_t * input_capacity, void * ctx, error_monitor_t em, int debug) { int32_t size; if (*input_capacity < sizeof(size)) { if (debug) { CPE_INFO( em, "decode %s: copy: not enought input data, require at least %d, but only %d!", dr_meta_name(meta), (int)sizeof(size), (int)*input_capacity); } *output_capacity = 0; *input_capacity = 0; return dr_cvt_result_not_enough_input; } memcpy(&size, input, sizeof(size)); if (*input_capacity < sizeof(size) + size) { if (debug) { CPE_INFO( em, "decode %s: copy: not enought input data, require %d(size=%d), but only %d!", dr_meta_name(meta), (int)sizeof(size) + size, size, (int)*input_capacity); } *output_capacity = 0; *input_capacity = 0; return dr_cvt_result_not_enough_input; } if (*output_capacity < (size_t)size) { if (debug) { CPE_INFO( em, "decode %s: copy: not enought output buf, require %d, but only %d!", dr_meta_name(meta), (int)size, (int)*output_capacity); } return dr_cvt_result_not_enough_output; } memcpy(output, ((const char *)input) + sizeof(size), size); *output_capacity = size; *input_capacity = size + sizeof(size); if (debug) { CPE_INFO( em, "decode %s: copy: copy %d data to output, input-size=%d", dr_meta_name(meta), (int)size, (int)*input_capacity); } return dr_cvt_result_success; }
dr_cvt_result_t dr_cvt_fun_copy_encode( LPDRMETA meta, void * output, size_t * output_capacity, const void * input, size_t * input_capacity, void * ctx, error_monitor_t em, int debug) { int32_t size; size_t require_size = sizeof(size) + *input_capacity; if (*output_capacity < require_size) { CPE_ERROR( em, "encode %s: copy: not enought output buf, require %d(input=%d), but only %d!", dr_meta_name(meta), (int)require_size, (int)*input_capacity, (int)*output_capacity); return dr_cvt_result_not_enough_output; } size = *input_capacity; memcpy(output, &size, sizeof(size)); memcpy(((char*)output) + sizeof(size), input, *input_capacity); *output_capacity = require_size; if (debug) { CPE_INFO( em, "encode %s: copy: copy %d data to output, input-size=%d", dr_meta_name(meta), (int)require_size, (int)*input_capacity); } return dr_cvt_result_success; }
void inet_sch_process_multi_info(inet_sch_manage_t mgr) { CURLMsg *msg; while ((msg = curl_multi_info_read(mgr->m_multi, NULL))) { struct inet_sch_task * task; CURL * easy; CURLcode res; char * eff_url; easy = msg->easy_handle; res = msg->data.result; curl_easy_getinfo(easy, CURLINFO_PRIVATE, &task); curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url); switch(msg->msg) { case CURLMSG_DONE: { if (mgr->m_debug) { CPE_INFO(mgr->m_em, "%s: DONE: %s => (%d) %s", inet_sch_manage_name(mgr), eff_url, res, ""/*connection->error*/); } inet_sch_task_complete(task); break; } default: CPE_ERROR(mgr->m_em, "%s: UNKNOWN: %s => (%d) %s", inet_sch_manage_name(mgr), eff_url, res, ""/*connection->error*/); } } }
dr_cvt_result_t dr_cvt_fun_bson_encode( LPDRMETA meta, void * output, size_t * output_capacity, const void * input, size_t * input_capacity, void * ctx, error_monitor_t em, int debug) { int r; r = dr_bson_write(output, *output_capacity, input, *input_capacity, meta, em); if (r < 0) { CPE_ERROR( em, "encode %s: bson: fail, input buf "FMT_SIZE_T", output buf "FMT_SIZE_T, dr_meta_name(meta), *input_capacity, *output_capacity); return dr_cvt_result_error; } *output_capacity = r; if (debug) { CPE_INFO( em, "encode %s: bson: ok, %d data to output, input-size="FMT_SIZE_T, dr_meta_name(meta), r, *input_capacity); } return dr_cvt_result_success; }
int inet_sch_manage_sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void * sockpp) { static const char * whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE"}; inet_sch_manage_t mgr; inet_sch_task_t task; mgr = (inet_sch_manage_t)cbp; assert(mgr); if (mgr->m_debug >= 2) { CPE_INFO( mgr->m_em, "%s: socket callback: e %p s %i what %s(%d)", inet_sch_manage_name(mgr), e, s, (what >= 0 && what < sizeof(whatstr) / sizeof(whatstr[0])) ? whatstr[what] : "???", what); } curl_easy_getinfo(e, CURLINFO_PRIVATE, &task); assert(task); if (what == CURL_POLL_REMOVE) { inet_sch_task_complete(task); } else { inet_sch_task_set_socket(task, s, what); } return 0; }
static void inet_sch_task_sock_event_cb(EV_P_ struct ev_io *w, int revents) { inet_sch_manage_t mgr; inet_sch_task_t task; CURLMcode rc; int action; task = (inet_sch_task_t)w->data; assert(task); mgr = task->m_mgr; action = (revents & EV_READ?CURL_POLL_IN : 0) | ( revents & EV_WRITE?CURL_POLL_OUT : 0); rc = curl_multi_socket_action(task->m_mgr->m_multi, w->fd, action, &mgr->m_still_running); if (rc != CURLM_OK) { CPE_ERROR( mgr->m_em, "%s: sock_event_cb: curl_multi_socket_handle fail, %s(%d)!", inet_sch_manage_name(task->m_mgr), inet_sch_curl_code_msg(rc), rc); return; } inet_sch_process_multi_info(mgr); if (mgr->m_still_running <= 0) { if (mgr->m_debug >= 2) { CPE_INFO( mgr->m_em, "%s: sock_event_cb: no task runing, stop timer!", inet_sch_manage_name(task->m_mgr)); } ev_timer_stop(mgr->m_loop, &mgr->m_timer_event); } }
EXPORT_DIRECTIVE int weibo_manage_app_init(gd_app_context_t app, gd_app_module_t module, cfg_t cfg) { weibo_manage_t weibo_manage; weibo_manage = weibo_manage_create(app, gd_app_module_name(module), gd_app_alloc(app), gd_app_em(app)); if (weibo_manage == NULL) return -1; weibo_manage->m_debug = cfg_get_int32(cfg, "debug", 0); if (weibo_manage->m_debug) { CPE_INFO( gd_app_em(app), "%s: create: done", weibo_manage_name(weibo_manage)); } return 0; }
static void inet_sch_timer_cb(EV_P_ struct ev_timer *w, int revents) { CURLMcode rc; inet_sch_manage_t mgr = (inet_sch_manage_t)w->data; if (mgr->m_debug >= 2) { CPE_INFO( mgr->m_em, "%s: timer: event=%d", inet_sch_manage_name(mgr), revents); } rc = curl_multi_socket_action(mgr->m_multi, CURL_SOCKET_TIMEOUT, 0, &mgr->m_still_running); if (rc != CURLM_OK) { CPE_ERROR( mgr->m_em, "%s: timer: curl_multi_socket_action fail, %d(%s)", inet_sch_manage_name(mgr), rc, inet_sch_curl_code_msg(rc)); } inet_sch_process_multi_info(mgr); }
int inet_sch_manage_timer_update_cb(CURLM *multi, long timeout_ms, inet_sch_manage_t mgr) { assert(mgr); if (mgr->m_debug >= 2) { CPE_INFO( mgr->m_em, "%s: timer update: timeout_ms=%ld\n", inet_sch_manage_name(mgr), timeout_ms); } ev_timer_stop(mgr->m_loop, &mgr->m_timer_event); if (timeout_ms > 0) { double t = timeout_ms / 1000; ev_timer_init(&mgr->m_timer_event, inet_sch_timer_cb, t, 0.); ev_timer_start(mgr->m_loop, &mgr->m_timer_event); } else { inet_sch_timer_cb(mgr->m_loop, &mgr->m_timer_event, 0); } return 0; }
int gd_timer_processor_alloc(gd_timer_mgr_t mgr, gd_timer_id_t * id) { ptr_int_t newStart; struct gd_timer_processor * newPage; size_t i; if (!cpe_range_mgr_is_empty(&mgr->m_ids)) { *id = (gd_timer_id_t)cpe_range_get_one(&mgr->m_ids); return 0; } if (mgr->m_timer_page_count + 1 >= mgr->m_timer_page_capacity) { size_t newProcessorPageCapacity; struct gd_timer_processor ** newProcessorBuf; newProcessorPageCapacity = mgr->m_timer_page_count + 128; newProcessorBuf = (struct gd_timer_processor **)mem_alloc(mgr->m_alloc, sizeof(struct gd_timer_processor*) * newProcessorPageCapacity); if (newProcessorBuf == NULL) return -1; bzero(newProcessorBuf, sizeof(struct gd_timer_processor *) * newProcessorPageCapacity); memcpy(newProcessorBuf, mgr->m_timer_buf, sizeof(struct gd_timer_processor*) * mgr->m_timer_page_count); if (mgr->m_timer_buf) mem_free(mgr->m_alloc, mgr->m_timer_buf); mgr->m_timer_buf = newProcessorBuf; mgr->m_timer_page_capacity = newProcessorPageCapacity; if (mgr->m_debug) { CPE_INFO(mgr->m_em, "%s: gd_timer_processor_alloc: resize processor buf to " FMT_SIZE_T, gd_timer_mgr_name(mgr), newProcessorPageCapacity); } } newStart = mgr->m_timer_page_count * mgr->m_timer_count_in_page; newPage = (struct gd_timer_processor *)mem_alloc(mgr->m_alloc, sizeof(struct gd_timer_processor) * mgr->m_timer_count_in_page); if (newPage == NULL) { return -1; } bzero(newPage, sizeof(struct gd_timer_processor) * mgr->m_timer_count_in_page); for(i = 0; i < mgr->m_timer_count_in_page; ++i) { newPage[i].m_id = newStart + i; } if (cpe_range_put_range(&mgr->m_ids, newStart, newStart + mgr->m_timer_count_in_page) != 0) { mem_free(mgr->m_alloc, newPage); return -1; } mgr->m_timer_buf[mgr->m_timer_page_count] = newPage; ++mgr->m_timer_page_count; if (mgr->m_debug) { CPE_INFO( mgr->m_em, "alloc a new processor page[%d,%d), page count is " FMT_SIZE_T, (gd_timer_id_t)newStart, (gd_timer_id_t)(newStart + mgr->m_timer_page_count), mgr->m_timer_page_count); } *id = (gd_timer_id_t)cpe_range_get_one(&mgr->m_ids); return 0; }