New test.
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Proxies / RealProxy.cs
index 84eb8823ca95a62f6ad5a4d43f4b81478d037b62..b5d74d99e66f3b2383b90a1d47d7af0fac34fbb8 100644 (file)
@@ -9,6 +9,29 @@
 // (C) 2001 Ximian, Inc.  http://www.ximian.com
 //
 
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.Reflection;
 using System.Runtime.Remoting;
@@ -28,24 +51,30 @@ namespace System.Runtime.Remoting.Proxies
        }
        
        public abstract class RealProxy {
-
+               // other classes visible to the runtime 
+               // derive from this class so keep these locals
+               // in sync with the definition RealProxy 
+               // in object-internals.h
+               #region Sync with object-internals.h
                Type class_to_proxy;
                internal Context _targetContext;
                MarshalByRefObject _server;
+               int _targetDomainId = -1;
+               internal string _targetUri;
                internal Identity _objectIdentity;
-               Object _objTP;
-               object _stubData;
+               Object _objTP;\r
+               object _stubData;\r
+        #endregion
 
                protected RealProxy ()
                {
-                       throw new NotImplementedException ();
                }
 
-               protected RealProxy (Type classToProxy) : this(classToProxy, (IntPtr) 0, null)
+               protected RealProxy (Type classToProxy) : this(classToProxy, IntPtr.Zero, null)
                {
                }
 
-               internal RealProxy (Type classToProxy, ClientIdentity identity) : this(classToProxy, (IntPtr) 0, null)
+               internal RealProxy (Type classToProxy, ClientIdentity identity) : this(classToProxy, IntPtr.Zero, null)
                {
                        _objectIdentity = identity;
                }
@@ -57,14 +86,20 @@ namespace System.Runtime.Remoting.Proxies
 
                        this.class_to_proxy = classToProxy;
 
-                       // TODO: Fix stub
-                       _objTP = InternalGetTransparentProxy();
+                       if (stub != IntPtr.Zero)
+                               throw new NotSupportedException ("stub is not used in Mono");
                }
 
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               extern static Type InternalGetProxyType (object transparentProxy);
+               
                public Type GetProxiedType() 
                {
-                       if (class_to_proxy.IsInterface) return typeof (MarshalByRefObject);
-                       else return class_to_proxy;
+                       if (_objTP == null) {
+                               if (class_to_proxy.IsInterface) return typeof(MarshalByRefObject);
+                               else return class_to_proxy;
+                       }
+                       return InternalGetProxyType (_objTP);
                }
 
                public virtual ObjRef CreateObjRef (Type requestedType)
