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