[librest/wip/baedert/tests] RestProxyCall: gio-ify _continuous



commit 367f96f1d161fbc94afcb78881ed82790e6d9403
Author: Timm Bäder <mail baedert org>
Date:   Tue Jul 26 09:47:28 2016 +0200

    RestProxyCall: gio-ify _continuous

 rest/rest-proxy-call.c |  158 ++++++++++++++++-------------------------------
 rest/rest-proxy-call.h |   17 +++---
 2 files changed, 62 insertions(+), 113 deletions(-)
---
diff --git a/rest/rest-proxy-call.c b/rest/rest-proxy-call.c
index ecbcd90..9d8f03e 100644
--- a/rest/rest-proxy-call.c
+++ b/rest/rest-proxy-call.c
@@ -32,7 +32,6 @@
 struct _RestProxyCallAsyncClosure {
   RestProxyCall *call;
   RestProxyCallAsyncCallback callback;
-  GObject *weak_object;
   gpointer userdata;
   SoupMessage *message;
 };
@@ -41,7 +40,6 @@ typedef struct _RestProxyCallAsyncClosure RestProxyCallAsyncClosure;
 struct _RestProxyCallContinuousClosure {
   RestProxyCall *call;
   RestProxyCallContinuousCallback callback;
-  GObject *weak_object;
   gpointer userdata;
   SoupMessage *message;
 };
@@ -50,7 +48,6 @@ typedef struct _RestProxyCallContinuousClosure RestProxyCallContinuousClosure;
 struct _RestProxyCallUploadClosure {
   RestProxyCall *call;
   RestProxyCallUploadCallback callback;
-  GObject *weak_object;
   gpointer userdata;
   SoupMessage *message;
   gsize uploaded;
@@ -532,11 +529,6 @@ rest_proxy_call_get_params (RestProxyCall *call)
   return GET_PRIVATE (call)->params;
 }
 
-
-
-static void _call_async_weak_notify_cb (gpointer *data,
-                                        GObject  *dead_object);
-
 static void
 _populate_headers_hash_table (const gchar *name,
                               const gchar *value,
@@ -645,14 +637,15 @@ finish_call (RestProxyCall *call, SoupMessage *message, GError **error)
 static void
 _continuous_call_message_completed_cb (SoupSession *session,
                                        SoupMessage *message,
-                                       gpointer     userdata)
+                                       gpointer     user_data)
 {
+  GTask *task = user_data;
   RestProxyCallContinuousClosure *closure;
   RestProxyCall *call;
   RestProxyCallPrivate *priv;
   GError *error = NULL;
 
-  closure = (RestProxyCallContinuousClosure *)userdata;
+  closure = (RestProxyCallContinuousClosure *)g_task_get_task_data (task);
   call = closure->call;
   priv = GET_PRIVATE (call);
 
@@ -661,39 +654,14 @@ _continuous_call_message_completed_cb (SoupSession *session,
 
   _handle_error_from_message (message, &error);
 
-  closure->callback (closure->call,
-                     NULL,
-                     0,
-                     error,
-                     closure->weak_object,
-                     closure->userdata);
-
-  g_clear_error (&error);
-
-  /* Success. We don't need the weak reference any more */
-  if (closure->weak_object)
-  {
-    g_object_weak_unref (closure->weak_object,
-        (GWeakNotify)_call_async_weak_notify_cb,
-        closure);
-  }
+  if (error != NULL)
+    g_task_return_error (task, error);
+  else
+    g_task_return_boolean (task, TRUE);
 
   priv->cur_call_closure = NULL;
   g_object_unref (closure->call);
-  g_slice_free (RestProxyCallContinuousClosure, closure);
-}
-
-
-static void
-_call_async_weak_notify_cb (gpointer *data,
-                            GObject  *dead_object)
-{
-  RestProxyCallAsyncClosure *closure;
-
-  closure = (RestProxyCallAsyncClosure *)data;
-
-  /* Will end up freeing the closure */
-  _rest_proxy_call_cancel (closure->call);
+  g_object_unref (task);
 }
 
 static void
@@ -995,79 +963,76 @@ _continuous_call_message_got_chunk_cb (SoupMessage                    *msg,
                      chunk->data,
                      chunk->length,
                      NULL,
-                     closure->weak_object,
                      closure->userdata);
 }
 
 
 /**
- * rest_proxy_call_continuous: (skip)
+ * rest_proxy_call_continuous:
  * @call: The #RestProxyCall
- * @callback: a #RestProxyCallContinuousCallback to invoke when data is available
- * @weak_object: The #GObject to weakly reference and tie the lifecycle to
- * @userdata: (closure): data to pass to @callback
- * @error: (out) (allow-none): a #GError, or %NULL
+ * @continuous_callback: a #RestProxyCallContinuousCallback to invoke when data is available
+ * @cancellable: (nullable): a #GCancellable that can be used to cancel this call, or %NULL
+ * @user_data: (closure): data to pass to @callback
  *
  * Asynchronously invoke @call but expect a continuous stream of content. This
  * means that the body data will not be accumulated and thus you cannot use
  * rest_proxy_call_get_payload()
  *
- * When there is data @callback will be called and when the connection is
+ * When there is data @continuous_callback will be called and when the connection is
  * closed or the stream ends @callback will also be called.
- *
- * If @weak_object is disposed during the call then this call will be
- * cancelled. If the call is cancelled then the callback will be invoked with
- * an error state.
- *
- * You may unref the call after calling this function since there is an
- * internal reference, or you may unref in the callback.
- *
- * Returns: %TRUE on success, %FALSE on failure, in which case
- *   @error will be set.
  */
