5 using System.Runtime.InteropServices;
6 using System.Diagnostics;
7 using System.Reflection;
9 using System.Collections;
11 public class Connection
14 /// A pointer to the underlying Connection structure
16 private IntPtr rawConnection;
19 /// The current slot number
21 private static int slot = -1;
23 private int timeout = -1;
25 internal Connection(IntPtr rawConnection)
27 RawConnection = rawConnection;
30 public Connection(string address)
32 // the assignment bumps the refcount
33 Error error = new Error();
35 RawConnection = dbus_connection_open(address, ref error);
36 if (RawConnection != IntPtr.Zero) {
37 dbus_connection_unref(RawConnection);
39 throw new DBusException(error);
45 public void SetupWithMain()
47 dbus_connection_setup_with_g_main(RawConnection, IntPtr.Zero);
52 if (RawConnection != IntPtr.Zero)
54 dbus_connection_disconnect(rawConnection);
56 RawConnection = IntPtr.Zero; // free the native object
59 internal static Connection Wrap(IntPtr rawConnection)
62 // If we already have a Connection object associated with this rawConnection then return it
63 IntPtr rawThis = dbus_connection_get_data (rawConnection, slot);
64 return (DBus.Connection) ((GCHandle)rawThis).Target;
68 // If it doesn't exist then create a new connection around it
69 return new Connection(rawConnection);
91 // We need to initialize the slot
92 if (!dbus_connection_allocate_data_slot (ref slot))
93 throw new OutOfMemoryException ();
95 Debug.Assert (slot >= 0);
102 internal IntPtr RawConnection
106 return rawConnection;
110 if (value == rawConnection)
113 if (rawConnection != IntPtr.Zero)
115 // Get the reference to this
116 IntPtr rawThis = dbus_connection_get_data (rawConnection, Slot);
117 Debug.Assert (rawThis != IntPtr.Zero);
119 // Blank over the reference
120 dbus_connection_set_data (rawConnection, Slot, IntPtr.Zero, IntPtr.Zero);
122 // Free the reference
123 ((GCHandle) rawThis).Free();
125 // Unref the connection
126 dbus_connection_unref(rawConnection);
129 this.rawConnection = value;
131 if (rawConnection != IntPtr.Zero)
135 dbus_connection_ref (rawConnection);
137 // We store a weak reference to the C# object on the C object
138 rawThis = GCHandle.Alloc (this, GCHandleType.WeakTrackResurrection);
140 dbus_connection_set_data(rawConnection, Slot, (IntPtr) rawThis, IntPtr.Zero);
145 [DllImport("dbus-glib-1")]
146 private extern static void dbus_connection_setup_with_g_main(IntPtr rawConnection,
149 [DllImport ("dbus-1")]
150 private extern static IntPtr dbus_connection_open (string address, ref Error error);
152 [DllImport ("dbus-1")]
153 private extern static void dbus_connection_unref (IntPtr ptr);
155 [DllImport ("dbus-1")]
156 private extern static void dbus_connection_ref (IntPtr ptr);
158 [DllImport ("dbus-1")]
159 private extern static bool dbus_connection_allocate_data_slot (ref int slot);
161 [DllImport ("dbus-1")]
162 private extern static void dbus_connection_free_data_slot (ref int slot);
164 [DllImport ("dbus-1")]
165 private extern static bool dbus_connection_set_data (IntPtr ptr,
168 IntPtr free_data_func);
170 [DllImport ("dbus-1")]
171 private extern static void dbus_connection_flush (IntPtr ptr);
173 [DllImport ("dbus-1")]
174 private extern static IntPtr dbus_connection_get_data (IntPtr ptr,
177 [DllImport ("dbus-1")]
178 private extern static void dbus_connection_disconnect (IntPtr ptr);