[glib-networking/mcatanzaro/tls-thread: 5/19] progress



commit 1ddd56856aa9f2d7ef02d9ce6754492107158acf
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Wed Jul 24 16:50:09 2019 -0500

    progress

 tls/base/gtlsconnection-base.c |  3 +-
 tls/base/gtlsconnection-base.h |  1 +
 tls/base/gtlsthread.c          | 65 ++++++++++++++++++++++++++++--------------
 3 files changed, 46 insertions(+), 23 deletions(-)
---
diff --git a/tls/base/gtlsconnection-base.c b/tls/base/gtlsconnection-base.c
index 8e34883..63008da 100644
--- a/tls/base/gtlsconnection-base.c
+++ b/tls/base/gtlsconnection-base.c
@@ -1844,8 +1844,7 @@ g_tls_connection_base_read (GTlsConnectionBase  *tls,
         }
       else
         {
-          status = G_TLS_CONNECTION_BASE_GET_CLASS (tls)->
-            read_fn (tls, buffer, size, timeout, &nread, cancellable, error);
+          status = g_tls_thread_read (priv->thread, buffer, size, timeout, &nread, cancellable, error);
         }
 
       yield_op (tls, G_TLS_CONNECTION_BASE_OP_READ, status);
diff --git a/tls/base/gtlsconnection-base.h b/tls/base/gtlsconnection-base.h
index 3e27ad2..365e711 100644
--- a/tls/base/gtlsconnection-base.h
+++ b/tls/base/gtlsconnection-base.h
@@ -91,6 +91,7 @@ struct _GTlsConnectionBaseClass
                                                              gboolean              success,
                                                              GError              **error);
 
