dbus/mono
Joe Shaw 2958e723fc 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
..
DBusType 2005-03-08 Joe Shaw <joeshaw@novell.com> 2005-03-08 20:45:03 +00:00
doc 2005-01-12 Joe Shaw <joeshaw@novell.com> 2005-01-12 22:41:29 +00:00
example 2005-01-25 Joe Shaw <joeshaw@novell.com> 2005-01-25 19:47:13 +00:00
.cvsignore Shhh... 2004-05-18 13:51:19 +00:00
Arguments.cs 2005-03-08 Joe Shaw <joeshaw@novell.com> 2005-03-09 04:36:15 +00:00
AssemblyInfo.cs.in 2004-09-20 Jon Trowbridge <trow@ximian.com> 2004-09-20 11:24:38 +00:00
Bus.cs 2005-03-08 Joe Shaw <joeshaw@novell.com> 2005-03-09 04:36:15 +00:00
BusDriver.cs 2005-01-25 Joe Shaw <joeshaw@novell.com> 2005-01-25 19:47:13 +00:00
Connection.cs 2005-03-08 Joe Shaw <joeshaw@novell.com> 2005-03-09 04:36:15 +00:00
dbus-sharp.dll.config.in Various mono related changes to facilitate GAC support. 2004-05-15 19:31:12 +00:00
dbus-sharp.snk Various mono related changes to facilitate GAC support. 2004-05-15 19:31:12 +00:00
DBusException.cs First checkin of the Mono bindings. 2004-03-23 12:10:32 +00:00
Error.cs First checkin of the Mono bindings. 2004-03-23 12:10:32 +00:00
ErrorMessage.cs Added signal support. 2004-03-26 15:25:59 +00:00
Handler.cs 2005-03-08 Joe Shaw <joeshaw@novell.com> 2005-03-09 04:36:15 +00:00
InterfaceAttribute.cs First checkin of the Mono bindings. 2004-03-23 12:10:32 +00:00
InterfaceProxy.cs Added signal support. 2004-03-26 15:25:59 +00:00
Introspector.cs Added InterfaceProxy to Mono bindings to avoid having to generate a proxy for every registered object. Also added object_path functions to dbus-message. 2004-03-23 18:07:48 +00:00
Makefile.am 2005-01-25 Joe Shaw <joeshaw@novell.com> 2005-01-25 19:47:13 +00:00
Message.cs 2005-03-08 Joe Shaw <joeshaw@novell.com> 2005-03-09 04:36:15 +00:00
MethodAttribute.cs First checkin of the Mono bindings. 2004-03-23 12:10:32 +00:00
MethodCall.cs First checkin of the Mono bindings. 2004-03-23 12:10:32 +00:00
MethodReturn.cs First checkin of the Mono bindings. 2004-03-23 12:10:32 +00:00
ProxyBuilder.cs 2005-03-08 Joe Shaw <joeshaw@novell.com> 2005-03-09 04:36:15 +00:00
README First checkin of the Mono bindings. 2004-03-23 12:10:32 +00:00
Server.cs First checkin of the Mono bindings. 2004-03-23 12:10:32 +00:00
Service.cs 2005-03-08 Joe Shaw <joeshaw@novell.com> 2005-03-09 04:36:15 +00:00
Signal.cs Added signal support. 2004-03-26 15:25:59 +00:00
SignalAttribute.cs Added signal support. 2004-03-26 15:25:59 +00:00
Test.cs Mapped CRLF -> LF for consistency. 2004-08-29 18:14:30 +00:00
TODO First checkin of the Mono bindings. 2004-03-23 12:10:32 +00:00

D-BUS Mono Bindings
===

These bindings are a 'thick' wrapper around the D-BUS API. For now
they rely on the main loop provided by the GLib bindings but this
dependancy will be removed in the near future.

The wrapper serves two main functions: firstly, it has the know-how to
introspect live objects passed to it by a server and service requests
to those objects via the D-BUS. Secondly, it can create a proxy for
clients who can pretend they are making calls to the regular
objects. This latter piece of magic is implemented using
Reflection.Emit to create an assembly on-the-fly containing
sub-classes of the classes the client thinks it's really using. These
sub-classes simply marshal each method's parameters off to the D-BUS,
demarshal the results and return them to the client as if nothing
happened.

Usage
===

A server do should something like this:

	namespace Foo
	{
	using System;
	using DBus;
	using Gtk;

	public class MyServer
	{
	public static int Main(string [] args)
	{
		Application.Init();
	
1		Connection connection = Bus.GetSessionBus();
2		Service service = new Service(connection, "org.foo");
3		MyObject myObject = new MyObject();
4		service.RegisterObject(myObject, "/org/foo/MyObject");
		
		Application.Run();

		return 0;
	}
	}
	}

In line 1 we get a connection to the session bus. Then, in line 2 we
create a service which will listen for requests to org.foo to
service. In line 3 we create a MyObject object and register it with an
object path in line 4. It's almost that simple. All that's missing is
to mark MyObject in such a way that dbus-sharp knows how to export
it. This is done using the attributes, Interface and Method,
as in the following example:

	namespace Foo
	{
	using System;
	using DBus;

	[Interface("org.foo.MyObject")]
	public class MyObject
	{
	[Method]
	public virtual string Echo(string message)
	{
		return "Reply: " + message;
	}
	}
	}

Note that the Methods should also be declared virtual in order for
the client to use same class declaration.

Now for the client:

	namespace Foo
	{
	using System;
	using DBus;

	public class MyClient
	{
	public static int Main(string [] args)
	{
1		Connection connection = Bus.GetSessionBus();
2		Service service = Service.Get(connection, "org.foo");
3		MyObject myObject = (MyObject) 
		   service.GetObject(typeof(MyObject), "/org/foo/MyObject");
4		System.Console.WriteLine(testObject.Echo("Hello world!"));

		return 0;
	}
	}
	}

Here we start off the same by getting a connection to the session
bus. This time though, in line 2, we get the existing service rather
than creating it. In line 3, we ask the service to get the object
"/org/foo/MyObject" as registered by the server and that return it as
a MyObject. Once obtained we can use it like any normal object as in
line 4. This supposes, of course, that you've just written MyObject
and happen to have it readily available. If that were not the case,
for example if you wanted to call a method on one of the well-known
services, then you will need to write a stub class, like the MyObject
class above, which has the method calls you need correctly defined but
needn't actually have any implementation.


Working Example
===

The example directory contains a working example similar to that
described above. It uses the session bus so first run dbus-launch and
then export DBUS_SESSION_BUS_ADDRESS, as displayed by dbus-launch, to
two terminals, one to run the server and one for the client. Then,
start the server in one terminal, the client in the other and cross
your fingers.