This repository was archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathBusObject.cs
More file actions
282 lines (229 loc) · 8.13 KB
/
Copy pathBusObject.cs
File metadata and controls
282 lines (229 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2006 Alp Toker <alp@atoker.com>
// This software is made available under the MIT License
// See COPYING for details
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
namespace DBus
{
using Protocol;
public class BusObject
{
static Dictionary<object,BusObject> boCache = new Dictionary<object,BusObject>();
protected Connection conn;
string bus_name;
ObjectPath object_path;
public BusObject ()
{
}
public BusObject (Connection conn, string bus_name, ObjectPath object_path)
{
this.conn = conn;
this.bus_name = bus_name;
this.object_path = object_path;
}
public Connection Connection
{
get {
return conn;
}
}
public string BusName
{
get {
return bus_name;
}
}
public ObjectPath Path
{
get {
return object_path;
}
}
public void ToggleSignal (string iface, string member, Delegate dlg, bool adding)
{
MatchRule rule = new MatchRule ();
rule.MessageType = MessageType.Signal;
rule.Fields.Add (FieldCode.Interface, new MatchTest (iface));
rule.Fields.Add (FieldCode.Member, new MatchTest (member));
rule.Fields.Add (FieldCode.Path, new MatchTest (object_path));
// FIXME: Cause a regression compared to 0.6 as name wasn't matched before
// the problem arises because busname is not used by DBus daemon and
// instead it uses the canonical name of the sender (i.e. similar to ':1.13')
// rule.Fields.Add (FieldCode.Sender, new MatchTest (bus_name));
if (adding) {
if (conn.Handlers.ContainsKey (rule))
conn.Handlers[rule] = Delegate.Combine (conn.Handlers[rule], dlg);
else {
conn.Handlers[rule] = dlg;
conn.AddMatch (rule.ToString ());
}
} else if (conn.Handlers.ContainsKey (rule)) {
conn.Handlers[rule] = Delegate.Remove (conn.Handlers[rule], dlg);
if (conn.Handlers[rule] == null) {
conn.RemoveMatch (rule.ToString ());
conn.Handlers.Remove (rule);
}
}
}
public void SendSignal (string iface, string member, string inSigStr, MessageWriter writer, Type retType, out Exception exception)
{
SendSignal (iface, member, inSigStr, writer, retType, null, out exception);
}
internal void SendSignal (string iface, string member, string inSigStr, MessageWriter writer, Type retType, DisposableList disposableList, out Exception exception)
{
exception = null;
Signature outSig = String.IsNullOrEmpty (inSigStr) ? Signature.Empty : new Signature (inSigStr);
MessageContainer signal = new MessageContainer {
Type = MessageType.Signal,
Path = object_path,
Interface = iface,
Member = member,
Signature = outSig,
};
Message signalMsg = signal.Message;
signalMsg.AttachBodyTo (writer);
conn.Send (signalMsg);
}
public MessageReader SendMethodCall (string iface, string member, string inSigStr, MessageWriter writer, Type retType, out Exception exception)
{
return SendMethodCall (iface, member, inSigStr, writer, retType, null, out exception);
}
public MessageReader SendMethodCall (string iface, string member, string inSigStr, MessageWriter writer, Type retType, DisposableList disposableList, out Exception exception)
{
if (string.IsNullOrEmpty (bus_name))
throw new ArgumentNullException ("bus_name");
if (object_path == null)
throw new ArgumentNullException ("object_path");
exception = null;
Signature inSig = String.IsNullOrEmpty (inSigStr) ? Signature.Empty : new Signature (inSigStr);
MessageContainer method_call = new MessageContainer {
Path = object_path,
Interface = iface,
Member = member,
Destination = bus_name,
Signature = inSig
};
Message callMsg = method_call.Message;
callMsg.AttachBodyTo (writer);
bool needsReply = true;
callMsg.ReplyExpected = needsReply;
callMsg.Signature = inSig;
if (!needsReply) {
conn.Send (callMsg);
return null;
}
#if PROTO_REPLY_SIGNATURE
if (needsReply) {
Signature outSig = Signature.GetSig (retType);
callMsg.Header[FieldCode.ReplySignature] = outSig;
}
#endif
Message retMsg = conn.SendWithReplyAndBlock (callMsg, disposableList != null);
if (disposableList != null && retMsg.UnixFDArray != null)
foreach (var fd in retMsg.UnixFDArray.FDs)
disposableList.Add (fd);
MessageReader retVal = null;
//handle the reply message
switch (retMsg.Header.MessageType) {
case MessageType.MethodReturn:
retVal = new MessageReader (retMsg);
break;
case MessageType.Error:
MessageContainer error = MessageContainer.FromMessage (retMsg);
string errMsg = String.Empty;
if (retMsg.Signature.Value.StartsWith ("s")) {
MessageReader reader = new MessageReader (retMsg);
errMsg = reader.ReadString ();
}
exception = new Exception (error.ErrorName + ": " + errMsg);
break;
default:
throw new Exception ("Got unexpected message of type " + retMsg.Header.MessageType + " while waiting for a MethodReturn or Error");
}
return retVal;
}
public object SendPropertyGet (string iface, string property)
{
Exception exception;
MessageWriter writer = new MessageWriter ();
writer.Write (iface);
writer.Write (property);
MessageReader reader = SendMethodCall ("org.freedesktop.DBus.Properties", "Get", "ss", writer, typeof(object), out exception);
return reader.ReadValue ();
}
public void SendPropertySet (string iface, string property, object value)
{
Exception exception;
MessageWriter writer = new MessageWriter ();
writer.Write (iface);
writer.Write (property);
writer.Write (typeof(object), value);
SendMethodCall ("org.freedesktop.DBus.Properties", "Set", "ssv", writer, typeof(void), out exception);
}
public void Invoke (MethodBase methodBase, string methodName, object[] inArgs, out object[] outArgs, out object retVal, out Exception exception)
{
Invoke (methodBase, methodName, inArgs, null, out outArgs, out retVal, out exception);
}
public void Invoke (MethodBase methodBase, string methodName, object[] inArgs, DisposableList disposableList, out object[] outArgs, out object retVal, out Exception exception)
{
outArgs = new object[0];
retVal = null;
exception = null;
MethodInfo mi = methodBase as MethodInfo;
if (mi != null && mi.IsSpecialName && (methodName.StartsWith ("add_") || methodName.StartsWith ("remove_"))) {
string[] parts = methodName.Split (new char[]{'_'}, 2);
string ename = parts[1];
Delegate dlg = (Delegate)inArgs[0];
ToggleSignal (Mapper.GetInterfaceName (mi), ename, dlg, parts[0] == "add");
return;
}
Type[] inTypes = Mapper.GetTypes (ArgDirection.In, mi.GetParameters ());
Signature inSig = Signature.GetSig (inTypes);
string iface = null;
if (mi != null)
iface = Mapper.GetInterfaceName (mi);
if (mi != null && mi.IsSpecialName) {
methodName = methodName.Replace ("get_", "Get");
methodName = methodName.Replace ("set_", "Set");
}
MessageWriter writer = new MessageWriter (conn);
if (inArgs != null && inArgs.Length != 0) {
for (int i = 0 ; i != inTypes.Length ; i++)
writer.Write (inTypes[i], inArgs[i]);
}
MessageReader reader = SendMethodCall (iface, methodName, inSig.Value, writer, mi.ReturnType, out exception);
if (reader == null)
return;
retVal = reader.ReadValue (mi.ReturnType);
}
public static object GetObject (Connection conn, string bus_name, ObjectPath object_path, Type declType)
{
Type proxyType = TypeImplementer.Root.GetImplementation (declType);
object instObj = Activator.CreateInstance (proxyType);
BusObject inst = GetBusObject (instObj);
inst.conn = conn;
inst.bus_name = bus_name;
inst.object_path = object_path;
return instObj;
}
public static BusObject GetBusObject (object instObj)
{
if (instObj is BusObject)
return (BusObject)instObj;
BusObject inst;
if (boCache.TryGetValue (instObj, out inst))
return inst;
inst = new BusObject ();
boCache[instObj] = inst;
return inst;
}
public Delegate GetHookupDelegate (EventInfo ei)
{
DynamicMethod hookupMethod = TypeImplementer.GetHookupMethod (ei);
Delegate d = hookupMethod.CreateDelegate (ei.EventHandlerType, this);
return d;
}
}
}