dbus/mono/Connection.cs

355 lines
9.6 KiB
C#
Raw Normal View History

2004-03-23 12:10:32 +00:00
namespace DBus
{
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
2004-03-23 12:10:32 +00:00
using System.Reflection;
using System.IO;
using System.Collections;
public delegate int DBusHandleMessageFunction (IntPtr rawConnection,
IntPtr rawMessage,
IntPtr userData);
2005-03-08 Joe Shaw <joeshaw@novell.com> Fix a bunch of lifecycle and memory management problems in the mono bindings. * mono/Arguments.cs (Arguments): Implement IDisposable * mono/Bus.cs (Bus): Don't allow public instantiation. This is strictly a static class. * mono/Connection.cs: Move the DBusObjectPathVTable and associated delegates into this file. (Connection): Implement IDisposable. (Dispose): Disconnect the connection and set the raw connection pointer to IntPtr.Zero. (~Connection): Call Dispose(). (RegisterObjectPath): Added. Manages the registration of object paths so we can cleanly disconnect them at dispose/finalize time. (UnregisterObjectPath): Ditto. (set_RawConnection): Unregister all of the object paths when changing the underlying DBusConnection. Add them back onto the new connection, if any. * mono/Handler.cs: Don't implement IDisposable; it doesn't use any more unmanaged resources anymore, so it's not necessary. Move all the DBusObjectPathVTable stuff out of here. (Handler): Save references to our delegates so that they don't get finalized. Call Connection.RegisterObjectPath() instead of dbus_connection_register_object_path() directly. (Message_Called): Dispose the message after we're finished with it. * mono/Message.cs (Message): Implement IDisposable. (Dispose): Dispose the Arguments, and set the RawMessage to IntPtr.Zero. (SendWithReplyAndBlock): We own the ref to the reply that comes back from dbus_connection_send_with_reply_and_block() so add a comment about that and unref it after we've constructed a managed MethodReturn class around it. Fixes a big, big leak. * mono/ProxyBuilder.cs: Reflect into Message to get the Dispose method. (BuildSignalHandler): After we've sent the Signal message, dispose of it. (BuildMethod): Dispose of the method call and reply messages after we've sent the message and extracted the data we want from the reply. * mono/Service.cs (UnregisterObject): Don't call handler.Dispose() anymore. (Service_FilterCalled): Dispose of the message after we're finished with it.
2005-03-09 04:36:15 +00:00
internal delegate void DBusObjectPathUnregisterFunction(IntPtr rawConnection,
IntPtr userData);
internal delegate int DBusObjectPathMessageFunction(IntPtr rawConnection,
IntPtr rawMessage,
IntPtr userData);
[StructLayout (LayoutKind.Sequential)]
internal struct DBusObjectPathVTable
{
public DBusObjectPathUnregisterFunction unregisterFunction;
public DBusObjectPathMessageFunction messageFunction;
public IntPtr padding1;
public IntPtr padding2;
public IntPtr padding3;
public IntPtr padding4;
public DBusObjectPathVTable(DBusObjectPathUnregisterFunction unregisterFunction,
DBusObjectPathMessageFunction messageFunction)
{
this.unregisterFunction = unregisterFunction;
this.messageFunction = messageFunction;
this.padding1 = IntPtr.Zero;
this.padding2 = IntPtr.Zero;
this.padding3 = IntPtr.Zero;
this.padding4 = IntPtr.Zero;
}
}
public class Connection : IDisposable
2004-03-23 12:10:32 +00:00
{
/// <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;
2005-03-08 Joe Shaw <joeshaw@novell.com> Fix a bunch of lifecycle and memory management problems in the mono bindings. * mono/Arguments.cs (Arguments): Implement IDisposable * mono/Bus.cs (Bus): Don't allow public instantiation. This is strictly a static class. * mono/Connection.cs: Move the DBusObjectPathVTable and associated delegates into this file. (Connection): Implement IDisposable. (Dispose): Disconnect the connection and set the raw connection pointer to IntPtr.Zero. (~Connection): Call Dispose(). (RegisterObjectPath): Added. Manages the registration of object paths so we can cleanly disconnect them at dispose/finalize time. (UnregisterObjectPath): Ditto. (set_RawConnection): Unregister all of the object paths when changing the underlying DBusConnection. Add them back onto the new connection, if any. * mono/Handler.cs: Don't implement IDisposable; it doesn't use any more unmanaged resources anymore, so it's not necessary. Move all the DBusObjectPathVTable stuff out of here. (Handler): Save references to our delegates so that they don't get finalized. Call Connection.RegisterObjectPath() instead of dbus_connection_register_object_path() directly. (Message_Called): Dispose the message after we're finished with it. * mono/Message.cs (Message): Implement IDisposable. (Dispose): Dispose the Arguments, and set the RawMessage to IntPtr.Zero. (SendWithReplyAndBlock): We own the ref to the reply that comes back from dbus_connection_send_with_reply_and_block() so add a comment about that and unref it after we've constructed a managed MethodReturn class around it. Fixes a big, big leak. * mono/ProxyBuilder.cs: Reflect into Message to get the Dispose method. (BuildSignalHandler): After we've sent the Signal message, dispose of it. (BuildMethod): Dispose of the method call and reply messages after we've sent the message and extracted the data we want from the reply. * mono/Service.cs (UnregisterObject): Don't call handler.Dispose() anymore. (Service_FilterCalled): Dispose of the message after we're finished with it.
2005-03-09 04:36:15 +00:00
private ArrayList filters = new ArrayList (); // of DBusHandleMessageFunction
private ArrayList matches = new ArrayList (); // of string
private Hashtable object_paths = new Hashtable (); // key: string value: DBusObjectPathVTable
2004-03-23 12:10:32 +00:00
internal Connection(IntPtr rawConnection)
{
RawConnection = rawConnection;
}
public Connection(string address)
{
// the assignment bumps the refcount
2004-03-23 12:10:32 +00:00
Error error = new Error();
error.Init();
RawConnection = dbus_connection_open(address, ref error);
if (RawConnection != IntPtr.Zero) {
dbus_connection_unref(RawConnection);
} else {
2004-03-23 12:10:32 +00:00
throw new DBusException(error);
}
2004-03-23 12:10:32 +00:00
SetupWithMain();
}
2005-03-08 Joe Shaw <joeshaw@novell.com> Fix a bunch of lifecycle and memory management problems in the mono bindings. * mono/Arguments.cs (Arguments): Implement IDisposable * mono/Bus.cs (Bus): Don't allow public instantiation. This is strictly a static class. * mono/Connection.cs: Move the DBusObjectPathVTable and associated delegates into this file. (Connection): Implement IDisposable. (Dispose): Disconnect the connection and set the raw connection pointer to IntPtr.Zero. (~Connection): Call Dispose(). (RegisterObjectPath): Added. Manages the registration of object paths so we can cleanly disconnect them at dispose/finalize time. (UnregisterObjectPath): Ditto. (set_RawConnection): Unregister all of the object paths when changing the underlying DBusConnection. Add them back onto the new connection, if any. * mono/Handler.cs: Don't implement IDisposable; it doesn't use any more unmanaged resources anymore, so it's not necessary. Move all the DBusObjectPathVTable stuff out of here. (Handler): Save references to our delegates so that they don't get finalized. Call Connection.RegisterObjectPath() instead of dbus_connection_register_object_path() directly. (Message_Called): Dispose the message after we're finished with it. * mono/Message.cs (Message): Implement IDisposable. (Dispose): Dispose the Arguments, and set the RawMessage to IntPtr.Zero. (SendWithReplyAndBlock): We own the ref to the reply that comes back from dbus_connection_send_with_reply_and_block() so add a comment about that and unref it after we've constructed a managed MethodReturn class around it. Fixes a big, big leak. * mono/ProxyBuilder.cs: Reflect into Message to get the Dispose method. (BuildSignalHandler): After we've sent the Signal message, dispose of it. (BuildMethod): Dispose of the method call and reply messages after we've sent the message and extracted the data we want from the reply. * mono/Service.cs (UnregisterObject): Don't call handler.Dispose() anymore. (Service_FilterCalled): Dispose of the message after we're finished with it.
2005-03-09 04:36:15 +00:00
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose (bool disposing)
{
if (disposing && RawConnection != IntPtr.Zero)
{
dbus_connection_disconnect(rawConnection);
RawConnection = IntPtr.Zero; // free the native object
}
}
2004-03-26 15:25:59 +00:00
public void Flush()
{
dbus_connection_flush(RawConnection);
}
2004-03-23 12:10:32 +00:00
public void SetupWithMain()
{
dbus_connection_setup_with_g_main(RawConnection, IntPtr.Zero);
}
2004-03-23 12:10:32 +00:00
~Connection ()
{
2005-03-08 Joe Shaw <joeshaw@novell.com> Fix a bunch of lifecycle and memory management problems in the mono bindings. * mono/Arguments.cs (Arguments): Implement IDisposable * mono/Bus.cs (Bus): Don't allow public instantiation. This is strictly a static class. * mono/Connection.cs: Move the DBusObjectPathVTable and associated delegates into this file. (Connection): Implement IDisposable. (Dispose): Disconnect the connection and set the raw connection pointer to IntPtr.Zero. (~Connection): Call Dispose(). (RegisterObjectPath): Added. Manages the registration of object paths so we can cleanly disconnect them at dispose/finalize time. (UnregisterObjectPath): Ditto. (set_RawConnection): Unregister all of the object paths when changing the underlying DBusConnection. Add them back onto the new connection, if any. * mono/Handler.cs: Don't implement IDisposable; it doesn't use any more unmanaged resources anymore, so it's not necessary. Move all the DBusObjectPathVTable stuff out of here. (Handler): Save references to our delegates so that they don't get finalized. Call Connection.RegisterObjectPath() instead of dbus_connection_register_object_path() directly. (Message_Called): Dispose the message after we're finished with it. * mono/Message.cs (Message): Implement IDisposable. (Dispose): Dispose the Arguments, and set the RawMessage to IntPtr.Zero. (SendWithReplyAndBlock): We own the ref to the reply that comes back from dbus_connection_send_with_reply_and_block() so add a comment about that and unref it after we've constructed a managed MethodReturn class around it. Fixes a big, big leak. * mono/ProxyBuilder.cs: Reflect into Message to get the Dispose method. (BuildSignalHandler): After we've sent the Signal message, dispose of it. (BuildMethod): Dispose of the method call and reply messages after we've sent the message and extracted the data we want from the reply. * mono/Service.cs (UnregisterObject): Don't call handler.Dispose() anymore. (Service_FilterCalled): Dispose of the message after we're finished with it.
2005-03-09 04:36:15 +00:00
Dispose (false);
}
2004-03-23 12:10:32 +00:00
internal static Connection Wrap(IntPtr rawConnection)
{
if (slot > -1) {
2004-04-03 22:00:40 +00:00
// Maybe we already have a Connection object associated with
// this rawConnection then return it
2004-03-23 12:10:32 +00:00
IntPtr rawThis = dbus_connection_get_data (rawConnection, slot);
2004-04-03 22:00:40 +00:00
if (rawThis != IntPtr.Zero) {
return (DBus.Connection) ((GCHandle)rawThis).Target;
2004-03-23 12:10:32 +00:00
}
2004-04-03 22:00:40 +00:00
}
// If it doesn't exist then create a new connection around it
return new Connection(rawConnection);
}
public void AddFilter (DBusHandleMessageFunction func)
{
if (!dbus_connection_add_filter (RawConnection,
func,
IntPtr.Zero,
IntPtr.Zero))
throw new OutOfMemoryException ();
this.filters.Add (func);
}
public void RemoveFilter (DBusHandleMessageFunction func)
{
dbus_connection_remove_filter (RawConnection, func, IntPtr.Zero);
this.filters.Remove (func);
}
public void AddMatch (string match_rule)
{
dbus_bus_add_match (RawConnection, match_rule, IntPtr.Zero);
this.matches.Add (match_rule);
}
public void RemoveMatch (string match_rule)
{
dbus_bus_remove_match (RawConnection, match_rule, IntPtr.Zero);
this.matches.Remove (match_rule);
}
2005-03-08 Joe Shaw <joeshaw@novell.com> Fix a bunch of lifecycle and memory management problems in the mono bindings. * mono/Arguments.cs (Arguments): Implement IDisposable * mono/Bus.cs (Bus): Don't allow public instantiation. This is strictly a static class. * mono/Connection.cs: Move the DBusObjectPathVTable and associated delegates into this file. (Connection): Implement IDisposable. (Dispose): Disconnect the connection and set the raw connection pointer to IntPtr.Zero. (~Connection): Call Dispose(). (RegisterObjectPath): Added. Manages the registration of object paths so we can cleanly disconnect them at dispose/finalize time. (UnregisterObjectPath): Ditto. (set_RawConnection): Unregister all of the object paths when changing the underlying DBusConnection. Add them back onto the new connection, if any. * mono/Handler.cs: Don't implement IDisposable; it doesn't use any more unmanaged resources anymore, so it's not necessary. Move all the DBusObjectPathVTable stuff out of here. (Handler): Save references to our delegates so that they don't get finalized. Call Connection.RegisterObjectPath() instead of dbus_connection_register_object_path() directly. (Message_Called): Dispose the message after we're finished with it. * mono/Message.cs (Message): Implement IDisposable. (Dispose): Dispose the Arguments, and set the RawMessage to IntPtr.Zero. (SendWithReplyAndBlock): We own the ref to the reply that comes back from dbus_connection_send_with_reply_and_block() so add a comment about that and unref it after we've constructed a managed MethodReturn class around it. Fixes a big, big leak. * mono/ProxyBuilder.cs: Reflect into Message to get the Dispose method. (BuildSignalHandler): After we've sent the Signal message, dispose of it. (BuildMethod): Dispose of the method call and reply messages after we've sent the message and extracted the data we want from the reply. * mono/Service.cs (UnregisterObject): Don't call handler.Dispose() anymore. (Service_FilterCalled): Dispose of the message after we're finished with it.
2005-03-09 04:36:15 +00:00
internal void RegisterObjectPath (string path, DBusObjectPathVTable vtable)
{
if (!dbus_connection_register_object_path (RawConnection, path, ref vtable, IntPtr.Zero))
throw new OutOfMemoryException ();
this.object_paths[path] = vtable;
}
internal void UnregisterObjectPath (string path)
{
dbus_connection_unregister_object_path (RawConnection, path);
this.object_paths.Remove (path);
}
2005-01-25 Joe Shaw <joeshaw@novell.com> * Land the mono binding changes to conform to the new APIs. * mono/Makefile.am: Remove Custom.cs, DBusType/Custom.cs, DBusType/Dict.cs, and DBusType/Nil.cs from the build. * mono/Arguments.cs (GetCodeAsString): Added. Returns the dbus type code as a string. (InitAppending): Rename dbus_message_append_iter_init() to dbus_message_iter_init_append(). * mono/BusDriver.cs: Rename ServiceEventHandler to NameOwnerChangedHandler. Rename GetServiceOwner to GetOwner. Rename ServiceOwnerChanged to NameOwnerChanged. * mono/Connection.cs: Rename BaseService to UniqueName, and the underlying C call. * mono/Custom.cs: Removed. The CUSTOM type has been removed. * mono/Service.cs: Rename Exists to HasOwner, internally rename dbus_bus_acquire_service() to dbus_bus_request_name(). * mono/DBusType/Array.cs (ctor): Use Type.GetElementType() instead of Type.UnderlyingSystemType to get the correct element type for the array. (ctor): Update code for new APIs: use dbus_message_iter_recurse(), dbus_message_get_{element|arg}_type() instead of dbus_message_iter_init_array_iterator(). (Append): Replace dbus_message_iter_append_array() with dbus_message_iter_open_container() and dbus_message_iter_close_container(). * mono/DBusType/Custom.cs, mono/DBusType/Nil.cs: Removed. These types have been removed. * mono/DBusType/*.cs: Replace calls of dbus_message_iter_get_[type]() to dbus_message_iter_get_basic(), but specify the type in the DllImport extern declaration. Ditto for dbus_message_iter_append_[type]() -> dbus_message_iter_append_basic(). * mono/example/BusListener.cs: Update for ServiceEventHandler -> NameOwnerChangedHandler.
2005-01-25 19:47:13 +00:00
public string UniqueName
{
get
{
2005-01-25 Joe Shaw <joeshaw@novell.com> * Land the mono binding changes to conform to the new APIs. * mono/Makefile.am: Remove Custom.cs, DBusType/Custom.cs, DBusType/Dict.cs, and DBusType/Nil.cs from the build. * mono/Arguments.cs (GetCodeAsString): Added. Returns the dbus type code as a string. (InitAppending): Rename dbus_message_append_iter_init() to dbus_message_iter_init_append(). * mono/BusDriver.cs: Rename ServiceEventHandler to NameOwnerChangedHandler. Rename GetServiceOwner to GetOwner. Rename ServiceOwnerChanged to NameOwnerChanged. * mono/Connection.cs: Rename BaseService to UniqueName, and the underlying C call. * mono/Custom.cs: Removed. The CUSTOM type has been removed. * mono/Service.cs: Rename Exists to HasOwner, internally rename dbus_bus_acquire_service() to dbus_bus_request_name(). * mono/DBusType/Array.cs (ctor): Use Type.GetElementType() instead of Type.UnderlyingSystemType to get the correct element type for the array. (ctor): Update code for new APIs: use dbus_message_iter_recurse(), dbus_message_get_{element|arg}_type() instead of dbus_message_iter_init_array_iterator(). (Append): Replace dbus_message_iter_append_array() with dbus_message_iter_open_container() and dbus_message_iter_close_container(). * mono/DBusType/Custom.cs, mono/DBusType/Nil.cs: Removed. These types have been removed. * mono/DBusType/*.cs: Replace calls of dbus_message_iter_get_[type]() to dbus_message_iter_get_basic(), but specify the type in the DllImport extern declaration. Ditto for dbus_message_iter_append_[type]() -> dbus_message_iter_append_basic(). * mono/example/BusListener.cs: Update for ServiceEventHandler -> NameOwnerChangedHandler.
2005-01-25 19:47:13 +00:00
return Marshal.PtrToStringAnsi (dbus_bus_get_unique_name (RawConnection));
}
}
2004-03-23 12:10:32 +00:00
public int Timeout
{
get
{
return this.timeout;
}
set
{
this.timeout = value;
}
}
2004-03-23 12:10:32 +00:00
private int Slot
{
get
{
if (slot == -1)
{
// We need to initialize the slot
if (!dbus_connection_allocate_data_slot (ref slot))
throw new OutOfMemoryException ();
Debug.Assert (slot >= 0);
}
return slot;
}
}
2004-03-23 12:10:32 +00:00
internal IntPtr RawConnection
{
get
{
return rawConnection;
}
set
{
if (value == rawConnection)
return;
if (rawConnection != IntPtr.Zero)
{
// Remove our callbacks from this connection
foreach (DBusHandleMessageFunction func in this.filters)
dbus_connection_remove_filter (rawConnection, func, IntPtr.Zero);
foreach (string match_rule in this.matches)
dbus_bus_remove_match (rawConnection, match_rule, IntPtr.Zero);
2005-03-08 Joe Shaw <joeshaw@novell.com> Fix a bunch of lifecycle and memory management problems in the mono bindings. * mono/Arguments.cs (Arguments): Implement IDisposable * mono/Bus.cs (Bus): Don't allow public instantiation. This is strictly a static class. * mono/Connection.cs: Move the DBusObjectPathVTable and associated delegates into this file. (Connection): Implement IDisposable. (Dispose): Disconnect the connection and set the raw connection pointer to IntPtr.Zero. (~Connection): Call Dispose(). (RegisterObjectPath): Added. Manages the registration of object paths so we can cleanly disconnect them at dispose/finalize time. (UnregisterObjectPath): Ditto. (set_RawConnection): Unregister all of the object paths when changing the underlying DBusConnection. Add them back onto the new connection, if any. * mono/Handler.cs: Don't implement IDisposable; it doesn't use any more unmanaged resources anymore, so it's not necessary. Move all the DBusObjectPathVTable stuff out of here. (Handler): Save references to our delegates so that they don't get finalized. Call Connection.RegisterObjectPath() instead of dbus_connection_register_object_path() directly. (Message_Called): Dispose the message after we're finished with it. * mono/Message.cs (Message): Implement IDisposable. (Dispose): Dispose the Arguments, and set the RawMessage to IntPtr.Zero. (SendWithReplyAndBlock): We own the ref to the reply that comes back from dbus_connection_send_with_reply_and_block() so add a comment about that and unref it after we've constructed a managed MethodReturn class around it. Fixes a big, big leak. * mono/ProxyBuilder.cs: Reflect into Message to get the Dispose method. (BuildSignalHandler): After we've sent the Signal message, dispose of it. (BuildMethod): Dispose of the method call and reply messages after we've sent the message and extracted the data we want from the reply. * mono/Service.cs (UnregisterObject): Don't call handler.Dispose() anymore. (Service_FilterCalled): Dispose of the message after we're finished with it.
2005-03-09 04:36:15 +00:00
foreach (string path in this.object_paths.Keys)
dbus_connection_unregister_object_path (rawConnection, path);
2004-03-23 12:10:32 +00:00
// 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);
// Add the callbacks to this new connection
foreach (DBusHandleMessageFunction func in this.filters)
dbus_connection_add_filter (rawConnection, func, IntPtr.Zero, IntPtr.Zero);
foreach (string match_rule in this.matches)
dbus_bus_add_match (rawConnection, match_rule, IntPtr.Zero);
2005-03-08 Joe Shaw <joeshaw@novell.com> Fix a bunch of lifecycle and memory management problems in the mono bindings. * mono/Arguments.cs (Arguments): Implement IDisposable * mono/Bus.cs (Bus): Don't allow public instantiation. This is strictly a static class. * mono/Connection.cs: Move the DBusObjectPathVTable and associated delegates into this file. (Connection): Implement IDisposable. (Dispose): Disconnect the connection and set the raw connection pointer to IntPtr.Zero. (~Connection): Call Dispose(). (RegisterObjectPath): Added. Manages the registration of object paths so we can cleanly disconnect them at dispose/finalize time. (UnregisterObjectPath): Ditto. (set_RawConnection): Unregister all of the object paths when changing the underlying DBusConnection. Add them back onto the new connection, if any. * mono/Handler.cs: Don't implement IDisposable; it doesn't use any more unmanaged resources anymore, so it's not necessary. Move all the DBusObjectPathVTable stuff out of here. (Handler): Save references to our delegates so that they don't get finalized. Call Connection.RegisterObjectPath() instead of dbus_connection_register_object_path() directly. (Message_Called): Dispose the message after we're finished with it. * mono/Message.cs (Message): Implement IDisposable. (Dispose): Dispose the Arguments, and set the RawMessage to IntPtr.Zero. (SendWithReplyAndBlock): We own the ref to the reply that comes back from dbus_connection_send_with_reply_and_block() so add a comment about that and unref it after we've constructed a managed MethodReturn class around it. Fixes a big, big leak. * mono/ProxyBuilder.cs: Reflect into Message to get the Dispose method. (BuildSignalHandler): After we've sent the Signal message, dispose of it. (BuildMethod): Dispose of the method call and reply messages after we've sent the message and extracted the data we want from the reply. * mono/Service.cs (UnregisterObject): Don't call handler.Dispose() anymore. (Service_FilterCalled): Dispose of the message after we're finished with it.
2005-03-09 04:36:15 +00:00
foreach (string path in this.object_paths.Keys) {
DBusObjectPathVTable vtable = (DBusObjectPathVTable) this.object_paths[path];
dbus_connection_register_object_path (rawConnection, path, ref vtable, IntPtr.Zero);
}
}
else
{
this.filters.Clear ();
this.matches.Clear ();
2005-03-08 Joe Shaw <joeshaw@novell.com> Fix a bunch of lifecycle and memory management problems in the mono bindings. * mono/Arguments.cs (Arguments): Implement IDisposable * mono/Bus.cs (Bus): Don't allow public instantiation. This is strictly a static class. * mono/Connection.cs: Move the DBusObjectPathVTable and associated delegates into this file. (Connection): Implement IDisposable. (Dispose): Disconnect the connection and set the raw connection pointer to IntPtr.Zero. (~Connection): Call Dispose(). (RegisterObjectPath): Added. Manages the registration of object paths so we can cleanly disconnect them at dispose/finalize time. (UnregisterObjectPath): Ditto. (set_RawConnection): Unregister all of the object paths when changing the underlying DBusConnection. Add them back onto the new connection, if any. * mono/Handler.cs: Don't implement IDisposable; it doesn't use any more unmanaged resources anymore, so it's not necessary. Move all the DBusObjectPathVTable stuff out of here. (Handler): Save references to our delegates so that they don't get finalized. Call Connection.RegisterObjectPath() instead of dbus_connection_register_object_path() directly. (Message_Called): Dispose the message after we're finished with it. * mono/Message.cs (Message): Implement IDisposable. (Dispose): Dispose the Arguments, and set the RawMessage to IntPtr.Zero. (SendWithReplyAndBlock): We own the ref to the reply that comes back from dbus_connection_send_with_reply_and_block() so add a comment about that and unref it after we've constructed a managed MethodReturn class around it. Fixes a big, big leak. * mono/ProxyBuilder.cs: Reflect into Message to get the Dispose method. (BuildSignalHandler): After we've sent the Signal message, dispose of it. (BuildMethod): Dispose of the method call and reply messages after we've sent the message and extracted the data we want from the reply. * mono/Service.cs (UnregisterObject): Don't call handler.Dispose() anymore. (Service_FilterCalled): Dispose of the message after we're finished with it.
2005-03-09 04:36:15 +00:00
this.object_paths.Clear ();
2004-03-23 12:10:32 +00:00
}
}
}
2004-03-23 12:10:32 +00:00
[DllImport("dbus-glib-1")]
private extern static void dbus_connection_setup_with_g_main(IntPtr rawConnection,
IntPtr rawContext);
2004-03-23 12:10:32 +00:00
[DllImport ("dbus-1")]
private extern static IntPtr dbus_connection_open (string address, ref Error error);
2004-03-23 12:10:32 +00:00
[DllImport ("dbus-1")]
private extern static void dbus_connection_unref (IntPtr ptr);
2004-03-23 12:10:32 +00:00
[DllImport ("dbus-1")]
private extern static void dbus_connection_ref (IntPtr ptr);
[DllImport ("dbus-1")]
private extern static bool dbus_connection_allocate_data_slot (ref int slot);
[DllImport ("dbus-1")]
private extern static void dbus_connection_free_data_slot (ref int slot);
[DllImport ("dbus-1")]
private extern static bool dbus_connection_set_data (IntPtr ptr,
int slot,
IntPtr data,
IntPtr free_data_func);
[DllImport ("dbus-1")]
private extern static void dbus_connection_flush (IntPtr ptr);
2004-03-23 12:10:32 +00:00
[DllImport ("dbus-1")]
private extern static IntPtr dbus_connection_get_data (IntPtr ptr,
int slot);
2004-03-23 12:10:32 +00:00
[DllImport ("dbus-1")]
private extern static void dbus_connection_disconnect (IntPtr ptr);
[DllImport ("dbus-1")]
2005-01-25 Joe Shaw <joeshaw@novell.com> * Land the mono binding changes to conform to the new APIs. * mono/Makefile.am: Remove Custom.cs, DBusType/Custom.cs, DBusType/Dict.cs, and DBusType/Nil.cs from the build. * mono/Arguments.cs (GetCodeAsString): Added. Returns the dbus type code as a string. (InitAppending): Rename dbus_message_append_iter_init() to dbus_message_iter_init_append(). * mono/BusDriver.cs: Rename ServiceEventHandler to NameOwnerChangedHandler. Rename GetServiceOwner to GetOwner. Rename ServiceOwnerChanged to NameOwnerChanged. * mono/Connection.cs: Rename BaseService to UniqueName, and the underlying C call. * mono/Custom.cs: Removed. The CUSTOM type has been removed. * mono/Service.cs: Rename Exists to HasOwner, internally rename dbus_bus_acquire_service() to dbus_bus_request_name(). * mono/DBusType/Array.cs (ctor): Use Type.GetElementType() instead of Type.UnderlyingSystemType to get the correct element type for the array. (ctor): Update code for new APIs: use dbus_message_iter_recurse(), dbus_message_get_{element|arg}_type() instead of dbus_message_iter_init_array_iterator(). (Append): Replace dbus_message_iter_append_array() with dbus_message_iter_open_container() and dbus_message_iter_close_container(). * mono/DBusType/Custom.cs, mono/DBusType/Nil.cs: Removed. These types have been removed. * mono/DBusType/*.cs: Replace calls of dbus_message_iter_get_[type]() to dbus_message_iter_get_basic(), but specify the type in the DllImport extern declaration. Ditto for dbus_message_iter_append_[type]() -> dbus_message_iter_append_basic(). * mono/example/BusListener.cs: Update for ServiceEventHandler -> NameOwnerChangedHandler.
2005-01-25 19:47:13 +00:00
private extern static IntPtr dbus_bus_get_unique_name (IntPtr ptr);
[DllImport("dbus-1")]
private extern static bool dbus_connection_add_filter(IntPtr rawConnection,
DBusHandleMessageFunction filter,
IntPtr userData,
IntPtr freeData);
[DllImport("dbus-1")]
private extern static void dbus_connection_remove_filter(IntPtr rawConnection,
DBusHandleMessageFunction filter,
IntPtr userData);
[DllImport("dbus-1")]
private extern static void dbus_bus_add_match(IntPtr rawConnection,
string rule,
IntPtr erro);
[DllImport("dbus-1")]
private extern static void dbus_bus_remove_match(IntPtr rawConnection,
string rule,
IntPtr erro);
2005-03-08 Joe Shaw <joeshaw@novell.com> Fix a bunch of lifecycle and memory management problems in the mono bindings. * mono/Arguments.cs (Arguments): Implement IDisposable * mono/Bus.cs (Bus): Don't allow public instantiation. This is strictly a static class. * mono/Connection.cs: Move the DBusObjectPathVTable and associated delegates into this file. (Connection): Implement IDisposable. (Dispose): Disconnect the connection and set the raw connection pointer to IntPtr.Zero. (~Connection): Call Dispose(). (RegisterObjectPath): Added. Manages the registration of object paths so we can cleanly disconnect them at dispose/finalize time. (UnregisterObjectPath): Ditto. (set_RawConnection): Unregister all of the object paths when changing the underlying DBusConnection. Add them back onto the new connection, if any. * mono/Handler.cs: Don't implement IDisposable; it doesn't use any more unmanaged resources anymore, so it's not necessary. Move all the DBusObjectPathVTable stuff out of here. (Handler): Save references to our delegates so that they don't get finalized. Call Connection.RegisterObjectPath() instead of dbus_connection_register_object_path() directly. (Message_Called): Dispose the message after we're finished with it. * mono/Message.cs (Message): Implement IDisposable. (Dispose): Dispose the Arguments, and set the RawMessage to IntPtr.Zero. (SendWithReplyAndBlock): We own the ref to the reply that comes back from dbus_connection_send_with_reply_and_block() so add a comment about that and unref it after we've constructed a managed MethodReturn class around it. Fixes a big, big leak. * mono/ProxyBuilder.cs: Reflect into Message to get the Dispose method. (BuildSignalHandler): After we've sent the Signal message, dispose of it. (BuildMethod): Dispose of the method call and reply messages after we've sent the message and extracted the data we want from the reply. * mono/Service.cs (UnregisterObject): Don't call handler.Dispose() anymore. (Service_FilterCalled): Dispose of the message after we're finished with it.
2005-03-09 04:36:15 +00:00
[DllImport ("dbus-1")]
private extern static bool dbus_connection_register_object_path (IntPtr rawConnection,
string path,
ref DBusObjectPathVTable vTable,
IntPtr userData);
[DllImport ("dbus-1")]
private extern static void dbus_connection_unregister_object_path (IntPtr rawConnection,
string path);
}
}