changeset 14780:ec9cc2219e55

[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 <tailor@pidgin.im>
author Daniel Atallah <daniel.atallah@gmail.com>
date Thu, 19 Oct 2006 03:23:01 +0000
parents 8379e256b062
children 56dbadfa6543
files libgaim/network.c
diffstat 1 files changed, 26 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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");