diff options
author | Jon Trowbridge <trow@ximian.com> | 2004-08-29 18:14:30 +0000 |
---|---|---|
committer | John Palmieri <johnp@remedyz.boston.redhat.com> | 2006-06-28 08:15:38 -0400 |
commit | b4170976d96f907eb180d8be4ac20bec57e5e86d (patch) | |
tree | 8fe95784b57636734ae5f313e1828820d84b678a | |
parent | 0b497fdac9ea957b469824a262afff5c586a5533 (diff) |
Mapped CRLF -> LF for consistency.
-rw-r--r-- | mono/Arguments.cs | 570 | ||||
-rw-r--r-- | mono/Connection.cs | 372 | ||||
-rw-r--r-- | mono/DBusException.cs | 24 | ||||
-rw-r--r-- | mono/DBusType/Array.cs | 276 | ||||
-rw-r--r-- | mono/DBusType/Boolean.cs | 172 | ||||
-rw-r--r-- | mono/DBusType/Byte.cs | 210 | ||||
-rw-r--r-- | mono/DBusType/Custom.cs | 218 | ||||
-rw-r--r-- | mono/DBusType/Dict.cs | 294 | ||||
-rw-r--r-- | mono/DBusType/Double.cs | 172 | ||||
-rw-r--r-- | mono/DBusType/IDBusType.cs | 32 | ||||
-rw-r--r-- | mono/DBusType/Int32.cs | 186 | ||||
-rw-r--r-- | mono/DBusType/Int64.cs | 188 | ||||
-rw-r--r-- | mono/DBusType/Nil.cs | 136 | ||||
-rw-r--r-- | mono/DBusType/ObjectPath.cs | 198 | ||||
-rw-r--r-- | mono/DBusType/String.cs | 172 | ||||
-rw-r--r-- | mono/DBusType/UInt32.cs | 190 | ||||
-rw-r--r-- | mono/DBusType/UInt64.cs | 190 | ||||
-rw-r--r-- | mono/Error.cs | 120 | ||||
-rw-r--r-- | mono/Message.cs | 754 | ||||
-rw-r--r-- | mono/MethodCall.cs | 160 | ||||
-rw-r--r-- | mono/Test.cs | 9 |
21 files changed, 2325 insertions, 2318 deletions
diff --git a/mono/Arguments.cs b/mono/Arguments.cs index f2278bd..ca178ae 100644 --- a/mono/Arguments.cs +++ b/mono/Arguments.cs @@ -1,285 +1,285 @@ -using System;
-using System.Collections;
-using System.Reflection;
-using System.Runtime.InteropServices;
-
-namespace DBus
-{
- // Holds the arguments of a message. Provides methods for appending
- // arguments and to assist in matching .NET types with D-BUS types.
- public class Arguments : IEnumerable
- {
- // Must follow sizeof(DBusMessageIter)
- internal const int DBusMessageIterSize = 14*4;
- private static Hashtable dbusTypes = null;
- private Message message;
- private IntPtr appenderIter = Marshal.AllocCoTaskMem(DBusMessageIterSize);
- private IEnumerator enumerator = null;
-
- internal Arguments()
- {
- }
-
- ~Arguments()
- {
- Marshal.FreeCoTaskMem(appenderIter);
- }
-
- internal Arguments(Message message)
- {
- this.message = message;
- }
-
- // Checks the suitability of a D-BUS type for supporting a .NET
- // type.
- public static bool Suits(Type dbusType, Type type)
- {
- object [] pars = new object[1];
- pars[0] = type;
-
- return (bool) dbusType.InvokeMember("Suits", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, pars, null);
- }
-
- // Find a suitable match for the given .NET type or throw an
- // exception if one can't be found.
- public static Type MatchType(Type type)
- {
- foreach(Type dbusType in DBusTypes.Values) {
- if (Suits(dbusType, type)) {
- return dbusType;
- }
- }
-
- throw new ApplicationException("No suitable DBUS type found for type '" + type + "'");
- }
-
- // The D-BUS types.
- public static Hashtable DBusTypes {
- get
- {
- if (dbusTypes == null) {
- dbusTypes = new Hashtable();
-
- foreach (Type type in Assembly.GetAssembly(typeof(DBusType.IDBusType)).GetTypes()) {
- if (type != typeof(DBusType.IDBusType) && typeof(DBusType.IDBusType).IsAssignableFrom(type)) {
- dbusTypes.Add(GetCode(type), type);
- }
- }
- }
-
- return dbusTypes;
- }
- }
-
- // Append an argument
- public void Append(DBusType.IDBusType dbusType)
- {
- dbusType.Append(appenderIter);
- }
-
- // Append an argument of the specified type
- private void AppendType(Type type, object val)
- {
- object [] pars = new Object[2];
- pars[0] = val;
- pars[1] = message.Service;
- DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(MatchType(type), pars);
- Append(dbusType);
- }
-
- // Append the results of a method call
- public void AppendResults(MethodInfo method, object retVal, object [] parameters)
- {
- InitAppending();
-
- if (method.ReturnType != typeof(void)) {
- AppendType(method.ReturnType, retVal);
- }
-
- for (int i = 0; i < method.GetParameters().Length; i++) {
- ParameterInfo par = method.GetParameters()[i];
- if (par.IsOut || par.ParameterType.ToString().EndsWith("&")) {
- // It's an OUT or INOUT parameter.
- AppendType(par.ParameterType.UnderlyingSystemType, parameters[i]);
- }
- }
- }
-
- // Get the parameters
- public object[] GetParameters(MethodInfo method)
- {
- ParameterInfo[] pars = method.GetParameters();
- ArrayList paramList = new ArrayList();
-
- enumerator = GetEnumerator();
- foreach (ParameterInfo par in pars) {
- if (!par.IsOut) {
- // It's an IN or INOUT paramter.
- enumerator.MoveNext();
- DBusType.IDBusType dbusType = (DBusType.IDBusType) enumerator.Current;
- paramList.Add(dbusType.Get(par.ParameterType));
- } else {
- // It's an OUT so just create a parameter for it
- object var = null;
- paramList.Add(var);
- }
- }
-
- return paramList.ToArray();
- }
-
- // Parse the IN & REF parameters to a method and return the types in a list.
- public static object[] ParseInParameters(MethodInfo method)
- {
- ArrayList types = new ArrayList();
-
- ParameterInfo[] pars = method.GetParameters();
- foreach (ParameterInfo par in pars) {
- if (!par.IsOut) {
- types.Add(MatchType(par.ParameterType));
- }
- }
-
- return types.ToArray();
- }
-
- // Parse the OUT & REF parameters to a method and return the types in a list.
- public static object[] ParseOutParameters(MethodInfo method)
- {
- ArrayList types = new ArrayList();
-
- ParameterInfo[] pars = method.GetParameters();
- foreach (ParameterInfo par in pars) {
- if (par.IsOut || par.ParameterType.ToString().EndsWith("&")) {
- types.Add(MatchType(par.ParameterType));
- }
- }
-
- return types.ToArray();
- }
-
- // Get the appropriate constructor for a D-BUS type
- public static ConstructorInfo GetDBusTypeConstructor(Type dbusType, Type type)
- {
- ConstructorInfo constructor = dbusType.GetConstructor(new Type[] {type.UnderlyingSystemType, typeof(Service)});
- if (constructor == null)
- throw new ArgumentException("There is no valid constructor for '" + dbusType + "' from type '" + type + "'");
-
- return constructor;
- }
-
- // Get the type code for a given D-BUS type
- public static char GetCode(Type dbusType)
- {
- return (char) dbusType.InvokeMember("Code", BindingFlags.Static | BindingFlags.GetField, null, null, null);
- }
-
- // Get a complete method signature
- public override string ToString()
- {
- IntPtr iter = Marshal.AllocCoTaskMem(DBusMessageIterSize);
- string key = "";
-
- // Iterate through the parameters getting the type codes to a string
- bool notEmpty = dbus_message_iter_init(message.RawMessage, iter);
-
- if (notEmpty) {
- do {
- char code = (char) dbus_message_iter_get_arg_type(iter);
- if (code == '\0')
- return key;
-
- key += code;
- } while (dbus_message_iter_next(iter));
- }
-
- Marshal.FreeCoTaskMem(iter);
-
- return key;
- }
-
- // Move to the next parameter
- public DBusType.IDBusType GetNext()
- {
- enumerator.MoveNext();
- return (DBusType.IDBusType) enumerator.Current;
- }
-
- // Begin appending
- public void InitAppending()
- {
- dbus_message_append_iter_init(message.RawMessage, appenderIter);
- }
-
- // Get the enumerator
- public IEnumerator GetEnumerator()
- {
- return new ArgumentsEnumerator(this);
- }
-
- private class ArgumentsEnumerator : IEnumerator
- {
- private Arguments arguments;
- private bool started = false;
- private bool notEmpty = false;
- private IntPtr iter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);
-
- public ArgumentsEnumerator(Arguments arguments)
- {
- this.arguments = arguments;
- Reset();
- }
-
- ~ArgumentsEnumerator()
- {
- Marshal.FreeCoTaskMem(iter);
- }
-
- public bool MoveNext()
- {
- if (started) {
- return dbus_message_iter_next(iter);
- } else {
- started = true;
- return notEmpty;
- }
- }
-
- public void Reset()
- {
- notEmpty = dbus_message_iter_init(arguments.message.RawMessage, iter);
- started = false;
- }
-
- public object Current
- {
- get
- {
- object [] pars = new Object[2];
- pars[0] = iter;
- pars[1] = arguments.message.Service;
-
- Type type = (Type) DBusTypes[(char) dbus_message_iter_get_arg_type(iter)];
- DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(type, pars);
-
- return dbusType;
- }
- }
- }
-
- [DllImport("dbus-1")]
- private extern static void dbus_message_append_iter_init(IntPtr rawMessage, IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_has_next(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_next(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_init(IntPtr rawMessage, IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static int dbus_message_iter_get_arg_type(IntPtr iter);
- }
-}
+using System; +using System.Collections; +using System.Reflection; +using System.Runtime.InteropServices; + +namespace DBus +{ + // Holds the arguments of a message. Provides methods for appending + // arguments and to assist in matching .NET types with D-BUS types. + public class Arguments : IEnumerable + { + // Must follow sizeof(DBusMessageIter) + internal const int DBusMessageIterSize = 14*4; + private static Hashtable dbusTypes = null; + private Message message; + private IntPtr appenderIter = Marshal.AllocCoTaskMem(DBusMessageIterSize); + private IEnumerator enumerator = null; + + internal Arguments() + { + } + + ~Arguments() + { + Marshal.FreeCoTaskMem(appenderIter); + } + + internal Arguments(Message message) + { + this.message = message; + } + + // Checks the suitability of a D-BUS type for supporting a .NET + // type. + public static bool Suits(Type dbusType, Type type) + { + object [] pars = new object[1]; + pars[0] = type; + + return (bool) dbusType.InvokeMember("Suits", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, pars, null); + } + + // Find a suitable match for the given .NET type or throw an + // exception if one can't be found. + public static Type MatchType(Type type) + { + foreach(Type dbusType in DBusTypes.Values) { + if (Suits(dbusType, type)) { + return dbusType; + } + } + + throw new ApplicationException("No suitable DBUS type found for type '" + type + "'"); + } + + // The D-BUS types. + public static Hashtable DBusTypes { + get + { + if (dbusTypes == null) { + dbusTypes = new Hashtable(); + + foreach (Type type in Assembly.GetAssembly(typeof(DBusType.IDBusType)).GetTypes()) { + if (type != typeof(DBusType.IDBusType) && typeof(DBusType.IDBusType).IsAssignableFrom(type)) { + dbusTypes.Add(GetCode(type), type); + } + } + } + + return dbusTypes; + } + } + + // Append an argument + public void Append(DBusType.IDBusType dbusType) + { + dbusType.Append(appenderIter); + } + + // Append an argument of the specified type + private void AppendType(Type type, object val) + { + object [] pars = new Object[2]; + pars[0] = val; + pars[1] = message.Service; + DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(MatchType(type), pars); + Append(dbusType); + } + + // Append the results of a method call + public void AppendResults(MethodInfo method, object retVal, object [] parameters) + { + InitAppending(); + + if (method.ReturnType != typeof(void)) { + AppendType(method.ReturnType, retVal); + } + + for (int i = 0; i < method.GetParameters().Length; i++) { + ParameterInfo par = method.GetParameters()[i]; + if (par.IsOut || par.ParameterType.ToString().EndsWith("&")) { + // It's an OUT or INOUT parameter. + AppendType(par.ParameterType.UnderlyingSystemType, parameters[i]); + } + } + } + + // Get the parameters + public object[] GetParameters(MethodInfo method) + { + ParameterInfo[] pars = method.GetParameters(); + ArrayList paramList = new ArrayList(); + + enumerator = GetEnumerator(); + foreach (ParameterInfo par in pars) { + if (!par.IsOut) { + // It's an IN or INOUT paramter. + enumerator.MoveNext(); + DBusType.IDBusType dbusType = (DBusType.IDBusType) enumerator.Current; + paramList.Add(dbusType.Get(par.ParameterType)); + } else { + // It's an OUT so just create a parameter for it + object var = null; + paramList.Add(var); + } + } + + return paramList.ToArray(); + } + + // Parse the IN & REF parameters to a method and return the types in a list. + public static object[] ParseInParameters(MethodInfo method) + { + ArrayList types = new ArrayList(); + + ParameterInfo[] pars = method.GetParameters(); + foreach (ParameterInfo par in pars) { + if (!par.IsOut) { + types.Add(MatchType(par.ParameterType)); + } + } + + return types.ToArray(); + } + + // Parse the OUT & REF parameters to a method and return the types in a list. + public static object[] ParseOutParameters(MethodInfo method) + { + ArrayList types = new ArrayList(); + + ParameterInfo[] pars = method.GetParameters(); + foreach (ParameterInfo par in pars) { + if (par.IsOut || par.ParameterType.ToString().EndsWith("&")) { + types.Add(MatchType(par.ParameterType)); + } + } + + return types.ToArray(); + } + + // Get the appropriate constructor for a D-BUS type + public static ConstructorInfo GetDBusTypeConstructor(Type dbusType, Type type) + { + ConstructorInfo constructor = dbusType.GetConstructor(new Type[] {type.UnderlyingSystemType, typeof(Service)}); + if (constructor == null) + throw new ArgumentException("There is no valid constructor for '" + dbusType + "' from type '" + type + "'"); + + return constructor; + } + + // Get the type code for a given D-BUS type + public static char GetCode(Type dbusType) + { + return (char) dbusType.InvokeMember("Code", BindingFlags.Static | BindingFlags.GetField, null, null, null); + } + + // Get a complete method signature + public override string ToString() + { + IntPtr iter = Marshal.AllocCoTaskMem(DBusMessageIterSize); + string key = ""; + + // Iterate through the parameters getting the type codes to a string + bool notEmpty = dbus_message_iter_init(message.RawMessage, iter); + + if (notEmpty) { + do { + char code = (char) dbus_message_iter_get_arg_type(iter); + if (code == '\0') + return key; + + key += code; + } while (dbus_message_iter_next(iter)); + } + + Marshal.FreeCoTaskMem(iter); + + return key; + } + + // Move to the next parameter + public DBusType.IDBusType GetNext() + { + enumerator.MoveNext(); + return (DBusType.IDBusType) enumerator.Current; + } + + // Begin appending + public void InitAppending() + { + dbus_message_append_iter_init(message.RawMessage, appenderIter); + } + + // Get the enumerator + public IEnumerator GetEnumerator() + { + return new ArgumentsEnumerator(this); + } + + private class ArgumentsEnumerator : IEnumerator + { + private Arguments arguments; + private bool started = false; + private bool notEmpty = false; + private IntPtr iter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize); + + public ArgumentsEnumerator(Arguments arguments) + { + this.arguments = arguments; + Reset(); + } + + ~ArgumentsEnumerator() + { + Marshal.FreeCoTaskMem(iter); + } + + public bool MoveNext() + { + if (started) { + return dbus_message_iter_next(iter); + } else { + started = true; + return notEmpty; + } + } + + public void Reset() + { + notEmpty = dbus_message_iter_init(arguments.message.RawMessage, iter); + started = false; + } + + public object Current + { + get + { + object [] pars = new Object[2]; + pars[0] = iter; + pars[1] = arguments.message.Service; + + Type type = (Type) DBusTypes[(char) dbus_message_iter_get_arg_type(iter)]; + DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(type, pars); + + return dbusType; + } + } + } + + [DllImport("dbus-1")] + private extern static void dbus_message_append_iter_init(IntPtr rawMessage, IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_has_next(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_next(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_init(IntPtr rawMessage, IntPtr iter); + + [DllImport("dbus-1")] + private extern static int dbus_message_iter_get_arg_type(IntPtr iter); + } +} diff --git a/mono/Connection.cs b/mono/Connection.cs index 221a3b3..fad0dc5 100644 --- a/mono/Connection.cs +++ b/mono/Connection.cs @@ -1,186 +1,186 @@ -namespace DBus
-{
-
- using System;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using System.Reflection;
- using System.IO;
- using System.Collections;
-
- 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;
-
- internal Connection(IntPtr rawConnection)
- {
- RawConnection = rawConnection;
- }
-
- public Connection(string address)
- {
- // the assignment bumps the refcount
- Error error = new Error();
- error.Init();
- RawConnection = dbus_connection_open(address, ref error);
- if (RawConnection != IntPtr.Zero) {
- dbus_connection_unref(RawConnection);
- } else {
- throw new DBusException(error);
- }
-
- SetupWithMain();
- }
-
- public void Flush()
- {
- dbus_connection_flush(RawConnection);
- }
-
- public void SetupWithMain()
- {
- dbus_connection_setup_with_g_main(RawConnection, IntPtr.Zero);
- }
-
- ~Connection ()
- {
- if (RawConnection != IntPtr.Zero)
- {
- dbus_connection_disconnect(rawConnection);
- }
- RawConnection = IntPtr.Zero; // free the native object
- }
-
- internal static Connection Wrap(IntPtr rawConnection)
- {
- if (slot > -1) {
- // Maybe we already have a Connection object associated with
- // this rawConnection then return it
- IntPtr rawThis = dbus_connection_get_data (rawConnection, slot);
- if (rawThis != IntPtr.Zero) {
- return (DBus.Connection) ((GCHandle)rawThis).Target;
- }
- }
-
- // If it doesn't exist then create a new connection around it
- return new Connection(rawConnection);
- }
-
- public int Timeout
- {
- get
- {
- return this.timeout;
- }
- set
- {
- this.timeout = value;
- }
- }
-
- 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;
- }
- }
-
- 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);
- }
- }
- }
-
- [DllImport("dbus-glib-1")]
- private extern static void dbus_connection_setup_with_g_main(IntPtr rawConnection,
- IntPtr rawContext);
-
- [DllImport ("dbus-1")]
- private extern static IntPtr dbus_connection_open (string address, ref Error error);
-
- [DllImport ("dbus-1")]
- private extern static void dbus_connection_unref (IntPtr ptr);
-
- [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);
-
- [DllImport ("dbus-1")]
- private extern static IntPtr dbus_connection_get_data (IntPtr ptr,
- int slot);
-
- [DllImport ("dbus-1")]
- private extern static void dbus_connection_disconnect (IntPtr ptr);
- }
-}
+namespace DBus +{ + + using System; + using System.Runtime.InteropServices; + using System.Diagnostics; + using System.Reflection; + using System.IO; + using System.Collections; + + 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; + + internal Connection(IntPtr rawConnection) + { + RawConnection = rawConnection; + } + + public Connection(string address) + { + // the assignment bumps the refcount + Error error = new Error(); + error.Init(); + RawConnection = dbus_connection_open(address, ref error); + if (RawConnection != IntPtr.Zero) { + dbus_connection_unref(RawConnection); + } else { + throw new DBusException(error); + } + + SetupWithMain(); + } + + public void Flush() + { + dbus_connection_flush(RawConnection); + } + + public void SetupWithMain() + { + dbus_connection_setup_with_g_main(RawConnection, IntPtr.Zero); + } + + ~Connection () + { + if (RawConnection != IntPtr.Zero) + { + dbus_connection_disconnect(rawConnection); + } + RawConnection = IntPtr.Zero; // free the native object + } + + internal static Connection Wrap(IntPtr rawConnection) + { + if (slot > -1) { + // Maybe we already have a Connection object associated with + // this rawConnection then return it + IntPtr rawThis = dbus_connection_get_data (rawConnection, slot); + if (rawThis != IntPtr.Zero) { + return (DBus.Connection) ((GCHandle)rawThis).Target; + } + } + + // If it doesn't exist then create a new connection around it + return new Connection(rawConnection); + } + + public int Timeout + { + get + { + return this.timeout; + } + set + { + this.timeout = value; + } + } + + 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; + } + } + + 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); + } + } + } + + [DllImport("dbus-glib-1")] + private extern static void dbus_connection_setup_with_g_main(IntPtr rawConnection, + IntPtr rawContext); + + [DllImport ("dbus-1")] + private extern static IntPtr dbus_connection_open (string address, ref Error error); + + [DllImport ("dbus-1")] + private extern static void dbus_connection_unref (IntPtr ptr); + + [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); + + [DllImport ("dbus-1")] + private extern static IntPtr dbus_connection_get_data (IntPtr ptr, + int slot); + + [DllImport ("dbus-1")] + private extern static void dbus_connection_disconnect (IntPtr ptr); + } +} diff --git a/mono/DBusException.cs b/mono/DBusException.cs index 41df7be..5c912cc 100644 --- a/mono/DBusException.cs +++ b/mono/DBusException.cs @@ -1,12 +1,12 @@ -namespace DBus
-{
- using System;
- using System.Runtime.InteropServices;
-
- public class DBusException : ApplicationException
- {
- internal DBusException (Error error) : base (error.Message) {
- error.Free();
- }
- }
-}
+namespace DBus +{ + using System; + using System.Runtime.InteropServices; + + public class DBusException : ApplicationException + { + internal DBusException (Error error) : base (error.Message) { + error.Free(); + } + } +} diff --git a/mono/DBusType/Array.cs b/mono/DBusType/Array.cs index 55f79c8..bf41763 100644 --- a/mono/DBusType/Array.cs +++ b/mono/DBusType/Array.cs @@ -1,138 +1,138 @@ -using System;
-using System.Collections;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// Array.
- /// </summary>
- public class Array : IDBusType
- {
- public const char Code = 'a';
- private System.Array val;
- private ArrayList elements;
- private Type elementType;
- private Service service = null;
-
- private Array()
- {
- }
-
- public Array(System.Array val, Service service)
- {
- this.val = val;
- this.elementType = Arguments.MatchType(val.GetType().UnderlyingSystemType);
- this.service = service;
- }
-
- public Array(IntPtr iter, Service service)
- {
- this.service = service;
-
- IntPtr arrayIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);
-
- int elementTypeCode;
- bool notEmpty = dbus_message_iter_init_array_iterator(iter, arrayIter, out elementTypeCode);
- this.elementType = (Type) Arguments.DBusTypes[(char) elementTypeCode];
-
- elements = new ArrayList();
-
- if (notEmpty) {
- do {
- object [] pars = new Object[2];
- pars[0] = arrayIter;
- pars[1] = service;
- DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
- elements.Add(dbusType);
- } while (dbus_message_iter_next(arrayIter));
- }
-
- Marshal.FreeCoTaskMem(arrayIter);
- }
-
- public void Append(IntPtr iter)
- {
- IntPtr arrayIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);
-
- if (!dbus_message_iter_append_array(iter,
- arrayIter,
- (int) Arguments.GetCode(this.elementType))) {
- throw new ApplicationException("Failed to append INT32 argument:" + val);
- }
-
- foreach (object element in this.val) {
- object [] pars = new Object[2];
- pars[0] = element;
- pars[1] = this.service;
- DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
- dbusType.Append(arrayIter);
- }
-
- Marshal.FreeCoTaskMem(arrayIter);
- }
-
- public static bool Suits(System.Type type)
- {
- if (type.IsArray) {
- return true;
- }
-
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_Ref);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Castclass, type);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_Ref);
- }
- }
-
- public object Get()
- {
- throw new ArgumentException("Cannot call Get on an Array without specifying type.");
- }
-
- public object Get(System.Type type)
- {
- if (Arguments.Suits(elementType, type.UnderlyingSystemType)) {
- this.val = System.Array.CreateInstance(type.UnderlyingSystemType, elements.Count);
- int i = 0;
- foreach (DBusType.IDBusType element in elements) {
- this.val.SetValue(element.Get(type.UnderlyingSystemType), i++);
- }
- } else {
- throw new ArgumentException("Cannot cast DBus.Type.Array to type '" + type.ToString() + "'");
- }
-
- return this.val;
- }
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_init_array_iterator(IntPtr iter,
- IntPtr arrayIter,
- out int elementType);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_array(IntPtr iter,
- IntPtr arrayIter,
- int elementType);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_has_next(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_next(IntPtr iter);
- }
-}
+using System; +using System.Collections; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// Array. + /// </summary> + public class Array : IDBusType + { + public const char Code = 'a'; + private System.Array val; + private ArrayList elements; + private Type elementType; + private Service service = null; + + private Array() + { + } + + public Array(System.Array val, Service service) + { + this.val = val; + this.elementType = Arguments.MatchType(val.GetType().UnderlyingSystemType); + this.service = service; + } + + public Array(IntPtr iter, Service service) + { + this.service = service; + + IntPtr arrayIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize); + + int elementTypeCode; + bool notEmpty = dbus_message_iter_init_array_iterator(iter, arrayIter, out elementTypeCode); + this.elementType = (Type) Arguments.DBusTypes[(char) elementTypeCode]; + + elements = new ArrayList(); + + if (notEmpty) { + do { + object [] pars = new Object[2]; + pars[0] = arrayIter; + pars[1] = service; + DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars); + elements.Add(dbusType); + } while (dbus_message_iter_next(arrayIter)); + } + + Marshal.FreeCoTaskMem(arrayIter); + } + + public void Append(IntPtr iter) + { + IntPtr arrayIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize); + + if (!dbus_message_iter_append_array(iter, + arrayIter, + (int) Arguments.GetCode(this.elementType))) { + throw new ApplicationException("Failed to append INT32 argument:" + val); + } + + foreach (object element in this.val) { + object [] pars = new Object[2]; + pars[0] = element; + pars[1] = this.service; + DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars); + dbusType.Append(arrayIter); + } + + Marshal.FreeCoTaskMem(arrayIter); + } + + public static bool Suits(System.Type type) + { + if (type.IsArray) { + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_Ref); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Castclass, type); + if (!isReturn) { + generator.Emit(OpCodes.Stind_Ref); + } + } + + public object Get() + { + throw new ArgumentException("Cannot call Get on an Array without specifying type."); + } + + public object Get(System.Type type) + { + if (Arguments.Suits(elementType, type.UnderlyingSystemType)) { + this.val = System.Array.CreateInstance(type.UnderlyingSystemType, elements.Count); + int i = 0; + foreach (DBusType.IDBusType element in elements) { + this.val.SetValue(element.Get(type.UnderlyingSystemType), i++); + } + } else { + throw new ArgumentException("Cannot cast DBus.Type.Array to type '" + type.ToString() + "'"); + } + + return this.val; + } + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_init_array_iterator(IntPtr iter, + IntPtr arrayIter, + out int elementType); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_array(IntPtr iter, + IntPtr arrayIter, + int elementType); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_has_next(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_next(IntPtr iter); + } +} diff --git a/mono/DBusType/Boolean.cs b/mono/DBusType/Boolean.cs index ea2a391..fa5e1bc 100644 --- a/mono/DBusType/Boolean.cs +++ b/mono/DBusType/Boolean.cs @@ -1,86 +1,86 @@ -using System;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// Boolean
- /// </summary>
- public class Boolean : IDBusType
- {
- public const char Code = 'b';
- private System.Boolean val;
-
- private Boolean()
- {
- }
-
- public Boolean(System.Boolean val, Service service)
- {
- this.val = val;
- }
-
- public Boolean(IntPtr iter, Service service)
- {
- this.val = dbus_message_iter_get_boolean(iter);
- }
-
- public void Append(IntPtr iter)
- {
- if (!dbus_message_iter_append_boolean(iter, val))
- throw new ApplicationException("Failed to append BOOLEAN argument:" + val);
- }
-
- public static bool Suits(System.Type type)
- {
- switch (type.ToString()) {
- case "System.Boolean":
- case "System.Boolean&":
- return true;
- }
-
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_I1);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Unbox, type);
- generator.Emit(OpCodes.Ldind_I1);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_I1);
- }
- }
-
- public object Get()
- {
- return this.val;
- }
-
- public object Get(System.Type type)
- {
- switch (type.ToString()) {
- case "System.Boolean":
- case "System.Boolean&":
- return this.val;
- default:
- throw new ArgumentException("Cannot cast DBus.Type.Boolean to type '" + type.ToString() + "'");
- }
- }
-
- [DllImport("dbus-1")]
- private extern static System.Boolean dbus_message_iter_get_boolean(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_boolean(IntPtr iter, System.Boolean value);
- }
-}
+using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// Boolean + /// </summary> + public class Boolean : IDBusType + { + public const char Code = 'b'; + private System.Boolean val; + + private Boolean() + { + } + + public Boolean(System.Boolean val, Service service) + { + this.val = val; + } + + public Boolean(IntPtr iter, Service service) + { + this.val = dbus_message_iter_get_boolean(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_boolean(iter, val)) + throw new ApplicationException("Failed to append BOOLEAN argument:" + val); + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "System.Boolean": + case "System.Boolean&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_I1); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_I1); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I1); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) { + case "System.Boolean": + case "System.Boolean&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.Boolean to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.Boolean dbus_message_iter_get_boolean(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_boolean(IntPtr iter, System.Boolean value); + } +} diff --git a/mono/DBusType/Byte.cs b/mono/DBusType/Byte.cs index 08aabf5..a3d0ac9 100644 --- a/mono/DBusType/Byte.cs +++ b/mono/DBusType/Byte.cs @@ -1,105 +1,105 @@ -using System;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// Byte
- /// </summary>
- public class Byte : IDBusType
- {
- public const char Code = 'y';
- private System.Byte val;
-
- private Byte()
- {
- }
-
- public Byte(System.Byte val, Service service)
- {
- this.val = val;
- }
-
- public Byte(System.Char val, Service service)
- {
- this.val = (byte) val;
- }
-
- public Byte(IntPtr iter, Service service)
- {
- this.val = dbus_message_iter_get_byte(iter);
- }
-
- public void Append(IntPtr iter)
- {
- if (!dbus_message_iter_append_byte(iter, val))
- throw new ApplicationException("Failed to append BYTE argument:" + val);
- }
-
- public static bool Suits(System.Type type)
- {
- if (type.IsEnum && type.UnderlyingSystemType == typeof(System.Byte)) {
- return true;
- }
-
- switch (type.ToString()) {
- case "System.Byte":
- case "System.Byte&":
- case "System.Char":
- case "System.Char&":
- return true;
- }
-
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_U1);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Unbox, type);
- generator.Emit(OpCodes.Ldind_U1);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_I1);
- }
- }
-
- public object Get()
- {
- return this.val;
- }
-
- public object Get(System.Type type)
- {
- if (type.IsEnum) {
- return Enum.ToObject(type, this.val);
- }
-
- switch (type.ToString()) {
- case "System.Byte":
- case "System.Byte&":
- return this.val;
- case "System.Char":
- case "System.Char&":
- char charVal = (char) this.val;
- return charVal;
- default:
- throw new ArgumentException("Cannot cast DBus.Type.Byte to type '" + type.ToString() + "'");
- }
- }
-
- [DllImport("dbus-1")]
- private extern static System.Byte dbus_message_iter_get_byte(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_byte(IntPtr iter, System.Byte value);
- }
-}
+using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// Byte + /// </summary> + public class Byte : IDBusType + { + public const char Code = 'y'; + private System.Byte val; + + private Byte() + { + } + + public Byte(System.Byte val, Service service) + { + this.val = val; + } + + public Byte(System.Char val, Service service) + { + this.val = (byte) val; + } + + public Byte(IntPtr iter, Service service) + { + this.val = dbus_message_iter_get_byte(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_byte(iter, val)) + throw new ApplicationException("Failed to append BYTE argument:" + val); + } + + public static bool Suits(System.Type type) + { + if (type.IsEnum && type.UnderlyingSystemType == typeof(System.Byte)) { + return true; + } + + switch (type.ToString()) { + case "System.Byte": + case "System.Byte&": + case "System.Char": + case "System.Char&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_U1); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_U1); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I1); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + if (type.IsEnum) { + return Enum.ToObject(type, this.val); + } + + switch (type.ToString()) { + case "System.Byte": + case "System.Byte&": + return this.val; + case "System.Char": + case "System.Char&": + char charVal = (char) this.val; + return charVal; + default: + throw new ArgumentException("Cannot cast DBus.Type.Byte to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.Byte dbus_message_iter_get_byte(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_byte(IntPtr iter, System.Byte value); + } +} diff --git a/mono/DBusType/Custom.cs b/mono/DBusType/Custom.cs index 4bd5243..9256064 100644 --- a/mono/DBusType/Custom.cs +++ b/mono/DBusType/Custom.cs @@ -1,109 +1,109 @@ -using System;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// A named byte array, used for custom types.
- /// </summary>
- public class Custom : IDBusType
- {
- public const char Code = 'c';
- private DBus.Custom val;
-
- private Custom()
- {
- }
-
- public Custom(DBus.Custom val, Service service)
- {
- this.val = val;
- }
-
- public Custom(IntPtr iter, Service service)
- {
- string name;
- IntPtr value;
- int len;
-
- if (!dbus_message_iter_get_custom(iter, out name, out value, out len)) {
- throw new ApplicationException("Failed to get CUSTOM argument.");
- }
-
- this.val.Name = name;
- this.val.Data = new byte[len];
- Marshal.Copy(value, this.val.Data, 0, len);
- }
-
- public void Append(IntPtr iter)
- {
- IntPtr data = Marshal.AllocCoTaskMem(this.val.Data.Length);
- try {
- Marshal.Copy(this.val.Data, 0, data, this.val.Data.Length);
- if (!dbus_message_iter_append_custom(iter, this.val.Name, data, this.val.Data.Length)) {
- throw new ApplicationException("Failed to append CUSTOM argument:" + val);
- }
- } finally {
- Marshal.FreeCoTaskMem(data);
- }
- }
-
- public static bool Suits(System.Type type)
- {
- switch (type.ToString()) {
- case "DBus.Custom":
- case "DBus.Custom&":
- return true;
- }
-
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldobj, type);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Unbox, type);
- generator.Emit(OpCodes.Ldobj, type);
- if (!isReturn) {
- generator.Emit(OpCodes.Stobj, type);
- }
- }
-
- public object Get()
- {
- return this.val;
- }
-
- public object Get(System.Type type)
- {
- switch (type.ToString()) {
- case "DBus.Custom":
- case "DBus.Custom&":
- return this.val;
- default:
- throw new ArgumentException("Cannot cast DBus.Type.Custom to type '" + type.ToString() + "'");
- }
- }
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_get_custom(IntPtr iter,
- out string name,
- out IntPtr value,
- out int len);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_custom(IntPtr iter,
- string name,
- IntPtr data,
- int len);
- }
-}
+using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// A named byte array, used for custom types. + /// </summary> + public class Custom : IDBusType + { + public const char Code = 'c'; + private DBus.Custom val; + + private Custom() + { + } + + public Custom(DBus.Custom val, Service service) + { + this.val = val; + } + + public Custom(IntPtr iter, Service service) + { + string name; + IntPtr value; + int len; + + if (!dbus_message_iter_get_custom(iter, out name, out value, out len)) { + throw new ApplicationException("Failed to get CUSTOM argument."); + } + + this.val.Name = name; + this.val.Data = new byte[len]; + Marshal.Copy(value, this.val.Data, 0, len); + } + + public void Append(IntPtr iter) + { + IntPtr data = Marshal.AllocCoTaskMem(this.val.Data.Length); + try { + Marshal.Copy(this.val.Data, 0, data, this.val.Data.Length); + if (!dbus_message_iter_append_custom(iter, this.val.Name, data, this.val.Data.Length)) { + throw new ApplicationException("Failed to append CUSTOM argument:" + val); + } + } finally { + Marshal.FreeCoTaskMem(data); + } + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "DBus.Custom": + case "DBus.Custom&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldobj, type); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldobj, type); + if (!isReturn) { + generator.Emit(OpCodes.Stobj, type); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) { + case "DBus.Custom": + case "DBus.Custom&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.Custom to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_get_custom(IntPtr iter, + out string name, + out IntPtr value, + out int len); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_custom(IntPtr iter, + string name, + IntPtr data, + int len); + } +} diff --git a/mono/DBusType/Dict.cs b/mono/DBusType/Dict.cs index da2103d..f5c76b4 100644 --- a/mono/DBusType/Dict.cs +++ b/mono/DBusType/Dict.cs @@ -1,147 +1,147 @@ -using System;
-using System.Collections;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// Dict.
- /// </summary>
- public class Dict : IDBusType
- {
- public const char Code = 'm';
- private Hashtable val;
-
- private Dict()
- {
- }
-
- public Dict(IDictionary val, Service service)
- {
- this.val = new Hashtable();
- foreach (DictionaryEntry entry in val) {
- this.val.Add(entry.Key, entry.Value);
- }
- }
-
- public Dict(IntPtr iter, Service service)
- {
- IntPtr dictIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);
-
- bool notEmpty = dbus_message_iter_init_dict_iterator(iter, dictIter);
-
- this.val = new Hashtable();
-
- if (notEmpty) {
- do {
- string key = dbus_message_iter_get_dict_key(dictIter);
-
- // Get the argument type and get the value
- Type elementType = (Type) DBus.Arguments.DBusTypes[(char) dbus_message_iter_get_arg_type(dictIter)];
- object [] pars = new Object[1];
- pars[0] = dictIter;
- DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
- this.val.Add(key, dbusType);
- } while (dbus_message_iter_next(dictIter));
- }
-
- Marshal.FreeCoTaskMem(dictIter);
- }
-
- public void Append(IntPtr iter)
- {
- IntPtr dictIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);
-
- if (!dbus_message_iter_append_dict(iter,
- dictIter)) {
- throw new ApplicationException("Failed to append DICT argument:" + val);
- }
-
- foreach (DictionaryEntry entry in this.val) {
- if (!dbus_message_iter_append_dict_key(dictIter, (string) entry.Key)) {
- throw new ApplicationException("Failed to append DICT key:" + entry.Key);
- }
-
- // Get the element type
- Type elementType = Arguments.MatchType(entry.Value.GetType());
- object [] pars = new Object[1];
- pars[0] = entry.Value;
- DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
- dbusType.Append(dictIter);
- }
-
- Marshal.FreeCoTaskMem(dictIter);
- }
-
- public static bool Suits(System.Type type)
- {
- if (typeof(IDictionary).IsAssignableFrom(type)) {
- return true;
- }
-
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_Ref);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Castclass, type);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_Ref);
- }
- }
-
- public object Get()
- {
- return Get(typeof(Hashtable));
- }
-
- public object Get(System.Type type)
- {
- IDictionary retVal;
-
- if (Suits(type)) {
- retVal = (IDictionary) Activator.CreateInstance(type, new object[0]);
- foreach (DictionaryEntry entry in this.val) {
- retVal.Add(entry.Key, ((IDBusType) entry.Value).Get());
- }
- } else {
- throw new ArgumentException("Cannot cast DBus.Type.Dict to type '" + type.ToString() + "'");
- }
-
- return retVal;
- }
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_init_dict_iterator(IntPtr iter,
- IntPtr dictIter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_dict(IntPtr iter,
- IntPtr dictIter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_has_next(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_next(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static string dbus_message_iter_get_dict_key (IntPtr dictIter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_dict_key (IntPtr dictIter,
- string value);
- [DllImport("dbus-1")]
- private extern static int dbus_message_iter_get_arg_type(IntPtr iter);
- }
-}
+using System; +using System.Collections; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// Dict. + /// </summary> + public class Dict : IDBusType + { + public const char Code = 'm'; + private Hashtable val; + + private Dict() + { + } + + public Dict(IDictionary val, Service service) + { + this.val = new Hashtable(); + foreach (DictionaryEntry entry in val) { + this.val.Add(entry.Key, entry.Value); + } + } + + public Dict(IntPtr iter, Service service) + { + IntPtr dictIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize); + + bool notEmpty = dbus_message_iter_init_dict_iterator(iter, dictIter); + + this.val = new Hashtable(); + + if (notEmpty) { + do { + string key = dbus_message_iter_get_dict_key(dictIter); + + // Get the argument type and get the value + Type elementType = (Type) DBus.Arguments.DBusTypes[(char) dbus_message_iter_get_arg_type(dictIter)]; + object [] pars = new Object[1]; + pars[0] = dictIter; + DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars); + this.val.Add(key, dbusType); + } while (dbus_message_iter_next(dictIter)); + } + + Marshal.FreeCoTaskMem(dictIter); + } + + public void Append(IntPtr iter) + { + IntPtr dictIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize); + + if (!dbus_message_iter_append_dict(iter, + dictIter)) { + throw new ApplicationException("Failed to append DICT argument:" + val); + } + + foreach (DictionaryEntry entry in this.val) { + if (!dbus_message_iter_append_dict_key(dictIter, (string) entry.Key)) { + throw new ApplicationException("Failed to append DICT key:" + entry.Key); + } + + // Get the element type + Type elementType = Arguments.MatchType(entry.Value.GetType()); + object [] pars = new Object[1]; + pars[0] = entry.Value; + DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars); + dbusType.Append(dictIter); + } + + Marshal.FreeCoTaskMem(dictIter); + } + + public static bool Suits(System.Type type) + { + if (typeof(IDictionary).IsAssignableFrom(type)) { + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_Ref); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Castclass, type); + if (!isReturn) { + generator.Emit(OpCodes.Stind_Ref); + } + } + + public object Get() + { + return Get(typeof(Hashtable)); + } + + public object Get(System.Type type) + { + IDictionary retVal; + + if (Suits(type)) { + retVal = (IDictionary) Activator.CreateInstance(type, new object[0]); + foreach (DictionaryEntry entry in this.val) { + retVal.Add(entry.Key, ((IDBusType) entry.Value).Get()); + } + } else { + throw new ArgumentException("Cannot cast DBus.Type.Dict to type '" + type.ToString() + "'"); + } + + return retVal; + } + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_init_dict_iterator(IntPtr iter, + IntPtr dictIter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_dict(IntPtr iter, + IntPtr dictIter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_has_next(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_next(IntPtr iter); + + [DllImport("dbus-1")] + private extern static string dbus_message_iter_get_dict_key (IntPtr dictIter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_dict_key (IntPtr dictIter, + string value); + [DllImport("dbus-1")] + private extern static int dbus_message_iter_get_arg_type(IntPtr iter); + } +} diff --git a/mono/DBusType/Double.cs b/mono/DBusType/Double.cs index 37f05e2..008f682 100644 --- a/mono/DBusType/Double.cs +++ b/mono/DBusType/Double.cs @@ -1,86 +1,86 @@ -using System;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// IEEE 754 double
- /// </summary>
- public class Double : IDBusType
- {
- public const char Code = 'd';
- private System.Double val;
-
- private Double()
- {
- }
-
- public Double(System.Double val, Service service)
- {
- this.val = val;
- }
-
- public Double(IntPtr iter, Service service)
- {
- this.val = dbus_message_iter_get_double(iter);
- }
-
- public void Append(IntPtr iter)
- {
- if (!dbus_message_iter_append_double(iter, val))
- throw new ApplicationException("Failed to append DOUBLE argument:" + val);
- }
-
- public static bool Suits(System.Type type)
- {
- switch (type.ToString()) {
- case "System.Double":
- case "System.Double&":
- return true;
- }
-
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_R8);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Unbox, type);
- generator.Emit(OpCodes.Ldind_R8);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_R8);
- }
- }
-
- public object Get()
- {
- return this.val;
- }
-
- public object Get(System.Type type)
- {
- switch (type.ToString()) {
- case "System.Double":
- case "System.Double&":
- return this.val;
- default:
- throw new ArgumentException("Cannot cast DBus.Type.Double to type '" + type.ToString() + "'");
- }
- }
-
- [DllImport("dbus-1")]
- private extern static System.Double dbus_message_iter_get_double(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_double(IntPtr iter, System.Double value);
- }
-}
+using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// IEEE 754 double + /// </summary> + public class Double : IDBusType + { + public const char Code = 'd'; + private System.Double val; + + private Double() + { + } + + public Double(System.Double val, Service service) + { + this.val = val; + } + + public Double(IntPtr iter, Service service) + { + this.val = dbus_message_iter_get_double(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_double(iter, val)) + throw new ApplicationException("Failed to append DOUBLE argument:" + val); + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "System.Double": + case "System.Double&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_R8); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_R8); + if (!isReturn) { + generator.Emit(OpCodes.Stind_R8); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) { + case "System.Double": + case "System.Double&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.Double to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.Double dbus_message_iter_get_double(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_double(IntPtr iter, System.Double value); + } +} diff --git a/mono/DBusType/IDBusType.cs b/mono/DBusType/IDBusType.cs index f273dda..447c820 100644 --- a/mono/DBusType/IDBusType.cs +++ b/mono/DBusType/IDBusType.cs @@ -1,16 +1,16 @@ -using System;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// Base class for DBusTypes
- /// </summary>
- public interface IDBusType
- {
- object Get();
-
- object Get(System.Type type);
-
- void Append(IntPtr iter);
- }
-}
+using System; + +namespace DBus.DBusType +{ + /// <summary> + /// Base class for DBusTypes + /// </summary> + public interface IDBusType + { + object Get(); + + object Get(System.Type type); + + void Append(IntPtr iter); + } +} diff --git a/mono/DBusType/Int32.cs b/mono/DBusType/Int32.cs index 5decf6e..681a55f 100644 --- a/mono/DBusType/Int32.cs +++ b/mono/DBusType/Int32.cs @@ -1,93 +1,93 @@ -using System;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// 32-bit integer.
- /// </summary>
- public class Int32 : IDBusType
- {
- public const char Code = 'i';
- private System.Int32 val;
-
- private Int32()
- {
- }
-
- public Int32(System.Int32 val, Service service)
- {
- this.val = val;
- }
-
- public Int32(IntPtr iter, Service service)
- {
- this.val = dbus_message_iter_get_int32(iter);
- }
-
- public void Append(IntPtr iter)
- {
- if (!dbus_message_iter_append_int32(iter, val))
- throw new ApplicationException("Failed to append INT32 argument:" + val);
- }
-
- public static bool Suits(System.Type type)
- {
- if (type.IsEnum && type.UnderlyingSystemType == typeof(System.Int32)) {
- return true;
- }
-
- switch (type.ToString()) {
- case "System.Int32":
- case "System.Int32&":
- return true; }
-
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_I4);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Unbox, type);
- generator.Emit(OpCodes.Ldind_I4);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_I4);
- }
- }
-
- public object Get()
- {
- return this.val;
- }
-
- public object Get(System.Type type)
- {
- if (type.IsEnum) {
- return Enum.ToObject(type, this.val);
- }
-
- switch (type.ToString()) {
- case "System.Int32":
- case "System.Int32&":
- return this.val;
- default:
- throw new ArgumentException("Cannot cast DBus.Type.Int32 to type '" + type.ToString() + "'");
- }
- }
-
- [DllImport("dbus-1")]
- private extern static System.Int32 dbus_message_iter_get_int32(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_int32(IntPtr iter, System.Int32 value);
- }
-}
+using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// 32-bit integer. + /// </summary> + public class Int32 : IDBusType + { + public const char Code = 'i'; + private System.Int32 val; + + private Int32() + { + } + + public Int32(System.Int32 val, Service service) + { + this.val = val; + } + + public Int32(IntPtr iter, Service service) + { + this.val = dbus_message_iter_get_int32(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_int32(iter, val)) + throw new ApplicationException("Failed to append INT32 argument:" + val); + } + + public static bool Suits(System.Type type) + { + if (type.IsEnum && type.UnderlyingSystemType == typeof(System.Int32)) { + return true; + } + + switch (type.ToString()) { + case "System.Int32": + case "System.Int32&": + return true; } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_I4); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_I4); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I4); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + if (type.IsEnum) { + return Enum.ToObject(type, this.val); + } + + switch (type.ToString()) { + case "System.Int32": + case "System.Int32&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.Int32 to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.Int32 dbus_message_iter_get_int32(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_int32(IntPtr iter, System.Int32 value); + } +} diff --git a/mono/DBusType/Int64.cs b/mono/DBusType/Int64.cs index 368fdf6..741d272 100644 --- a/mono/DBusType/Int64.cs +++ b/mono/DBusType/Int64.cs @@ -1,94 +1,94 @@ -using System;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// 64-bit integer.
- /// </summary>
- public class Int64 : IDBusType
- {
- public const char Code = 'x';
- private System.Int64 val;
-
- private Int64()
- {
- }
-
- public Int64(System.Int64 val, Service service)
- {
- this.val = val;
- }
-
- public Int64(IntPtr iter, Service service)
- {
- this.val = dbus_message_iter_get_int64(iter);
- }
-
- public void Append(IntPtr iter)
- {
- if (!dbus_message_iter_append_int64(iter, val))
- throw new ApplicationException("Failed to append INT64 argument:" + val);
- }
-
- public static bool Suits(System.Type type)
- {
- if (type.IsEnum && type.UnderlyingSystemType == typeof(System.Int64)) {
- return true;
- }
-
- switch (type.ToString()) {
- case "System.Int64":
- case "System.Int64&":
- return true;
- }
-
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_I8);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Unbox, type);
- generator.Emit(OpCodes.Ldind_I8);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_I8);
- }
- }
-
- public object Get()
- {
- return this.val;
- }
-
- public object Get(System.Type type)
- {
- if (type.IsEnum) {
- return Enum.ToObject(type, this.val);
- }
-
- switch (type.ToString()) {
- case "System.Int64":
- case "System.Int64&":
- return this.val;
- default:
- throw new ArgumentException("Cannot cast DBus.Type.Int64 to type '" + type.ToString() + "'");
- }
- }
-
- [DllImport("dbus-1")]
- private extern static System.Int64 dbus_message_iter_get_int64(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_int64(IntPtr iter, System.Int64 value);
- }
-}
+using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// 64-bit integer. + /// </summary> + public class Int64 : IDBusType + { + public const char Code = 'x'; + private System.Int64 val; + + private Int64() + { + } + + public Int64(System.Int64 val, Service service) + { + this.val = val; + } + + public Int64(IntPtr iter, Service service) + { + this.val = dbus_message_iter_get_int64(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_int64(iter, val)) + throw new ApplicationException("Failed to append INT64 argument:" + val); + } + + public static bool Suits(System.Type type) + { + if (type.IsEnum && type.UnderlyingSystemType == typeof(System.Int64)) { + return true; + } + + switch (type.ToString()) { + case "System.Int64": + case "System.Int64&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_I8); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_I8); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I8); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + if (type.IsEnum) { + return Enum.ToObject(type, this.val); + } + + switch (type.ToString()) { + case "System.Int64": + case "System.Int64&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.Int64 to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.Int64 dbus_message_iter_get_int64(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_int64(IntPtr iter, System.Int64 value); + } +} diff --git a/mono/DBusType/Nil.cs b/mono/DBusType/Nil.cs index bbd538e..a271db3 100644 --- a/mono/DBusType/Nil.cs +++ b/mono/DBusType/Nil.cs @@ -1,68 +1,68 @@ -using System;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// Marks a "void"/"unset"/"nonexistent"/"null" argument.
- /// </summary>
- public class Nil : IDBusType
- {
- public const char Code = 'v';
-
- private Nil()
- {
- }
-
- public Nil(object nil, Service service)
- {
- }
-
- public Nil(IntPtr iter, Service service)
- {
- }
-
- public void Append(IntPtr iter)
- {
- if (!dbus_message_iter_append_nil(iter))
- throw new ApplicationException("Failed to append NIL argument");
- }
-
- public static bool Suits(System.Type type)
- {
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_I1);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Unbox, type);
- generator.Emit(OpCodes.Ldind_I1);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_I1);
- }
- }
-
- public object Get()
- {
- return null;
- }
-
- public object Get(System.Type type)
- {
- throw new ArgumentException("Cannot cast DBus.Type.Nil to type '" + type.ToString() + "'");
- }
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_nil(IntPtr iter);
- }
-}
+using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// Marks a "void"/"unset"/"nonexistent"/"null" argument. + /// </summary> + public class Nil : IDBusType + { + public const char Code = 'v'; + + private Nil() + { + } + + public Nil(object nil, Service service) + { + } + + public Nil(IntPtr iter, Service service) + { + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_nil(iter)) + throw new ApplicationException("Failed to append NIL argument"); + } + + public static bool Suits(System.Type type) + { + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_I1); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_I1); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I1); + } + } + + public object Get() + { + return null; + } + + public object Get(System.Type type) + { + throw new ArgumentException("Cannot cast DBus.Type.Nil to type '" + type.ToString() + "'"); + } + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_nil(IntPtr iter); + } +} diff --git a/mono/DBusType/ObjectPath.cs b/mono/DBusType/ObjectPath.cs index a7014b5..f82c680 100644 --- a/mono/DBusType/ObjectPath.cs +++ b/mono/DBusType/ObjectPath.cs @@ -1,99 +1,99 @@ -using System;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// An object path.
- /// </summary>
- public class ObjectPath : IDBusType
- {
- public const char Code = 'o';
- private string path = null;
- private object val = null;
- private Service service = null;
-
- private ObjectPath()
- {
- }
-
- public ObjectPath(object val, Service service)
- {
- this.val = val;
- this.service = service;
- }
-
- public ObjectPath(IntPtr iter, Service service)
- {
-
- this.path = Marshal.PtrToStringAnsi(dbus_message_iter_get_object_path(iter));
- this.service = service;
- }
-
- private string Path
- {
- get {
- if (this.path == null && this.val != null) {
- Handler handler = this.service.GetHandler(this.val);
- this.path = handler.Path;
- }
-
- return this.path;
- }
- }
-
- public void Append(IntPtr iter)
- {
- if (!dbus_message_iter_append_object_path(iter, Marshal.StringToHGlobalAnsi(Path)))
- throw new ApplicationException("Failed to append OBJECT_PATH argument:" + val);
- }
-
- public static bool Suits(System.Type type)
- {
- object[] attributes = type.GetCustomAttributes(typeof(InterfaceAttribute), false);
- if (attributes.Length == 1) {
- return true;
- } else {
- return false;
- }
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_Ref);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Castclass, type);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_Ref);
- }
- }
-
- public object Get()
- {
- throw new ArgumentException("Cannot call Get on an ObjectPath without specifying type.");
- }
-
- public object Get(System.Type type)
- {
- try {
- return this.service.GetObject(type, Path);
- } catch(Exception ex) {
- throw new ArgumentException("Cannot cast object pointed to by Object Path to type '" + type.ToString() + "': " + ex);
- }
- }
-
- [DllImport("dbus-1")]
- private extern static IntPtr dbus_message_iter_get_object_path(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_object_path(IntPtr iter, IntPtr path);
- }
-}
+using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// An object path. + /// </summary> + public class ObjectPath : IDBusType + { + public const char Code = 'o'; + private string path = null; + private object val = null; + private Service service = null; + + private ObjectPath() + { + } + + public ObjectPath(object val, Service service) + { + this.val = val; + this.service = service; + } + + public ObjectPath(IntPtr iter, Service service) + { + + this.path = Marshal.PtrToStringAnsi(dbus_message_iter_get_object_path(iter)); + this.service = service; + } + + private string Path + { + get { + if (this.path == null && this.val != null) { + Handler handler = this.service.GetHandler(this.val); + this.path = handler.Path; + } + + return this.path; + } + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_object_path(iter, Marshal.StringToHGlobalAnsi(Path))) + throw new ApplicationException("Failed to append OBJECT_PATH argument:" + val); + } + + public static bool Suits(System.Type type) + { + object[] attributes = type.GetCustomAttributes(typeof(InterfaceAttribute), false); + if (attributes.Length == 1) { + return true; + } else { + return false; + } + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_Ref); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Castclass, type); + if (!isReturn) { + generator.Emit(OpCodes.Stind_Ref); + } + } + + public object Get() + { + throw new ArgumentException("Cannot call Get on an ObjectPath without specifying type."); + } + + public object Get(System.Type type) + { + try { + return this.service.GetObject(type, Path); + } catch(Exception ex) { + throw new ArgumentException("Cannot cast object pointed to by Object Path to type '" + type.ToString() + "': " + ex); + } + } + + [DllImport("dbus-1")] + private extern static IntPtr dbus_message_iter_get_object_path(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_object_path(IntPtr iter, IntPtr path); + } +} diff --git a/mono/DBusType/String.cs b/mono/DBusType/String.cs index 55213aa..bced310 100644 --- a/mono/DBusType/String.cs +++ b/mono/DBusType/String.cs @@ -1,86 +1,86 @@ -using System;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// A string.
- /// </summary>
- public class String : IDBusType
- {
- public const char Code = 's';
- private string val;
-
- private String()
- {
- }
-
- public String(string val, Service service)
- {
- this.val = val;
- }
-
- public String(IntPtr iter, Service service)
- {
- this.val = Marshal.PtrToStringAnsi(dbus_message_iter_get_string(iter));
- }
-
- public void Append(IntPtr iter)
- {
- if (!dbus_message_iter_append_string(iter, Marshal.StringToHGlobalAnsi(val)))
- throw new ApplicationException("Failed to append STRING argument:" + val);
- }
-
- public static bool Suits(System.Type type)
- {
- switch (type.ToString()) {
- case "System.String":
- case "System.String&":
- return true;
- }
-
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_Ref);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Castclass, type);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_Ref);
- }
- }
-
- public object Get()
- {
- return this.val;
- }
-
- public object Get(System.Type type)
- {
- switch (type.ToString())
- {
- case "System.String":
- case "System.String&":
- return this.val;
- default:
- throw new ArgumentException("Cannot cast DBus.Type.String to type '" + type.ToString() + "'");
- }
- }
-
- [DllImport("dbus-1")]
- private extern static IntPtr dbus_message_iter_get_string(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_string(IntPtr iter, IntPtr value);
- }
-}
+using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// A string. + /// </summary> + public class String : IDBusType + { + public const char Code = 's'; + private string val; + + private String() + { + } + + public String(string val, Service service) + { + this.val = val; + } + + public String(IntPtr iter, Service service) + { + this.val = Marshal.PtrToStringAnsi(dbus_message_iter_get_string(iter)); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_string(iter, Marshal.StringToHGlobalAnsi(val))) + throw new ApplicationException("Failed to append STRING argument:" + val); + } + + public static bool Suits(System.Type type) + { + switch (type.ToString()) { + case "System.String": + case "System.String&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_Ref); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Castclass, type); + if (!isReturn) { + generator.Emit(OpCodes.Stind_Ref); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + switch (type.ToString()) + { + case "System.String": + case "System.String&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.String to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static IntPtr dbus_message_iter_get_string(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_string(IntPtr iter, IntPtr value); + } +} diff --git a/mono/DBusType/UInt32.cs b/mono/DBusType/UInt32.cs index b09bafb..e97aa37 100644 --- a/mono/DBusType/UInt32.cs +++ b/mono/DBusType/UInt32.cs @@ -1,95 +1,95 @@ -using System;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// 32-bit unsigned integer.
- /// </summary>
- public class UInt32 : IDBusType
- {
- public const char Code = 'u';
- private System.UInt32 val;
-
- private UInt32()
- {
- }
-
- public UInt32(System.UInt32 val, Service service)
- {
- this.val = val;
- }
-
- public UInt32(IntPtr iter, Service service)
- {
- this.val = dbus_message_iter_get_uint32(iter);
- }
-
- public void Append(IntPtr iter)
- {
- if (!dbus_message_iter_append_uint32(iter, val))
- throw new ApplicationException("Failed to append UINT32 argument:" + val);
- }
-
- public static bool Suits(System.Type type)
- {
- if (type.IsEnum && type.UnderlyingSystemType == typeof(System.UInt32)) {
- return true;
- }
-
- switch (type.ToString()) {
- case "System.UInt32":
- case "System.UInt32&":
- return true;
- }
-
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_U4);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Unbox, type);
- generator.Emit(OpCodes.Ldind_U4);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_I4);
- }
- }
-
- public object Get()
- {
- return this.val;
- }
-
- public object Get(System.Type type)
- {
- if (type.IsEnum) {
- return Enum.ToObject(type, this.val);
- }
-
- switch (type.ToString())
- {
- case "System.UInt32":
- case "System.UInt32&":
- return this.val;
- default:
- throw new ArgumentException("Cannot cast DBus.Type.UInt32 to type '" + type.ToString() + "'");
- }
- }
-
- [DllImport("dbus-1")]
- private extern static System.UInt32 dbus_message_iter_get_uint32(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_uint32(IntPtr iter, System.UInt32 value);
- }
-}
+using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// 32-bit unsigned integer. + /// </summary> + public class UInt32 : IDBusType + { + public const char Code = 'u'; + private System.UInt32 val; + + private UInt32() + { + } + + public UInt32(System.UInt32 val, Service service) + { + this.val = val; + } + + public UInt32(IntPtr iter, Service service) + { + this.val = dbus_message_iter_get_uint32(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_uint32(iter, val)) + throw new ApplicationException("Failed to append UINT32 argument:" + val); + } + + public static bool Suits(System.Type type) + { + if (type.IsEnum && type.UnderlyingSystemType == typeof(System.UInt32)) { + return true; + } + + switch (type.ToString()) { + case "System.UInt32": + case "System.UInt32&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_U4); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_U4); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I4); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + if (type.IsEnum) { + return Enum.ToObject(type, this.val); + } + + switch (type.ToString()) + { + case "System.UInt32": + case "System.UInt32&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.UInt32 to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.UInt32 dbus_message_iter_get_uint32(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_uint32(IntPtr iter, System.UInt32 value); + } +} diff --git a/mono/DBusType/UInt64.cs b/mono/DBusType/UInt64.cs index e1d4dba..84fef06 100644 --- a/mono/DBusType/UInt64.cs +++ b/mono/DBusType/UInt64.cs @@ -1,95 +1,95 @@ -using System;
-using System.Runtime.InteropServices;
-using System.Reflection.Emit;
-
-using DBus;
-
-namespace DBus.DBusType
-{
- /// <summary>
- /// 64-bit unsigned integer.
- /// </summary>
- public class UInt64 : IDBusType
- {
- public const char Code = 't';
- private System.UInt64 val;
-
- private UInt64()
- {
- }
-
- public UInt64(System.UInt64 val, Service service)
- {
- this.val = val;
- }
-
- public UInt64(IntPtr iter, Service service)
- {
- this.val = dbus_message_iter_get_uint64(iter);
- }
-
- public void Append(IntPtr iter)
- {
- if (!dbus_message_iter_append_uint64(iter, val))
- throw new ApplicationException("Failed to append UINT64 argument:" + val);
- }
-
- public static bool Suits(System.Type type)
- {
- if (type.IsEnum && type.UnderlyingSystemType == typeof(System.UInt64)) {
- return true;
- }
-
- switch (type.ToString()) {
- case "System.UInt64":
- case "System.UInt64&":
- return true;
- }
-
- return false;
- }
-
- public static void EmitMarshalIn(ILGenerator generator, Type type)
- {
- if (type.IsByRef) {
- generator.Emit(OpCodes.Ldind_I8);
- }
- }
-
- public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
- {
- generator.Emit(OpCodes.Unbox, type);
- generator.Emit(OpCodes.Ldind_I8);
- if (!isReturn) {
- generator.Emit(OpCodes.Stind_I8);
- }
- }
-
- public object Get()
- {
- return this.val;
- }
-
- public object Get(System.Type type)
- {
- if (type.IsEnum) {
- return Enum.ToObject(type, this.val);
- }
-
- switch (type.ToString())
- {
- case "System.UInt64":
- case "System.UInt64&":
- return this.val;
- default:
- throw new ArgumentException("Cannot cast DBus.Type.UInt64 to type '" + type.ToString() + "'");
- }
- }
-
- [DllImport("dbus-1")]
- private extern static System.UInt64 dbus_message_iter_get_uint64(IntPtr iter);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_iter_append_uint64(IntPtr iter, System.UInt64 value);
- }
-}
+using System; +using System.Runtime.InteropServices; +using System.Reflection.Emit; + +using DBus; + +namespace DBus.DBusType +{ + /// <summary> + /// 64-bit unsigned integer. + /// </summary> + public class UInt64 : IDBusType + { + public const char Code = 't'; + private System.UInt64 val; + + private UInt64() + { + } + + public UInt64(System.UInt64 val, Service service) + { + this.val = val; + } + + public UInt64(IntPtr iter, Service service) + { + this.val = dbus_message_iter_get_uint64(iter); + } + + public void Append(IntPtr iter) + { + if (!dbus_message_iter_append_uint64(iter, val)) + throw new ApplicationException("Failed to append UINT64 argument:" + val); + } + + public static bool Suits(System.Type type) + { + if (type.IsEnum && type.UnderlyingSystemType == typeof(System.UInt64)) { + return true; + } + + switch (type.ToString()) { + case "System.UInt64": + case "System.UInt64&": + return true; + } + + return false; + } + + public static void EmitMarshalIn(ILGenerator generator, Type type) + { + if (type.IsByRef) { + generator.Emit(OpCodes.Ldind_I8); + } + } + + public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) + { + generator.Emit(OpCodes.Unbox, type); + generator.Emit(OpCodes.Ldind_I8); + if (!isReturn) { + generator.Emit(OpCodes.Stind_I8); + } + } + + public object Get() + { + return this.val; + } + + public object Get(System.Type type) + { + if (type.IsEnum) { + return Enum.ToObject(type, this.val); + } + + switch (type.ToString()) + { + case "System.UInt64": + case "System.UInt64&": + return this.val; + default: + throw new ArgumentException("Cannot cast DBus.Type.UInt64 to type '" + type.ToString() + "'"); + } + } + + [DllImport("dbus-1")] + private extern static System.UInt64 dbus_message_iter_get_uint64(IntPtr iter); + + [DllImport("dbus-1")] + private extern static bool dbus_message_iter_append_uint64(IntPtr iter, System.UInt64 value); + } +} diff --git a/mono/Error.cs b/mono/Error.cs index 1340221..d89a013 100644 --- a/mono/Error.cs +++ b/mono/Error.cs @@ -1,60 +1,60 @@ -namespace DBus
-{
-
- using System;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
-
- // FIXME add code to verify that size of DBus.Error
- // matches the C code.
-
- [StructLayout (LayoutKind.Sequential)]
- internal struct Error
- {
- internal IntPtr name;
- internal IntPtr message;
- private int dummies;
- private IntPtr padding1;
-
- public void Init()
- {
- dbus_error_init(ref this);
- }
-
- public void Free()
- {
- dbus_error_free(ref this);
- }
-
- public string Message
- {
- get
- {
- return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(message);
- }
- }
-
- public string Name
- {
- get
- {
- return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(name);
- }
- }
-
- public bool IsSet
- {
- get
- {
- return (name != IntPtr.Zero);
- }
- }
-
-
- [DllImport ("dbus-1", EntryPoint="dbus_error_init")]
- private extern static void dbus_error_init (ref Error error);
-
- [DllImport ("dbus-1", EntryPoint="dbus_error_free")]
- private extern static void dbus_error_free (ref Error error);
- }
-}
+namespace DBus +{ + + using System; + using System.Runtime.InteropServices; + using System.Diagnostics; + + // FIXME add code to verify that size of DBus.Error + // matches the C code. + + [StructLayout (LayoutKind.Sequential)] + internal struct Error + { + internal IntPtr name; + internal IntPtr message; + private int dummies; + private IntPtr padding1; + + public void Init() + { + dbus_error_init(ref this); + } + + public void Free() + { + dbus_error_free(ref this); + } + + public string Message + { + get + { + return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(message); + } + } + + public string Name + { + get + { + return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(name); + } + } + + public bool IsSet + { + get + { + return (name != IntPtr.Zero); + } + } + + + [DllImport ("dbus-1", EntryPoint="dbus_error_init")] + private extern static void dbus_error_init (ref Error error); + + [DllImport ("dbus-1", EntryPoint="dbus_error_free")] + private extern static void dbus_error_free (ref Error error); + } +} diff --git a/mono/Message.cs b/mono/Message.cs index e88a98d..54472d1 100644 --- a/mono/Message.cs +++ b/mono/Message.cs @@ -1,377 +1,377 @@ -namespace DBus
-{
-
- using System;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using System.Collections;
-
- public class Message
- {
-
- /// <summary>
- /// A pointer to the underlying Message structure
- /// </summary>
- private IntPtr rawMessage;
-
- /// <summary>
- /// The current slot number
- /// </summary>
- private static int slot = -1;
-
- // Keep in sync with C
- public enum MessageType
- {
- Invalid = 0,
- MethodCall = 1,
- MethodReturn = 2,
- Error = 3,
- Signal = 4
- }
-
- private Arguments arguments = null;
-
- protected Service service = null;
- protected string pathName = null;
- protected string interfaceName = null;
- protected string name = null;
- private string key= null;
-
- protected Message()
- {
- // An empty constructor for the sake of sub-classes which know how to construct theirselves.
- }
-
- protected Message(IntPtr rawMessage, Service service)
- {
- RawMessage = rawMessage;
- this.service = service;
- }
-
- protected Message(MessageType messageType)
- {
- // the assignment bumps the refcount
- RawMessage = dbus_message_new((int) messageType);
-
- if (RawMessage == IntPtr.Zero) {
- throw new OutOfMemoryException();
- }
-
- dbus_message_unref(RawMessage);
- }
-
- protected Message(MessageType messageType, Service service) : this(messageType)
- {
- this.service = service;
- }
-
- ~Message()
- {
- RawMessage = IntPtr.Zero; // free the native object
- }
-
- public static Message Wrap(IntPtr rawMessage, Service service)
- {
- if (slot > -1) {
- // If we already have a Message object associated with this rawMessage then return it
- IntPtr rawThis = dbus_message_get_data(rawMessage, slot);
- if (rawThis != IntPtr.Zero)
- return (DBus.Message) ((GCHandle)rawThis).Target;
- }
- // If it doesn't exist then create a new Message around it
- Message message = null;
- MessageType messageType = (MessageType) dbus_message_get_type(rawMessage);
-
- switch (messageType) {
- case MessageType.Signal:
- message = new Signal(rawMessage, service);
- break;
- case MessageType.MethodCall:
- message = new MethodCall(rawMessage, service);
- break;
- case MessageType.MethodReturn:
- message = new MethodReturn(rawMessage, service);
- break;
- case MessageType.Error:
- message = new ErrorMessage(rawMessage, service);
- break;
- default:
- throw new ApplicationException("Unknown message type to wrap: " + messageType);
- }
-
- return message;
- }
-
- internal IntPtr RawMessage
- {
- get
- {
- return rawMessage;
- }
- set
- {
- if (value == rawMessage)
- return;
-
- if (rawMessage != IntPtr.Zero)
- {
- // Get the reference to this
- IntPtr rawThis = dbus_message_get_data(rawMessage, Slot);
- Debug.Assert (rawThis != IntPtr.Zero);
-
- // Blank over the reference
- dbus_message_set_data(rawMessage, Slot, IntPtr.Zero, IntPtr.Zero);
-
- // Free the reference
- ((GCHandle) rawThis).Free();
-
- // Unref the connection
- dbus_message_unref(rawMessage);
- }
-
- this.rawMessage = value;
-
- if (rawMessage != IntPtr.Zero)
- {
- GCHandle rawThis;
-
- dbus_message_ref(rawMessage);
-
- // We store a weak reference to the C# object on the C object
- rawThis = GCHandle.Alloc(this, GCHandleType.WeakTrackResurrection);
-
- dbus_message_set_data(rawMessage, Slot, (IntPtr) rawThis, IntPtr.Zero);
- }
- }
- }
-
- public void Send(ref int serial)
- {
- if (!dbus_connection_send (Service.Connection.RawConnection, RawMessage, ref serial))
- throw new OutOfMemoryException ();
-
- Service.Connection.Flush();
- }
-
- public void Send()
- {
- int ignored = 0;
- Send(ref ignored);
- }
-
- public void SendWithReply()
- {
- IntPtr rawPendingCall = IntPtr.Zero;
-
- if (!dbus_connection_send_with_reply (Service.Connection.RawConnection, RawMessage, rawPendingCall, Service.Connection.Timeout))
- throw new OutOfMemoryException();
- }
-
- public MethodReturn SendWithReplyAndBlock()
- {
- Error error = new Error();
- error.Init();
-
- IntPtr rawMessage = dbus_connection_send_with_reply_and_block(Service.Connection.RawConnection,
- RawMessage,
- Service.Connection.Timeout,
- ref error);
-
- if (rawMessage != IntPtr.Zero) {
- MethodReturn methodReturn = new MethodReturn(rawMessage, Service);
- return methodReturn;
- } else {
- throw new DBusException(error);
- }
- }
-
- public MessageType Type
- {
- get
- {
- return (MessageType) dbus_message_get_type(RawMessage);
- }
- }
-
- public Service Service
- {
- set
- {
- if (this.service != null && (value.Name != this.service.Name)) {
- if (!dbus_message_set_destination(RawMessage, value.Name)) {
- throw new OutOfMemoryException();
- }
- }
-
- this.service = value;
- }
- get
- {
- return this.service;
- }
- }
-
- protected virtual string PathName
- {
- set
- {
- if (value != this.pathName)
- {
- if (!dbus_message_set_path(RawMessage, value)) {
- throw new OutOfMemoryException();
- }
-
- this.pathName = value;
- }
- }
- get
- {
- if (this.pathName == null) {
- this.pathName = Marshal.PtrToStringAnsi(dbus_message_get_path(RawMessage));
- }
-
- return this.pathName;
- }
- }
-
- protected virtual string InterfaceName
- {
- set
- {
- if (value != this.interfaceName)
- {
- dbus_message_set_interface (RawMessage, value);
- this.interfaceName = value;
- }
- }
- get
- {
- if (this.interfaceName == null) {
- this.interfaceName = Marshal.PtrToStringAnsi(dbus_message_get_interface(RawMessage));
- }
-
- return this.interfaceName;
- }
- }
-
- protected virtual string Name
- {
- set {
- if (value != this.name) {
- dbus_message_set_member(RawMessage, value);
- this.name = value;
- }
- }
- get {
- if (this.name == null) {
- this.name = Marshal.PtrToStringAnsi(dbus_message_get_member(RawMessage));
- }
-
- return this.name;
- }
- }
-
- public string Key
- {
- get {
- if (this.key == null) {
- this.key = Name + " " + Arguments;
- }
-
- return this.key;
- }
- }
-
- public Arguments Arguments
- {
- get
- {
- if (this.arguments == null) {
- this.arguments = new Arguments(this);
- }
-
- return this.arguments;
- }
- }
-
- protected int Slot
- {
- get
- {
- if (slot == -1)
- {
- // We need to initialize the slot
- if (!dbus_message_allocate_data_slot (ref slot))
- throw new OutOfMemoryException ();
-
- Debug.Assert (slot >= 0);
- }
-
- return slot;
- }
- }
-
- [DllImport ("dbus-1", EntryPoint="dbus_message_new")]
- protected extern static IntPtr dbus_message_new (int messageType);
-
- [DllImport ("dbus-1", EntryPoint="dbus_message_unref")]
- protected extern static void dbus_message_unref (IntPtr ptr);
-
- [DllImport ("dbus-1", EntryPoint="dbus_message_ref")]
- protected extern static void dbus_message_ref (IntPtr ptr);
-
- [DllImport ("dbus-1", EntryPoint="dbus_message_allocate_data_slot")]
- protected extern static bool dbus_message_allocate_data_slot (ref int slot);
-
- [DllImport ("dbus-1", EntryPoint="dbus_message_free_data_slot")]
- protected extern static void dbus_message_free_data_slot (ref int slot);
-
- [DllImport ("dbus-1", EntryPoint="dbus_message_set_data")]
- protected extern static bool dbus_message_set_data (IntPtr ptr,
- int slot,
- IntPtr data,
- IntPtr free_data_func);
-
- [DllImport ("dbus-1", EntryPoint="dbus_message_get_data")]
- protected extern static IntPtr dbus_message_get_data (IntPtr ptr,
- int slot);
-
- [DllImport ("dbus-1", EntryPoint="dbus_connection_send")]
- private extern static bool dbus_connection_send (IntPtr ptr,
- IntPtr message,
- ref int client_serial);
-
- [DllImport ("dbus-1", EntryPoint="dbus_connection_send_with_reply")]
- private extern static bool dbus_connection_send_with_reply (IntPtr rawConnection, IntPtr rawMessage, IntPtr rawPendingCall, int timeout);
-
- [DllImport ("dbus-1", EntryPoint="dbus_connection_send_with_reply_and_block")]
- private extern static IntPtr dbus_connection_send_with_reply_and_block (IntPtr rawConnection, IntPtr message, int timeout, ref Error error);
-
- [DllImport("dbus-1")]
- private extern static int dbus_message_get_type(IntPtr rawMessage);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_set_path(IntPtr rawMessage, string pathName);
-
- [DllImport("dbus-1")]
- private extern static IntPtr dbus_message_get_path(IntPtr rawMessage);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_set_interface (IntPtr rawMessage, string interfaceName);
-
- [DllImport("dbus-1")]
- private extern static IntPtr dbus_message_get_interface(IntPtr rawMessage);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_set_member(IntPtr rawMessage, string name);
-
- [DllImport("dbus-1")]
- private extern static IntPtr dbus_message_get_member(IntPtr rawMessage);
-
- [DllImport("dbus-1")]
- private extern static bool dbus_message_set_destination(IntPtr rawMessage, string serviceName);
-
- [DllImport("dbus-1")]
- private extern static IntPtr dbus_message_get_destination(IntPtr rawMessage);
- }
-}
+namespace DBus +{ + + using System; + using System.Runtime.InteropServices; + using System.Diagnostics; + using System.Collections; + + public class Message + { + + /// <summary> + /// A pointer to the underlying Message structure + /// </summary> + private IntPtr rawMessage; + + /// <summary> + /// The current slot number + /// </summary> + private static int slot = -1; + + // Keep in sync with C + public enum MessageType + { + Invalid = 0, + MethodCall = 1, + MethodReturn = 2, + Error = 3, + Signal = 4 + } + + private Arguments arguments = null; + + protected Service service = null; + protected string pathName = null; + protected string interfaceName = null; + protected string name = null; + private string key= null; + + protected Message() + { + // An empty constructor for the sake of sub-classes which know how to construct theirselves. + } + + protected Message(IntPtr rawMessage, Service service) + { + RawMessage = rawMessage; + this.service = service; + } + + protected Message(MessageType messageType) + { + // the assignment bumps the refcount + RawMessage = dbus_message_new((int) messageType); + + if (RawMessage == IntPtr.Zero) { + throw new OutOfMemoryException(); + } + + dbus_message_unref(RawMessage); + } + + protected Message(MessageType messageType, Service service) : this(messageType) + { + this.service = service; + } + + ~Message() + { + RawMessage = IntPtr.Zero; // free the native object + } + + public static Message Wrap(IntPtr rawMessage, Service service) + { + if (slot > -1) { + // If we already have a Message object associated with this rawMessage then return it + IntPtr rawThis = dbus_message_get_data(rawMessage, slot); + if (rawThis != IntPtr.Zero) + return (DBus.Message) ((GCHandle)rawThis).Target; + } + // If it doesn't exist then create a new Message around it + Message message = null; + MessageType messageType = (MessageType) dbus_message_get_type(rawMessage); + + switch (messageType) { + case MessageType.Signal: + message = new Signal(rawMessage, service); + break; + case MessageType.MethodCall: + message = new MethodCall(rawMessage, service); + break; + case MessageType.MethodReturn: + message = new MethodReturn(rawMessage, service); + break; + case MessageType.Error: + message = new ErrorMessage(rawMessage, service); + break; + default: + throw new ApplicationException("Unknown message type to wrap: " + messageType); + } + + return message; + } + + internal IntPtr RawMessage + { + get + { + return rawMessage; + } + set + { + if (value == rawMessage) + return; + + if (rawMessage != IntPtr.Zero) + { + // Get the reference to this + IntPtr rawThis = dbus_message_get_data(rawMessage, Slot); + Debug.Assert (rawThis != IntPtr.Zero); + + // Blank over the reference + dbus_message_set_data(rawMessage, Slot, IntPtr.Zero, IntPtr.Zero); + + // Free the reference + ((GCHandle) rawThis).Free(); + + // Unref the connection + dbus_message_unref(rawMessage); + } + + this.rawMessage = value; + + if (rawMessage != IntPtr.Zero) + { + GCHandle rawThis; + + dbus_message_ref(rawMessage); + + // We store a weak reference to the C# object on the C object + rawThis = GCHandle.Alloc(this, GCHandleType.WeakTrackResurrection); + + dbus_message_set_data(rawMessage, Slot, (IntPtr) rawThis, IntPtr.Zero); + } + } + } + + public void Send(ref int serial) + { + if (!dbus_connection_send (Service.Connection.RawConnection, RawMessage, ref serial)) + throw new OutOfMemoryException (); + + Service.Connection.Flush(); + } + + public void Send() + { + int ignored = 0; + Send(ref ignored); + } + + public void SendWithReply() + { + IntPtr rawPendingCall = IntPtr.Zero; + + if (!dbus_connection_send_with_reply (Service.Connection.RawConnection, RawMessage, rawPendingCall, Service.Connection.Timeout)) + throw new OutOfMemoryException(); + } + + public MethodReturn SendWithReplyAndBlock() + { + Error error = new Error(); + error.Init(); + + IntPtr rawMessage = dbus_connection_send_with_reply_and_block(Service.Connection.RawConnection, + RawMessage, + Service.Connection.Timeout, + ref error); + + if (rawMessage != IntPtr.Zero) { + MethodReturn methodReturn = new MethodReturn(rawMessage, Service); + return methodReturn; + } else { + throw new DBusException(error); + } + } + + public MessageType Type + { + get + { + return (MessageType) dbus_message_get_type(RawMessage); + } + } + + public Service Service + { + set + { + if (this.service != null && (value.Name != this.service.Name)) { + if (!dbus_message_set_destination(RawMessage, value.Name)) { + throw new OutOfMemoryException(); + } + } + + this.service = value; + } + get + { + return this.service; + } + } + + protected virtual string PathName + { + set + { + if (value != this.pathName) + { + if (!dbus_message_set_path(RawMessage, value)) { + throw new OutOfMemoryException(); + } + + this.pathName = value; + } + } + get + { + if (this.pathName == null) { + this.pathName = Marshal.PtrToStringAnsi(dbus_message_get_path(RawMessage)); + } + + return this.pathName; + } + } + + protected virtual string InterfaceName + { + set + { + if (value != this.interfaceName) + { + dbus_message_set_interface (RawMessage, value); + this.interfaceName = value; + } + } + get + { + if (this.interfaceName == null) { + this.interfaceName = Marshal.PtrToStringAnsi(dbus_message_get_interface(RawMessage)); + } + + return this.interfaceName; + } + } + + protected virtual string Name + { + set { + if (value != this.name) { + dbus_message_set_member(RawMessage, value); + this.name = value; + } + } + get { + if (this.name == null) { + this.name = Marshal.PtrToStringAnsi(dbus_message_get_member(RawMessage)); + } + + return this.name; + } + } + + public string Key + { + get { + if (this.key == null) { + this.key = Name + " " + Arguments; + } + + return this.key; + } + } + + public Arguments Arguments + { + get + { + if (this.arguments == null) { + this.arguments = new Arguments(this); + } + + return this.arguments; + } + } + + protected int Slot + { + get + { + if (slot == -1) + { + // We need to initialize the slot + if (!dbus_message_allocate_data_slot (ref slot)) + throw new OutOfMemoryException (); + + Debug.Assert (slot >= 0); + } + + return slot; + } + } + + [DllImport ("dbus-1", EntryPoint="dbus_message_new")] + protected extern static IntPtr dbus_message_new (int messageType); + + [DllImport ("dbus-1", EntryPoint="dbus_message_unref")] + protected extern static void dbus_message_unref (IntPtr ptr); + + [DllImport ("dbus-1", EntryPoint="dbus_message_ref")] + protected extern static void dbus_message_ref (IntPtr ptr); + + [DllImport ("dbus-1", EntryPoint="dbus_message_allocate_data_slot")] + protected extern static bool dbus_message_allocate_data_slot (ref int slot); + + [DllImport ("dbus-1", EntryPoint="dbus_message_free_data_slot")] + protected extern static void dbus_message_free_data_slot (ref int slot); + + [DllImport ("dbus-1", EntryPoint="dbus_message_set_data")] + protected extern static bool dbus_message_set_data (IntPtr ptr, + int slot, + IntPtr data, + IntPtr free_data_func); + + [DllImport ("dbus-1", EntryPoint="dbus_message_get_data")] + protected extern static IntPtr dbus_message_get_data (IntPtr ptr, + int slot); + + [DllImport ("dbus-1", EntryPoint="dbus_connection_send")] + private extern static bool dbus_connection_send (IntPtr ptr, + IntPtr message, + ref int client_serial); + + [DllImport ("dbus-1", EntryPoint="dbus_connection_send_with_reply")] + private extern static bool dbus_connection_send_with_reply (IntPtr rawConnection, IntPtr rawMessage, IntPtr rawPendingCall, int timeout); + + [DllImport ("dbus-1", EntryPoint="dbus_connection_send_with_reply_and_block")] + private extern static IntPtr dbus_connection_send_with_reply_and_block (IntPtr rawConnection, IntPtr message, int timeout, ref Error error); + + [DllImport("dbus-1")] + private extern static int dbus_message_get_type(IntPtr rawMessage); + + [DllImport("dbus-1")] + private extern static bool dbus_message_set_path(IntPtr rawMessage, string pathName); + + [DllImport("dbus-1")] + private extern static IntPtr dbus_message_get_path(IntPtr rawMessage); + + [DllImport("dbus-1")] + private extern static bool dbus_message_set_interface (IntPtr rawMessage, string interfaceName); + + [DllImport("dbus-1")] + private extern static IntPtr dbus_message_get_interface(IntPtr rawMessage); + + [DllImport("dbus-1")] + private extern static bool dbus_message_set_member(IntPtr rawMessage, string name); + + [DllImport("dbus-1")] + private extern static IntPtr dbus_message_get_member(IntPtr rawMessage); + + [DllImport("dbus-1")] + private extern static bool dbus_message_set_destination(IntPtr rawMessage, string serviceName); + + [DllImport("dbus-1")] + private extern static IntPtr dbus_message_get_destination(IntPtr rawMessage); + } +} diff --git a/mono/MethodCall.cs b/mono/MethodCall.cs index ab9043e..ab7a4a3 100644 --- a/mono/MethodCall.cs +++ b/mono/MethodCall.cs @@ -1,80 +1,80 @@ -namespace DBus
-{
- using System;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
-
- public class MethodCall : Message
- {
- public MethodCall() : base(MessageType.MethodCall)
- {
- }
-
- internal MethodCall(IntPtr rawMessage, Service service) : base(rawMessage, service)
- {
- }
-
- public MethodCall(Service service) : base(MessageType.MethodCall, service)
- {
- }
-
- public MethodCall(Service service, string pathName, string interfaceName, string name)
- {
- this.service = service;
-
- RawMessage = dbus_message_new_method_call(service.Name, pathName, interfaceName, name);
-
- if (RawMessage == IntPtr.Zero) {
- throw new OutOfMemoryException();
- }
-
- this.pathName = pathName;
- this.interfaceName = interfaceName;
- this.name = name;
-
- dbus_message_unref(RawMessage);
- }
-
- public new string PathName
- {
- get
- {
- return base.PathName;
- }
-
- set
- {
- base.PathName = value;
- }
- }
-
- public new string InterfaceName
- {
- get
- {
- return base.InterfaceName;
- }
-
- set
- {
- base.InterfaceName = value;
- }
- }
-
- public new string Name
- {
- get
- {
- return base.Name;
- }
-
- set
- {
- base.Name = value;
- }
- }
-
- [DllImport("dbus-1")]
- private extern static IntPtr dbus_message_new_method_call(string serviceName, string pathName, string interfaceName, string name);
- }
-}
+namespace DBus +{ + using System; + using System.Runtime.InteropServices; + using System.Diagnostics; + + public class MethodCall : Message + { + public MethodCall() : base(MessageType.MethodCall) + { + } + + internal MethodCall(IntPtr rawMessage, Service service) : base(rawMessage, service) + { + } + + public MethodCall(Service service) : base(MessageType.MethodCall, service) + { + } + + public MethodCall(Service service, string pathName, string interfaceName, string name) + { + this.service = service; + + RawMessage = dbus_message_new_method_call(service.Name, pathName, interfaceName, name); + + if (RawMessage == IntPtr.Zero) { + throw new OutOfMemoryException(); + } + + this.pathName = pathName; + this.interfaceName = interfaceName; + this.name = name; + + dbus_message_unref(RawMessage); + } + + public new string PathName + { + get + { + return base.PathName; + } + + set + { + base.PathName = value; + } + } + + public new string InterfaceName + { + get + { + return base.InterfaceName; + } + + set + { + base.InterfaceName = value; + } + } + + public new string Name + { + get + { + return base.Name; + } + + set + { + base.Name = value; + } + } + + [DllImport("dbus-1")] + private extern static IntPtr dbus_message_new_method_call(string serviceName, string pathName, string interfaceName, string name); + } +} diff --git a/mono/Test.cs b/mono/Test.cs index 028986c..cb482cb 100644 --- a/mono/Test.cs +++ b/mono/Test.cs @@ -17,12 +17,17 @@ namespace DBus.Test serverThread.Start(); connection = Bus.GetSessionBus(); - service = Service.Get(connection, "org.freedesktop.Test"); + service = Service.Get(connection, "org.freedesktop.Test"); + Thread.Sleep (1000); TestObject testObject = (TestObject) service.GetObject(typeof(TestObject), "/org/freedesktop/Test/TestObject"); + + Console.WriteLine ("Got object [{0}]", testObject); System.Console.WriteLine(testObject.Test1("Hello")); + Console.WriteLine ("Got object [{0}]", testObject); + //RunTests(testObject); return 0; @@ -49,6 +54,8 @@ namespace DBus.Test service = new Service(connection, "org.freedesktop.Test"); TestObject testObject = new TestObject(); service.RegisterObject(testObject, "/org/freedesktop/Test/TestObject"); + + System.Console.WriteLine("Foo!"); } public void StartServer() |