int uman_init(struct hub_info* hub) { struct hub_user_manager* users = NULL; if (!hub) return -1; users = (struct hub_user_manager*) hub_malloc_zero(sizeof(struct hub_user_manager)); if (!users) return -1; users->list = list_create(); users->sids = sid_pool_create(net_get_max_sockets()); if (!users->list) { list_destroy(users->list); hub_free(users); return -1; } if (net_backend_get_timeout_queue()) { users->timeout = hub_malloc_zero(sizeof(struct timeout_evt)); timeout_evt_initialize(users->timeout, timer_statistics, hub); timeout_queue_insert(net_backend_get_timeout_queue(), users->timeout, TIMEOUT_STATS); } hub->users = users; return 0; }
void hub_shutdown_service(struct hub_info* hub) { LOG_DEBUG("hub_shutdown_service()"); if (net_backend_get_timeout_queue()) { timeout_queue_remove(net_backend_get_timeout_queue(), hub->stats.timeout); hub_free(hub->stats.timeout); } #ifdef SSL_SUPPORT unload_ssl_certificates(hub); #endif event_queue_shutdown(hub->queue); net_con_close(hub->server); server_alt_port_stop(hub); uman_shutdown(hub->users); list_clear(hub->muxes, &hub_mux_destroy); list_destroy(hub->muxes); hub->status = hub_status_stopped; hub_free(hub->sendbuf); hub_free(hub->recvbuf); list_clear(hub->logout_info, &hub_free); list_destroy(hub->logout_info); command_shutdown(hub->commands); hub_free(hub); hub = 0; g_hub = 0; }
void net_con_set_timeout(struct net_connection* con, int seconds) { if (!con->timeout) { con->timeout = hub_malloc_zero(sizeof(struct timeout_evt)); timeout_evt_initialize(con->timeout, timeout_callback, con); timeout_queue_insert(net_backend_get_timeout_queue(), con->timeout, seconds); } else { timeout_queue_reschedule(net_backend_get_timeout_queue(), con->timeout, seconds); } }
void net_con_clear_timeout(struct net_connection* con) { if (con->timeout && timeout_evt_is_scheduled(con->timeout)) { timeout_queue_remove(net_backend_get_timeout_queue(), con->timeout); hub_free(con->timeout); con->timeout = 0; } }
int uman_shutdown(struct hub_info* hub) { if (!hub || !hub->users) return -1; if (net_backend_get_timeout_queue()) { timeout_queue_remove(net_backend_get_timeout_queue(), hub->users->timeout); hub_free(hub->users->timeout); } if (hub->users->list) { list_clear(hub->users->list, &clear_user_list_callback); list_destroy(hub->users->list); } sid_pool_destroy(hub->users->sids); hub_free(hub->users); hub->users = 0; return 0; }
struct hub_info* hub_start_service(struct hub_config* config) { struct hub_info* hub = 0; int ipv6_supported; hub = hub_malloc_zero(sizeof(struct hub_info)); if (!hub) { LOG_FATAL("Unable to allocate memory for hub"); return 0; } hub->tm_started = time(0); ipv6_supported = net_is_ipv6_supported(); if (ipv6_supported) LOG_DEBUG("IPv6 supported."); else LOG_DEBUG("IPv6 not supported."); hub->server = start_listening_socket(config->server_bind_addr, config->server_port, config->server_listen_backlog, hub); if (!hub->server) { hub_free(hub); LOG_FATAL("Unable to start hub service"); return 0; } LOG_INFO("Starting " PRODUCT "/" VERSION ", listening on %s:%d...", net_get_local_address(hub->server->sd), config->server_port); #ifdef SSL_SUPPORT if (!load_ssl_certificates(hub, config)) { hub_free(hub); return 0; } #endif hub->config = config; hub->users = NULL; hub->muxes = list_create(); hub->users = uman_init(); if (!hub->users) { net_con_close(hub->server); hub_free(hub); return 0; } if (event_queue_initialize(&hub->queue, hub_event_dispatcher, (void*) hub) == -1) { net_con_close(hub->server); uman_shutdown(hub->users); hub_free(hub); return 0; } hub->recvbuf = hub_malloc(MAX_RECV_BUF); hub->sendbuf = hub_malloc(MAX_SEND_BUF); if (!hub->recvbuf || !hub->sendbuf) { net_con_close(hub->server); hub_free(hub->recvbuf); hub_free(hub->sendbuf); uman_shutdown(hub->users); hub_free(hub); return 0; } hub->logout_info = (struct linked_list*) list_create(); server_alt_port_start(hub, config); hub->status = hub_status_running; g_hub = hub; if (net_backend_get_timeout_queue()) { hub->stats.timeout = hub_malloc_zero(sizeof(struct timeout_evt)); timeout_evt_initialize(hub->stats.timeout, hub_timer_statistics, hub); timeout_queue_insert(net_backend_get_timeout_queue(), hub->stats.timeout, TIMEOUT_STATS); } // Start the hub command sub-system hub->commands = command_initialize(hub); return hub; }
static void hub_timer_statistics(struct timeout_evt* t) { struct hub_info* hub = (struct hub_info*) t->ptr; hub_update_stats(hub); timeout_queue_reschedule(net_backend_get_timeout_queue(), hub->stats.timeout, TIMEOUT_STATS); }