[gnio] Change socket error to be construct only



commit ea5434d379d9f295eeb7c82c564444017d2c8062
Author: Alexander Larsson <alexl redhat com>
Date:   Mon Apr 27 17:02:32 2009 +0200

    Change socket error to be construct only
    
    Rename socket->priv->error to construct_only.
    Set only during construct.
    When reporting errors due to this in other calls report as
    INVALID_ARGUMENT, whith the real error in the message.
    Also check for a closed socket when verifying the socket state.
---
 gio/gsocket.c |  116 +++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 72 insertions(+), 44 deletions(-)

diff --git a/gio/gsocket.c b/gio/gsocket.c
index 6bca820..88f65fe 100644
--- a/gio/gsocket.c
+++ b/gio/gsocket.c
@@ -69,12 +69,32 @@ struct _GSocketPrivate
   gboolean        blocking;
   gint            backlog;
   gboolean        reuse_address;
-  GError         *error;
+  GError         *construct_error;
   GSocketAddress *local_address;
   GSocketAddress *remote_address;
   gboolean        closed;
 };
 
+static gboolean
+check_socket (GSocket *socket,
+	      GError **error)
+{
+  if (socket->priv->construct_error)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
+		   "Invalid socket, creation failed due to: %s",
+		   socket->priv->construct_error->message);
+      return FALSE;
+    }
+  if (socket->priv->closed)
+    {
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
+                           "Stream is already closed");
+      return FALSE;
+    }
+  return TRUE;
+}
+
 static void
 g_socket_details_from_fd (GSocket *socket)
 {
@@ -102,7 +122,7 @@ g_socket_details_from_fd (GSocket *socket)
                    fd, g_strerror (saved_errno));
 
          default:
