static void dr_metalib_validate_align_i(error_monitor_t em, LPDRMETALIB metalib) { int meta_pos; int meta_count; meta_count = dr_lib_meta_num(metalib); for(meta_pos = 0; meta_pos < meta_count; ++meta_pos) { LPDRMETA meta; int entry_pos; int entry_count; meta = dr_lib_meta_at(metalib, meta_pos); entry_count = dr_meta_entry_num(meta); for(entry_pos = 0; entry_pos < entry_count; ++entry_pos) { LPDRMETAENTRY entry = dr_meta_entry_at(meta, entry_pos); int align = dr_entry_align(entry); if (align != 1 && align != 2 && align != 4 && align != 8) { CPE_ERROR( em, "%s.%s: type align %d error", dr_meta_name(meta), dr_entry_name(entry), align); continue; } if ((int)dr_entry_data_start_pos(entry) % align) { CPE_ERROR( em, "%s.%s: start pos error, align is %d, startpos is %d", dr_meta_name(meta), dr_entry_name(entry), align, (int)dr_entry_data_start_pos(entry)); continue; } } } }
int inet_sch_task_run(inet_sch_task_t task) { CURLMcode rc; assert(task); if (task->m_state != inet_sch_task_state_init) { CPE_ERROR( task->m_mgr->m_em, "%s: run task: task run in error state %d!", inet_sch_manage_name(task->m_mgr), task->m_state); return -1; } rc = curl_multi_add_handle(task->m_mgr->m_multi, task->m_handler); if (rc != CURLM_OK) { CPE_ERROR( task->m_mgr->m_em, "%s: run task: curl_multi_add_handle fail, %s(%d)!", inet_sch_manage_name(task->m_mgr), inet_sch_curl_code_msg(rc), rc); return -1; } task->m_state = inet_sch_task_state_runing; return 0; }
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); }
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); } }
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*/); } } }
void net_ep_close_i(net_ep_t ep, net_ep_event_t ev) { assert(ep); assert(ev == net_ep_event_close_by_user || ev == net_ep_event_close_by_peer || ev == net_ep_event_close_by_error || ev == net_ep_event_close_by_shutdown); if (ep->m_fd < 0) return; #ifdef _MSC_VER //if (ep->m_type = net_ep_socket) { net_socket_close(&ep->m_fd, ep->m_mgr->m_em); //} //else { // CPE_ERROR( // ep->m_mgr->m_em, "net_ep_close: close fail, errno=%d (%s)", // cpe_sock_errno(), cpe_sock_errstr(cpe_sock_errno())); //} #else if (close(ep->m_fd) != 0) { CPE_ERROR( ep->m_mgr->m_em, "net_ep_close: close fail, errno=%d (%s)", cpe_sock_errno(), cpe_sock_errstr(cpe_sock_errno())); } #endif if (net_ep_calc_ev_events(ep)) { ev_io_stop(ep->m_mgr->m_ev_loop, &ep->m_watcher); } ep->m_fd = -1; if (ep->m_process_fun) ep->m_process_fun(ep, ep->m_process_ctx, ev); }
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; }
inet_sch_task_t inet_sch_task_create(inet_sch_manage_t mgr) { inet_sch_task_t task; task = mem_alloc(mgr->m_alloc, sizeof(struct inet_sch_task)); if (task == NULL) return NULL; task->m_mgr = mgr; task->m_state = inet_sch_task_state_init; task->m_evset = 0; task->m_error[0] = '\0'; task->m_handler = curl_easy_init(); if (task->m_handler == NULL) { CPE_ERROR( mgr->m_em, "%s: create task: curl_easy_init fail!", inet_sch_manage_name(mgr)); mem_free(mgr->m_alloc, task); return NULL; } curl_easy_setopt(task->m_handler, CURLOPT_PRIVATE, task); curl_easy_setopt(task->m_handler, CURLOPT_ERRORBUFFER, task->m_error); task->m_ev.data = task; return task; }
static int do_generate_lib_c(cpe_dr_generate_ctx_t ctx) { struct write_stream_file stream; int i; int rv; rv = 0; for(i = 0; i < o_lib_c->count; ++i) { FILE * fp; if (o_lib_c_arg->count == 0) { CPE_ERROR(ctx->m_em, "not arg name setted!"); return -1; } fp = file_stream_open(o_lib_c->filename[i], "w", ctx->m_em); if (fp == NULL) { rv = -1; continue; } write_stream_file_init(&stream, fp, ctx->m_em); cpe_dr_generate_lib_c((write_stream_t)&stream, o_lib_c_arg->sval[0], ctx); file_stream_close(fp, ctx->m_em); } return rv; }
int main(int argc, char * argv[]) { void* argtable[] = { input = arg_filen( "i", "input", "<string>", 0, 100, "input file") , o_validate = arg_strn( "v", "validate", "<string>", 0, 10, "validate operations") , o_h = arg_file0( NULL, "output-h", "<string>", "output h file dir") , o_lib_c = arg_file0( NULL, "output-lib-c", "<string>", "output c lib file") , o_lib_c_arg = arg_str0( NULL, "output-lib-c-arg", "<string>", "output c lib file") , o_lib_bin = arg_file0( NULL, "output-lib-bin", "<string>", "output c lib file") , help = arg_lit0( NULL, "help", "print this help and exit") , end = arg_end(20) }; struct error_monitor em_buf; error_monitor_t em; int rv; int nerrors; cpe_error_monitor_init(&em_buf, cpe_error_log_to_consol, 0); em = &em_buf; rv = -1; if (arg_nullcheck(argtable) != 0) { CPE_ERROR(em, "init arg table fail!"); goto exit; } nerrors = arg_parse(argc,argv,argtable); if (help->count > 0) { printf("Usage: %s", argv[0]); arg_print_syntax(stdout,argtable,"\n"); rv = 0; goto exit; } if (nerrors > 0) { arg_print_errors(stdout, end, argv[0]); printf("Try '%s --help' for more information.\n", argv[0]); goto exit; } rv = tools_main(em); exit: arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return rv; }
int tools_main(error_monitor_t em) { struct cpe_dr_generate_ctx ctx; struct mem_buffer buffer; int rv; ctx.m_builder = NULL; ctx.m_metalib = NULL; ctx.m_em = em; rv = 0; ctx.m_builder = dr_metalib_builder_create(NULL, em); if (ctx.m_builder == NULL) { CPE_ERROR(em, "create metalib builder fail!"); return -1; } prepare_input(ctx.m_builder, em); dr_metalib_builder_analize(ctx.m_builder); mem_buffer_init(&buffer, 0); if (dr_inbuild_build_lib( &buffer, dr_metalib_bilder_lib(ctx.m_builder), em) == 0) { ctx.m_metalib = (LPDRMETALIB)mem_buffer_make_continuous(&buffer, 0), mem_buffer_size(&buffer); if (ctx.m_metalib) { if (do_validate(&ctx) != 0) rv = -1; if (do_generate_h(&ctx) != 0) rv = -1; if (do_generate_lib_bin(&ctx) != 0) rv = -1; if (do_generate_lib_c(&ctx) != 0) rv = -1; } } else { rv = -1; } mem_buffer_clear(&buffer); dr_metalib_builder_free(ctx.m_builder); return rv; }
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 net_ep_set_fd(net_ep_t ep, int fd) { assert(ep); if (net_ep_is_open(ep)) { CPE_ERROR( ep->m_mgr->m_em, "net_mgr: ep %d: set fd to a opend ep", (int)ep->m_id); net_ep_close_i(ep, net_ep_event_close_by_error); } ep->m_fd = fd; ep->m_watcher.data = ep; ev_init(&ep->m_watcher, net_ep_cb); net_ep_update_events(ep, 0); return 0; }
int do_validate(cpe_dr_generate_ctx_t ctx) { int rv; int i; rv = 0; for(i = 0; i < o_validate->count; ++i) { const char * validate_name; validate_name = o_validate->sval[i]; if (strcmp(validate_name, "align") == 0) { if (dr_metalib_validate_align(ctx->m_em, ctx->m_metalib) != 0) { rv = -1; } } else { CPE_ERROR(ctx->m_em, "validate %s is unknown!", validate_name); rv = -1; } } return rv; }
void prepare_input(dr_metalib_builder_t builder, error_monitor_t em) { int i; for(i = 0; i < input->count; ++i) { const char * filename; size_t filename_len; filename = input->filename[i]; filename_len = strlen(filename); if (filename[filename_len - 1] == '\\' || filename[filename_len - 1] == '/') { ((char *)filename)[filename_len - 1] = 0; } if (dir_exist(filename, em)) { dir_search(&g_input_search_visitor, builder, filename, 5, em, NULL); } else if (file_exist(input->filename[i], em)) { dr_metalib_builder_add_file(builder, NULL, filename); } else { CPE_ERROR(em, "input %s not exist!", filename); } } }
net_mgr_t net_mgr_create(mem_allocrator_t alloc, error_monitor_t em) { net_mgr_t nmgr; nmgr = (net_mgr_t)mem_alloc(alloc, sizeof(struct net_mgr)); if (nmgr == NULL) return NULL; bzero(nmgr, sizeof(struct net_mgr)); if (cpe_range_mgr_init(&nmgr->m_ep_ids, alloc) != 0) { CPE_ERROR(em, "init ep ids fail!"); mem_free(alloc, nmgr); return NULL; } nmgr->m_ev_loop = ev_loop_new(EVFLAG_AUTO); if (!nmgr->m_ev_loop) { CPE_ERROR(em, "net_mgr_create: create event loop fail!"); cpe_range_mgr_fini(&nmgr->m_ep_ids); mem_free(alloc, nmgr); return NULL; } nmgr->m_alloc = alloc; nmgr->m_em = em; nmgr->m_debug = 0; nmgr->m_ep_page_capacity = 0; nmgr->m_ep_pages = NULL; TAILQ_INIT(&nmgr->m_chanels); if (cpe_hash_table_init( &nmgr->m_listeners, alloc, (cpe_hash_fun_t)net_listener_hash, (cpe_hash_cmp_t)net_listener_cmp, CPE_HASH_OBJ2ENTRY(net_listener, m_hh), 256) != 0) { CPE_ERROR(em, "net_mgr_create: init listener hash list fail!"); cpe_range_mgr_fini(&nmgr->m_ep_ids); mem_free(alloc, nmgr); return NULL; } if (cpe_hash_table_init( &nmgr->m_connectors, alloc, (cpe_hash_fun_t)net_connector_hash, (cpe_hash_cmp_t)net_connector_cmp, CPE_HASH_OBJ2ENTRY(net_connector, m_hh), 256) != 0) { CPE_ERROR(em, "net_mgr_create: init connector hash list fail!"); cpe_hash_table_fini(&nmgr->m_listeners); cpe_range_mgr_fini(&nmgr->m_ep_ids); mem_free(alloc, nmgr); return NULL; } return nmgr; }
dr_dm_data_t dr_dm_data_create(dr_dm_manage_t mgr, const void * data, size_t data_size, const char ** duplicate_index) { char * buf; dr_dm_data_t role; size_t data_capacity; dr_dm_data_id_t role_id; int generate_role_id; size_t index_count; struct cpe_hash_it index_it; struct dr_dm_data_index * index; size_t i; index_count = cpe_hash_table_count(&mgr->m_indexes); if (duplicate_index) *duplicate_index = NULL; if (mgr->m_role_meta == NULL) { CPE_ERROR( mgr->m_em, "%s: dr_dm_data_create: role meta not exist!", dr_dm_manage_name(mgr)); return NULL; } if (mgr->m_id_index == NULL) { CPE_ERROR( mgr->m_em, "%s: dr_dm_data_create: role id entry not exist!", dr_dm_manage_name(mgr)); return NULL; } data_capacity = dr_meta_size(mgr->m_role_meta); if (data_size > data_capacity) { CPE_ERROR( mgr->m_em, "%s: dr_dm_data_create: data too long, data_size=%d, data_capacity=%d!", dr_dm_manage_name(mgr), (int)data_size, (int)data_capacity); return NULL; } generate_role_id = 0; role_id = dr_entry_read_int64(data, mgr->m_id_index->m_entry); if (role_id == 0) { if (mgr->m_id_generate) { if (gd_id_generator_generate(&role_id, mgr->m_id_generate) != 0) { CPE_ERROR( mgr->m_em, "%s: dr_dm_data_create: generate id from %s fail!", dr_dm_manage_name(mgr), gd_id_generator_name(mgr->m_id_generate)); return NULL; } else { generate_role_id = 1; } } } buf = (char *)mem_alloc( mgr->m_alloc, sizeof(struct cpe_hash_entry) * index_count + sizeof(struct dr_dm_data) + data_capacity); if (buf == NULL) return NULL; role = (dr_dm_data_t)(buf + sizeof(struct cpe_hash_entry) * index_count); role->m_mgr = mgr; memcpy(dr_dm_data_data(role), data, data_capacity); if (generate_role_id) { if (dr_entry_set_from_int64(dr_dm_data_data(role), role_id, mgr->m_id_index->m_entry, NULL) != 0) { CPE_ERROR( mgr->m_em, "%s: dr_dm_data_create: set generated id to data fail!", dr_dm_manage_name(mgr)); mem_free(mgr->m_alloc, buf); return NULL; } } for(i = 0; i < index_count; ++i) { cpe_hash_entry_init(((struct cpe_hash_entry*)buf) + i); } cpe_hash_it_init(&index_it, &mgr->m_indexes); while((index = cpe_hash_it_next(&index_it))) { if (dr_dm_data_index_add(index, role) != 0) { struct dr_dm_data_index * index_fall_back; CPE_ERROR( mgr->m_em, "%s: dr_dm_data_create: add to index %s: duplicate!", dr_dm_manage_name(mgr), dr_entry_name(index->m_entry)); if (duplicate_index) *duplicate_index = dr_dm_data_index_name(index); cpe_hash_it_init(&index_it, &mgr->m_indexes); while((index_fall_back = cpe_hash_it_next(&index_it)) && index_fall_back != index) { dr_dm_data_index_remove(index_fall_back, role); } mem_free(mgr->m_alloc, buf); return NULL; } } return role; }