-gboolean
+void
 rest_proxy_call_continuous (RestProxyCall                    *call,
-                            RestProxyCallContinuousCallback   callback,
-                            GObject                          *weak_object,
-                            gpointer                          userdata,
-                            GError                          **error)
+                            RestProxyCallContinuousCallback   continuous_callback,
+                            GCancellable                     *cancellable,
+                            GAsyncReadyCallback               callback,
+                            gpointer                          user_data)
 {
   RestProxyCallPrivate *priv = GET_PRIVATE (call);
+  GTask *task;
+  GError *error = NULL;
   SoupMessage *message;
   RestProxyCallContinuousClosure *closure;
 
-  g_return_val_if_fail (REST_IS_PROXY_CALL (call), FALSE);
+  g_return_if_fail (REST_IS_PROXY_CALL (call));
+  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
   g_assert (priv->proxy);
 
+  task = g_task_new (call, cancellable, callback, user_data);
+
   if (priv->cur_call_closure)
   {
     g_warning (G_STRLOC ": re-use of RestProxyCall %p, don't do this", call);
-    return FALSE;
+    g_task_return_boolean (task, FALSE);
+    return;
   }
 
-  message = prepare_message (call, error);
+  message = prepare_message (call, &error);
   if (message == NULL)
-    return FALSE;
+    {
+      g_task_return_error (task, error);
+      return;
+    }
 
   /* Must turn off accumulation */
   soup_message_body_set_accumulate (message->response_body, FALSE);
 
-  closure = g_slice_new0 (RestProxyCallContinuousClosure);
+  closure = g_malloc (sizeof (RestProxyCallContinuousClosure));
   closure->call = g_object_ref (call);
-  closure->callback = callback;
-  closure->weak_object = weak_object;
+  closure->callback = continuous_callback;
   closure->message = message;
-  closure->userdata = userdata;
+  closure->userdata = user_data;
 
   priv->cur_call_closure = (RestProxyCallAsyncClosure *)closure;
 
-  /* Weakly reference this object. We remove our callback if it goes away. */
-  if (closure->weak_object)
-  {
-    g_object_weak_ref (closure->weak_object,
-        (GWeakNotify)_call_async_weak_notify_cb,
-        closure);
-  }
+  g_task_set_task_data (task, closure, g_free);
+
+  if (cancellable != NULL)
+    {
+      priv->cancel_sig = g_signal_connect (cancellable, "cancelled",
+          G_CALLBACK (_call_message_call_cancelled_cb), call);
+      priv->cancellable = g_object_ref (cancellable);
+    }
 
   g_signal_connect (message,
                     "got-chunk",
@@ -1077,8 +1042,18 @@ rest_proxy_call_continuous (RestProxyCall                    *call,
   _rest_proxy_queue_message (priv->proxy,
                              message,
                              _continuous_call_message_completed_cb,
-                             closure);
-  return TRUE;
+                             task);
+}
+
+gboolean
+rest_proxy_call_continuous_finish (RestProxyCall *call,
+                                   GAsyncResult  *result,
+                                   GError        **error)
+{
+  g_return_val_if_fail (REST_IS_PROXY_CALL (call), FALSE);
+  g_return_val_if_fail (g_task_is_valid (result, call), FALSE);
+
+  return g_task_propagate_boolean (G_TASK (result), error);
 }
 
 static void
@@ -1104,19 +1079,10 @@ _upload_call_message_completed_cb (SoupSession *session,
                      closure->uploaded,
                      closure->uploaded,
                      error,
-                     closure->weak_object,
                      closure->userdata);
 
   g_clear_error (&error);
 
-  /* Success. We don't need the weak reference any more */
-  if (closure->weak_object)
-  {
-    g_object_weak_unref (closure->weak_object,
-        (GWeakNotify)_call_async_weak_notify_cb,
-        closure);
-  }
-
   priv->cur_call_closure = NULL;
   g_object_unref (closure->call);
   g_slice_free (RestProxyCallUploadClosure, closure);
@@ -1134,7 +1100,6 @@ _upload_call_message_wrote_data_cb (SoupMessage                *msg,
                        msg->request_body->length,
                        closure->uploaded,
                        NULL,
-                       closure->weak_object,
                        closure->userdata);
 }
 
