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