| 1 |
// Copyright 2006 Alp Toker <[email protected]> |
| 2 |
// This software is made available under the MIT License |
| 3 |
// See COPYING for details |
| 4 |
|
| 5 |
using System; |
| 6 |
using System.Collections.Generic; |
| 7 |
using System.Reflection; |
| 8 |
|
| 9 |
namespace NDesk.DBus |
| 10 |
{ |
| 11 |
static class Mapper |
| 12 |
{ |
| 13 |
//TODO: move these Get*Name helpers somewhere more appropriate |
| 14 |
public static string GetArgumentName (ParameterInfo pi) |
| 15 |
{ |
| 16 |
string argName = pi.Name; |
| 17 |
|
| 18 |
if (pi.IsRetval && String.IsNullOrEmpty (argName)) |
| 19 |
argName = "ret"; |
| 20 |
|
| 21 |
return GetArgumentName ((ICustomAttributeProvider)pi, argName); |
| 22 |
} |
| 23 |
|
| 24 |
public static string GetArgumentName (ICustomAttributeProvider attrProvider, string defaultName) |
| 25 |
{ |
| 26 |
string argName = defaultName; |
| 27 |
|
| 28 |
//TODO: no need for foreach |
| 29 |
foreach (ArgumentAttribute aa in attrProvider.GetCustomAttributes (typeof (ArgumentAttribute), true)) |
| 30 |
argName = aa.Name; |
| 31 |
|
| 32 |
return argName; |
| 33 |
} |
| 34 |
|
| 35 |
//TODO: these two methods are quite messy and need review |
| 36 |
public static IEnumerable<MemberInfo> GetPublicMembers (Type type) |
| 37 |
{ |
| 38 |
//note that Type.GetInterfaces() returns all interfaces with flattened hierarchy |
| 39 |
foreach (Type ifType in type.GetInterfaces ()) |
| 40 |
foreach (MemberInfo mi in GetDeclaredPublicMembers (ifType)) |
| 41 |
yield return mi; |
| 42 |
|
| 43 |
if (IsPublic (type)) |
| 44 |
foreach (MemberInfo mi in GetDeclaredPublicMembers (type)) |
| 45 |
yield return mi; |
| 46 |
} |
| 47 |
|
| 48 |
static IEnumerable<MemberInfo> GetDeclaredPublicMembers (Type type) |
| 49 |
{ |
| 50 |
if (IsPublic (type)) |
| 51 |
foreach (MemberInfo mi in type.GetMembers (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) |
| 52 |
yield return mi; |
| 53 |
} |
| 54 |
|
| 55 |
//this method walks the interface tree in an undefined manner and returns the first match, or if no matches are found, null |
| 56 |
//the logic needs review and cleanup |
| 57 |
//TODO: unify member name mapping as is already done with interfaces and args |
| 58 |
public static MethodInfo GetMethod (Type type, MethodCall method_call) |
| 59 |
{ |
| 60 |
foreach (MemberInfo member in Mapper.GetPublicMembers (type)) { |
| 61 |
//this could be made more efficient by using the given interface name earlier and avoiding walking through all public interfaces |
| 62 |
if (method_call.Interface != null) |
| 63 |
if (GetInterfaceName (member) != method_call.Interface) |
| 64 |
continue; |
| 65 |
|
| 66 |
MethodInfo meth = null; |
| 67 |
Type[] inTypes = null; |
| 68 |
|
| 69 |
if (member is PropertyInfo) { |
| 70 |
PropertyInfo prop = member as PropertyInfo; |
| 71 |
|
| 72 |
MethodInfo getter = prop.GetGetMethod (false); |
| 73 |
MethodInfo setter = prop.GetSetMethod (false); |
| 74 |
|
| 75 |
if (getter != null && "Get" + prop.Name == method_call.Member) { |
| 76 |
meth = getter; |
| 77 |
inTypes = Type.EmptyTypes; |
| 78 |
} else if (setter != null && "Set" + prop.Name == method_call.Member) { |
| 79 |
meth = setter; |
| 80 |
inTypes = new Type[] {prop.PropertyType}; |
| 81 |
} |
| 82 |
} else { |
| 83 |
meth = member as MethodInfo; |
| 84 |
|
| 85 |
if (meth == null) |
| 86 |
continue; |
| 87 |
|
| 88 |
if (meth.Name != method_call.Member) |
| 89 |
continue; |
| 90 |
|
| 91 |
inTypes = Mapper.GetTypes (ArgDirection.In, meth.GetParameters ()); |
| 92 |
} |
| 93 |
|
| 94 |
if (meth == null || inTypes == null) |
| 95 |
continue; |
| 96 |
|
| 97 |
Signature inSig = Signature.GetSig (inTypes); |
| 98 |
|
| 99 |
if (inSig != method_call.Signature) |
| 100 |
continue; |
| 101 |
|
| 102 |
return meth; |
| 103 |
} |
| 104 |
|
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
public static bool IsPublic (MemberInfo mi) |
| 109 |
{ |
| 110 |
return IsPublic (mi.DeclaringType); |
| 111 |
} |
| 112 |
|
| 113 |
public static bool IsPublic (Type type) |
| 114 |
{ |
| 115 |
//we need to have a proper look at what's really public at some point |
| 116 |
//this will do for now |
| 117 |
|
| 118 |
if (type.IsDefined (typeof (InterfaceAttribute), false)) |
| 119 |
return true; |
| 120 |
|
| 121 |
if (type.IsSubclassOf (typeof (MarshalByRefObject))) |
| 122 |
return true; |
| 123 |
|
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
public static string GetInterfaceName (MemberInfo mi) |
| 128 |
{ |
| 129 |
return GetInterfaceName (mi.DeclaringType); |
| 130 |
} |
| 131 |
|
| 132 |
public static string GetInterfaceName (Type type) |
| 133 |
{ |
| 134 |
string interfaceName = type.FullName; |
| 135 |
|
| 136 |
//TODO: better fallbacks and namespace mangling when no InterfaceAttribute is available |
| 137 |
|
| 138 |
//TODO: no need for foreach |
| 139 |
foreach (InterfaceAttribute ia in type.GetCustomAttributes (typeof (InterfaceAttribute), true)) |
| 140 |
interfaceName = ia.Name; |
| 141 |
|
| 142 |
return interfaceName; |
| 143 |
} |
| 144 |
|
| 145 |
public static Type[] GetTypes (ArgDirection dir, ParameterInfo[] parms) |
| 146 |
{ |
| 147 |
List<Type> types = new List<Type> (); |
| 148 |
|
| 149 |
//TODO: consider InOut/Ref |
| 150 |
|
| 151 |
for (int i = 0 ; i != parms.Length ; i++) { |
| 152 |
switch (dir) { |
| 153 |
case ArgDirection.In: |
| 154 |
//docs say IsIn isn't reliable, and this is indeed true |
| 155 |
//if (parms[i].IsIn) |
| 156 |
if (!parms[i].IsOut) |
| 157 |
types.Add (parms[i].ParameterType); |
| 158 |
break; |
| 159 |
case ArgDirection.Out: |
| 160 |
if (parms[i].IsOut) { |
| 161 |
//TODO: note that IsOut is optional to the compiler, we may want to use IsByRef instead |
| 162 |
//eg: if (parms[i].ParameterType.IsByRef) |
| 163 |
types.Add (parms[i].ParameterType.GetElementType ()); |
| 164 |
} |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return types.ToArray (); |
| 170 |
} |
| 171 |
|
| 172 |
public static bool IsDeprecated (ICustomAttributeProvider attrProvider) |
| 173 |
{ |
| 174 |
return attrProvider.IsDefined (typeof (ObsoleteAttribute), true); |
| 175 |
} |
| 176 |
|
| 177 |
static bool AreEqual (Type[] a, Type[] b) |
| 178 |
{ |
| 179 |
if (a.Length != b.Length) |
| 180 |
return false; |
| 181 |
|
| 182 |
for (int i = 0 ; i != a.Length ; i++) |
| 183 |
if (a[i] != b[i]) |
| 184 |
return false; |
| 185 |
|
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
//workaround for Mono bug #81035 (memory leak) |
| 190 |
static List<Type> genTypes = new List<Type> (); |
| 191 |
internal static Type GetGenericType (Type defType, Type[] parms) |
| 192 |
{ |
| 193 |
foreach (Type genType in genTypes) { |
| 194 |
if (genType.GetGenericTypeDefinition () != defType) |
| 195 |
continue; |
| 196 |
|
| 197 |
Type[] genParms = genType.GetGenericArguments (); |
| 198 |
|
| 199 |
if (!AreEqual (genParms, parms)) |
| 200 |
continue; |
| 201 |
|
| 202 |
return genType; |
| 203 |
} |
| 204 |
|
| 205 |
Type type = defType.MakeGenericType (parms); |
| 206 |
genTypes.Add (type); |
| 207 |
return type; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
//TODO: this class is messy, move the methods somewhere more appropriate |
| 212 |
static class MessageHelper |
| 213 |
{ |
| 214 |
public static Message CreateUnknownMethodError (MethodCall method_call) |
| 215 |
{ |
| 216 |
if (!method_call.message.ReplyExpected) |
| 217 |
return null; |
| 218 |
|
| 219 |
string errMsg = String.Format ("Method \"{0}\" with signature \"{1}\" on interface \"{2}\" doesn't exist", method_call.Member, method_call.Signature.Value, method_call.Interface); |
| 220 |
|
| 221 |
Error error = new Error ("org.freedesktop.DBus.Error.UnknownMethod", method_call.message.Header.Serial); |
| 222 |
error.message.Signature = new Signature (DType.String); |
| 223 |
|
| 224 |
MessageWriter writer = new MessageWriter (Connection.NativeEndianness); |
| 225 |
writer.Write (errMsg); |
| 226 |
error.message.Body = writer.ToArray (); |
| 227 |
|
| 228 |
//TODO: we should be more strict here, but this fallback was added as a quick fix for p2p |
| 229 |
if (method_call.Sender != null) |
| 230 |
error.message.Header.Fields[FieldCode.Destination] = method_call.Sender; |
| 231 |
|
| 232 |
return error.message; |
| 233 |
} |
| 234 |
|
| 235 |
public static void WriteDynamicValues (MessageWriter mw, ParameterInfo[] parms, object[] vals) |
| 236 |
{ |
| 237 |
foreach (ParameterInfo parm in parms) { |
| 238 |
if (!parm.IsOut) |
| 239 |
continue; |
| 240 |
|
| 241 |
Type actualType = parm.ParameterType.GetElementType (); |
| 242 |
mw.Write (actualType, vals[parm.Position]); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
public static object[] GetDynamicValues (Message msg, ParameterInfo[] parms) |
| 247 |
{ |
| 248 |
//TODO: this validation check should provide better information, eg. message dump or a stack trace, or at least the interface/member |
| 249 |
/* |
| 250 |
if (Protocol.Verbose) { |
| 251 |
Signature expected = Signature.GetSig (types); |
| 252 |
Signature actual = msg.Signature; |
| 253 |
if (actual != expected) |
| 254 |
Console.Error.WriteLine ("Warning: The signature of the message does not match that of the handler: " + "Expected '" + expected + "', got '" + actual + "'"); |
| 255 |
} |
| 256 |
*/ |
| 257 |
|
| 258 |
object[] vals = new object[parms.Length]; |
| 259 |
|
| 260 |
if (msg.Body != null) { |
| 261 |
MessageReader reader = new MessageReader (msg); |
| 262 |
foreach (ParameterInfo parm in parms) { |
| 263 |
if (parm.IsOut) |
| 264 |
continue; |
| 265 |
|
| 266 |
vals[parm.Position] = reader.ReadValue (parm.ParameterType); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
return vals; |
| 271 |
} |
| 272 |
|
| 273 |
public static object[] GetDynamicValues (Message msg, Type[] types) |
| 274 |
{ |
| 275 |
//TODO: this validation check should provide better information, eg. message dump or a stack trace, or at least the interface/member |
| 276 |
if (Protocol.Verbose) { |
| 277 |
Signature expected = Signature.GetSig (types); |
| 278 |
Signature actual = msg.Signature; |
| 279 |
if (actual != expected) |
| 280 |
Console.Error.WriteLine ("Warning: The signature of the message does not match that of the handler: " + "Expected '" + expected + "', got '" + actual + "'"); |
| 281 |
} |
| 282 |
|
| 283 |
object[] vals = new object[types.Length]; |
| 284 |
|
| 285 |
if (msg.Body != null) { |
| 286 |
MessageReader reader = new MessageReader (msg); |
| 287 |
|
| 288 |
for (int i = 0 ; i != types.Length ; i++) |
| 289 |
vals[i] = reader.ReadValue (types[i]); |
| 290 |
} |
| 291 |
|
| 292 |
return vals; |
| 293 |
} |
| 294 |
|
| 295 |
public static Message ConstructReply (MethodCall method_call, params object[] vals) |
| 296 |
{ |
| 297 |
MethodReturn method_return = new MethodReturn (method_call.message.Header.Serial); |
| 298 |
Message replyMsg = method_return.message; |
| 299 |
|
| 300 |
Signature inSig = Signature.GetSig (vals); |
| 301 |
|
| 302 |
if (vals != null && vals.Length != 0) { |
| 303 |
MessageWriter writer = new MessageWriter (Connection.NativeEndianness); |
| 304 |
|
| 305 |
foreach (object arg in vals) |
| 306 |
writer.Write (arg.GetType (), arg); |
| 307 |
|
| 308 |
replyMsg.Body = writer.ToArray (); |
| 309 |
} |
| 310 |
|
| 311 |
//TODO: we should be more strict here, but this fallback was added as a quick fix for p2p |
| 312 |
if (method_call.Sender != null) |
| 313 |
replyMsg.Header.Fields[FieldCode.Destination] = method_call.Sender; |
| 314 |
|
| 315 |
replyMsg.Signature = inSig; |
| 316 |
|
| 317 |
//replyMsg.WriteHeader (); |
| 318 |
|
| 319 |
return replyMsg; |
| 320 |
} |
| 321 |
|
| 322 |
public static Message ConstructDynamicReply (MethodCall method_call, MethodInfo mi, object retVal, object[] vals) |
| 323 |
{ |
| 324 |
Type retType = mi.ReturnType; |
| 325 |
|
| 326 |
MethodReturn method_return = new MethodReturn (method_call.message.Header.Serial); |
| 327 |
Message replyMsg = method_return.message; |
| 328 |
|
| 329 |
Signature outSig = Signature.GetSig (retType); |
| 330 |
outSig += Signature.GetSig (Mapper.GetTypes (ArgDirection.Out, mi.GetParameters ())); |
| 331 |
|
| 332 |
if (outSig != Signature.Empty) { |
| 333 |
MessageWriter writer = new MessageWriter (Connection.NativeEndianness); |
| 334 |
|
| 335 |
//first write the return value, if any |
| 336 |
if (retType != null && retType != typeof (void)) |
| 337 |
writer.Write (retType, retVal); |
| 338 |
|
| 339 |
//then write the out args |
| 340 |
WriteDynamicValues (writer, mi.GetParameters (), vals); |
| 341 |
|
| 342 |
replyMsg.Body = writer.ToArray (); |
| 343 |
} |
| 344 |
|
| 345 |
//TODO: we should be more strict here, but this fallback was added as a quick fix for p2p |
| 346 |
if (method_call.Sender != null) |
| 347 |
replyMsg.Header.Fields[FieldCode.Destination] = method_call.Sender; |
| 348 |
|
| 349 |
replyMsg.Signature = outSig; |
| 350 |
|
| 351 |
return replyMsg; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
[AttributeUsage (AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple=false, Inherited=true)] |
| 356 |
public class InterfaceAttribute : Attribute |
| 357 |
{ |
| 358 |
public string Name; |
| 359 |
|
| 360 |
public InterfaceAttribute (string name) |
| 361 |
{ |
| 362 |
this.Name = name; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
[AttributeUsage (AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple=false, Inherited=true)] |
| 367 |
public class ArgumentAttribute : Attribute |
| 368 |
{ |
| 369 |
public string Name; |
| 370 |
|
| 371 |
public ArgumentAttribute (string name) |
| 372 |
{ |
| 373 |
this.Name = name; |
| 374 |
} |
| 375 |
} |
| 376 |
} |