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