@@ -1143,7 +1108,6 @@ _upload_call_message_wrote_data_cb (SoupMessage                *msg,
  * @call: The #RestProxyCall
  * @callback: (scope async): a #RestProxyCallUploadCallback to invoke when a chunk
  *   of data was uploaded
- * @weak_object: The #GObject to weakly reference and tie the lifecycle to
  * @userdata: data to pass to @callback
  * @error: a #GError, or %NULL
  *
@@ -1153,10 +1117,6 @@ _upload_call_message_wrote_data_cb (SoupMessage                *msg,
  * When the callback is invoked with the uploaded byte count equaling the message
  * byte count, the call has completed.
  *
- * If @weak_object is disposed during the call then this call will be
- * cancelled. If the call is cancelled then the callback will be invoked with
- * an error state.
- *
  * You may unref the call after calling this function since there is an
  * internal reference, or you may unref in the callback.
  *
@@ -1166,7 +1126,6 @@ _upload_call_message_wrote_data_cb (SoupMessage                *msg,
 gboolean
 rest_proxy_call_upload (RestProxyCall                *call,
                         RestProxyCallUploadCallback   callback,
-                        GObject                      *weak_object,
                         gpointer                      userdata,
                         GError                      **error)
 {
@@ -1190,21 +1149,12 @@ rest_proxy_call_upload (RestProxyCall                *call,
   closure = g_slice_new0 (RestProxyCallUploadClosure);
   closure->call = g_object_ref (call);
   closure->callback = callback;
-  closure->weak_object = weak_object;
   closure->message = message;
   closure->userdata = userdata;
   closure->uploaded = 0;
 
   priv->cur_call_closure = (RestProxyCallAsyncClosure *)closure;
 
-  /* Weakly reference this object. We remove our callback if it goes away. */
-  if (closure->weak_object)
-  {
-    g_object_weak_ref (closure->weak_object,
-        (GWeakNotify)_call_async_weak_notify_cb,
-        closure);
-  }
-
   g_signal_connect (message,
                     "wrote-body-data",
                     (GCallback) _upload_call_message_wrote_data_cb,
diff --git a/rest/rest-proxy-call.h b/rest/rest-proxy-call.h
index f8ff714..4c09bd1 100644
--- a/rest/rest-proxy-call.h
+++ b/rest/rest-proxy-call.h
@@ -150,7 +150,6 @@ RestParams *rest_proxy_call_get_params (RestProxyCall *call);
 
 typedef void (*RestProxyCallAsyncCallback)(RestProxyCall *call,
                                            const GError  *error,
-                                           GObject       *weak_object,
                                            gpointer       userdata);
 
 void rest_proxy_call_invoke_async (RestProxyCall       *call,
@@ -166,25 +165,25 @@ typedef void (*RestProxyCallContinuousCallback) (RestProxyCall *call,
                                                  const gchar   *buf,
                                                  gsize          len,
                                                  const GError  *error,
-                                                 GObject       *weak_object,
                                                  gpointer       userdata);
 
-gboolean rest_proxy_call_continuous (RestProxyCall                    *call,
-                                     RestProxyCallContinuousCallback   callback,
-                                     GObject                          *weak_object,
-                                     gpointer                          userdata,
-                                     GError                          **error);
+void rest_proxy_call_continuous (RestProxyCall                    *call,
+                                 RestProxyCallContinuousCallback   continuous_callback,
+                                 GCancellable                     *cancellable,
+                                 GAsyncReadyCallback               callback,
+                                 gpointer                          user_data);
+gboolean rest_proxy_call_continuous_finish (RestProxyCall *call,
+                                            GAsyncResult  *result,
+                                            GError        **error);
 
 typedef void (*RestProxyCallUploadCallback) (RestProxyCall *call,
                                              gsize          total,
                                              gsize          uploaded,
                                              const GError  *error,
-                                             GObject       *weak_object,
                                              gpointer       userdata);
 
 gboolean rest_proxy_call_upload (RestProxyCall                *call,
                                  RestProxyCallUploadCallback   callback,
-                                 GObject                      *weak_object,
                                  gpointer                      userdata,
                                  GError                      **error);
 


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