RemotingServices::IsTransparentProxy doesn't work on mobile.
[mono.git] / mcs / class / corlib / System.Runtime.Remoting / RemotingServices.cs
index 9fa45c85328fe4eb1c1201bc9b481fd2b0faf7d1..988a1ed8aeefdbccf9a24abc23767b01c4010c7b 100644 (file)
@@ -46,14 +46,18 @@ using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System.Runtime.Remoting.Services;
 using System.Security.Permissions;
-
-#if NET_2_0
 using System.Runtime.ConstrainedExecution;
-#endif
+using System.Runtime.Serialization.Formatters;
 
 namespace System.Runtime.Remoting
 {
-       public sealed class RemotingServices 
+       [System.Runtime.InteropServices.ComVisible (true)]
+#if NET_4_0
+       static
+#else
+       sealed
+#endif
+       public class RemotingServices 
        {
                // Holds the identities of the objects, using uri as index
                static Hashtable uri_hash = new Hashtable ();           
@@ -61,9 +65,13 @@ namespace System.Runtime.Remoting
                static BinaryFormatter _serializationFormatter;
                static BinaryFormatter _deserializationFormatter;
                
-               internal static string app_id;
+               static string app_id;
+               static readonly object app_id_lock = new object ();
+               
                static int next_id = 1;
-               static readonly BindingFlags methodBindings = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
+               const BindingFlags methodBindings = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
+               static readonly MethodInfo FieldSetterMethod;
+               static readonly MethodInfo FieldGetterMethod;
                
                // Holds information in xdomain calls. Names are short to minimize serialized size.
                [Serializable]
@@ -78,24 +86,38 @@ namespace System.Runtime.Remoting
                        StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
                        _serializationFormatter = new BinaryFormatter (surrogateSelector, context);
                        _deserializationFormatter = new BinaryFormatter (null, context);
+                       _serializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Full;
+                       _deserializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Full;
                        
                        RegisterInternalChannels ();
-                       app_id = Guid.NewGuid().ToString().Replace('-', '_') + "/";
                        CreateWellKnownServerIdentity (typeof(RemoteActivator), "RemoteActivationService.rem", WellKnownObjectMode.Singleton);
+                       
+                       FieldSetterMethod = typeof(object).GetMethod ("FieldSetter", BindingFlags.NonPublic|BindingFlags.Instance);
+                       FieldGetterMethod = typeof(object).GetMethod ("FieldGetter", BindingFlags.NonPublic|BindingFlags.Instance);
                }
-       
+#if !NET_4_0
                private RemotingServices () {}
+#endif
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                internal extern static object InternalExecute (MethodBase method, Object obj,
                                                               Object[] parameters, out object [] out_args);
 
-#if NET_2_0
+               // Returns the actual implementation of @method in @type.
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               internal extern static MethodBase GetVirtualMethod (Type type, MethodBase method);
+
+#if MOBILE
+               public static bool IsTransparentProxy (object proxy)
+               {
+                       throw new NotSupportedException ();
+               }
+#else
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static bool IsTransparentProxy (object proxy);
-               
+#endif
+
                internal static IMethodReturnMessage InternalExecuteMessage (
                        MarshalByRefObject target, IMethodCallMessage reqMsg)
                {
@@ -103,11 +125,24 @@ namespace System.Runtime.Remoting
                        
                        Type tt = target.GetType ();
                        MethodBase method;
-                       if (reqMsg.MethodBase.DeclaringType == tt /*|| reqMsg.MethodBase.DeclaringType.IsInterface*/)
+                       if (reqMsg.MethodBase.DeclaringType == tt ||
+                           reqMsg.MethodBase == FieldSetterMethod || 
+                           reqMsg.MethodBase == FieldGetterMethod) {
                                method = reqMsg.MethodBase;
-                       else
-                               method = tt.GetMethod (reqMsg.MethodName, BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance, null, (Type[]) reqMsg.MethodSignature, null);
-                               
+                       } else {
+                               method = GetVirtualMethod (tt, reqMsg.MethodBase);
+
+                               if (method == null)
+                                       throw new RemotingException (
+                                               String.Format ("Cannot resolve method {0}:{1}", tt, reqMsg.MethodName));
+                       }
+
+                       if (reqMsg.MethodBase.IsGenericMethod) {
+                               Type[] genericArguments = reqMsg.MethodBase.GetGenericArguments ();
+                               MethodInfo gmd = ((MethodInfo)method).GetGenericMethodDefinition ();
+                               method = gmd.MakeGenericMethod (genericArguments);
+                       }
+
                        object oldContext = CallContext.SetCurrentCallContext (reqMsg.LogicalCallContext);
                        
                        try 
@@ -158,12 +193,14 @@ namespace System.Runtime.Remoting
                                return InternalExecuteMessage (target, reqMsg);
                }
 
+               [System.Runtime.InteropServices.ComVisible (true)]
                public static object Connect (Type classToProxy, string url)
                {
                        ObjRef objRef = new ObjRef (classToProxy, url, null);
                        return GetRemoteObject (objRef, classToProxy);
                }
 
+               [System.Runtime.InteropServices.ComVisible (true)]
                public static object Connect (Type classToProxy, string url, object data)
                {
                        ObjRef objRef = new ObjRef (classToProxy, url, data);
@@ -203,9 +240,9 @@ namespace System.Runtime.Remoting
                        }
                }
 
