[corlib] Improve flow of ExecutionContext (few tests taken from https://github.com...
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Proxies / RealProxy.cs
1 //
2 // System.Runtime.Remoting.Proxies.RealProxy.cs
3 //
4 // Authors:
5 //   Dietmar Maurer (dietmar@ximian.com)
6 //   Lluis Sanchez (lsg@ctv.es)
7 //   Patrik Torstensson
8 //
9 // (C) 2001 Ximian, Inc.  http://www.ximian.com
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
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:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
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.
33 //
34
35 using System;
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;
44 using System.Threading;
45
46 namespace System.Runtime.Remoting.Proxies
47 {
48 #pragma warning disable 169, 649
49         [StructLayout (LayoutKind.Sequential)]
50         internal class TransparentProxy {
51                 public RealProxy _rp;
52                 IntPtr _class;
53                 bool _custom_type_info;
54         }
55 #pragma warning restore 169, 649
56         
57         [ComVisible (true)]
58         [StructLayout (LayoutKind.Sequential)]
59         public abstract class RealProxy {
60                 // other classes visible to the runtime 
61                 // derive from this class so keep these locals
62                 // in sync with the definition RealProxy 
63                 // in object-internals.h
64                 
65 #pragma warning disable 169, 414                
66                 #region Sync with object-internals.h
67                 Type class_to_proxy;
68                 internal Context _targetContext;
69                 MarshalByRefObject _server;
70                 int _targetDomainId = -1;
71                 internal string _targetUri;
72                 internal Identity _objectIdentity;
73                 Object _objTP;
74                 object _stubData;
75         #endregion
76 #pragma warning restore 169, 414
77
78                 protected RealProxy ()
79                 {
80                 }
81
82                 protected RealProxy (Type classToProxy) : this(classToProxy, IntPtr.Zero, null)
83                 {
84                 }
85
86                 internal RealProxy (Type classToProxy, ClientIdentity identity) : this(classToProxy, IntPtr.Zero, null)
87                 {
88                         _objectIdentity = identity;
89                 }
90
91                 protected RealProxy (Type classToProxy, IntPtr stub, object stubData)
92                 {
93                         if (!classToProxy.IsMarshalByRef && !classToProxy.IsInterface)
94                                 throw new ArgumentException("object must be MarshalByRef");
95
96                         this.class_to_proxy = classToProxy;
97
98                         if (stub != IntPtr.Zero)
99                                 throw new NotSupportedException ("stub is not used in Mono");
100                 }
101
102                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
103                 extern static Type InternalGetProxyType (object transparentProxy);
104                 
105                 public Type GetProxiedType() 
106                 {
107                         if (_objTP == null) {
108                                 if (class_to_proxy.IsInterface) return typeof(MarshalByRefObject);
109                                 else return class_to_proxy;
110                         }
111                         return InternalGetProxyType (_objTP);
112                 }
113
114                 public virtual ObjRef CreateObjRef (Type requestedType)
115                 {
116                         return RemotingServices.Marshal ((MarshalByRefObject) GetTransparentProxy(), null, requestedType);
117                 }
118
119                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
120                 {
121                         Object obj = GetTransparentProxy();
122                         RemotingServices.GetObjectData (obj, info, context);            
123                 }
124                 
125                 internal Identity ObjectIdentity
126                 {
127                         get { return _objectIdentity; }
128                         set { _objectIdentity = value; }
129                 }
130                 
131                 [MonoTODO]
132                 public virtual IntPtr GetCOMIUnknown (bool fIsMarshalled)
133                 {
134                         throw new NotImplementedException ();
135                 }
136                 
137                 [MonoTODO]
138                 public virtual void SetCOMIUnknown (IntPtr i)
139                 {
140                         throw new NotImplementedException ();
141                 }
142                 
143                 [MonoTODO]
144                 public virtual IntPtr SupportsInterface (ref Guid iid)
145                 {
146                         throw new NotImplementedException ();
147                 }
148                 
149                 public static object GetStubData (RealProxy rp)
150                 {
151                         return rp._stubData;
152                 }
153                 
154                 public static void SetStubData (RealProxy rp, object stubData)
155                 {
156                         rp._stubData = stubData;
157                 }
158
159                 public abstract IMessage Invoke (IMessage msg);
160
161                 /* this is called from unmanaged code */
162                 internal static object PrivateInvoke (RealProxy rp, IMessage msg, out Exception exc,
163                                                       out object [] out_args)
164                 {
165                         MonoMethodMessage mMsg = (MonoMethodMessage) msg;
166                         mMsg.LogicalCallContext = ExecutionContext.CreateLogicalCallContext (true);
167                         CallType call_type = mMsg.CallType;
168                         bool is_remproxy = (rp is RemotingProxy);
169
170                         out_args = null;
171                         IMethodReturnMessage res_msg = null;
172                         
173                         if (call_type == CallType.BeginInvoke) 
174                                 // todo: set CallMessage in runtime instead
175                                 mMsg.AsyncResult.CallMessage = mMsg;
176
177                         if (call_type == CallType.EndInvoke)
178                                 res_msg = (IMethodReturnMessage)mMsg.AsyncResult.EndInvoke ();
179
180                         // Check for constructor msg
181                         if (mMsg.MethodBase.IsConstructor) 
182                         {
183                                 if (is_remproxy) 
184                                         res_msg = (IMethodReturnMessage) (rp as RemotingProxy).ActivateRemoteObject ((IMethodMessage) msg);
185                                 else 
186                                         msg = new ConstructionCall (rp.GetProxiedType ());
187                         }
188                                 
189                         if (null == res_msg) 
190                         {
191                                 bool failed = false;
192                                 
193                                 try {
194                                         res_msg = (IMethodReturnMessage)rp.Invoke (msg);
195                                 } catch (Exception ex) {
196                                         failed = true;
197                                         if (call_type == CallType.BeginInvoke) {
198                                                 // If async dispatch crashes, don't propagate the exception.
199                                                 // The exception will be raised when calling EndInvoke.
200                                                 mMsg.AsyncResult.SyncProcessMessage (new ReturnMessage (ex, msg as IMethodCallMessage));
201                                                 res_msg = new ReturnMessage (null, null, 0, null, msg as IMethodCallMessage);
202                                         } else
203                                                 throw;
204                                 }
205                                 
206                                 // Note, from begining this code used AsyncResult.IsCompleted for
207                                 // checking if it was a remoting or custom proxy, but in some
208                                 // cases the remoting proxy finish before the call returns
209                                 // causing this method to be called, therefore causing all kind of bugs.
210                                 if ((!is_remproxy) && call_type == CallType.BeginInvoke && !failed)
211                                 {
212                                         IMessage asyncMsg = null;
213
214                                         // allow calltype EndInvoke to finish
215                                         asyncMsg = mMsg.AsyncResult.SyncProcessMessage (res_msg as IMessage);
216                                         out_args = res_msg.OutArgs;
217                                         res_msg = new ReturnMessage (asyncMsg, null, 0, null, res_msg as IMethodCallMessage);
218                                 }
219                         }
220                         
221                         if (res_msg.LogicalCallContext != null && res_msg.LogicalCallContext.HasInfo)
222                                 CallContext.UpdateCurrentLogicalCallContext (res_msg.LogicalCallContext);
223
224                         exc = res_msg.Exception;
225
226                         // todo: remove throw exception from the runtime invoke
227                         if (null != exc) {
228                                 out_args = null;
229                                 throw exc.FixRemotingException();
230                         }
231                         else if (res_msg is IConstructionReturnMessage) {
232                                 if (out_args == null)
233                                         out_args = res_msg.OutArgs;
234                         }
235                         else if (mMsg.CallType == CallType.BeginInvoke) {
236                                 // We don't have OutArgs in this case.
237                         }
238                         else if (mMsg.CallType == CallType.Sync) {
239                                 out_args = ProcessResponse (res_msg, mMsg);
240                         }
241                         else if (mMsg.CallType == CallType.EndInvoke) {
242                                 out_args = ProcessResponse (res_msg, mMsg.AsyncResult.CallMessage);
243                         }
244                         else {
245                                 if (out_args == null)
246                                         out_args = res_msg.OutArgs;
247                         }
248
249                         return res_msg.ReturnValue;
250                 }
251
252                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
253                 internal extern virtual object InternalGetTransparentProxy (string className);
254
255                 public virtual object GetTransparentProxy () 
256                 {
257                         if (_objTP == null) 
258                         {
259                                 string name;
260                                 IRemotingTypeInfo rti = this as IRemotingTypeInfo;
261                                 
262                                 if (rti != null) {
263                                         name = rti.TypeName;
264                                         if (name == null || name == typeof(MarshalByRefObject).AssemblyQualifiedName)
265                                                 name = class_to_proxy.AssemblyQualifiedName;
266                                 }
267                                 else
268                                         name = class_to_proxy.AssemblyQualifiedName;
269                                         
270                                 _objTP = InternalGetTransparentProxy (name);
271                         }
272                         return _objTP;
273                 }
274
275                 [MonoTODO]
276                 [ComVisible (true)]
277                 public IConstructionReturnMessage InitializeServerObject(IConstructionCallMessage ctorMsg)
278                 {
279                         throw new NotImplementedException();
280                 }
281
282                 protected void AttachServer(MarshalByRefObject s)
283                 {
284                         _server = s;
285                 }
286
287                 protected MarshalByRefObject DetachServer()
288                 {
289                         MarshalByRefObject ob = _server;
290                         _server = null;
291                         return ob;
292                 }
293
294                 protected MarshalByRefObject GetUnwrappedServer()
295                 {
296                         return _server;
297                 }
298                 
299                 internal void SetTargetDomain (int domainId)
300                 {
301                         _targetDomainId = domainId;
302                 }
303                 
304                 // Called by the runtime
305                 internal object GetAppDomainTarget ()
306                 {
307                         if (_server == null) {
308                                 ClientActivatedIdentity identity = RemotingServices.GetIdentityForUri (_targetUri) as ClientActivatedIdentity;
309                                 if (identity == null) throw new RemotingException ("Server for uri '" + _targetUri + "' not found");
310                                 _server = identity.GetServerObject ();
311                         }
312                         return _server;
313                 }
314
315                 static object[] ProcessResponse (IMethodReturnMessage mrm, MonoMethodMessage call)
316                 {
317                         // Check return type
318
319                         MethodInfo mi = (MethodInfo) call.MethodBase;
320                         if (mrm.ReturnValue != null && !mi.ReturnType.IsInstanceOfType (mrm.ReturnValue))
321                                 throw new InvalidCastException ("Return value has an invalid type");
322
323                         // Check out parameters
324
325                         
326                         int no;
327                         
328                         if (call.NeedsOutProcessing (out no))
329                         {
330                                 ParameterInfo[] parameters = mi.GetParameters();
331                                 object[] outArgs = new object [no];
332                                 int narg = 0;
333         
334                                 foreach (ParameterInfo par in parameters)
335                                 {
336                                         if (par.IsOut && !par.ParameterType.IsByRef)
337                                         {
338                                                 // Special marshalling required
339                                                 object outArg = par.Position < mrm.ArgCount ? mrm.GetArg (par.Position) : null;
340                                                 if (outArg != null) {
341                                                         object local = call.GetArg (par.Position);
342                                                         if (local == null) throw new RemotingException ("Unexpected null value in local out parameter '" + par.Name + "'");
343                                                         RemotingServices.UpdateOutArgObject (par, local, outArg);
344                                                 }
345                                         }
346                                         else if (par.ParameterType.IsByRef)
347                                         {
348                                                 object outArg = par.Position < mrm.ArgCount ? mrm.GetArg (par.Position) : null;
349                                                 if (outArg != null && !par.ParameterType.GetElementType ().IsInstanceOfType (outArg))
350                                                 {
351                                                         throw new InvalidCastException ("Return argument '" + par.Name + "' has an invalid type");
352                                                 }
353                                                 outArgs [narg++] = outArg;
354                                         }
355                                 }
356                                 return outArgs;
357                         }
358                         else
359                                 return new object [0];
360                 }
361         }
362 }