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