static void receive_tcp_client_data(Client *a_client) { char buf[BUF_SIZE] = {0}; char address[64] = {0}; int res; res = connection_tcp_receive(a_client->conn, buf, BUF_SIZE); if (res) { vector_delete_first_equal(_s_clients, a_client, cmp_clients_by_ptr); char message[BUF_SIZE] = {0}; sprintf(message, "Something happened. The client '%s' left the chat.", a_client->name); send_broadcast_msg(message); info("%s", message); client_destroy(a_client); return; } sprintf(address, "%s:%d", connection_get_address_str(a_client->conn), connection_get_port(a_client->conn)); info("From address '%s' recived message '%s'", address, buf); handle_client_data(a_client, buf, address); }
int cmp_clients_by_addr(void *a_item, void *a_pattern) { Client *cl_item = a_item; struct sockaddr_in *pat_addr = a_pattern; in_addr_t ip = connection_get_address(cl_item->conn); int port = connection_get_port(cl_item->conn); if (port == pat_addr->sin_port && ip == pat_addr->sin_addr.s_addr) { return 0; } return 1; }
void mpd_async_request(MpdAsyncCallback * callback, gpointer callback_data, MpdAsyncFunction * function, gpointer function_data) { gchar *string = NULL; GSimpleAsyncResult *res = NULL; MpdAsyncData *data = g_new0(MpdAsyncData, 1); data->mi = mpd_new_default(); data->function = function; data->function_data = function_data; data->callback = callback; data->callback_data = callback_data; /** * Set Hostname */ string = connection_get_hostname(); mpd_set_hostname(data->mi, string); /** * Set port */ mpd_set_port(data->mi, connection_get_port()); /** * Timeout */ mpd_set_connection_timeout(data->mi, cfg_get_single_value_as_float_with_default(config, "connection", "timeout", DEFAULT_TIMEOUT)); if (connection_use_auth()) { string = connection_get_password(); mpd_set_password(data->mi, string); } else { mpd_set_password(data->mi, ""); } res = g_simple_async_result_new(NULL, __mpd_async_simple_callback, NULL, mpd_async_request); g_simple_async_result_set_op_res_gpointer(G_SIMPLE_ASYNC_RESULT(res), data, __mpd_async_result_free_data); g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Start async thread %p\n", data); g_simple_async_result_run_in_thread(res, __async_handler_function, G_PRIORITY_LOW, NULL); }