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 using System.Reflection;
14 using System.Runtime.Remoting;
15 using System.Runtime.Remoting.Messaging;
16 using System.Runtime.Remoting.Activation;
17 using System.Runtime.Remoting.Contexts;
18 using System.Runtime.CompilerServices;
19 using System.Runtime.Serialization;
22 namespace System.Runtime.Remoting.Proxies
24 internal class TransparentProxy {
27 bool _custom_type_info;
30 public abstract class RealProxy {
33 internal Context _targetContext;
34 MarshalByRefObject _server;
35 internal Identity _objectIdentity;
39 protected RealProxy ()
41 throw new NotImplementedException ();
44 protected RealProxy (Type classToProxy) : this(classToProxy, (IntPtr) 0, null)
48 internal RealProxy (Type classToProxy, ClientIdentity identity) : this(classToProxy, (IntPtr) 0, null)
50 _objectIdentity = identity;
53 protected RealProxy (Type classToProxy, IntPtr stub, object stubData)
55 if (!classToProxy.IsMarshalByRef && !classToProxy.IsInterface)
56 throw new ArgumentException("object must be MarshalByRef");
58 this.class_to_proxy = classToProxy;
61 _objTP = InternalGetTransparentProxy();
64 public Type GetProxiedType()
66 if (class_to_proxy.IsInterface) return typeof (MarshalByRefObject);
67 else return class_to_proxy;
70 public virtual ObjRef CreateObjRef (Type requestedType)
72 return RemotingServices.Marshal ((MarshalByRefObject) GetTransparentProxy(), null, requestedType);
75 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
77 Object obj = GetTransparentProxy();
\r
78 RemotingServices.GetObjectData (obj, info, context);
\r
81 internal Identity ObjectIdentity
83 get { return _objectIdentity; }
84 set { _objectIdentity = value; }
88 public virtual IntPtr GetCOMIUnknown (bool fIsMarshalled)
90 throw new NotImplementedException ();
94 public virtual void SetCOMIUnknown (IntPtr i)
96 throw new NotImplementedException ();
100 public virtual IntPtr SupportsInterface (ref Guid iid)
102 throw new NotImplementedException ();
105 public static object GetStubData (RealProxy rp)
110 public static void SetStubData (RealProxy rp, object stubData)
112 rp._stubData = stubData;
115 public abstract IMessage Invoke (IMessage msg);
117 /* this is called from unmanaged code */
118 internal static object PrivateInvoke (RealProxy rp, IMessage msg, out Exception exc,
119 out object [] out_args)
121 MonoMethodMessage mMsg = (MonoMethodMessage) msg;
122 mMsg.LogicalCallContext = CallContext.CreateLogicalCallContext();
124 if (mMsg.CallType == CallType.BeginInvoke)
125 mMsg.AsyncResult.CallMessage = mMsg; // TODO: do this in the runtime
127 IMethodReturnMessage res_msg = (IMethodReturnMessage)rp.Invoke (msg);
129 if (res_msg.LogicalCallContext != null && res_msg.LogicalCallContext.HasInfo)
130 CallContext.UpdateCurrentCallContext (res_msg.LogicalCallContext);
132 exc = res_msg.Exception;
134 // todo: remove throw exception from the runtime invoke
137 throw exc.FixRemotingException();
139 else if (res_msg is IConstructionReturnMessage || mMsg.CallType == CallType.BeginInvoke) {
140 out_args = res_msg.OutArgs;
142 else if (mMsg.CallType == CallType.Sync) {
143 out_args = ProcessResponse (res_msg, mMsg);
145 else if (mMsg.CallType == CallType.EndInvoke) {
146 out_args = ProcessResponse (res_msg, mMsg.AsyncResult.CallMessage);
149 out_args = res_msg.OutArgs;
152 return res_msg.ReturnValue;
155 [MethodImplAttribute(MethodImplOptions.InternalCall)]
156 internal extern virtual object InternalGetTransparentProxy ();
158 public virtual object GetTransparentProxy ()
164 public IConstructionReturnMessage InitializeServerObject(IConstructionCallMessage ctorMsg)
166 throw new NotImplementedException();
169 protected void AttachServer(MarshalByRefObject s)
174 protected MarshalByRefObject DetachServer()
176 MarshalByRefObject ob = _server;
181 protected MarshalByRefObject GetUnwrappedServer()
186 static object[] ProcessResponse (IMethodReturnMessage mrm, IMethodCallMessage call)
190 MethodInfo mi = (MethodInfo) call.MethodBase;
191 if (mrm.ReturnValue != null && !mi.ReturnType.IsInstanceOfType (mrm.ReturnValue))
192 throw new RemotingException ("Return value has an invalid type");
194 // Check out parameters
196 if (mrm.OutArgCount > 0)
198 ParameterInfo[] parameters = mi.GetParameters();
200 foreach (ParameterInfo par in parameters)
201 if (par.ParameterType.IsByRef) no++;
203 object[] outArgs = new object [no];
207 foreach (ParameterInfo par in parameters)
209 if (par.IsOut && !par.ParameterType.IsByRef)
211 // Special marshalling required
213 object outArg = mrm.GetOutArg (nout++);
214 if (outArg != null) {
215 object local = call.GetArg (par.Position);
216 if (local == null) throw new RemotingException ("Unexpected null value in local out parameter '" + par.Position + " " + par.Name + "'");
217 RemotingServices.UpdateOutArgObject (par, local, outArg);
220 else if (par.ParameterType.IsByRef)
222 object outArg = mrm.GetOutArg (nout++);
223 if (outArg != null && !par.ParameterType.IsInstanceOfType (outArg))
225 throw new RemotingException ("Return argument '" + par.Name + "' has an invalid type");
227 outArgs [narg++] = outArg;
233 return new object [0];