Ensure WSAStartup() is called on Windows more reliably
authorTor Lillqvist <tml@iki.fi>
Thu, 5 Nov 2009 19:25:36 +0000 (21:25 +0200)
committerTor Lillqvist <tml@iki.fi>
Thu, 5 Nov 2009 19:30:04 +0000 (21:30 +0200)
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
libsoup/soup-socket.c
tests/ssl-test.c

index c4e8085..f6168d6 100644 (file)
@@ -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 *
index 51bbe5e..00fd881 100644 (file)
  * 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
 }
 
 
index 3c22dbb..3df070a 100644 (file)
@@ -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) {