1Packages: gio-2.0 gio-unix-2.0 posix
2D-Bus
3
4Program: client
5
6[DBus (name = "org.example.Test")]
7interface Test : Object {
8	public abstract UnixInputStream test_in (UnixInputStream i, out UnixInputStream j) throws IOError;
9}
10
11void main () {
12	// client
13	Test test = Bus.get_proxy_sync (BusType.SESSION, "org.example.Test", "/org/example/test");
14
15	uint8[] buffer = new uint8[1];
16
17	int[] pipe1 = new int[2];
18	assert (Posix.pipe (pipe1) == 0);
19	buffer[0] = 42;
20	assert (Posix.write (pipe1[1], buffer, 1) == 1);
21	Posix.close (pipe1[1]);
22
23	UnixInputStream j, k;
24	k = test.test_in (new UnixInputStream (pipe1[0], true), out j);
25
26	assert (j.read (buffer) == 1);
27	assert (buffer[0] == 23);
28
29	assert (k.read (buffer) == 1);
30	assert (buffer[0] == 11);
31}
32
33Program: server
34
35[DBus (name = "org.example.Test")]
36class Test : Object {
37	public UnixInputStream test_in (UnixInputStream i, out UnixInputStream j) throws IOError {
38		uint8[] buffer = new uint8[1];
39
40		assert (i.read (buffer) == 1);
41		assert (buffer[0] == 42);
42
43		int[] pipe1 = new int[2];
44		assert (Posix.pipe (pipe1) == 0);
45		buffer[0] = 23;
46		assert (Posix.write (pipe1[1], buffer, 1) == 1);
47		Posix.close (pipe1[1]);
48
49		int[] pipe2 = new int[2];
50		assert (Posix.pipe (pipe2) == 0);
51		buffer[0] = 11;
52		assert (Posix.write (pipe2[1], buffer, 1) == 1);
53		Posix.close (pipe2[1]);
54
55		j = new UnixInputStream (pipe1[0], true);
56		return new UnixInputStream (pipe2[0], true);
57	}
58}
59
60MainLoop main_loop;
61
62void client_exit (Pid pid, int status) {
63	// client finished, terminate server
64	assert (status == 0);
65	main_loop.quit ();
66}
67
68void main () {
69	var conn = Bus.get_sync (BusType.SESSION);
70	conn.register_object ("/org/example/test", new Test ());
71
72	// try to register service in session bus
73	var request_result = conn.call_sync ("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "RequestName",
74	                                      new Variant ("(su)", "org.example.Test", 0x4), null, 0, -1);
75	assert ((uint) request_result.get_child_value (0) == 1);
76
77	// server ready, spawn client
78	Pid client_pid;
79	Process.spawn_async (null, { "dbus_filedescriptor_client" }, null, SpawnFlags.DO_NOT_REAP_CHILD, null, out client_pid);
80	ChildWatch.add (client_pid, client_exit);
81
82	main_loop = new MainLoop ();
83	main_loop.run ();
84}
85