GDBusWorker: move flush async op into continue_writing()
[platform/upstream/glib.git] / gio / gsocketconnection.c
index c14e3c1..6f2f102 100644 (file)
@@ -37,7 +37,6 @@
 #include "gtcpconnection.h"
 #include "glibintl.h"
 
-#include "gioalias.h"
 
 /**
  * SECTION:gsocketconnection
  * can be created either by #GSocketClient when connecting to a host,
  * or by #GSocketListener when accepting a new client.
  *
- * The type of the #GSocketConnection object returned from these calls depends
- * on the type of the underlying socket that is in use. For instance, for a
- * TCP/IP connection it will be a #GTcpConnection.
+ * The type of the #GSocketConnection object returned from these calls
+ * depends on the type of the underlying socket that is in use. For
+ * instance, for a TCP/IP connection it will be a #GTcpConnection.
  *
- * Chosing what type of object to construct is done with the socket connection
- * factory, and it is possible for 3rd parties to register custom socket connection
- * types for specific combination of socket family/type/protocol using
- * g_socket_connection_factory_register_type().
+ * Choosing what type of object to construct is done with the socket
+ * connection factory, and it is possible for 3rd parties to register
+ * custom socket connection types for specific combination of socket
+ * family/type/protocol using g_socket_connection_factory_register_type().
  *
  * Since: 2.22
- **/
+ */
 
-G_DEFINE_TYPE (GSocketConnection,
-              g_socket_connection, G_TYPE_IO_STREAM);
+G_DEFINE_TYPE (GSocketConnection, g_socket_connection, G_TYPE_IO_STREAM);
 
 enum
 {
@@ -75,6 +73,8 @@ struct _GSocketConnectionPrivate
   GSocket       *socket;
   GInputStream  *input_stream;
   GOutputStream *output_stream;
+
+  gboolean       in_dispose;
 };
 
 static gboolean g_socket_connection_close         (GIOStream            *stream,
@@ -115,16 +115,16 @@ g_socket_connection_get_output_stream (GIOStream *io_stream)
 
 /**
  * g_socket_connection_get_socket:
- * @connection: a #GSocketConnection.
+ * @connection: a #GSocketConnection
  *
  * Gets the underlying #GSocket object of the connection.
  * This can be useful if you want to do something unusual on it
  * not supported by the #GSocketConnection APIs.
  *
- * Returns: a #GSocketAddress or %NULL on error.
+ * Returns: (transfer none): a #GSocketAddress or %NULL on error.
  *
  * Since: 2.22
- **/
+ */
 GSocket *
 g_socket_connection_get_socket (GSocketConnection *connection)
 {
@@ -135,45 +135,47 @@ g_socket_connection_get_socket (GSocketConnection *connection)
 
 /**
  * g_socket_connection_get_local_address:
- * @connection: a #GSocketConnection.
+ * @connection: a #GSocketConnection
  * @error: #GError for error reporting, or %NULL to ignore.
  *
  * Try to get the local address of a socket connection.
  *
- * Returns: a #GSocketAddress or %NULL on error.
+ * Returns: (transfer full): a #GSocketAddress or %NULL on error.
  *     Free the returned object with g_object_unref().
  *
  * Since: 2.22
- **/
+ */
 GSocketAddress *
 g_socket_connection_get_local_address (GSocketConnection  *connection,
-                                      GError  **error)
+                                      GError            **error)
 {
   return g_socket_get_local_address (connection->priv->socket, error);
 }
 
 /**
  * g_socket_connection_get_remote_address:
- * @connection: a #GSocketConnection.
+ * @connection: a #GSocketConnection
  * @error: #GError for error reporting, or %NULL to ignore.
  *
- * Try to get the remove address of a socket connection.
+ * Try to get the remote address of a socket connection.
  *
- * Returns: a #GSocketAddress or %NULL on error.
+ * Returns: (transfer full): a #GSocketAddress or %NULL on error.
  *     Free the returned object with g_object_unref().
  *
  * Since: 2.22
- **/
+ */
 GSocketAddress *
 g_socket_connection_get_remote_address (GSocketConnection  *connection,
-                                       GError  **error)
+                                       GError            **error)
 {
   return g_socket_get_remote_address (connection->priv->socket, error);
 }
 
 static void
