2 // System.Runtime.Remoting.Proxies.RealProxy.cs
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Lluis Sanchez (lsg@ctv.es)
9 // (C) 2001 Ximian, Inc. http://www.ximian.com
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Reflection;
37 using System.Runtime.Remoting;
38 using System.Runtime.Remoting.Messaging;
39 using System.Runtime.Remoting.Activation;
40 using System.Runtime.Remoting.Contexts;
41 using System.Runtime.CompilerServices;
42 using System.Runtime.Serialization;
45 namespace System.Runtime.Remoting.Proxies
47 internal class TransparentProxy {
50 bool _custom_type_info;
53 public abstract class RealProxy {
54 // other classes visible to the runtime
55 // derive from this class so keep these locals
56 // in sync with the definition RealProxy
57 // in object-internals.h
58 #region Sync with object-internals.h
60 internal Context _targetContext;
61 MarshalByRefObject _server;
62 int _targetDomainId = -1;
63 internal string _targetUri;
64 internal Identity _objectIdentity;
69 protected RealProxy ()
73 protected RealProxy (Type classToProxy) : this(classToProxy, IntPtr.Zero, null)
77 internal RealProxy (Type classToProxy, ClientIdentity identity) : this(classToProxy, IntPtr.Zero, null)
79 _objectIdentity = identity;
82 protected RealProxy (Type classToProxy, IntPtr stub, object stubData)
84 if (!classToProxy.IsMarshalByRef && !classToProxy.IsInterface)
85 throw new ArgumentException("object must be MarshalByRef");
87 this.class_to_proxy = classToProxy;
89 if (stub != IntPtr.Zero)
90 throw new NotSupportedException ("stub is not used in Mono");
93 [MethodImplAttribute(MethodImplOptions.InternalCall)]
94 extern static Type InternalGetProxyType (object transparentProxy);
96 public Type GetProxiedType()
99 if (class_to_proxy.IsInterface) return typeof(MarshalByRefObject);
100 else return class_to_proxy;
102 return InternalGetProxyType (_objTP);
105 public virtual ObjRef CreateObjRef (Type requestedType)
107 return RemotingServices.Marshal ((MarshalByRefObject) GetTransparentProxy(), null, requestedType);
110 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
112 Object obj = GetTransparentProxy();
\r
113 RemotingServices.GetObjectData (obj, info, context);
\r
116 internal Identity ObjectIdentity
118 get { return _objectIdentity; }
119 set { _objectIdentity = value; }
123 public virtual IntPtr GetCOMIUnknown (bool fIsMarshalled)
125 throw new NotImplementedException ();
129 public virtual void SetCOMIUnknown (IntPtr i)
131 throw new NotImplementedException ();
135 public virtual IntPtr SupportsInterface (ref Guid iid)
137 throw new NotImplementedException ();
140 public static object GetStubData (RealProxy rp)
145 public static void SetStubData (RealProxy rp, object stubData)
147 rp._stubData = stubData;
150 public abstract IMessage Invoke (IMessage msg);
152 /* this is called from unmanaged code */
153 internal static object PrivateInvoke (RealProxy rp, IMessage msg, out Exception exc,
154 out object [] out_args)
156 MonoMethodMessage mMsg = (MonoMethodMessage) msg;
157 mMsg.LogicalCallContext = CallContext.CreateLogicalCallContext (true);
158 CallType call_type = mMsg.CallType;
159 bool is_remproxy = (rp as RemotingProxy) != null;
161 IMethodReturnMessage res_msg = null;
163 if (call_type == CallType.BeginInvoke)
\r
164 // todo: set CallMessage in runtime instead
165 mMsg.AsyncResult.CallMessage = mMsg;
167 if (call_type == CallType.EndInvoke)
168 res_msg = (IMethodReturnMessage)mMsg.AsyncResult.EndInvoke ();
170 // Check for constructor msg
171 if (mMsg.MethodBase.IsConstructor)
\r
174 res_msg = (IMethodReturnMessage) (rp as RemotingProxy).ActivateRemoteObject ((IMethodMessage) msg);
176 msg = new ConstructionCall (rp.GetProxiedType ());
181 res_msg = (IMethodReturnMessage)rp.Invoke (msg);
183 // Note, from begining this code used AsyncResult.IsCompleted for
\r
184 // checking if it was a remoting or custom proxy, but in some
\r
185 // cases the remoting proxy finish before the call returns
\r
186 // causing this method to be called, therefore causing all kind of bugs.
\r
187 if ((!is_remproxy) && call_type == CallType.BeginInvoke)
\r
189 IMessage asyncMsg = null;
191 // allow calltype EndInvoke to finish
192 asyncMsg = mMsg.AsyncResult.SyncProcessMessage (res_msg as IMessage);
193 res_msg = new ReturnMessage (asyncMsg, null, 0, null, res_msg as IMethodCallMessage);
197 if (res_msg.LogicalCallContext != null && res_msg.LogicalCallContext.HasInfo)
198 CallContext.UpdateCurrentCallContext (res_msg.LogicalCallContext);
200 exc = res_msg.Exception;
202 // todo: remove throw exception from the runtime invoke
205 throw exc.FixRemotingException();
207 else if (res_msg is IConstructionReturnMessage || mMsg.CallType == CallType.BeginInvoke) {
208 out_args = res_msg.OutArgs;
210 else if (mMsg.CallType == CallType.Sync) {
211 out_args = ProcessResponse (res_msg, mMsg);
213 else if (mMsg.CallType == CallType.EndInvoke) {
214 out_args = ProcessResponse (res_msg, mMsg.AsyncResult.CallMessage);
217 out_args = res_msg.OutArgs;
220 return res_msg.ReturnValue;
223 [MethodImplAttribute(MethodImplOptions.InternalCall)]
224 internal extern virtual object InternalGetTransparentProxy (string className);
226 public virtual object GetTransparentProxy ()
231 IRemotingTypeInfo rti = this as IRemotingTypeInfo;
235 if (name == null || name == typeof(MarshalByRefObject).AssemblyQualifiedName)
236 name = class_to_proxy.AssemblyQualifiedName;
239 name = class_to_proxy.AssemblyQualifiedName;
241 _objTP = InternalGetTransparentProxy (name);
247 public IConstructionReturnMessage InitializeServerObject(IConstructionCallMessage ctorMsg)
249 throw new NotImplementedException();
252 protected void AttachServer(MarshalByRefObject s)
257 protected MarshalByRefObject DetachServer()
259 MarshalByRefObject ob = _server;
264 protected MarshalByRefObject GetUnwrappedServer()
269 internal void SetTargetDomain (int domainId)
271 _targetDomainId = domainId;
274 // Called by the runtime
275 internal object GetAppDomainTarget ()
277 if (_server == null) {
278 ClientActivatedIdentity identity = RemotingServices.GetIdentityForUri (_targetUri) as ClientActivatedIdentity;
279 if (identity == null) throw new RemotingException ("Server for uri '" + _targetUri + "' not found");
280 _server = identity.GetServerObject ();
285 static object[] ProcessResponse (IMethodReturnMessage mrm, MonoMethodMessage call)
289 MethodInfo mi = (MethodInfo) call.MethodBase;
290 if (mrm.ReturnValue != null && !mi.ReturnType.IsInstanceOfType (mrm.ReturnValue))
291 throw new InvalidCastException ("Return value has an invalid type");
293 // Check out parameters
298 if (call.NeedsOutProcessing (out no))
300 ParameterInfo[] parameters = mi.GetParameters();
301 object[] outArgs = new object [no];
304 foreach (ParameterInfo par in parameters)
306 if (par.IsOut && !par.ParameterType.IsByRef)
308 // Special marshalling required
309 object outArg = par.Position < mrm.ArgCount ? mrm.GetArg (par.Position) : null;
310 if (outArg != null) {
311 object local = call.GetArg (par.Position);
312 if (local == null) throw new RemotingException ("Unexpected null value in local out parameter '" + par.Name + "'");
313 RemotingServices.UpdateOutArgObject (par, local, outArg);
316 else if (par.ParameterType.IsByRef)
318 object outArg = par.Position < mrm.ArgCount ? mrm.GetArg (par.Position) : null;
319 if (outArg != null && !par.ParameterType.GetElementType ().IsInstanceOfType (outArg))
321 throw new InvalidCastException ("Return argument '" + par.Name + "' has an invalid type");
323 outArgs [narg++] = outArg;
329 return new object [0];