[libsoup] Ensure WSAStartup() is called on Windows more reliably



commit 56d6058b5aea82681f40b6784d92cf81dc461908
Author: Tor Lillqvist <tml iki fi>
Date:   Thu Nov 5 21:25:36 2009 +0200

    Ensure WSAStartup() is called on Windows more reliably
    
    It is apparently no longer enough to call WSAStartup() from
    soup_address_class_init() and expect that to be called from
    soup_address_get_type().
    
    Furthermore, soup-address.c doesn't do any socket API calls any more
    so it is more natural to do the WSAStartup() call in
    soup-socket.c. Use G_DEFINE_TYPE_WITH_CODE() to make sure
    soup_socket_get_type() calls once exactly a function that calls
    WSAStartup(). This is what GInetAddress does too, for instance.
    
    Ajust ssl-test.c accordingly.
    
    Approved in bug #600689.

 libsoup/soup-address.c |    9 ---------
 libsoup/soup-socket.c  |   18 ++++++++++++------
 tests/ssl-test.c       |    2 +-
 3 files changed, 13 insertions(+), 16 deletions(-)
---
diff --git a/libsoup/soup-address.c b/libsoup/soup-address.c
index c4e8085..f6168d6 100644
--- a/libsoup/soup-address.c
+++ b/libsoup/soup-address.c
@@ -211,15 +211,6 @@ soup_address_class_init (SoupAddressClass *address_class)
 				      "sockaddr",
 				      "struct sockaddr for this address",
 				      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
-
-#ifdef G_OS_WIN32
-	/* This hopefully is a good place to call WSAStartup */
-	{
-		WSADATA wsadata;
-		if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0)
-			g_error ("Windows Sockets could not be initialized");
-	}
-#endif
 }
 
 static GObject *
diff --git a/libsoup/soup-socket.c b/libsoup/soup-socket.c
index 51bbe5e..00fd881 100644
--- a/libsoup/soup-socket.c
+++ b/libsoup/soup-socket.c
@@ -35,7 +35,18 @@
  * soup_socket_get_remote_address()) may be useful to applications.
  **/
 
-G_DEFINE_TYPE (SoupSocket, soup_socket, G_TYPE_OBJECT)
+static void
+_soup_networking_init (void)
+{
+#ifdef G_OS_WIN32
+  WSADATA wsadata;
+  if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0)
+    g_error ("Windows Sockets could not be initialized");
+#endif
+}
+
+G_DEFINE_TYPE_WITH_CODE (SoupSocket, soup_socket, G_TYPE_OBJECT,
+			 _soup_networking_init ();)
 
 enum {
 	READABLE,
@@ -368,11 +379,6 @@ soup_socket_class_init (SoupSocketClass *socket_class)
 				   "Value in seconds to timeout a blocking I/O",
 				   0, G_MAXUINT, 0,
 				   G_PARAM_READWRITE));
-
-#ifdef G_OS_WIN32
-	/* Make sure WSAStartup() gets called. */
-	soup_address_get_type ();
-#endif
 }
 
 
diff --git a/tests/ssl-test.c b/tests/ssl-test.c
index 3c22dbb..3df070a 100644
--- a/tests/ssl-test.c
+++ b/tests/ssl-test.c
@@ -231,7 +231,7 @@ main (int argc, char **argv)
 	g_type_init ();
 
 	/* On Windows, this will call WSAStartup() */
-	soup_address_get_type ();
+	soup_socket_get_type ();
 
 	while ((opt = getopt (argc, argv, "c:d:k:")) != -1) {
 		switch (opt) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]