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;
43 using System.Runtime.InteropServices;
45 namespace System.Runtime.Remoting.Proxies
47 #pragma warning disable 169, 649
48 [StructLayout (LayoutKind.Sequential)]
49 internal class TransparentProxy {
52 bool _custom_type_info;
54 #pragma warning restore 169, 649
57 [StructLayout (LayoutKind.Sequential)]
58 public abstract class RealProxy {
59 // other classes visible to the runtime
60 // derive from this class so keep these locals
61 // in sync with the definition RealProxy
62 // in object-internals.h
64 #pragma warning disable 169, 414
65 #region Sync with object-internals.h
67 internal Context _targetContext;
68 MarshalByRefObject _server;
69 int _targetDomainId = -1;
70 internal string _targetUri;
71 internal Identity _objectIdentity;
75 #pragma warning restore 169, 414
77 protected RealProxy ()
81 protected RealProxy (Type classToProxy) : this(classToProxy, IntPtr.Zero, null)
85 internal RealProxy (Type classToProxy, ClientIdentity identity) : this(classToProxy, IntPtr.Zero, null)
87 _objectIdentity = identity;
90 protected RealProxy (Type classToProxy, IntPtr stub, object stubData)
92 if (!classToProxy.IsMarshalByRef && !classToProxy.IsInterface)
93 throw new ArgumentException("object must be MarshalByRef");
95 this.class_to_proxy = classToProxy;
97 if (stub != IntPtr.Zero)
98 throw new NotSupportedException ("stub is not used in Mono");
101 [MethodImplAttribute(MethodImplOptions.InternalCall)]
102 extern static Type InternalGetProxyType (object transparentProxy);
104 public Type GetProxiedType()
106 if (_objTP == null) {
107 if (class_to_proxy.IsInterface) return typeof(MarshalByRefObject);
108 else return class_to_proxy;
110 return InternalGetProxyType (_objTP);
113 public virtual ObjRef CreateObjRef (Type requestedType)
115 return RemotingServices.Marshal ((MarshalByRefObject) GetTransparentProxy(), null, requestedType);
118 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
120 Object obj = GetTransparentProxy();
121 RemotingServices.GetObjectData (obj, info, context);
124 internal Identity ObjectIdentity
126 get { return _objectIdentity; }
127 set { _objectIdentity = value; }
131 public virtual IntPtr GetCOMIUnknown (bool fIsMarshalled)
133 throw new NotImplementedException ();
137 public virtual void SetCOMIUnknown (IntPtr i)
139 throw new NotImplementedException ();
143 public virtual IntPtr SupportsInterface (ref Guid iid)
145 throw new NotImplementedException ();
148 public static object GetStubData (RealProxy rp)
153 public static void SetStubData (RealProxy rp, object stubData)
155 rp._stubData = stubData;
158 public abstract IMessage Invoke (IMessage msg);
160 /* this is called from unmanaged code */
161 internal static object PrivateInvoke (RealProxy rp, IMessage msg, out Exception exc,
162 out object [] out_args)
164 MonoMethodMessage mMsg = (MonoMethodMessage) msg;
165 mMsg.LogicalCallContext = CallContext.CreateLogicalCallContext (true);
166 CallType call_type = mMsg.CallType;
167 bool is_remproxy = (rp is RemotingProxy);
170 IMethodReturnMessage res_msg = null;
172 if (call_type == CallType.BeginInvoke)
173 // todo: set CallMessage in runtime instead
174 mMsg.AsyncResult.CallMessage = mMsg;
176 if (call_type == CallType.EndInvoke)
177 res_msg = (IMethodReturnMessage)mMsg.AsyncResult.EndInvoke ();
179 // Check for constructor msg
180 if (mMsg.MethodBase.IsConstructor)
183 res_msg = (IMethodReturnMessage) (rp as RemotingProxy).ActivateRemoteObject ((IMethodMessage) msg);
185 msg = new ConstructionCall (rp.GetProxiedType ());
193 res_msg = (IMethodReturnMessage)rp.Invoke (msg);
194 } catch (Exception ex) {
196 if (call_type == CallType.BeginInvoke) {
197 // If async dispatch crashes, don't propagate the exception.
198 // The exception will be raised when calling EndInvoke.
199 mMsg.AsyncResult.SyncProcessMessage (new ReturnMessage (ex, msg as IMethodCallMessage));
200 res_msg = new ReturnMessage (null, null, 0, null, msg as IMethodCallMessage);
205 // Note, from begining this code used AsyncResult.IsCompleted for
206 // checking if it was a remoting or custom proxy, but in some
207 // cases the remoting proxy finish before the call returns
208 // causing this method to be called, therefore causing all kind of bugs.
209 if ((!is_remproxy) && call_type == CallType.BeginInvoke && !failed)
211 IMessage asyncMsg = null;
213 // allow calltype EndInvoke to finish
214 asyncMsg = mMsg.AsyncResult.SyncProcessMessage (res_msg as IMessage);
215 out_args = res_msg.OutArgs;
216 res_msg = new ReturnMessage (asyncMsg, null, 0, null, res_msg as IMethodCallMessage);
220 if (res_msg.LogicalCallContext != null && res_msg.LogicalCallContext.HasInfo)
221 CallContext.UpdateCurrentCallContext (res_msg.LogicalCallContext);
223 exc = res_msg.Exception;
225 // todo: remove throw exception from the runtime invoke
228 throw exc.FixRemotingException();
230 else if (res_msg is IConstructionReturnMessage) {
231 if (out_args == null)
232 out_args = res_msg.OutArgs;
234 else if (mMsg.CallType == CallType.BeginInvoke) {
235 // We don't have OutArgs in this case.
237 else if (mMsg.CallType == CallType.Sync) {
238 out_args = ProcessResponse (res_msg, mMsg);
240 else if (mMsg.CallType == CallType.EndInvoke) {
241 out_args = ProcessResponse (res_msg, mMsg.AsyncResult.CallMessage);
244 if (out_args == null)
245 out_args = res_msg.OutArgs;
248 return res_msg.ReturnValue;
251 [MethodImplAttribute(MethodImplOptions.InternalCall)]
252 internal extern virtual object InternalGetTransparentProxy (string className);
254 public virtual object GetTransparentProxy ()
259 IRemotingTypeInfo rti = this as IRemotingTypeInfo;
263 if (name == null || name == typeof(MarshalByRefObject).AssemblyQualifiedName)
264 name = class_to_proxy.AssemblyQualifiedName;
267 name = class_to_proxy.AssemblyQualifiedName;
269 _objTP = InternalGetTransparentProxy (name);
276 public IConstructionReturnMessage InitializeServerObject(IConstructionCallMessage ctorMsg)
278 throw new NotImplementedException();
281 protected void AttachServer(MarshalByRefObject s)
286 protected MarshalByRefObject DetachServer()
288 MarshalByRefObject ob = _server;
293 protected MarshalByRefObject GetUnwrappedServer()
298 internal void SetTargetDomain (int domainId)
300 _targetDomainId = domainId;
303 // Called by the runtime
304 internal object GetAppDomainTarget ()
306 if (_server == null) {
307 ClientActivatedIdentity identity = RemotingServices.GetIdentityForUri (_targetUri) as ClientActivatedIdentity;
308 if (identity == null) throw new RemotingException ("Server for uri '" + _targetUri + "' not found");
309 _server = identity.GetServerObject ();
314 static object[] ProcessResponse (IMethodReturnMessage mrm, MonoMethodMessage call)
318 MethodInfo mi = (MethodInfo) call.MethodBase;
319 if (mrm.ReturnValue != null && !mi.ReturnType.IsInstanceOfType (mrm.ReturnValue))
320 throw new InvalidCastException ("Return value has an invalid type");
322 // Check out parameters
327 if (call.NeedsOutProcessing (out no))
329 ParameterInfo[] parameters = mi.GetParameters();
330 object[] outArgs = new object [no];
333 foreach (ParameterInfo par in parameters)
335 if (par.IsOut && !par.ParameterType.IsByRef)
337 // Special marshalling required
338 object outArg = par.Position < mrm.ArgCount ? mrm.GetArg (par.Position) : null;
339 if (outArg != null) {
340 object local = call.GetArg (par.Position);
341 if (local == null) throw new RemotingException ("Unexpected null value in local out parameter '" + par.Name + "'");
342 RemotingServices.UpdateOutArgObject (par, local, outArg);
345 else if (par.ParameterType.IsByRef)
347 object outArg = par.Position < mrm.ArgCount ? mrm.GetArg (par.Position) : null;
348 if (outArg != null && !par.ParameterType.GetElementType ().IsInstanceOfType (outArg))
350 throw new InvalidCastException ("Return argument '" + par.Name + "' has an invalid type");
352 outArgs [narg++] = outArg;
358 return new object [0];