+  /* FIXME: must remove timeout parameters from all vfuncs, including handshake vfuncs */
   GTlsConnectionBaseStatus    (*read_fn)                    (GTlsConnectionBase   *tls,
                                                              void                 *buffer,
                                                              gsize                 size,
diff --git a/tls/base/gtlsthread.c b/tls/base/gtlsthread.c
index 9268632..98dd6c9 100644
--- a/tls/base/gtlsthread.c
+++ b/tls/base/gtlsthread.c
@@ -73,12 +73,15 @@ typedef enum {
 
 typedef struct {
   GTlsThreadOperationType type;
+  GTlsConnectionBase *connection; /* FIXME: threadsafety nightmare */
   void *data; /* unowned */
   gsize size;
   gint64 timeout;
   GCancellable *cancellable;
   GMainLoop *main_loop;
-  gint result;
+  GTlsConnectionBaseStatus result;
+  gssize count; /* Bytes read or written */
+  GError *error;
 } GTlsThreadOperation;
 
 enum
@@ -90,10 +93,11 @@ enum
 
 static GParamSpec *obj_properties[LAST_PROP];
 
-G_DEFINE_TYPE (GTlsThread, g_tls_thread, G_TYPE_TLS_THREAD)
+G_DEFINE_TYPE (GTlsThread, g_tls_thread, G_TYPE_OBJECT)
 
 static GTlsThreadOperation *
 g_tls_thread_operation_new (GTlsThreadOperationType  type,
+                            GTlsConnectionBase      *connection,
                             void                    *data,
                             gsize                    size,
                             gint64                   timeout,
@@ -102,14 +106,14 @@ g_tls_thread_operation_new (GTlsThreadOperationType  type,
 {
   GTlsThreadOperation *op;
 
-  op = g_new (GTlsThreadOperation, 1);
+  op = g_new0 (GTlsThreadOperation, 1);
   op->type = type;
+  op->connection = g_object_ref (connection);
   op->data = data;
   op->size = size;
   op->timeout = timeout;
   op->cancellable = g_object_ref (cancellable);
   op->main_loop = g_main_loop_ref (main_loop);
-  op->result = 0;
 
   return op;
 }
@@ -128,16 +132,18 @@ g_tls_thread_shutdown_operation_new (void)
 static void
 g_tls_thread_operation_free (GTlsThreadOperation *op)
 {
+  g_clear_object (&op->connection);
   g_clear_object (&op->cancellable);
   g_clear_pointer (&op->main_loop, g_main_loop_unref);
   g_free (op);
 }
 
-gssize
+GTlsConnectionBaseStatus
 g_tls_thread_read (GTlsThread    *self,
                    void          *buffer,
                    gsize          size,
                    gint64         timeout,
+                   gssize        *nread,
                    GCancellable  *cancellable,
                    GError       **error)
 {
@@ -148,19 +154,25 @@ g_tls_thread_read (GTlsThread    *self,
   main_context = g_main_context_new ();
   main_loop = g_main_loop_new (main_context, FALSE);
   op = g_tls_thread_operation_new (G_TLS_THREAD_OP_READ,
+                                   self->connection,
                                    buffer, size, timeout,
                                    cancellable, main_loop);
   g_async_queue_push (self->queue, op);
 
   g_main_loop_run (main_loop);
 
-  /* FIXME: do something with op->result */
+  *nread = op->count;
+
+  if (op->error)
+    {
+      g_propagate_error (error, op->error);
+      op->error = NULL;
+    }
 
   g_main_context_unref (main_context);
   g_main_loop_unref (main_loop);
 
-  /* FIXME: return something */
-  return 0;
+  return op->result;
 }
 
 static gpointer
@@ -178,8 +190,13 @@ tls_thread (gpointer data)
       switch (op->type)
         {
         case G_TLS_THREAD_OP_READ:
-          /* FIXME: handle this */
-          /* FIXME: must respect timeout somehow */
+          /* FIXME: this is not async when timeout != 0 */
+          op->result = G_TLS_CONNECTION_BASE_GET_CLASS (op->connection)->read_fn (op->connection,
+                                                                                  op->data, op->size,
+                                                                                  op->timeout,
+                                                                                  &op->count,
+                                                                                  op->cancellable,
+                                                                                  &op->error);
           break;
         case G_TLS_THREAD_OP_SHUTDOWN:
           break;
@@ -208,6 +225,7 @@ g_tls_thread_get_property (GObject    *object,
   switch (prop_id)
     {
     case PROP_TLS_CONNECTION:
+      g_assert (self->connection);
       g_value_set_object (value, self->connection);
       break;
 
@@ -228,6 +246,13 @@ g_tls_thread_set_property (GObject      *object,
     {
     case PROP_TLS_CONNECTION:
       self->connection = g_value_get_object (value);
+
+      /* This weak pointer is not required for correctness, because the
+       * GTlsThread should never outlive its GTlsConnection. It's only here
+       * as a sanity-check and debugging aid, to ensure self->connection
+       * isn't ever dangling.
+       */
+      g_object_add_weak_pointer (G_OBJECT (self->connection), (gpointer *)&self->connection);
       break;
 
     default:
@@ -243,18 +268,17 @@ g_tls_thread_init (GTlsThread *self)
 }
 
 static void
-g_tls_thread_dispose (GObject *object)
+g_tls_thread_finalize (GObject *object)
 {
   GTlsThread *self = G_TLS_THREAD (object);
 
-  if (self->queue)
-    {
-      g_async_queue_push (self->queue, g_tls_thread_shutdown_operation_new ());
-      g_clear_pointer (&self->thread, g_thread_join);
-      g_clear_pointer (&self->queue, g_async_queue_unref);
-    }
+  g_object_remove_weak_pointer (G_OBJECT (self->connection), (gpointer *)&self->connection);
 
-  G_OBJECT_CLASS (g_tls_thread_parent_class)->dispose (object);
+  g_async_queue_push (self->queue, g_tls_thread_shutdown_operation_new ());
+  g_clear_pointer (&self->thread, g_thread_join);
+  g_clear_pointer (&self->queue, g_async_queue_unref);
+
+  G_OBJECT_CLASS (g_tls_thread_parent_class)->finalize (object);
 }
 
 static void
@@ -262,7 +286,7 @@ g_tls_thread_class_init (GTlsThreadClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
-  gobject_class->dispose = g_tls_thread_dispose;
+  gobject_class->finalize     = g_tls_thread_finalize;
   gobject_class->get_property = g_tls_thread_get_property;
   gobject_class->set_property = g_tls_thread_set_property;
 
@@ -271,7 +295,7 @@ g_tls_thread_class_init (GTlsThreadClass *klass)
                          "TLS Connection",
                          "The thread's GTlsConnection",
                          G_TYPE_TLS_CONNECTION_BASE,
-                         G_PARAM_READABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+                         G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | 
G_PARAM_STATIC_STRINGS);
 
   g_object_class_install_properties (gobject_class, LAST_PROP, obj_properties);
 }
@@ -284,6 +308,5 @@ g_tls_thread_new (GTlsConnectionBase *tls)
   thread = g_object_new (G_TYPE_TLS_THREAD,
                          "tls-connection", tls,
                          NULL);
-
   return thread;
 }


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