| 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.Reflection; |
| 7 |
using System.Runtime.Remoting.Proxies; |
| 8 |
using System.Runtime.Remoting.Messaging; |
| 9 |
|
| 10 |
namespace NDesk.DBus |
| 11 |
{ |
| 12 |
//marked internal because this is really an implementation detail and needs to be replaced |
| 13 |
internal class DProxy : RealProxy |
| 14 |
{ |
| 15 |
protected BusObject busObject; |
| 16 |
|
| 17 |
public DProxy (BusObject busObject, Type type) : base(type) |
| 18 |
{ |
| 19 |
this.busObject = busObject; |
| 20 |
} |
| 21 |
|
| 22 |
static MethodInfo mi_GetHashCode = typeof (object).GetMethod ("GetHashCode"); |
| 23 |
static MethodInfo mi_Equals = typeof (object).GetMethod ("Equals", BindingFlags.Instance); |
| 24 |
static MethodInfo mi_ToString = typeof (object).GetMethod ("ToString"); |
| 25 |
static MethodInfo mi_GetLifetimeService = typeof (MarshalByRefObject).GetMethod ("GetLifetimeService"); |
| 26 |
|
| 27 |
object GetDefaultReturn (MethodBase mi, object[] inArgs) |
| 28 |
{ |
| 29 |
if (mi == mi_GetHashCode) |
| 30 |
return busObject.Path.Value.GetHashCode (); |
| 31 |
if (mi == mi_Equals) |
| 32 |
return busObject.Path.Value == ((BusObject)((MarshalByRefObject)inArgs[0]).GetLifetimeService ()).Path.Value; |
| 33 |
if (mi == mi_ToString) |
| 34 |
return busObject.Path.Value; |
| 35 |
if (mi == mi_GetLifetimeService) |
| 36 |
return busObject; |
| 37 |
|
| 38 |
return null; |
| 39 |
} |
| 40 |
|
| 41 |
public override IMessage Invoke (IMessage message) |
| 42 |
{ |
| 43 |
IMethodCallMessage callMessage = (IMethodCallMessage) message; |
| 44 |
|
| 45 |
object defaultRetVal = GetDefaultReturn (callMessage.MethodBase, callMessage.InArgs); |
| 46 |
if (defaultRetVal != null) { |
| 47 |
MethodReturnMessageWrapper defaultReturnMessage = new MethodReturnMessageWrapper ((IMethodReturnMessage) message); |
| 48 |
defaultReturnMessage.ReturnValue = defaultRetVal; |
| 49 |
|
| 50 |
return defaultReturnMessage; |
| 51 |
} |
| 52 |
|
| 53 |
object[] outArgs; |
| 54 |
object retVal; |
| 55 |
Exception exception; |
| 56 |
busObject.Invoke (callMessage.MethodBase, callMessage.MethodName, callMessage.InArgs, out outArgs, out retVal, out exception); |
| 57 |
|
| 58 |
MethodReturnMessageWrapper returnMessage = new MethodReturnMessageWrapper ((IMethodReturnMessage) message); |
| 59 |
returnMessage.Exception = exception; |
| 60 |
returnMessage.ReturnValue = retVal; |
| 61 |
|
| 62 |
return returnMessage; |
| 63 |
} |
| 64 |
|
| 65 |
/* |
| 66 |
public override ObjRef CreateObjRef (Type ServerType) |
| 67 |
{ |
| 68 |
throw new System.NotImplementedException (); |
| 69 |
} |
| 70 |
*/ |
| 71 |
|
| 72 |
~DProxy () |
| 73 |
{ |
| 74 |
//FIXME: remove handlers/match rules here |
| 75 |
if (Protocol.Verbose) |
| 76 |
Console.Error.WriteLine ("Warning: Finalization of " + busObject.Path + " not yet supported"); |
| 77 |
} |
| 78 |
} |
| 79 |
} |