Remove Glib workarounds for Windows socket io.
authorAlex Graveley <alex@ximian.com>
Thu, 27 Sep 2001 17:18:49 +0000 (17:18 +0000)
committerAlex Graveley <orph@src.gnome.org>
Thu, 27 Sep 2001 17:18:49 +0000 (17:18 +0000)
2001-09-27  Alex Graveley  <alex@ximian.com>

        * src/libsoup/soup-socket.c: Remove Glib workarounds for Windows
        socket io.

ChangeLog
libsoup/soup-socket.c

index cbd2fb7..a6891aa 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2001-09-27  Alex Graveley  <alex@ximian.com>
 
+       * src/libsoup/soup-socket.c: Remove Glib workarounds for Windows 
+       socket io.
+
+2001-09-27  Alex Graveley  <alex@ximian.com>
+
        * src/libsoup/soup-socket.c (soup_address_new): #ifdef protect the
        ptrace attach/detach code until I have a chance to look at why it
        fails on some platforms.
index 6a73121..302ea2c 100644 (file)
@@ -87,14 +87,11 @@ guint soup_io_watch_ID;
 GIOChannel *soup_iochannel;
 
 GHashTable *soup_hash;
-GHashTable *soup_select_hash; /* soup_socket_new needs its own hash */
 HANDLE soup_Mutex;
-HANDLE soup_select_Mutex;
 HANDLE soup_hostent_Mutex;
 
 #define IA_NEW_MSG 100         /* soup_address_new */
 #define GET_NAME_MSG 101       /* soup_address_get_name */
-#define TCP_SOCK_MSG 102       /* soup_socket_new  */
 
 /* 
  * Windows does not have inet_aton, but it does have inet_addr.
@@ -1940,7 +1937,7 @@ soup_socket_new (SoupAddress      *addr,
                return NULL;
        }
 
-       chan = g_io_channel_unix_new (sockfd);
+       chan = SOUP_SOCKET_IOCHANNEL_NEW (sockfd);
 
        /* Wait for the connection */
        state = g_new0 (SoupSocketState, 1);
@@ -1959,24 +1956,6 @@ soup_socket_new (SoupAddress      *addr,
        return state;
 }
 
-/**
- * soup_socket_new_cancel:
- * @id: ID of the connection.
- *
- * Cancel an asynchronous connection that was started with
- * soup_socket_new().
- **/
-void
-soup_socket_new_cancel (SoupSocketNewId id)
-{
-       SoupSocketState* state = (SoupSocketState*) id;
-
-       g_source_remove (state->connect_watch);
-       soup_address_unref (state->addr);
-       g_free (state);
-}
-
-
 #else  /*********** Windows code ***********/
 
 static gboolean
