[vala/gdbus: 7/11] D-Bus: Add support for receiving file descriptors with GDBus



commit d80a54df4157584e0a6db967a73fd29f7d604c85
Author: Jürg Billeter <j bitron ch>
Date:   Mon Oct 18 17:59:09 2010 +0200

    D-Bus: Add support for receiving file descriptors with GDBus

 codegen/valagdbusclientmodule.vala |    8 ++++--
 codegen/valagdbusmodule.vala       |   46 ++++++++++++++++++++++++++++++++++++
 codegen/valagdbusservermodule.vala |    7 ++++-
 3 files changed, 57 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valagdbusclientmodule.vala b/codegen/valagdbusclientmodule.vala
index 44670df..34d158d 100644
--- a/codegen/valagdbusclientmodule.vala
+++ b/codegen/valagdbusclientmodule.vala
@@ -575,6 +575,8 @@ public class Vala.GDBusClientModule : GDBusModule {
 
 			bool has_result = !(m.return_type is VoidType);
 
+			ccode.add_declaration ("gint", new CCodeVariableDeclarator.zero ("_fd_index", new CCodeConstant ("0")));
+
 			foreach (FormalParameter param in m.get_parameters ()) {
 				if (param.direction == ParameterDirection.OUT) {
 					has_result = true;
@@ -607,7 +609,7 @@ public class Vala.GDBusClientModule : GDBusModule {
 						}
 
 						var target = new CCodeIdentifier ("_" + param.name);
-						read_expression (param.variable_type, new CCodeIdentifier ("_reply_iter"), target, param);
+						receive_dbus_value (param.variable_type, new CCodeIdentifier ("_reply_message"), new CCodeIdentifier ("_reply_iter"), target, param);
 
 						// TODO check that parameter is not NULL (out parameters are optional)
 						// free value if parameter is NULL
@@ -625,7 +627,7 @@ public class Vala.GDBusClientModule : GDBusModule {
 				if (!(m.return_type is VoidType)) {
 					if (m.return_type.is_real_non_null_struct_type ()) {
 						var target = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("result"));
-						read_expression (m.return_type, new CCodeIdentifier ("_reply_iter"), target, m);
+						receive_dbus_value (m.return_type, new CCodeIdentifier ("_reply_message"), new CCodeIdentifier ("_reply_iter"), target, m);
 					} else {
 						ccode.add_declaration (m.return_type.get_cname (), new CCodeVariableDeclarator ("_result"));
 
@@ -637,7 +639,7 @@ public class Vala.GDBusClientModule : GDBusModule {
 							}
 						}
 
-						read_expression (m.return_type, new CCodeIdentifier ("_reply_iter"), new CCodeIdentifier ("_result"), m);
+						receive_dbus_value (m.return_type, new CCodeIdentifier ("_reply_message"), new CCodeIdentifier ("_reply_iter"), new CCodeIdentifier ("_result"), m);
 
 						if (array_type != null) {
 							for (int dim = 1; dim <= array_type.rank; dim++) {
diff --git a/codegen/valagdbusmodule.vala b/codegen/valagdbusmodule.vala
index 11b7f15..7105017 100644
--- a/codegen/valagdbusmodule.vala
+++ b/codegen/valagdbusmodule.vala
@@ -150,4 +150,50 @@ public class Vala.GDBusModule : GVariantModule {
 			write_expression (type, builder_expr, expr, sym);
 		}
 	}
+
+	CCodeExpression? create_from_file_descriptor (DataType type, CCodeExpression expr) {
+		if (type is ObjectType) {
+			if (type.data_type.get_full_name () == "GLib.UnixInputStream") {
+				var result = new CCodeFunctionCall (new CCodeIdentifier ("g_unix_input_stream_new"));
+				result.add_argument (expr);
+				result.add_argument (new CCodeConstant ("TRUE"));
+				return result;
+			} else if (type.data_type.get_full_name () == "GLib.UnixOutputStream") {
+				var result = new CCodeFunctionCall (new CCodeIdentifier ("g_unix_output_stream_new"));
+				result.add_argument (expr);
+				result.add_argument (new CCodeConstant ("TRUE"));
+				return result;
+			} else if (type.data_type.get_full_name () == "GLib.Socket") {
+				var result = new CCodeFunctionCall (new CCodeIdentifier ("g_socket_new_from_fd"));
+				result.add_argument (expr);
+				result.add_argument (new CCodeConstant ("NULL"));
+				return result;
+			}
+		}
+
+		return null;
+	}
+
+	public void receive_dbus_value (DataType type, CCodeExpression message_expr, CCodeExpression iter_expr, CCodeExpression target_expr, Symbol? sym) {
+		var fd_list = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_message_get_unix_fd_list"));
+		fd_list.add_argument (message_expr);
+
+		var fd = new CCodeFunctionCall (new CCodeIdentifier ("g_unix_fd_list_get"));
+		fd.add_argument (fd_list);
+		fd.add_argument (new CCodeIdentifier ("_fd_index"));
+		fd.add_argument (new CCodeConstant ("NULL"));
+
+		var stream = create_from_file_descriptor (type, fd);
+		if (stream != null) {
+			var get_fd = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_iter_next"));
+			get_fd.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, iter_expr));
+			get_fd.add_argument (new CCodeConstant ("\"h\""));
+			get_fd.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("_fd_index")));
+			ccode.add_expression (get_fd);
+
+			ccode.add_expression (new CCodeAssignment (target_expr, stream));
+		} else {
+			read_expression (type, iter_expr, target_expr, sym);
+		}
+	}
 }
diff --git a/codegen/valagdbusservermodule.vala b/codegen/valagdbusservermodule.vala
index fff2f26..10fbe54 100644
--- a/codegen/valagdbusservermodule.vala
+++ b/codegen/valagdbusservermodule.vala
@@ -109,6 +109,8 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 		}
 
 		if (!ready) {
+			ccode.add_declaration ("gint", new CCodeVariableDeclarator.zero ("_fd_index", new CCodeConstant ("0")));
+
 			foreach (FormalParameter param in m.get_parameters ()) {
 				if (param.direction != ParameterDirection.IN) {
 					continue;
@@ -128,7 +130,10 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 					}
 				}
 
-				read_expression (param.variable_type, new CCodeIdentifier ("_arguments_iter"), new CCodeIdentifier (param.name), param);
+				var message_expr = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_method_invocation_get_message"));
+				message_expr.add_argument (new CCodeIdentifier ("invocation"));
+
+				receive_dbus_value (param.variable_type, message_expr, new CCodeIdentifier ("_arguments_iter"), new CCodeIdentifier (param.name), param);
 			}
 		}
 



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