<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:arial,helvetica,sans-serif;font-size:12pt">Hi,<br>I want to pass an image file which is around 10K using dbus glib bindings. I tried to find about it and the only data type that seems to suite my need is GArray. However debugging through the code, i found out that the actual message size is four times the size of my image file. That i think is because dbus maps GArray to DBUS_TYPE_ARRAY. GArray has a pointer of gchar * while DBUS_TYPE_ARRAY is an array of uint32. I guess that the reason of the message size increasing by 4 times as 3 NULL bytes are appended to every byte of the GArray. Now it seems that that GArray is not useful and i must use some other data type. Any suggestion as to which data type might work. Also some example code demonstrating the usage will be gr8. <br><br>The following code demonstrates the case : <br><br>------- client code
------<br>int main (int argc, char **argv)<br>{<br> DBusGConnection *connection;<br> GError *error;<br> DBusGProxy *proxy;<br> GArray *array;<br> guint length;<br> FILE *fp = NULL;<br> int len;<br><br> g_type_init ();<br><br> error = NULL;<br> connection = dbus_g_bus_get (DBUS_BUS_SESSION,<br> &error);<br> if (connection == NULL)<br> {<br> g_printerr ("Failed to open connection to bus: %s\n",<br> error->message);<br>
g_error_free (error);<br> exit (1);<br> }<br><br> proxy = dbus_g_proxy_new_for_name (connection,<br> "com.example.SomeObject",<br> "/com/example/SomeObject",<br> "com.example.SomeObject");<br><br> error = NULL;<br><br>
if(!com_example_SomeObject_method1(proxy,10,&array,&length,&error))<br> {<br> printf("Error --- %s\n",error->message);<br> return 1;<br> }<br><br> printf("File details... length = %d\n",length);<br> error = NULL;<br><br> fp = fopen("copy-client","wb");<br> len = fwrite(array->data,4,array->len,fp);<br> fclose(fp);<br><br> return 0;<br>}<br><br>---------- End Client code ------------<br><br>--------- Server code -------------<br>//some-object.h<br>#ifndef __SOME_OBJECT_H__<br>#define __SOME_OBJECT_H__<br><br>#include <glib-object.h><br><br>typedef struct _SomeObject SomeObject;<br>struct _SomeObject<br>{<br> GObject parent_obj;<br> gint
m_a;<br> gchar* m_b;<br> gfloat m_c;<br>};<br><br>typedef struct _SomeObjectClass SomeObjectClass;<br>struct _SomeObjectClass<br>{<br> GObjectClass parent_class;<br><br> /* Some useful methods may follow. */<br> gboolean (*method1) (SomeObject *self, gint x,GArray **y,gint *z,GError **error);<br> void (*method2) (SomeObject *self, gchar*);<br>};<br><br>GType some_object_get_type ();<br><br>gboolean some_object_method1 (SomeObject *self, gint x,GArray **y,gint *z,GError **); /* virtual */<br>void some_object_method2 (SomeObject *self, gchar*); /* virtual
*/<br>void some_object_method3 (SomeObject *self, gfloat); /* non-virtual */<br><br><br>/* Handy macros */<br>#define SOME_OBJECT_TYPE (some_object_get_type ())<br>#define SOME_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SOME_OBJECT_TYPE, SomeObject))<br>#define SOME_OBJECT_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), SOME_OBJECT_TYPE, SomeObjectClass))<br>#define SOME_IS_OBJECT(obj) (G_TYPE_CHECK_TYPE ((obj), SOME_OBJECT_TYPE))<br>#define SOME_IS_OBJECT_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), SOME_OBJECT_TYPE))<br>#define SOME_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SOME_OBJECT_TYPE, SomeObjectClass))<br><br>#endif<br>// End some-object.h<br><br>/// some-object.c<br>#include
<string.h><br>#include <sys/stat.h><br>#include <stdio.h><br>#include <stdlib.h><br>#include "some-object.h"<br>#include "some-object-glue.h"<br>#include <dbus/dbus-glib-bindings.h><br>#include <errno.h><br><br><br>static GObjectClass *parent_class = ((void *)0);<br>static void some_object_init (SomeObject *self);<br><br>char *FILENAME = "test.jpeg";<br>int SIZE = 0;<br><br>gboolean some_object_method1_impl (SomeObject *self, gint a, GArray **y,gint *z,GError **error)<br>{<br> struct stat stat_buf;<br> gchar *ptr = NULL;<br> FILE *fp = NULL;<br> int err;<br> unsigned int length = 0;<br> self->m_a = a;<br> g_print ("Method1: %i\n", self->m_a);<br> <br> *y = g_array_new(FALSE,FALSE,1);<br><br>
stat(FILENAME,&stat_buf);<br> SIZE = stat_buf.st_size;<br> ptr = g_malloc(SIZE);<br> errno = 0;<br> fp = fopen(FILENAME,"rb");<br> err = errno;<br> if(fp)<br> {<br> FILE *fp2 = fopen("c:\\copy-server","wb");<br> fread(ptr,1,SIZE,fp);<br> <br> if(fp2)<br> fwrite(ptr,1,SIZE,fp2);<br> <br> g_array_append_vals(*y,ptr,SIZE/4);<br> *z = SIZE; <br> }<br> <br> fclose(fp);<br> <br> free(ptr);<br>
<br> return TRUE;<br>}<br><br>void some_object_method2_impl (SomeObject *self, gchar* b)<br>{<br> self->m_b = b;<br> g_print ("Method2: %s\n", self->m_b);<br>}<br><br><br>/* Public methods. */<br>gboolean some_object_method1 (SomeObject *self, gint a,GArray **y,gint *z,GError **error)<br>{<br> return SOME_OBJECT_GET_CLASS (self)->method1 (self, a,y,z,error);<br>}<br><br>void some_object_method2 (SomeObject *self, gchar* b)<br>{<br> SOME_OBJECT_GET_CLASS (self)->method2 (self, b);<br>}<br><br>void some_object_method3 (SomeObject *self, gfloat c)<br>{<br> self->m_c = c;<br> g_print ("Method3: %f\n", self->m_c);<br>}<br><br><br>void some_object_dispose (GObject *self)<br>{<br> static gboolean first_run = TRUE;<br><br> if
(first_run)<br> {<br> first_run = FALSE;<br> <br> /* Call g_object_unref on any GObjects that we hold, but don't break the object */<br><br> parent_class-> dispose (self);<br> }<br>}<br><br>void some_object_finalize (GObject *self)<br>{<br> parent_class-> finalize (self);<br>}<br><br>/* Here is where we override any functions. Since we have no properties or even fields, none of the below are needed. */<br>void some_object_class_init (gpointer g_class, gpointer class_data)<br>{<br> GObjectClass *object_class = G_OBJECT_CLASS (g_class);<br> SomeObjectClass *this_class =
SOME_OBJECT_CLASS (g_class);<br> <br> //assign value to parent class<br> parent_class = g_type_class_peek_parent (g_class);<br> <br> //assing pointer values to the base class members<br> object_class-> dispose = &some_object_dispose;<br> object_class-> finalize = &some_object_finalize;<br> <br> //assign value to derived class members<br> this_class->method1 = &some_object_method1_impl;<br> this_class->method2 = &some_object_method2_impl;<br><br> dbus_g_object_type_install_info(G_TYPE_FROM_CLASS(this_class),&dbus_glib__object_info);<br>}<br><br>void some_object_init (SomeObject *self)<br>{<br> self->m_a = 1;<br> self->m_c = 1.03f;<br> self->m_b =
"sumit";<br>}<br><br>GType some_object_get_type () <br>{<br> static GType g_define_type_id = 0; <br> if ((g_define_type_id == 0)) <br> { <br> static const GTypeInfo g_define_type_info = <br> { <br> sizeof (SomeObjectClass), <br> (GBaseInitFunc) ((void *)0), <br> (GBaseFinalizeFunc) ((void *)0), <br> (GClassInitFunc) some_object_class_init, <br> (GClassFinalizeFunc) ((void *)0), <br> ((void *)0), <br> sizeof (SomeObject), <br>
0, <br> (GInstanceInitFunc) some_object_init, <br> }; <br><br> g_define_type_id = g_type_register_static <br> (<br> G_TYPE_OBJECT, <br> "SomeObject", <br> &g_define_type_info, <br> (GTypeFlags) 0<br> );<br> <br> } <br><br> return g_define_type_id; <br>}<br><br>int main(int argc,char *argv[])<br>{<br> SomeObject *so = NULL;<br> DBusGConnection *bus;<br> GMainLoop
*mainLoop = NULL;<br> unsigned int request_ret;<br> GError *error = NULL;<br><br> DBusGProxy *proxy = NULL;<br> char *x;<br> <br> g_type_init();<br><br> so = g_object_new(SOME_OBJECT_TYPE,NULL);<br><br> bus = dbus_g_bus_get(DBUS_BUS_SESSION,NULL);<br><br> proxy = dbus_g_proxy_new_for_name(bus,DBUS_SERVICE_DBUS,DBUS_PATH_DBUS,DBUS_INTERFACE_DBUS);<br><br> dbus_g_connection_register_g_object(bus,"/com/example/SomeObject",G_OBJECT(so));<br><br> if(!org_freedesktop_DBus_request_name(proxy,"com.example.SomeObject",0,&request_ret,&error))<br> {<br> g_print("Unable to register service\n");<br> return 1;<br> }<br><br> mainLoop =
g_main_loop_new(NULL,FALSE);<br> g_main_loop_run(mainLoop);<br> <br> return 0;<br>}<br><br>----- End Server code ----<br><br><div> </div><p>Regards,</p><p>Sumit</p><p><br></p></div><br>
<!--4--><hr size=1></hr> 5, 50, 500, 5000 - Store N number of mails in your inbox. <a href="http://in.rd.yahoo.com/tagline_mail_4/*http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html/">Click here.</a></body></html>