2 // System.Runtime.Remoting.Proxies.RealProxy.cs
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Lluis Sanchez (lsg@ctv.es)
9 // (C) 2001 Ximian, Inc. http://www.ximian.com
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
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;
45 namespace System.Runtime.Remoting.Proxies
47 internal class TransparentProxy {
50 bool _custom_type_info;
53 public abstract class RealProxy {
55 #region Sync with object-internals.h
57 internal Context _targetContext;
58 MarshalByRefObject _server;
59 int _targetDomainId = -1;
60 internal string _targetUri;
63 internal Identity _objectIdentity;
67 protected RealProxy ()
71 protected RealProxy (Type classToProxy) : this(classToProxy, IntPtr.Zero, null)
75 internal RealProxy (Type classToProxy, ClientIdentity identity) : this(classToProxy, IntPtr.Zero, null)
77 _objectIdentity = identity;
80 protected RealProxy (Type classToProxy, IntPtr stub, object stubData)
82 if (!classToProxy.IsMarshalByRef && !classToProxy.IsInterface)
83 throw new ArgumentException("object must be MarshalByRef");
85 this.class_to_proxy = classToProxy;
87 if (stub != IntPtr.Zero)
88 throw new NotSupportedException ("stub is not used in Mono");
91 [MethodImplAttribute(MethodImplOptions.InternalCall)]
92 extern static Type InternalGetProxyType (object transparentProxy);
94 public Type GetProxiedType()
97 if (class_to_proxy.IsInterface) return typeof(MarshalByRefObject);
98 else return class_to_proxy;
100 return InternalGetProxyType (_objTP);
103 public virtual ObjRef CreateObjRef (Type requestedType)
105 return RemotingServices.Marshal ((MarshalByRefObject) GetTransparentProxy(), null, requestedType);
108 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
110 Object obj = GetTransparentProxy();
\r
111 RemotingServices.GetObjectData (obj, info, context);
\r
114 internal Identity ObjectIdentity
116 get { return _objectIdentity; }
117 set { _objectIdentity = value; }
121 public virtual IntPtr GetCOMIUnknown (bool fIsMarshalled)
123 throw new NotImplementedException ();
127 public virtual void SetCOMIUnknown (IntPtr i)
129 throw new NotImplementedException ();
133 public virtual IntPtr SupportsInterface (ref Guid iid)
135 throw new NotImplementedException ();
138 public static object GetStubData (RealProxy rp)
143 public static void SetStubData (RealProxy rp, object stubData)
145 rp._stubData = stubData;
148 public abstract IMessage Invoke (IMessage msg);
150 /* this is called from unmanaged code */
151 internal static object PrivateInvoke (RealProxy rp, IMessage msg, out Exception exc,
152 out object [] out_args)
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;
159 IMethodReturnMessage res_msg = null;
161 if (call_type == CallType.BeginInvoke)
\r
162 // todo: set CallMessage in runtime instead
163 mMsg.AsyncResult.CallMessage = mMsg;
165 if (call_type == CallType.EndInvoke)
166 res_msg = (IMethodReturnMessage)mMsg.AsyncResult.EndInvoke ();
168 // Check for constructor msg
169 if (mMsg.MethodBase.IsConstructor)
\r
172 res_msg = (IMethodReturnMessage) (rp as RemotingProxy).ActivateRemoteObject ((IMethodMessage) msg);
174 msg = new ConstructionCall (rp.GetProxiedType ());
179 res_msg = (IMethodReturnMessage)rp.Invoke (msg);
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
187 IMessage asyncMsg = null;
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);
195 if (res_msg.LogicalCallContext != null && res_msg.LogicalCallContext.HasInfo)
196 CallContext.UpdateCurrentCallContext (res_msg.LogicalCallContext);
198 exc = res_msg.Exception;
200 // todo: remove throw exception from the runtime invoke
203 throw exc.FixRemotingException();
205 else if (res_msg is IConstructionReturnMessage || mMsg.CallType == CallType.BeginInvoke) {
206 out_args = res_msg.OutArgs;
208 else if (mMsg.CallType == CallType.Sync) {
209 out_args = ProcessResponse (res_msg, mMsg);
211 else if (mMsg.CallType == CallType.EndInvoke) {
212 out_args = ProcessResponse (res_msg, mMsg.AsyncResult.CallMessage);
215 out_args = res_msg.OutArgs;
218 return res_msg.ReturnValue;
221 [MethodImplAttribute(MethodImplOptions.InternalCall)]
222 internal extern virtual object InternalGetTransparentProxy (string className);
224 public virtual object GetTransparentProxy ()
229 IRemotingTypeInfo rti = this as IRemotingTypeInfo;
233 if (name == null || name == typeof(MarshalByRefObject).AssemblyQualifiedName)
234 name = class_to_proxy.AssemblyQualifiedName;
237 name = class_to_proxy.AssemblyQualifiedName;
239 _objTP = InternalGetTransparentProxy (name);
245 public IConstructionReturnMessage InitializeServerObject(IConstructionCallMessage ctorMsg)
247 throw new NotImplementedException();
250 protected void AttachServer(MarshalByRefObject s)
255 protected MarshalByRefObject DetachServer()
257 MarshalByRefObject ob = _server;
262 protected MarshalByRefObject GetUnwrappedServer()
267 internal void SetTargetDomain (int domainId)
269 _targetDomainId = domainId;
272 // Called by the runtime
273 internal object GetAppDomainTarget ()
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 ();
283 static object[] ProcessResponse (IMethodReturnMessage mrm, IMethodCallMessage call)
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");
291 // Check out parameters
293 if (mrm.OutArgCount > 0)
295 ParameterInfo[] parameters = mi.GetParameters();
297 foreach (ParameterInfo par in parameters)
298 if (par.ParameterType.IsByRef) no++;
300 object[] outArgs = new object [no];
304 foreach (ParameterInfo par in parameters)
306 if (par.IsOut && !par.ParameterType.IsByRef)
308 // Special marshalling required
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);
317 else if (par.ParameterType.IsByRef)
319 object outArg = mrm.GetOutArg (nout++);
320 if (outArg != null && !par.ParameterType.IsInstanceOfType (outArg))
322 throw new RemotingException ("Return argument '" + par.Name + "' has an invalid type");
324 outArgs [narg++] = outArg;
330 return new object [0];