@@ -119,13 +154,46 @@ namespace System.Runtime.Remoting.Proxies
                                                      out object [] out_args)
                {
                        MonoMethodMessage mMsg = (MonoMethodMessage) msg;
-                       mMsg.LogicalCallContext = CallContext.CreateLogicalCallContext();
+                       mMsg.LogicalCallContext = CallContext.CreateLogicalCallContext (true);
+                       CallType call_type = mMsg.CallType;
+                       bool is_remproxy = (rp as RemotingProxy) != null;
+
+                       IMethodReturnMessage res_msg = null;
                        
-                       if (mMsg.CallType == CallType.BeginInvoke) 
-                               mMsg.AsyncResult.CallMessage = mMsg;    // TODO: do this in the runtime
+                       if (call_type == CallType.BeginInvoke) \r
+                               // todo: set CallMessage in runtime instead
+                               mMsg.AsyncResult.CallMessage = mMsg;
+
+                       if (call_type == CallType.EndInvoke)
+                               res_msg = (IMethodReturnMessage)mMsg.AsyncResult.EndInvoke ();
 
-                       IMethodReturnMessage res_msg = (IMethodReturnMessage)rp.Invoke (msg);
+                       // Check for constructor msg
+                       if (mMsg.MethodBase.IsConstructor) \r
+                       {
+                               if (is_remproxy) \r
+                                       res_msg = (IMethodReturnMessage) (rp as RemotingProxy).ActivateRemoteObject ((IMethodMessage) msg);
+                               else \r
+                                       msg = new ConstructionCall (rp.GetProxiedType ());
+                       }
+                               
+                       if (null == res_msg) 
+                       {
+                               res_msg = (IMethodReturnMessage)rp.Invoke (msg);
+\r
+                               // Note, from begining this code used AsyncResult.IsCompleted for\r
+                               // checking if it was a remoting or custom proxy, but in some\r
+                               // cases the remoting proxy finish before the call returns\r
+                               // causing this method to be called, therefore causing all kind of bugs.\r
+                               if ((!is_remproxy) && call_type == CallType.BeginInvoke)\r
+                               {\r
+                                       IMessage asyncMsg = null;
 
+                                       // allow calltype EndInvoke to finish
+                                       asyncMsg = mMsg.AsyncResult.SyncProcessMessage (res_msg as IMessage);
+                                       res_msg = new ReturnMessage (asyncMsg, null, 0, null, res_msg as IMethodCallMessage);
+                               }\r
+                       }
+                       
                        if (res_msg.LogicalCallContext != null && res_msg.LogicalCallContext.HasInfo)
                                CallContext.UpdateCurrentCallContext (res_msg.LogicalCallContext);
 
@@ -153,10 +221,25 @@ namespace System.Runtime.Remoting.Proxies
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               internal extern virtual object InternalGetTransparentProxy ();
+               internal extern virtual object InternalGetTransparentProxy (string className);
 
                public virtual object GetTransparentProxy () 
                {
+                       if (_objTP == null) 
+                       {
+                               string name;
+                               IRemotingTypeInfo rti = this as IRemotingTypeInfo;
+                               
+                               if (rti != null) {
+                                       name = rti.TypeName;
+                                       if (name == null || name == typeof(MarshalByRefObject).AssemblyQualifiedName)
+                                               name = class_to_proxy.AssemblyQualifiedName;
+                               }
+                               else
+                                       name = class_to_proxy.AssemblyQualifiedName;
+                                       
+                               _objTP = InternalGetTransparentProxy (name);
+                       }
                        return _objTP;
                }
 
@@ -182,47 +265,60 @@ namespace System.Runtime.Remoting.Proxies
                {
                        return _server;
                }
+               
+               internal void SetTargetDomain (int domainId)
+               {
+                       _targetDomainId = domainId;
+               }
+               
+               // Called by the runtime
+               internal object GetAppDomainTarget ()
+               {
+                       if (_server == null) {
+                               ClientActivatedIdentity identity = RemotingServices.GetIdentityForUri (_targetUri) as ClientActivatedIdentity;
+                               if (identity == null) throw new RemotingException ("Server for uri '" + _targetUri + "' not found");
+                               _server = identity.GetServerObject ();
+                       }
+                       return _server;
+               }
 
-               static object[] ProcessResponse (IMethodReturnMessage mrm, IMethodCallMessage call)
+               static object[] ProcessResponse (IMethodReturnMessage mrm, MonoMethodMessage call)
                {
                        // Check return type
 
                        MethodInfo mi = (MethodInfo) call.MethodBase;
                        if (mrm.ReturnValue != null && !mi.ReturnType.IsInstanceOfType (mrm.ReturnValue))
-                               throw new RemotingException ("Return value has an invalid type");
+                               throw new InvalidCastException ("Return value has an invalid type");
 
                        // Check out parameters
 
-                       if (mrm.OutArgCount > 0)
+                       
+                       int no;
+                       
+                       if (call.NeedsOutProcessing (out no))
                        {
                                ParameterInfo[] parameters = mi.GetParameters();
-                               int no = 0;
-                               foreach (ParameterInfo par in parameters)
-                                       if (par.ParameterType.IsByRef) no++;
-                               
                                object[] outArgs = new object [no];
                                int narg = 0;
-                               int nout = 0;
        
                                foreach (ParameterInfo par in parameters)
                                {
                                        if (par.IsOut && !par.ParameterType.IsByRef)
                                        {
                                                // Special marshalling required
-                                               
-                                               object outArg = mrm.GetOutArg (nout++);
+                                               object outArg = par.Position < mrm.ArgCount ? mrm.GetArg (par.Position) : null;
                                                if (outArg != null) {
                                                        object local = call.GetArg (par.Position);
-                                                       if (local == null) throw new RemotingException ("Unexpected null value in local out parameter '" + par.Position + " " + par.Name + "'");
+                                                       if (local == null) throw new RemotingException ("Unexpected null value in local out parameter '" + par.Name + "'");
                                                        RemotingServices.UpdateOutArgObject (par, local, outArg);
                                                }
                                        }
                                        else if (par.ParameterType.IsByRef)
                                        {
-                                               object outArg = mrm.GetOutArg (nout++);
-                                               if (outArg != null && !par.ParameterType.IsInstanceOfType (outArg))
+                                               object outArg = par.Position < mrm.ArgCount ? mrm.GetArg (par.Position) : null;
+                                               if (outArg != null && !par.ParameterType.GetElementType ().IsInstanceOfType (outArg))
                                                {
-                                                       throw new RemotingException ("Return argument '" + par.Name + "' has an invalid type");
+                                                       throw new InvalidCastException ("Return argument '" + par.Name + "' has an invalid type");
                                                }
                                                outArgs [narg++] = outArg;
                                        }