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