-g_socket_connection_get_property (GObject *object, guint prop_id,
-                                  GValue *value, GParamSpec *pspec)
+g_socket_connection_get_property (GObject    *object,
+                                  guint       prop_id,
+                                  GValue     *value,
+                                  GParamSpec *pspec)
 {
   GSocketConnection *connection = G_SOCKET_CONNECTION (object);
 
@@ -189,8 +191,10 @@ g_socket_connection_get_property (GObject *object, guint prop_id,
 }
 
 static void
-g_socket_connection_set_property (GObject *object, guint prop_id,
-                                  const GValue *value, GParamSpec *pspec)
+g_socket_connection_set_property (GObject      *object,
+                                  guint         prop_id,
+                                  const GValue *value,
+                                  GParamSpec   *pspec)
 {
   GSocketConnection *connection = G_SOCKET_CONNECTION (object);
 
@@ -214,6 +218,19 @@ g_socket_connection_constructed (GObject *object)
 }
 
 static void
+g_socket_connection_dispose (GObject *object)
+{
+  GSocketConnection *connection = G_SOCKET_CONNECTION (object);
+
+  connection->priv->in_dispose = TRUE;
+
+  G_OBJECT_CLASS (g_socket_connection_parent_class)
+    ->dispose (object);
+
+  connection->priv->in_dispose = FALSE;
+}
+
+static void
 g_socket_connection_finalize (GObject *object)
 {
   GSocketConnection *connection = G_SOCKET_CONNECTION (object);
@@ -242,6 +259,7 @@ g_socket_connection_class_init (GSocketConnectionClass *klass)
   gobject_class->get_property = g_socket_connection_get_property;
   gobject_class->constructed = g_socket_connection_constructed;
   gobject_class->finalize = g_socket_connection_finalize;
+  gobject_class->dispose = g_socket_connection_dispose;
 
   stream_class->get_input_stream = g_socket_connection_get_input_stream;
   stream_class->get_output_stream = g_socket_connection_get_output_stream;
@@ -249,12 +267,15 @@ g_socket_connection_class_init (GSocketConnectionClass *klass)
   stream_class->close_async = g_socket_connection_close_async;
   stream_class->close_finish = g_socket_connection_close_finish;
 
-  g_object_class_install_property (gobject_class, PROP_SOCKET,
-    g_param_spec_object ("socket",
-                        P_("Socket"),
-                        P_("The underlying GSocket"),
-                         G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
-                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (gobject_class,
+                                   PROP_SOCKET,
+                                   g_param_spec_object ("socket",
+                                                       P_("Socket"),
+                                                       P_("The underlying GSocket"),
+                                                        G_TYPE_SOCKET,
+                                                        G_PARAM_CONSTRUCT_ONLY |
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_STATIC_STRINGS));
 }
 
 static void
@@ -266,9 +287,9 @@ g_socket_connection_init (GSocketConnection *connection)
 }
 
 static gboolean
-g_socket_connection_close (GIOStream            *stream,
-                          GCancellable         *cancellable,
-                          GError              **error)
+g_socket_connection_close (GIOStream     *stream,
+                          GCancellable  *cancellable,
+                          GError       **error)
 {
   GSocketConnection *connection = G_SOCKET_CONNECTION (stream);
 
@@ -279,16 +300,25 @@ g_socket_connection_close (GIOStream            *stream,
     g_input_stream_close (connection->priv->input_stream,
                          cancellable, NULL);
 
+  /* Don't close the underlying socket if this is being called
+   * as part of dispose(); when destroying the GSocketConnection,
+   * we only want to close the socket if we're holding the last
+   * reference on it, and in that case it will close itself when
+   * we unref it in finalize().
+   */
+  if (connection->priv->in_dispose)
+    return TRUE;
+
   return g_socket_close (connection->priv->socket, error);
 }
 
 
 static void
-g_socket_connection_close_async (GIOStream        *stream,
-                                int               io_priority,
-                                GCancellable     *cancellable,
-                                GAsyncReadyCallback callback,
-                                gpointer          user_data)
+g_socket_connection_close_async (GIOStream           *stream,
+                                int                  io_priority,
+                                GCancellable        *cancellable,
+                                GAsyncReadyCallback  callback,
+                                gpointer             user_data)
 {
   GSimpleAsyncResult *res;
   GIOStreamClass *class;
@@ -301,10 +331,9 @@ g_socket_connection_close_async (GIOStream        *stream,
   if (class->close_fn &&
       !class->close_fn (stream, cancellable, &error))
     {
-      g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+      g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
                                            callback, user_data,
                                            error);
-      g_error_free (error);
       return;
     }
 
@@ -317,7 +346,7 @@ g_socket_connection_close_async (GIOStream        *stream,
 }
 
 static gboolean
-g_socket_connection_close_finish (GIOStream  *stream,
+g_socket_connection_close_finish (GIOStream     *stream,
                                  GAsyncResult  *result,
                                  GError       **error)
 {
@@ -369,23 +398,23 @@ G_LOCK_DEFINE_STATIC(connection_factories);
 
 /**
  * g_socket_connection_factory_register_type:
- * @g_type: a #GType, inheriting from G_SOCKET_CONNECTION
- * @family: a #GSocketFamily.
+ * @g_type: a #GType, inheriting from %G_TYPE_SOCKET_CONNECTION
+ * @family: a #GSocketFamily
  * @type: a #GSocketType
  * @protocol: a protocol id
  *
  * Looks up the #GType to be used when creating socket connections on
- * sockets with the specified @family,@type and @protocol.
+ * sockets with the specified @family, @type and @protocol.
  *
  * If no type is registered, the #GSocketConnection base type is returned.
  *
  * Since: 2.22
- **/
+ */
 void
-g_socket_connection_factory_register_type (GType g_type,
+g_socket_connection_factory_register_type (GType         g_type,
                                           GSocketFamily family,
-                                          GSocketType type,
-                                          gint protocol)
+                                          GSocketType   type,
+                                          gint          protocol)
 {
   ConnectionFactory *factory;
 
@@ -419,26 +448,28 @@ init_builtin_types (void)
   a_type = g_unix_connection_get_type ();
 #endif
   a_type = g_tcp_connection_get_type ();
+  (a_type); /* To avoid -Wunused-but-set-variable */
 }
 
 /**
  * g_socket_connection_factory_lookup_type:
- * @family: a #GSocketFamily.
+ * @family: a #GSocketFamily
  * @type: a #GSocketType
  * @protocol_id: a protocol id
  *
  * Looks up the #GType to be used when creating socket connections on
- * sockets with the specified @family,@type and @protocol_id.
+ * sockets with the specified @family, @type and @protocol_id.
  *
  * If no type is registered, the #GSocketConnection base type is returned.
  *
  * Returns: a #GType
+ *
  * Since: 2.22
- **/
+ */
 GType
 g_socket_connection_factory_lookup_type (GSocketFamily family,
-                                        GSocketType type,
-                                        gint protocol_id)
+                                        GSocketType   type,
+                                        gint          protocol_id)
 {
   ConnectionFactory *factory, key;
   GType g_type;
@@ -467,15 +498,15 @@ g_socket_connection_factory_lookup_type (GSocketFamily family,
 
 /**
  * g_socket_connection_factory_create_connection:
- * @socket: a #GSocket.
+ * @socket: a #GSocket
  *
  * Creates a #GSocketConnection subclass of the right type for
  * @socket.
  *
- * Returns: a #GSocketConnection
+ * Returns: (transfer full): a #GSocketConnection
  *
  * Since: 2.22
- **/
+ */
 GSocketConnection *
 g_socket_connection_factory_create_connection (GSocket *socket)
 {
@@ -483,9 +514,6 @@ g_socket_connection_factory_create_connection (GSocket *socket)
 
   type = g_socket_connection_factory_lookup_type (g_socket_get_family (socket),
                                                  g_socket_get_socket_type (socket),
-                                                 g_socket_get_protocol_id (socket));
+                                                 g_socket_get_protocol (socket));
   return g_object_new (type, "socket", socket, NULL);
 }
-
-#define __G_SOCKET_CONNECTION_C__
-#include "gioaliasdef.c"