2004-03-23 12:10:32 +00:00
|
|
|
namespace DBus
|
|
|
|
|
{
|
2003-06-22 22:59:31 +00:00
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Diagnostics;
|
2004-03-23 12:10:32 +00:00
|
|
|
using System.Reflection;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections;
|
2003-06-22 22:59:31 +00:00
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
public class Connection
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A pointer to the underlying Connection structure
|
|
|
|
|
/// </summary>
|
|
|
|
|
private IntPtr rawConnection;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The current slot number
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static int slot = -1;
|
|
|
|
|
|
|
|
|
|
private int timeout = -1;
|
2003-06-22 22:59:31 +00:00
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
internal Connection(IntPtr rawConnection)
|
|
|
|
|
{
|
|
|
|
|
RawConnection = rawConnection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Connection(string address)
|
|
|
|
|
{
|
2003-06-22 22:59:31 +00:00
|
|
|
// the assignment bumps the refcount
|
2004-03-23 12:10:32 +00:00
|
|
|
Error error = new Error();
|
|
|
|
|
error.Init();
|
|
|
|
|
RawConnection = dbus_connection_open(address, ref error);
|
|
|
|
|
if (RawConnection != IntPtr.Zero) {
|
|
|
|
|
dbus_connection_unref(RawConnection);
|
2003-06-22 22:59:31 +00:00
|
|
|
} else {
|
2004-03-23 12:10:32 +00:00
|
|
|
throw new DBusException(error);
|
2003-06-22 22:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
SetupWithMain();
|
2003-06-23 02:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-26 15:25:59 +00:00
|
|
|
public void Flush()
|
|
|
|
|
{
|
|
|
|
|
dbus_connection_flush(RawConnection);
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
public void SetupWithMain()
|
|
|
|
|
{
|
|
|
|
|
dbus_connection_setup_with_g_main(RawConnection, IntPtr.Zero);
|
2003-06-23 02:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
~Connection ()
|
|
|
|
|
{
|
|
|
|
|
if (RawConnection != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
dbus_connection_disconnect(rawConnection);
|
|
|
|
|
}
|
|
|
|
|
RawConnection = IntPtr.Zero; // free the native object
|
2003-06-23 02:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
internal static Connection Wrap(IntPtr rawConnection)
|
|
|
|
|
{
|
|
|
|
|
if (slot > -1) {
|
|
|
|
|
// If we already have a Connection object associated with this rawConnection then return it
|
|
|
|
|
IntPtr rawThis = dbus_connection_get_data (rawConnection, slot);
|
|
|
|
|
return (DBus.Connection) ((GCHandle)rawThis).Target;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// If it doesn't exist then create a new connection around it
|
|
|
|
|
return new Connection(rawConnection);
|
|
|
|
|
}
|
2003-06-22 22:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
public int Timeout
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return this.timeout;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
this.timeout = value;
|
|
|
|
|
}
|
2003-06-22 22:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
private int Slot
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (slot == -1)
|
|
|
|
|
{
|
|
|
|
|
// We need to initialize the slot
|
|
|
|
|
if (!dbus_connection_allocate_data_slot (ref slot))
|
|
|
|
|
throw new OutOfMemoryException ();
|
|
|
|
|
|
|
|
|
|
Debug.Assert (slot >= 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return slot;
|
|
|
|
|
}
|
2003-06-22 22:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
internal IntPtr RawConnection
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return rawConnection;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == rawConnection)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (rawConnection != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
// Get the reference to this
|
|
|
|
|
IntPtr rawThis = dbus_connection_get_data (rawConnection, Slot);
|
|
|
|
|
Debug.Assert (rawThis != IntPtr.Zero);
|
|
|
|
|
|
|
|
|
|
// Blank over the reference
|
|
|
|
|
dbus_connection_set_data (rawConnection, Slot, IntPtr.Zero, IntPtr.Zero);
|
|
|
|
|
|
|
|
|
|
// Free the reference
|
|
|
|
|
((GCHandle) rawThis).Free();
|
|
|
|
|
|
|
|
|
|
// Unref the connection
|
|
|
|
|
dbus_connection_unref(rawConnection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.rawConnection = value;
|
|
|
|
|
|
|
|
|
|
if (rawConnection != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
GCHandle rawThis;
|
|
|
|
|
|
|
|
|
|
dbus_connection_ref (rawConnection);
|
|
|
|
|
|
|
|
|
|
// We store a weak reference to the C# object on the C object
|
|
|
|
|
rawThis = GCHandle.Alloc (this, GCHandleType.WeakTrackResurrection);
|
|
|
|
|
|
|
|
|
|
dbus_connection_set_data(rawConnection, Slot, (IntPtr) rawThis, IntPtr.Zero);
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-06-22 22:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
[DllImport("dbus-glib-1")]
|
|
|
|
|
private extern static void dbus_connection_setup_with_g_main(IntPtr rawConnection,
|
|
|
|
|
IntPtr rawContext);
|
2003-06-22 22:59:31 +00:00
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
[DllImport ("dbus-1")]
|
|
|
|
|
private extern static IntPtr dbus_connection_open (string address, ref Error error);
|
2003-06-23 02:12:19 +00:00
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
[DllImport ("dbus-1")]
|
|
|
|
|
private extern static void dbus_connection_unref (IntPtr ptr);
|
2003-06-23 02:12:19 +00:00
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
[DllImport ("dbus-1")]
|
|
|
|
|
private extern static void dbus_connection_ref (IntPtr ptr);
|
|
|
|
|
|
|
|
|
|
[DllImport ("dbus-1")]
|
|
|
|
|
private extern static bool dbus_connection_allocate_data_slot (ref int slot);
|
|
|
|
|
|
|
|
|
|
[DllImport ("dbus-1")]
|
|
|
|
|
private extern static void dbus_connection_free_data_slot (ref int slot);
|
|
|
|
|
|
|
|
|
|
[DllImport ("dbus-1")]
|
|
|
|
|
private extern static bool dbus_connection_set_data (IntPtr ptr,
|
|
|
|
|
int slot,
|
|
|
|
|
IntPtr data,
|
|
|
|
|
IntPtr free_data_func);
|
|
|
|
|
|
|
|
|
|
[DllImport ("dbus-1")]
|
|
|
|
|
private extern static void dbus_connection_flush (IntPtr ptr);
|
2003-06-23 02:12:19 +00:00
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
[DllImport ("dbus-1")]
|
|
|
|
|
private extern static IntPtr dbus_connection_get_data (IntPtr ptr,
|
|
|
|
|
int slot);
|
2003-06-23 02:12:19 +00:00
|
|
|
|
2004-03-23 12:10:32 +00:00
|
|
|
[DllImport ("dbus-1")]
|
|
|
|
|
private extern static void dbus_connection_disconnect (IntPtr ptr);
|
2003-06-22 22:59:31 +00:00
|
|
|
}
|
|
|
|
|
}
|