-               public static Type GetServerTypeForUri (string uri)
+               public static Type GetServerTypeForUri (string URI)
                {
-                       ServerIdentity ident = GetIdentityForUri (uri) as ServerIdentity;
+                       ServerIdentity ident = GetIdentityForUri (URI) as ServerIdentity;
                        if (ident == null) return null;
                        return ident.ObjectType;
                }
@@ -218,19 +255,19 @@ namespace System.Runtime.Remoting
                        else return null;
                }
 
-               public static object Unmarshal (ObjRef objref)
+               public static object Unmarshal (ObjRef objectRef)
                {
-                       return Unmarshal(objref, true);
+                       return Unmarshal (objectRef, true);
                }
 
-               public static object Unmarshal (ObjRef objref, bool fRefine)
+               public static object Unmarshal (ObjRef objectRef, bool fRefine)
                {
-                       Type classToProxy = fRefine ? objref.ServerType : typeof (MarshalByRefObject);
+                       Type classToProxy = fRefine ? objectRef.ServerType : typeof (MarshalByRefObject);
                        if (classToProxy == null) classToProxy = typeof (MarshalByRefObject);
 
-                       if (objref.IsReferenceToWellKnow) {
-                               object obj = GetRemoteObject(objref, classToProxy);
-                               TrackingServices.NotifyUnmarshaledObject (obj, objref);
+                       if (objectRef.IsReferenceToWellKnow) {
+                               object obj = GetRemoteObject (objectRef, classToProxy);
+                               TrackingServices.NotifyUnmarshaledObject (obj, objectRef);
                                return obj;
                        }
                        else
@@ -239,34 +276,34 @@ namespace System.Runtime.Remoting
                                
                                if (classToProxy.IsContextful) {
                                        // Look for a ProxyAttribute
-                                       ProxyAttribute att = (ProxyAttribute) Attribute.GetCustomAttribute (classToProxy, typeof(ProxyAttribute),true);
+                                       ProxyAttribute att = (ProxyAttribute) Attribute.GetCustomAttribute (classToProxy, typeof(ProxyAttribute), true);
                                        if (att != null) {
-                                               obj = att.CreateProxy (objref, classToProxy, null, null).GetTransparentProxy();
-                                               TrackingServices.NotifyUnmarshaledObject (obj, objref);
+                                               obj = att.CreateProxy (objectRef, classToProxy, null, null).GetTransparentProxy();
+                                               TrackingServices.NotifyUnmarshaledObject (obj, objectRef);
                                                return obj;
                                        }
                                }
-                               obj = GetProxyForRemoteObject (objref, classToProxy);
-                               TrackingServices.NotifyUnmarshaledObject (obj, objref);
+                               obj = GetProxyForRemoteObject (objectRef, classToProxy);
+                               TrackingServices.NotifyUnmarshaledObject (obj, objectRef);
                                return obj;
                        }
                }
 
-               public static ObjRef Marshal (MarshalByRefObject obj)
+               public static ObjRef Marshal (MarshalByRefObject Obj)
                {
-                       return Marshal (obj, null, null);
+                       return Marshal (Obj, null, null);
                }
                
-               public static ObjRef Marshal (MarshalByRefObject obj, string uri)
+               public static ObjRef Marshal (MarshalByRefObject Obj, string URI)
                {
-                       return Marshal (obj, uri, null);
+                       return Marshal (Obj, URI, null);
                }
                
