2 // System.Runtime.Remoting.Proxies.RemotingProxy.cs
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Lluis Sanchez Gual (lsg@ctv.es)
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
12 using System.Reflection;
13 using System.Runtime.Remoting.Messaging;
14 using System.Runtime.Remoting.Activation;
15 using System.Runtime.Remoting.Channels;
16 using System.Runtime.Remoting.Contexts;
17 using System.Runtime.CompilerServices;
18 using System.Threading;
21 namespace System.Runtime.Remoting.Proxies
24 internal class RemotingProxy : RealProxy, IRemotingTypeInfo
26 static MethodInfo _cache_GetTypeMethod = typeof(System.Object).GetMethod("GetType");
27 static MethodInfo _cache_GetHashCodeMethod = typeof(System.Object).GetMethod("GetHashCode");
31 ConstructionCall _ctorCall;
34 internal RemotingProxy (Type type, ClientIdentity identity) : base (type, identity)
36 _sink = identity.ChannelSink;
37 _hasEnvoySink = false;
38 _targetUri = identity.TargetUri;
41 internal RemotingProxy (Type type, string activationUrl, object[] activationAttributes) : base (type)
43 _hasEnvoySink = false;
44 _ctorCall = ActivationServices.CreateConstructionCall (type, activationUrl, activationAttributes);
47 public override IMessage Invoke (IMessage request)
49 MonoMethodMessage mMsg = (MonoMethodMessage) request;
51 if (mMsg.MethodBase == _cache_GetHashCodeMethod)
52 return new MethodResponse(ObjectIdentity.GetHashCode(), null, null, request as IMethodCallMessage);
54 if (mMsg.MethodBase == _cache_GetTypeMethod)
55 return new MethodResponse(GetProxiedType(), null, null, request as IMethodCallMessage);
57 mMsg.Uri = _targetUri;
58 ((IInternalMessage)mMsg).TargetIdentity = _objectIdentity;
60 _objectIdentity.NotifyClientDynamicSinks (true, request, true, false);
65 // Needs to go through the client context sink?
66 if (Thread.CurrentContext.HasExitSinks && !_hasEnvoySink)
67 sink = Thread.CurrentContext.GetClientContextSinkChain ();
71 if (mMsg.CallType == CallType.Sync)
72 response = sink.SyncProcessMessage (request);
75 AsyncResult ares = mMsg.AsyncResult;
76 IMessageCtrl mctrl = sink.AsyncProcessMessage (request, ares);
77 if (ares != null) ares.SetMessageCtrl (mctrl);
78 response = new ReturnMessage (null, new object[0], 0, null, mMsg);
81 _objectIdentity.NotifyClientDynamicSinks (false, request, true, false);
86 internal void AttachIdentity (Identity identity)
88 _objectIdentity = identity;
90 if (identity is ClientActivatedIdentity) // It is a CBO
92 ClientActivatedIdentity cai = (ClientActivatedIdentity)identity;
93 _targetContext = cai.Context;
94 AttachServer (cai.GetServerObject ());
97 if (identity is ClientIdentity)
99 ((ClientIdentity)identity).ClientProxy = (MarshalByRefObject) GetTransparentProxy();
100 _targetUri = ((ClientIdentity)identity).TargetUri;
103 _targetUri = identity.ObjectUri;
105 if (_objectIdentity.EnvoySink != null)
107 _sink = _objectIdentity.EnvoySink;
108 _hasEnvoySink = true;
111 _sink = _objectIdentity.ChannelSink;
113 _ctorCall = null; // Object already constructed
116 internal IMessage ActivateRemoteObject (IMethodMessage request)
118 if (_ctorCall == null) // It must be a WKO
119 return new ConstructionResponse (this, null, (IMethodCallMessage) request); // Ignore constructor call for WKOs
121 _ctorCall.CopyFrom (request);
122 return ActivationServices.Activate (this, _ctorCall);
125 public string TypeName
129 if (_objectIdentity is ClientIdentity) {
130 ObjRef oref = _objectIdentity.CreateObjRef (null);
131 if (oref.TypeInfo != null) return oref.TypeInfo.TypeName;
133 return GetProxiedType().AssemblyQualifiedName;
138 throw new NotSupportedException ();
142 public bool CanCastTo (Type fromType, object o)
144 if (_objectIdentity is ClientIdentity) {
145 ObjRef oref = _objectIdentity.CreateObjRef (null);
146 if (oref.IsReferenceToWellKnow && (fromType.IsInterface || GetProxiedType() == typeof(MarshalByRefObject))) return true;
147 if (oref.TypeInfo != null) return oref.TypeInfo.CanCastTo (fromType, o);
149 return fromType.IsAssignableFrom (GetProxiedType());
154 if (_objectIdentity != null)
156 if (!(_objectIdentity is ClientActivatedIdentity)) // Local CBO proxy?
157 RemotingServices.DisposeIdentity (_objectIdentity);