// Init BSP-core int core_init() { if (!core_settings.initialized) { init_core_setting(); } if (core_settings.is_daemonize) { proc_daemonize(); } fd_init(0); log_init(); load_runtime_setting(); status_op_core(STATUS_OP_INSTANCE_ID, (size_t) core_settings.instance_id); thread_init(); save_pid(); signal_init(); socket_init(); memdb_init(); online_init(); // Load modules BSP_VALUE *val = object_get_hash_str(runtime_settings, "modules"); BSP_OBJECT *vobj = value_get_object(val); BSP_STRING *vstr = NULL; if (vobj && OBJECT_TYPE_ARRAY == vobj->type) { size_t varr_size = object_size(vobj), i; if (varr_size > MAX_MODULES) { varr_size = MAX_MODULES; } reset_object(vobj); for (i = 0; i < varr_size; i ++) { val = object_get_array(vobj, i); vstr = value_get_string(val); if (vstr) { script_load_module(vstr, 1); } } } return BSP_RTN_SUCCESS; }
static void _dump_object(BSP_OBJECT *obj, int layer) { if (!obj) { return; } int i; BSP_VALUE *val; reset_object(obj); switch (obj->type) { case OBJECT_TYPE_SINGLE : // Single fprintf(stderr, "\033[1;37mObject type : [SINGLE]\033[0m\n"); val = object_get_single(obj); _dump_value(val, layer); break; case OBJECT_TYPE_ARRAY : // Array fprintf(stderr, "\033[1;37mObject type : [ARRAY]\033[0m\n"); size_t idx = 0; for (idx = 0; idx < object_size(obj); idx ++) { for (i = 0; i <= layer; i ++) { fprintf(stderr, "\t"); } fprintf(stderr, "\033[1;35m%lld\033[0m\t=> ", (long long int) idx); val = object_get_array(obj, idx); _dump_value(val, layer); } fprintf(stderr, "\n"); break; case OBJECT_TYPE_HASH : // Dict fprintf(stderr, "\033[1;37mObject type : [HASH]\033[0m\n"); val = curr_item(obj); BSP_STRING *key; while (val) { key = curr_hash_key(obj); for (i = 0; i <= layer; i ++) { fprintf(stderr, "\t"); } if (key) { fprintf(stderr, "\033[1;33m"); write(STDERR_FILENO, STR_STR(key), STR_LEN(key)); fprintf(stderr, "\033[0m"); } else { fprintf(stderr, "### NO KEY ###"); } fprintf(stderr, "\t=> "); _dump_value(val, layer); next_item(obj); val = curr_item(obj); } fprintf(stderr, "\n"); break; case OBJECT_TYPE_UNDETERMINED : default : // Null fprintf(stderr, "\033[1;36mObject type : [UNKNOWN]\033[0m\n"); break; } return; }
// Start main loop int core_loop(void (* server_event)(BSP_CALLBACK *)) { BSP_THREAD *t = get_thread(MAIN_THREAD); if (!t) { trigger_exit(BSP_RTN_FATAL, "Main thread lost!"); } // Servers BSP_VALUE *val = object_get_hash_str(runtime_settings, "servers"); BSP_OBJECT *vobj = value_get_object(val); BSP_STRING *vstr = NULL; if (vobj && OBJECT_TYPE_ARRAY == vobj->type) { struct bsp_conf_server_t srv; BSP_OBJECT *vsrv = NULL; size_t varr_size = object_size(vobj), i; reset_object(vobj); for (i = 0; i < varr_size; i ++) { val = object_get_array(vobj, i); vsrv = value_get_object(val); if (vsrv && OBJECT_TYPE_HASH == vsrv->type) { // Default value memset(&srv, 0, sizeof(struct bsp_conf_server_t)); srv.server_inet = INET_TYPE_ANY; srv.server_sock = SOCK_TYPE_ANY; srv.def_client_type = CLIENT_TYPE_DATA; srv.def_data_type = DATA_TYPE_PACKET; val = object_get_hash_str(vsrv, "name"); vstr = value_get_string(val); srv.server_name = bsp_strndup(STR_STR(vstr), STR_LEN(vstr)); val = object_get_hash_str(vsrv, "inet"); vstr = value_get_string(val); if (vstr) { if (0 == strncasecmp(STR_STR(vstr), "ipv6", 4)) { srv.server_inet = INET_TYPE_IPV6; } else if (0 == strncasecmp(STR_STR(vstr), "ipv4", 4)) { srv.server_inet = INET_TYPE_IPV4; } else if (0 == strncasecmp(STR_STR(vstr), "local", 5)) { srv.server_inet = INET_TYPE_LOCAL; } } val = object_get_hash_str(vsrv, "sock"); vstr = value_get_string(val); if (vstr) { if (0 == strncasecmp(STR_STR(vstr), "tcp", 3)) { srv.server_sock = SOCK_TYPE_TCP; } else if (0 == strncasecmp(STR_STR(vstr), "udp", 3)) { srv.server_sock = SOCK_TYPE_UDP; } } val = object_get_hash_str(vsrv, "addr"); vstr = value_get_string(val); srv.server_addr = bsp_strndup(STR_STR(vstr), STR_LEN(vstr)); val = object_get_hash_str(vsrv, "port"); srv.server_port = (int) value_get_int(val); val = object_get_hash_str(vsrv, "heartbeat_check"); srv.heartbeat_check = (int) value_get_int(val); val = object_get_hash_str(vsrv, "debug_input"); srv.debug_hex_input = value_get_boolean(val); val = object_get_hash_str(vsrv, "debug_output"); srv.debug_hex_input = value_get_boolean(val); val = object_get_hash_str(vsrv, "max_clients"); srv.max_clients = (int) value_get_int(val); val = object_get_hash_str(vsrv, "max_packet_length"); srv.max_packet_length = (size_t) value_get_int(val); val = object_get_hash_str(vsrv, "websocket"); if (value_get_boolean(val)) { srv.def_client_type = CLIENT_TYPE_WEBSOCKET_HANDSHAKE; } val = object_get_hash_str(vsrv, "data_type"); vstr = value_get_string(val); if (vstr && 0 == strncasecmp(STR_STR(vstr), "stream", 6)) { srv.def_data_type = DATA_TYPE_STREAM; } // Add server BSP_SERVER *s; int srv_fds[MAX_SERVER_PER_CREATION], srv_ct, fd_type; int nfds = MAX_SERVER_PER_CREATION; nfds = new_server(srv.server_addr, srv.server_port, srv.server_inet, srv.server_sock, srv_fds, &nfds); for (srv_ct = 0; srv_ct < nfds; srv_ct ++) { fd_type = FD_TYPE_SOCKET_SERVER; s = (BSP_SERVER *) get_fd(srv_fds[srv_ct], &fd_type); if (s) { s->name = srv.server_name; s->heartbeat_check = srv.heartbeat_check; s->def_client_type = srv.def_client_type; s->def_data_type = srv.def_data_type; s->max_packet_length = srv.max_packet_length; s->max_clients = srv.max_clients; s->debug_hex_input = srv.debug_hex_input; s->debug_hex_output = srv.debug_hex_output; add_server(s); } else { bsp_free(srv.server_name); } } } } } // Server event if (server_event) { core_settings.on_srv_events = server_event; } // Create 1 Hz clock BSP_TIMER *tmr = new_timer(BASE_CLOCK_SEC, BASE_CLOCK_USEC, -1); tmr->on_timer = base_timer; core_settings.main_timer = tmr; dispatch_to_thread(tmr->fd, MAIN_THREAD); start_timer(tmr); // Let's go load_bootstrap(); trace_msg(TRACE_LEVEL_CORE, "Core : Main thread loop started"); thread_process((void *) t); return BSP_RTN_SUCCESS; }