# HG changeset patch # User Daniel Atallah # Date 1161228181 0 # Node ID ec9cc2219e552fd8614d315636c8d214fdfa2836 # Parent 8379e256b062dc88af30f4b4fb04c5acd55b7fb0 [gaim-migrate @ 17544] Robustify the wingaim network managers stuff. Apparently if people disable certain windows services the networks lookup fails and the "wait for changes" function returns immediately. This will make wingaim not go ape when this is the case. committer: Tailor Script diff -r 8379e256b062 -r ec9cc2219e55 libgaim/network.c --- a/libgaim/network.c Thu Oct 19 03:14:51 2006 +0000 +++ b/libgaim/network.c Thu Oct 19 03:23:01 2006 +0000 @@ -377,7 +377,7 @@ } #ifdef _WIN32 -static guint +static gint wgaim_get_connected_network_count(void) { guint net_cnt = 0; @@ -397,7 +397,7 @@ gaim_debug_warning("network", "Couldn't look up connected networks. %s (%d).\n", msg, errorid); g_free(msg); - net_cnt = 1; /* Assume something is connected */ + return -1; } else { char buf[1024]; WSAQUERYSET *res = (LPWSAQUERYSET) buf; @@ -418,11 +418,14 @@ static gboolean wgaim_network_change_thread_cb(gpointer data) { - guint new_count; + gint new_count; GaimConnectionUiOps *ui_ops = gaim_connections_get_ui_ops(); new_count = wgaim_get_connected_network_count(); + if (new_count < 0) + return FALSE; + gaim_debug_info("network", "Received Network Change Notification. Current network count is %d, previous count was %d.\n", new_count, current_network_count); if (new_count > 0) { @@ -440,6 +443,7 @@ { HANDLE h; WSAQUERYSET qs; + time_t last_trigger = time(NULL); int WSAAPI (*MyWSANSPIoctl) ( HANDLE hLookup, DWORD dwControlCode, LPVOID lpvInBuffer, @@ -463,12 +467,20 @@ retval = WSALookupServiceBegin(&qs, LUP_RETURN_ALL, &h); + /* Make sure at least 30 seconds have elapsed since the last + * notification so we don't peg the cpu if this keeps changing. */ + if ((time(NULL) - last_trigger) < 30) + Sleep(30000); + + last_trigger = time(NULL); + /* This will block until there is a network change */ retval = MyWSANSPIoctl(h, SIO_NSP_NOTIFY_CHANGE, NULL, 0, NULL, 0, &retLen, NULL); retval = WSALookupServiceEnd(h); g_idle_add(wgaim_network_change_thread_cb, NULL); + } } #endif @@ -534,9 +546,17 @@ { #ifdef _WIN32 GError *err = NULL; - current_network_count = wgaim_get_connected_network_count(); - if (!g_thread_create(wgaim_network_change_thread, NULL, FALSE, &err)) - gaim_debug_error("network", "Couldn't create Network Monitor thread: %s\n", err ? err->message : ""); + gint cnt = wgaim_get_connected_network_count(); + + if (cnt < 0) /* Assume there is a network */ + current_network_count = 1; + /* Don't listen for network changes if we can't tell anyway */ + else + { + current_network_count = cnt; + if (!g_thread_create(wgaim_network_change_thread, NULL, FALSE, &err)) + gaim_debug_error("network", "Couldn't create Network Monitor thread: %s\n", err ? err->message : ""); + } #endif gaim_prefs_add_none ("/core/network");