4 using System.Runtime.InteropServices;
5 using System.Diagnostics;
6 using System.Reflection;
7 using System.Collections;
16 internal class Handler
18 private string path = null;
19 private Introspector introspector = null;
20 private object handledObject = null;
21 private DBusObjectPathVTable vTable;
22 private Connection connection;
23 private Service service;
25 // We need to hold extra references to these callbacks so that they don't
26 // get garbage collected before they are called back into from unmanaged
28 private DBusObjectPathUnregisterFunction unregister_func;
29 private DBusObjectPathMessageFunction message_func;
31 public Handler(object handledObject,
36 Connection = service.Connection;
37 HandledObject = handledObject;
40 // Create the vTable and register the path
41 this.unregister_func = new DBusObjectPathUnregisterFunction (Unregister_Called);
42 this.message_func = new DBusObjectPathMessageFunction (Message_Called);
44 vTable = new DBusObjectPathVTable (this.unregister_func, this.message_func);
45 Connection.RegisterObjectPath (Path, vTable);
46 RegisterSignalHandlers();
49 private void RegisterSignalHandlers()
51 ProxyBuilder proxyBuilder = new ProxyBuilder(Service, HandledObject.GetType(), Path);
53 foreach (DictionaryEntry interfaceEntry in this.introspector.InterfaceProxies) {
54 InterfaceProxy interfaceProxy = (InterfaceProxy) interfaceEntry.Value;
55 foreach (DictionaryEntry signalEntry in interfaceProxy.Signals) {
56 EventInfo eventE = (EventInfo) signalEntry.Value;
57 Delegate del = Delegate.CreateDelegate(eventE.EventHandlerType, proxyBuilder.GetSignalProxy(), "Proxy_" + eventE.Name);
58 eventE.AddEventHandler(HandledObject, del);
63 public object HandledObject
66 return this.handledObject;
70 this.handledObject = value;
72 // Register the methods
73 this.introspector = Introspector.GetIntrospector(value.GetType());
77 public void Unregister_Called(IntPtr rawConnection,
80 if (service != null) {
81 service.UnregisterObject(HandledObject);
87 private int Message_Called(IntPtr rawConnection,
91 Message message = Message.Wrap(rawMessage, Service);
92 Result res = Result.NotYetHandled;
94 switch (message.Type) {
95 case Message.MessageType.MethodCall:
96 res = HandleMethod ((MethodCall) message);
99 case Message.MessageType.Signal:
100 // We're not interested in signals here because we're the ones
101 // that generate them!
110 private Result HandleMethod(MethodCall methodCall)
112 methodCall.Service = service;
114 InterfaceProxy interfaceProxy = this.introspector.GetInterface(methodCall.InterfaceName);
115 if (interfaceProxy == null || !interfaceProxy.HasMethod(methodCall.Key)) {
116 // No such interface here.
117 return Result.NotYetHandled;
120 MethodInfo method = interfaceProxy.GetMethod(methodCall.Key);
122 Message.Push (methodCall);
124 // Now call the method. FIXME: Error handling
125 object [] args = methodCall.Arguments.GetParameters(method);
126 object retVal = method.Invoke(this.handledObject, args);
130 // Create the reply and send it
131 MethodReturn methodReturn = new MethodReturn(methodCall);
132 methodReturn.Arguments.AppendResults(method, retVal, args);
135 return Result.Handled;
146 internal Connection Connection
155 this.connection = value;
159 public Service Service
168 this.service = value;