-               public static ObjRef Marshal (MarshalByRefObject obj, string uri, Type requested_type)
+               public static ObjRef Marshal (MarshalByRefObject Obj, string ObjURI, Type RequestedType)
                {
-                       if (IsTransparentProxy (obj))
+                       if (IsTransparentProxy (Obj))
                        {
-                               RealProxy proxy = RemotingServices.GetRealProxy(obj);
+                               RealProxy proxy = RemotingServices.GetRealProxy (Obj);
                                Identity identity = proxy.ObjectIdentity;
 
                                if (identity != null)
@@ -275,58 +312,63 @@ namespace System.Runtime.Remoting
                                        {
                                                // Unregistered local contextbound object. Register now.
                                                ClientActivatedIdentity cboundIdentity = (ClientActivatedIdentity)identity;
-                                               if (uri == null) uri = NewUri();
-                                               cboundIdentity.ObjectUri = uri;
+                                               if (ObjURI == null) ObjURI = NewUri();
+                                               cboundIdentity.ObjectUri = ObjURI;
                                                RegisterServerIdentity (cboundIdentity);
-                                               cboundIdentity.StartTrackingLifetime ((ILease)obj.InitializeLifetimeService());
-                                               return cboundIdentity.CreateObjRef(requested_type);
+                                               cboundIdentity.StartTrackingLifetime ((ILease)Obj.InitializeLifetimeService());
+                                               return cboundIdentity.CreateObjRef (RequestedType);
                                        }
-                                       else if (uri != null)
+                                       else if (ObjURI != null)
                                                throw new RemotingException ("It is not possible marshal a proxy of a remote object.");
 
-                                       ObjRef or = proxy.ObjectIdentity.CreateObjRef(requested_type);
-                                       TrackingServices.NotifyMarshaledObject (obj, or);
+                                       ObjRef or = proxy.ObjectIdentity.CreateObjRef (RequestedType);
+                                       TrackingServices.NotifyMarshaledObject (Obj, or);
                                        return or;
                                }
                        }
 
-                       if (requested_type == null) requested_type = obj.GetType();
+                       if (RequestedType == null) RequestedType = Obj.GetType ();
 
-                       if (uri == null) 
+                       if (ObjURI == null) 
                        {
-                               if (obj.ObjectIdentity == null)
+                               if (Obj.ObjectIdentity == null)
                                {
-                                       uri = NewUri();
-                                       CreateClientActivatedServerIdentity (obj, requested_type, uri);
+                                       ObjURI = NewUri();
+                                       CreateClientActivatedServerIdentity (Obj, RequestedType, ObjURI);
                                }
                        }
                        else
                        {
-                               ClientActivatedIdentity identity = GetIdentityForUri ("/" + uri) as ClientActivatedIdentity;
-                               if (identity == null || obj != identity.GetServerObject()) 
-                                       CreateClientActivatedServerIdentity (obj, requested_type, uri);
+                               ClientActivatedIdentity identity = GetIdentityForUri ("/" + ObjURI) as ClientActivatedIdentity;
+                               if (identity == null || Obj != identity.GetServerObject()) 
+                                       CreateClientActivatedServerIdentity (Obj, RequestedType, ObjURI);
                        }
 
                        ObjRef oref;
                        
-                       if (IsTransparentProxy (obj))
-                               oref = RemotingServices.GetRealProxy(obj).ObjectIdentity.CreateObjRef (requested_type);
+                       if (IsTransparentProxy (Obj))
+                               oref = RemotingServices.GetRealProxy (Obj).ObjectIdentity.CreateObjRef (RequestedType);
                        else
-                               oref = obj.CreateObjRef(requested_type);
+                               oref = Obj.CreateObjRef (RequestedType);
                        