-          g_set_error (&socket->priv->error, G_IO_ERROR,
+          g_set_error (&socket->priv->construct_error, G_IO_ERROR,
                        g_io_error_from_errno (saved_errno),
                        "creating GSocket from fd: %s",
                        g_strerror (saved_errno));
@@ -138,7 +158,7 @@ g_socket_details_from_fd (GSocket *socket)
     {
       gint saved_errno = errno;
 
-      g_set_error (&socket->priv->error, G_IO_ERROR,
+      g_set_error (&socket->priv->construct_error, G_IO_ERROR,
                    g_io_error_from_errno (saved_errno),
                    "creating GSocket from fd: %s",
                    g_strerror (saved_errno));
@@ -245,7 +265,7 @@ g_socket_constructed (GObject *object)
     /* create the fd from socket->priv info */
     socket->priv->fd = g_socket_create_socket (socket->priv->family,
                                                socket->priv->type,
-                                               &socket->priv->error);
+                                               &socket->priv->construct_error);
 }
 
 static void
@@ -339,9 +359,10 @@ g_socket_finalize (GObject *object)
 {
   GSocket *socket = G_SOCKET (object);
 
-  g_clear_error (&socket->priv->error);
+  g_clear_error (&socket->priv->construct_error);
 
-  if (!socket->priv->closed)
+  if (socket->priv->fd != -1 &&
+      !socket->priv->closed)
     g_socket_close (socket);
 
   if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
@@ -460,7 +481,7 @@ g_socket_init (GSocket *socket)
   socket->priv->blocking = TRUE;
   socket->priv->backlog = 10;
   socket->priv->reuse_address = FALSE;
-  socket->priv->error = NULL;
+  socket->priv->construct_error = NULL;
   socket->priv->remote_address = NULL;
   socket->priv->local_address = NULL;
 }
@@ -584,11 +605,11 @@ g_socket_has_error (GSocket  *socket,
 {
   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
 
-  if (!socket->priv->error)
+  if (!socket->priv->construct_error)
     return FALSE;
 
   if (error)
-    *error = g_error_copy (socket->priv->error);
+    *error = g_error_copy (socket->priv->construct_error);
 
   return TRUE;
 }
@@ -607,7 +628,7 @@ g_socket_listen (GSocket  *socket,
 {
   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
 
-  if (g_socket_has_error (socket, error))
+  if (!check_socket (socket, error))
     return FALSE;
 
   if (listen (socket->priv->fd, socket->priv->backlog) < 0)
@@ -626,7 +647,7 @@ g_socket_bind (GSocket         *socket,
 {
   g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
 
-  if (g_socket_has_error (socket, error))
+  if (!check_socket (socket, error))
     return FALSE;
 
   {
@@ -637,8 +658,9 @@ g_socket_bind (GSocket         *socket,
 
     if (bind (socket->priv->fd, (struct sockaddr *) addr, g_socket_address_get_native_size (address)) < 0)
       {
-        socket->priv->error = g_error_new (G_IO_ERROR, g_io_error_from_errno (errno), "error binding to address: %s", g_strerror (errno));
-        g_propagate_error (error, g_error_copy (socket->priv->error));
+	g_set_error (error,
+		     G_IO_ERROR, g_io_error_from_errno (errno),
+		     "error binding to address: %s", g_strerror (errno));
         return FALSE;
       }
 
@@ -658,19 +680,15 @@ g_socket_accept (GSocket       *socket,
 
   g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
 
-  if (g_socket_has_error (socket, error))
+  if (!check_socket (socket, error))
     return NULL;
 
   if ((ret = accept (socket->priv->fd, NULL, 0)) < 0)
     {
-      if (errno == EAGAIN)
-        g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK, "operation would block");
-      else
-        {
-          socket->priv->error = g_error_new (G_IO_ERROR, g_io_error_from_errno (errno), "error accepting connection: %s", g_strerror (errno));
-          g_propagate_error (error, g_error_copy (socket->priv->error));
-        }
-      return FALSE;
+      g_set_error (error, G_IO_ERROR,
+		   g_io_error_from_errno (errno),
+		   "error accepting connection: %s", g_strerror (errno));
+      return NULL;
     }
 
   return g_socket_new_from_fd (ret);
@@ -685,7 +703,7 @@ g_socket_connect (GSocket         *socket,
 
   g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
 
-  if (g_socket_has_error (socket, error))
+  if (!check_socket (socket, error))
     return FALSE;
 
   g_socket_address_to_native (address, buffer, sizeof buffer);
@@ -697,12 +715,12 @@ g_socket_connect (GSocket         *socket,
 #else
       if (errno == WSAEINPROGRESS)
 #endif
-    	g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING, "connection in progress");
+	g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
+		     "connection in progress");
       else
-        {
-          socket->priv->error = g_error_new (G_IO_ERROR, g_io_error_from_errno (errno), "error connecting: %s", g_strerror (errno));
-          g_propagate_error (error, g_error_copy (socket->priv->error));
-        }
+	g_set_error (error, G_IO_ERROR,
+		     g_io_error_from_errno (errno),
+		     "error connecting: %s", g_strerror (errno));
 
       return FALSE;
     }
@@ -722,15 +740,14 @@ g_socket_receive (GSocket       *socket,
 
   g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, FALSE);
 
+  if (!check_socket (socket, error))
+    return -1;
+
   if ((ret = recv (socket->priv->fd, buffer, size, 0)) < 0)
     {
-      if (errno == EAGAIN)
-        g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK, "operation would block");
-      else
-        {
-          socket->priv->error = g_error_new (G_IO_ERROR, g_io_error_from_errno (errno), "error receiving data: %s", g_strerror (errno));
-          g_propagate_error (error, g_error_copy (socket->priv->error));
-        }
+      g_set_error (error, G_IO_ERROR,
+		   g_io_error_from_errno (errno),
+		   "error receiving data: %s", g_strerror (errno));
       return -1;
     }
 
@@ -747,15 +764,14 @@ g_socket_send (GSocket      *socket,
 
   g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, FALSE);
 
+  if (!check_socket (socket, error))
+    return -1;
+
   if ((ret = send (socket->priv->fd, buffer, size, 0)) < 0)
     {
-      if (errno == EAGAIN)
-        g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK, "operation would block");
-      else
-        {
-          socket->priv->error = g_error_new (G_IO_ERROR, g_io_error_from_errno (errno), "error sending data: %s", g_strerror (errno));
-          g_propagate_error (error, g_error_copy (socket->priv->error));
-        }
+      g_set_error (error, G_IO_ERROR,
+		   g_io_error_from_errno (errno),
+		   "error sending data: %s", g_strerror (errno));
       return -1;
     }
 
@@ -812,6 +828,9 @@ g_socket_condition_check (GSocket       *socket,
   GPollFD poll_fd;
   gint result;
 
+  if (!check_socket (socket, NULL))
+    return 0;
+
   poll_fd.fd = socket->priv->fd;
   poll_fd.events = condition;
 
@@ -846,6 +865,9 @@ g_socket_condition_wait (GSocket       *socket,
   gint result;
   gint num;
 
+  if (!check_socket (socket, error))
+    return FALSE;
+
   poll_fd[0].fd = socket->priv->fd;
   poll_fd[0].events = condition;
   num = 1;
@@ -877,6 +899,9 @@ g_socket_send_message (GSocket                *socket,
   struct msghdr msg;
   gssize result;
 
+  if (!check_socket (socket, error))
+    return -1;
+
   /* name */
   if (address)
     {
@@ -890,7 +915,7 @@ g_socket_send_message (GSocket                *socket,
     if (num_vectors == 0)
       {
         char *zero;
-       
+
         zero = g_newa (char, 1);
         *zero = '\0';
 
@@ -1033,6 +1058,9 @@ g_socket_receive_message (GSocket                 *socket,
   struct msghdr msg;
   gssize result;
 
+  if (!check_socket (socket, error))
+    return -1;
+
   /* name */
   {
     if (address)
@@ -1158,7 +1186,7 @@ g_socket_receive_message (GSocket                 *socket,
             my_messages = g_new (GSocketControlMessage *, (allocated + 1));
             allocated = 1;
           }
-        
+
         my_messages[index++] = message;
       }
 



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