Fixed problem with msg destination in gkdbus.c. Cleanup code.
[platform/upstream/glib.git] / gio / gdbusprivate.c
index 6525db3..38be85d 100644 (file)
 #include "glib/gstdio.h"
 #include "gsocketcontrolmessage.h"
 #include "gsocketconnection.h"
-#include "gkdbusconnection.h"
 #include "gsocketoutputstream.h"
 
 #ifdef G_OS_UNIX
 #include "gunixfdmessage.h"
 #include "gunixconnection.h"
+#include "gkdbusconnection.h"
 #include "gunixcredentialsmessage.h"
 #endif
 
@@ -118,6 +118,29 @@ typedef struct
   gboolean from_mainloop;
 } ReadWithControlData;
 
+typedef struct
+{
+  GKdbus *kdbus;
+  GCancellable *cancellable;
+
+  void *buffer;
+  gsize count;
+
+  GSimpleAsyncResult *simple;
+
+  gboolean from_mainloop;
+} ReadKdbusData;
+
+static void
+read_kdbus_data_free (ReadKdbusData *data)
+{
+  //g_object_unref (data->kdbus); TODO
+  if (data->cancellable != NULL)
+    g_object_unref (data->cancellable);
+  g_object_unref (data->simple);
+  g_free (data);
+}
+
 static void
 read_with_control_data_free (ReadWithControlData *data)
 {
@@ -129,6 +152,38 @@ read_with_control_data_free (ReadWithControlData *data)
 }
 
 static gboolean
+_g_kdbus_read_ready (GKdbus      *kdbus,
+                     GIOCondition  condition,
+                     gpointer      user_data)
+{
+  ReadKdbusData *data = user_data;
+  GError *error;
+  gssize result;
+  
+  error = NULL;
+  
+  result = g_kdbus_receive (data->kdbus,
+                            data->buffer,
+                            &error);
+  if (result >= 0)
+    {
+      g_simple_async_result_set_op_res_gssize (data->simple, result);
+    }
+  else
+    {
+      g_assert (error != NULL);
+      g_simple_async_result_take_error (data->simple, error);
+    }
+
+  if (data->from_mainloop)
+    g_simple_async_result_complete (data->simple);
+  else
+    g_simple_async_result_complete_in_idle (data->simple);
+
+  return FALSE;
+}
+
+static gboolean
 _g_socket_read_with_control_messages_ready (GSocket      *socket,
                                             GIOCondition  condition,
                                             gpointer      user_data)
@@ -169,6 +224,41 @@ _g_socket_read_with_control_messages_ready (GSocket      *socket,
 }
 
 static void
+_g_kdbus_read (GKdbus                  *kdbus,
+               void                    *buffer,
+               gsize                    count,
+               GCancellable            *cancellable,
+               GAsyncReadyCallback      callback,
+               gpointer                 user_data)
+{
+  ReadKdbusData *data;
+  GSource *source;
+
+  data = g_new0 (ReadKdbusData, 1);
+  data->kdbus = kdbus; /*g_object_ref (socket);*/
+  data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
+  data->buffer = buffer;
+  data->count = count;
+
+  data->simple = g_simple_async_result_new (G_OBJECT (kdbus),
+                                            callback,
+                                            user_data,
+                                            _g_kdbus_read);
+  g_simple_async_result_set_check_cancellable (data->simple, cancellable);
+
+  data->from_mainloop = TRUE;
+  source = g_kdbus_create_source (data->kdbus,
+                                   G_IO_IN | G_IO_HUP | G_IO_ERR,
+                                   cancellable);
+  g_source_set_callback (source,
+                         (GSourceFunc) _g_kdbus_read_ready,
+                         data,
+                         (GDestroyNotify) read_kdbus_data_free);
+  g_source_attach (source, g_main_context_get_thread_default ());
+  g_source_unref (source);
+}
+
+static void
 _g_socket_read_with_control_messages (GSocket                 *socket,
                                       void                    *buffer,
                                       gsize                    count,
@@ -217,6 +307,22 @@ _g_socket_read_with_control_messages (GSocket                 *socket,
 }
 
 static gssize
+_g_kdbus_read_finish (GKdbus       *kdbus,
+                                             GAsyncResult  *result,
+                                             GError       **error)
+{
+  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
+
+  g_return_val_if_fail (G_IS_KDBUS (kdbus), -1);
+  g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == _g_kdbus_read);
+
+  if (g_simple_async_result_propagate_error (simple, error))
+      return -1;
+  else
+    return g_simple_async_result_get_op_res_gssize (simple);
+}
+
+static gssize
 _g_socket_read_with_control_messages_finish (GSocket       *socket,
                                              GAsyncResult  *result,
                                              GError       **error)