@@ -1986,15 +1965,11 @@ soup_socket_new_cb (GIOChannel* iochannel,
 {
        SoupSocketState* state = (SoupSocketState*) data;
        SoupSocket *s;
-
-       if (state->errorcode) {
-               soup_address_unref (state->addr);
-               (*state->func) ((SoupSocket *) NULL,
-                               SOUP_SOCKET_NEW_STATUS_ERROR,
-                               state->data);
-               g_free (state);
-               return FALSE;
-       }
+\r
+       /* Remove the watch now in case we don't return immediately */\r
+       g_source_remove (state->connect_watch);\r
+\r
+       if (condition & ~(G_IO_IN | G_IO_OUT)) goto _ERROR;\r
 
        s = g_new0 (SoupSocket, 1);
        s->ref_count = 1;
@@ -2003,6 +1978,15 @@ soup_socket_new_cb (GIOChannel* iochannel,
 
        (*state->func) (s, SOUP_SOCKET_NEW_STATUS_OK, state->data);
        g_free (state);
+       return FALSE;\r
+\r
+ _ERROR:\r
+       soup_address_unref (state->addr);\r
+       (*state->func) ((SoupSocket *) NULL,\r
+                       SOUP_SOCKET_NEW_STATUS_ERROR,\r
+                       state->data);\r
+       g_free (state);\r
+\r
        return FALSE;
 }
 
@@ -2013,7 +1997,9 @@ soup_socket_new (SoupAddress     *addr,
 {
        gint sockfd;
        gint status;
-       SoupSocketState* state = (SoupSocketState*) data;
+       SoupSocketState* state = (SoupSocketState*) data;\r
+       u_long arg = 1;\r
+       GIOChannel *chan;
 
        g_return_val_if_fail (addr != NULL, NULL);
        g_return_val_if_fail (func != NULL, NULL);
@@ -2024,14 +2010,9 @@ soup_socket_new (SoupAddress     *addr,
                (func) (NULL, SOUP_SOCKET_NEW_STATUS_ERROR, data);
                return NULL;
        }
-
-       /* Note: WSAAsunc automatically sets the socket to noblocking mode */
-       status = WSAAsyncSelect (sockfd, soup_hWnd, TCP_SOCK_MSG, FD_CONNECT);
-
-       if (status == SOCKET_ERROR) {
-               (func) (NULL, SOUP_SOCKET_NEW_STATUS_ERROR, data);
-               return NULL;
-       }
+\r
+       /* Set non-blocking mode */
+       ioctlsocket(sockfd, FIONBIO, &arg);\r
 
        status = connect (sockfd, &addr->sa, sizeof(addr->sa));
        /* Returning an error is ok, unless.. */
@@ -2054,36 +2035,43 @@ soup_socket_new (SoupAddress     *addr,
                (*state->func) (s, SOUP_SOCKET_NEW_STATUS_OK, state->data);
                return NULL;
        }
+\r
+       chan = SOUP_SOCKET_IOCHANNEL_NEW (sockfd),\r
 
        /* Wait for the connection */
        state = g_new0 (SoupSocketState, 1);
        state->addr = addr;
        state->func = func;
        state->data = data;
-       state->sockfd = sockfd;
-
-       WaitForSingleObject (soup_select_Mutex, INFINITE);
-       /*using sockfd as the key into the 'select' hash */
-       g_hash_table_insert (soup_select_hash,
-                            (gpointer) state->sockfd,
-                            (gpointer) state);
-       ReleaseMutex (soup_select_Mutex);
+       state->sockfd = sockfd;\r
+       state->connect_watch = g_io_add_watch (chan,\r
+                                              SOUP_ANY_IO_CONDITION,\r
+                                              soup_socket_new_cb, \r
+                                              state);\r
+\r
+       g_io_channel_unref (chan);\r
 
        return state;
 }
 
-void
-soup_socket_new_cancel (SoupSocketNewId id)
-{
-       SoupSocketState* state = (SoupSocketState*) id;
-
-       /* Cancel event posting on the socket */
-       WSAAsyncSelect (state->sockfd, soup_hWnd, 0, 0);
-       soup_address_unref (state->addr);
-       g_free (state);
-}
-
-#endif         /*********** End Windows code ***********/
+#endif         /*********** End Windows code ***********/\r
+\r
+/**\r
+ * soup_socket_new_cancel:\r
+ * @id: ID of the connection.\r
+ *\r
+ * Cancel an asynchronous connection that was started with\r
+ * soup_socket_new().\r
+ **/\r
+void\r
+soup_socket_new_cancel (SoupSocketNewId id)\r
+{\r
+       SoupSocketState* state = (SoupSocketState*) id;\r
+\r
+       g_source_remove (state->connect_watch);\r
+       soup_address_unref (state->addr);\r
+       g_free (state);\r
+}\r
 
 static void
 soup_socket_new_sync_cb (SoupSocket*         socket,
@@ -2175,7 +2163,7 @@ soup_socket_get_iochannel (SoupSocket* socket)
        g_return_val_if_fail (socket != NULL, NULL);
 
        if (socket->iochannel == NULL)
-               socket->iochannel = SOUP_SOCKET_IOCHANNEL_NEW(socket->sockfd);
+               socket->iochannel = SOUP_SOCKET_IOCHANNEL_NEW (socket->sockfd);
 
        g_io_channel_ref (socket->iochannel);
 
@@ -2534,10 +2522,9 @@ soup_MainCallBack (GIOChannel   *iochannel,
        gpointer data;
        SoupAddressState *IAstate;
        SoupAddressReverseState *IARstate;
-       SoupSocketState *TCPNEWstate;
 
        /*Take the msg off the message queue */
-       GetMessage (&msg, NULL, 0, 0);
+       PeekMessage (&msg, soup_hWnd, 0, 0, PM_REMOVE);
 
        switch (msg.message) {
        case IA_NEW_MSG:
@@ -2568,20 +2555,7 @@ soup_MainCallBack (GIOChannel   *iochannel,
                soup_address_get_name_cb (NULL,
                                          G_IO_IN,
                                          (gpointer) IARstate);
-               break;
-       case TCP_SOCK_MSG:
-               WaitForSingleObject (soup_select_Mutex, INFINITE);
-               data = g_hash_table_lookup (soup_select_hash,
-                                           (gpointer) msg.wParam);
-               g_hash_table_remove (soup_select_hash, (gpointer) msg.wParam);
-               ReleaseMutex (soup_select_Mutex);
-
-               TCPNEWstate = (SoupSocketState*) data;
-               TCPNEWstate->errorcode = WSAGETSELECTERROR (msg.lParam);
-               soup_socket_new_cb (NULL,
-                                   G_IO_IN,
-                                   (gpointer) TCPNEWstate);
-               break;
+               break;\r
        }
 
        return 1;
@@ -2608,13 +2582,6 @@ SoupWndProc (HWND hwnd,        /* handle to window */
        }
 }
 
-gboolean
-RemoveHashEntry(gpointer key, gpointer value, gpointer user_data)
-{
-       g_free (value);
-       return TRUE;
-}
-
 BOOL WINAPI
 DllMain (HINSTANCE hinstDLL,  /* handle to DLL module */
         DWORD fdwReason,     /* reason for calling functionm */
@@ -2698,23 +2665,13 @@ DllMain (HINSTANCE hinstDLL,  /* handle to DLL module */
                                        NULL);
 
                soup_hash = g_hash_table_new (NULL, NULL);
-               soup_select_hash = g_hash_table_new (NULL, NULL);
-
 
                soup_Mutex = CreateMutex (NULL, FALSE, "soup_Mutex");
-
                if (soup_Mutex == NULL) return FALSE;
 
-               soup_select_Mutex = CreateMutex (NULL,
-                                                FALSE,
-                                                "soup_select_Mutex");
-
-               if (soup_select_Mutex == NULL) return FALSE;
-
                soup_hostent_Mutex = CreateMutex (NULL,
                                                  FALSE,
                                                  "soup_hostent_Mutex");
-
                if (soup_hostent_Mutex == NULL) return FALSE;
 
                break;
@@ -2729,18 +2686,6 @@ DllMain (HINSTANCE hinstDLL,  /* handle to DLL module */
                g_free (soup_iochannel);
                DestroyWindow (soup_hWnd);
 
-               WaitForSingleObject (soup_Mutex, INFINITE);
-               WaitForSingleObject (soup_select_Mutex, INFINITE);
-               g_hash_table_foreach_remove (soup_hash, RemoveHashEntry, NULL);
-               g_hash_table_foreach_remove (soup_select_hash,
-                                            RemoveHashEntry,
-                                            NULL);
-               g_hash_table_destroy (soup_select_hash);
-               g_hash_table_destroy (soup_hash);
-               ReleaseMutex (soup_Mutex);
-               ReleaseMutex (soup_select_Mutex);
-               ReleaseMutex (soup_hostent_Mutex);
-
                WSACleanup ();
 
                break;