-                       TrackingServices.NotifyMarshaledObject (obj, oref);
+                       TrackingServices.NotifyMarshaledObject (Obj, oref);
                        return oref;
                }
 
                static string NewUri ()
                {
+                       if (app_id == null) {
+                               lock (app_id_lock) {
+                                       if (app_id == null)
+                                               app_id = Guid.NewGuid().ToString().Replace('-', '_') + "/";
+                               }
+                       }
+
                        int n = Interlocked.Increment (ref next_id);
-                       return app_id + Environment.TickCount + "_" + n + ".rem";
+                       return app_id + Environment.TickCount.ToString("x") + "_" + n + ".rem";
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public static RealProxy GetRealProxy (object proxy)
                {
                        if (!IsTransparentProxy(proxy)) throw new RemotingException("Cannot get the real proxy from an object that is not a transparent proxy.");
@@ -338,10 +380,10 @@ namespace System.Runtime.Remoting
                        Type type = Type.GetType (msg.TypeName);
                        if (type == null)
                                throw new RemotingException ("Type '" + msg.TypeName + "' not found.");
-                               
+
                        return GetMethodBaseFromName (type, msg.MethodName, (Type[]) msg.MethodSignature);
                }
-               
+
                internal static MethodBase GetMethodBaseFromName (Type type, string methodName, Type[] signature)
                {
                        if (type.IsInterface) {
@@ -354,8 +396,14 @@ namespace System.Runtime.Remoting
                                else
                                        method = type.GetMethod (methodName, methodBindings, null, (Type[]) signature, null);
                                
-                               if (method != null) 
+                               if (method != null)
                                        return method;
+                                       
+                               if (methodName == "FieldSetter")
+                                       return FieldSetterMethod;
+
+                               if (methodName == "FieldGetter")
+                                       return FieldGetterMethod;
                                
                                if (signature == null)
                                        return type.GetConstructor (methodBindings, null, Type.EmptyTypes, null);
@@ -414,6 +462,7 @@ namespace System.Runtime.Remoting
 
                [MonoTODO]
                [Conditional ("REMOTING_PERF")]
+               [Obsolete ("It existed for only internal use in .NET and unimplemented in mono")]
                public static void LogRemotingStage (int stage)
                {
                        throw new NotImplementedException ();
@@ -503,7 +552,10 @@ namespace System.Runtime.Remoting
        
                internal static object CreateClientProxy (Type objectType, string url, object[] activationAttributes)
                {
-                       string activationUrl = url + "/RemoteActivationService.rem";
+                       string activationUrl = url;
+                       if (!activationUrl.EndsWith ("/"))
+                               activationUrl += "/";
+                       activationUrl += "RemoteActivationService.rem";
 
                        string objectUri;
                        GetClientChannelSinkChain (activationUrl, null, out objectUri);
@@ -529,13 +581,13 @@ namespace System.Runtime.Remoting
                        RemotingProxy proxy = new RemotingProxy (type, ChannelServices.CrossContextUrl, activationAttributes);
                        return proxy.GetTransparentProxy();
                }
-
+#if !NET_2_1
                internal static object CreateClientProxyForComInterop (Type type)
                {
-                       Mono.Interop.ComInteropProxy proxy = new Mono.Interop.ComInteropProxy (type);
+                       Mono.Interop.ComInteropProxy proxy = Mono.Interop.ComInteropProxy.CreateProxy (type);
                        return proxy.GetTransparentProxy ();
                }
-       
+#endif
                internal static Identity GetIdentityForUri (string uri)
                {
                        string normUri = GetNormalizedUri (uri);
@@ -619,7 +671,6 @@ namespace System.Runtime.Remoting
 
                                // Registers the identity
                                uri_hash [uri] = identity;
-                               
                                if (proxyType != null)
                                {
                                        RemotingProxy proxy = new RemotingProxy (proxyType, identity);
@@ -630,7 +681,6 @@ namespace System.Runtime.Remoting
                                        clientProxy = proxy.GetTransparentProxy();
                                        identity.ClientProxy = (MarshalByRefObject) clientProxy;
                                }
-
                                return identity;
                        }
                }
@@ -807,7 +857,9 @@ namespace System.Runtime.Remoting
                                AppDomain.InternalSetContext (currentContext);
                        }                               
 
-                       MemoryStream stream = new MemoryStream (data);
+                       byte[] data_copy = new byte [data.Length];
+                       data.CopyTo (data_copy, 0);
+                       MemoryStream stream = new MemoryStream (data_copy);
                        ObjRef appref = (ObjRef) CADSerializer.DeserializeObject (stream);
                        return (AppDomain) RemotingServices.Unmarshal(appref);
                }
@@ -855,14 +907,7 @@ namespace System.Runtime.Remoting
                
                internal static bool UpdateOutArgObject (ParameterInfo pi, object local, object remote)
                {
-                       if (local is StringBuilder) 
-                       {
-                               StringBuilder sb = local as StringBuilder;
-                               sb.Remove (0, sb.Length);
-                               sb.Append (remote.ToString());
-                               return true;
-                       }
-                       else if (pi.ParameterType.IsArray && ((Array)local).Rank == 1)
+                       if (pi.ParameterType.IsArray && ((Array)local).Rank == 1)
                        {
                                Array alocal = (Array) local;
                                if (alocal.Rank == 1)