@@ -586,7 +692,12 @@ _g_dbus_worker_do_read_cb (GInputStream  *input_stream,
     goto out;
 
   error = NULL;
-  if (worker->socket == NULL)
+  if (G_IS_KDBUS_CONNECTION (worker->stream))
+    {
+      bytes_read = _g_kdbus_read_finish (worker->kdbus,
+                                         res,
+                                         &error);
+  } else if (worker->socket == NULL)
     bytes_read = g_input_stream_read_finish (g_io_stream_get_input_stream (worker->stream),
                                              res,
                                              &error);
@@ -724,6 +835,11 @@ _g_dbus_worker_do_read_cb (GInputStream  *input_stream,
   read_message_print_transport_debug (bytes_read, worker);
 
   worker->read_buffer_cur_size += bytes_read;
+
+  /* TODO: [KDBUS] Sprawdzic pole read_buffer_bytes_wanted */
+  if (G_IS_KDBUS_CONNECTION (worker->stream))
+    worker->read_buffer_bytes_wanted = worker->read_buffer_cur_size;
+
   if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size)
     {
       /* OK, got what we asked for! */
@@ -848,7 +964,17 @@ _g_dbus_worker_do_read_unlocked (GDBusWorker *worker)
       worker->read_buffer = g_realloc (worker->read_buffer, worker->read_buffer_allocated_size);
     }
 
-  if (worker->socket == NULL)
+  if (G_IS_KDBUS_CONNECTION (worker->stream))
+    {
+      _g_kdbus_read(worker->kdbus, 
+                    worker->read_buffer,
+                    worker->read_buffer_bytes_wanted,
+                                            worker->cancellable,
+                                            (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
+                                            _g_dbus_worker_ref (worker));
+      
+
+  } else if (worker->socket == NULL)
     g_input_stream_read_async (g_io_stream_get_input_stream (worker->stream),
                                worker->read_buffer + worker->read_buffer_cur_size,
                                worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
@@ -876,11 +1002,11 @@ _g_dbus_worker_do_read_unlocked (GDBusWorker *worker)
 static gboolean
 _g_dbus_worker_do_initial_read (gpointer data)
 {
-  //GDBusWorker *worker = data;
-  //g_mutex_lock (&worker->read_lock);
-  //_g_dbus_worker_do_read_unlocked (worker);
-  //g_mutex_unlock (&worker->read_lock);
-  //return FALSE;
+  GDBusWorker *worker = data;
+  g_mutex_lock (&worker->read_lock);
+  _g_dbus_worker_do_read_unlocked (worker);
+  g_mutex_unlock (&worker->read_lock);
+  return FALSE;
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -996,13 +1122,11 @@ write_message_continue_writing (MessageToWriteData *data)
       GError *error;
       error = NULL;
       data->total_written = g_kdbus_send_message(data->worker, data->worker->kdbus, data->message, data->blob, data->blob_size, &error);
-      
-      if (data->total_written == data->blob_size)
-        {
-          g_simple_async_result_complete (simple);
-          g_object_unref (simple);
-          goto out;
-        }
+    
+      write_message_print_transport_debug (data->total_written, data);  
+      g_simple_async_result_complete (simple);
+      g_object_unref (simple);
+      goto out;
     }
   else
     {
@@ -1652,10 +1776,6 @@ _g_dbus_worker_send_message (GDBusWorker    *worker,
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
-GKdbus*        g_dbus_worker_get_kdbus    (GDBusWorker  *worker)
-{
-  return worker->kdbus;
-}
 
 GDBusWorker *
 _g_dbus_worker_new (GIOStream                              *stream,
@@ -2178,12 +2298,11 @@ write_message_print_transport_debug (gssize bytes_written,
   g_print ("========================================================================\n"
            "GDBus-debug:Transport:\n"
            "  >>>> WROTE %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
-           "       size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT " on a %s\n",
+           "       size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT "\n",
            bytes_written,
            g_dbus_message_get_serial (data->message),
            data->blob_size,
-           data->total_written,
-           g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
+           data->total_written);
   _g_dbus_debug_print_unlock ();
  out:
   ;
@@ -2229,12 +2348,11 @@ read_message_print_transport_debug (gssize bytes_read,
   g_print ("========================================================================\n"
            "GDBus-debug:Transport:\n"
            "  <<<< READ %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
-           "       size %d to offset %" G_GSIZE_FORMAT " from a %s\n",
+           "       size %d to offset %" G_GSIZE_FORMAT "\n",
            bytes_read,
            serial,
            message_length,
-           worker->read_buffer_cur_size,
-           g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))));
+           worker->read_buffer_cur_size);
   _g_dbus_debug_print_unlock ();
  out:
   ;