2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Runtime.Remoting / RemotingServices.cs
index 012cb7ad2f2b1a0e47c0e63cafb378022730f4ed..9b35a91a8c7a210f4a7af42df7840236b77883a9 100644 (file)
@@ -46,6 +46,7 @@ using System.Runtime.Remoting.Activation;
 using System.Runtime.Remoting.Lifetime;
 using System.Runtime.CompilerServices;
 using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 namespace System.Runtime.Remoting
@@ -55,12 +56,27 @@ namespace System.Runtime.Remoting
                // Holds the identities of the objects, using uri as index
                static Hashtable uri_hash = new Hashtable ();           
 
+               static BinaryFormatter _serializationFormatter;
+               static BinaryFormatter _deserializationFormatter;
+               
                internal static string app_id;
                static int next_id = 1;
                static readonly BindingFlags methodBindings = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
                
+               // Holds information in xdomain calls. Names are short to minimize serialized size.
+               [Serializable]
+               class CACD {
+                       public object d;        /* call data */
+                       public object c;        /* call context */
+               }
+               
                static RemotingServices ()
                {
+                       RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
+                       StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
+                       _serializationFormatter = new BinaryFormatter (surrogateSelector, context);
+                       _deserializationFormatter = new BinaryFormatter (null, context);
+                       
                        RegisterInternalChannels ();
                        app_id = Guid.NewGuid().ToString().Replace('-', '_') + "/";
                        CreateWellKnownServerIdentity (typeof(RemoteActivator), "RemoteActivationService.rem", WellKnownObjectMode.Singleton);
@@ -80,7 +96,8 @@ namespace System.Runtime.Remoting
                {
                        ReturnMessage result;
                        
-                       MethodBase method = reqMsg.MethodBase;
+                       Type tt = target.GetType ();
+                       MethodBase method = reqMsg.MethodBase.DeclaringType == tt ? reqMsg.MethodBase : tt.GetMethod(reqMsg.MethodName, BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance, null, (Type[]) reqMsg.MethodSignature, null);
                        object oldContext = CallContext.SetCurrentCallContext (reqMsg.LogicalCallContext);
                        
                        try 
@@ -107,7 +124,7 @@ namespace System.Runtime.Remoting
                                                returnArgs [n++] = null; 
                                }
                                
-                               result = new ReturnMessage (rval, returnArgs, n, CallContext.CreateLogicalCallContext(), reqMsg);
+                               result = new ReturnMessage (rval, returnArgs, n, CallContext.CreateLogicalCallContext (true), reqMsg);
                        } 
                        catch (Exception e) 
                        {
@@ -499,10 +516,9 @@ namespace System.Runtime.Remoting
                        // It will also get the object uri from the url.
 
                        object channelData = objRef.ChannelInfo != null ? objRef.ChannelInfo.ChannelData : null;
-                       string url = (channelData == null) ? objRef.URI : null;
 
                        string objectUri;
-                       IMessageSink sink = GetClientChannelSinkChain (url, channelData, out objectUri);
+                       IMessageSink sink = GetClientChannelSinkChain (objRef.URI, channelData, out objectUri);
 
                        if (objectUri == null) objectUri = objRef.URI;
 
@@ -534,6 +550,10 @@ namespace System.Runtime.Remoting
                                if (proxyType != null)
                                {
                                        RemotingProxy proxy = new RemotingProxy (proxyType, identity);
+                                       CrossAppDomainSink cds = sink as CrossAppDomainSink;
+                                       if (cds != null)
+                                               proxy.SetTargetDomain (cds.TargetDomainId);
+
                                        clientProxy = proxy.GetTransparentProxy();
                                        identity.ClientProxy = (MarshalByRefObject) clientProxy;
                                }
@@ -614,7 +634,88 @@ namespace System.Runtime.Remoting
                        GetOrCreateClientIdentity (objRef, proxyType, out proxy);
                        return proxy;
                }
+               
+               // This method is called by the runtime
+               internal static object GetServerObject (string uri)
+               {
+                       ClientActivatedIdentity identity = GetIdentityForUri (uri) as ClientActivatedIdentity;
+                       if (identity == null) throw new RemotingException ("Server for uri '" + uri + "' not found");
+                       return identity.GetServerObject ();
+               }
 
+               // This method is called by the runtime
+               internal static byte[] SerializeCallData (object obj)
+               {
+                       LogicalCallContext ctx = CallContext.CreateLogicalCallContext (false);
+                       if (ctx != null) {
+                               CACD cad = new CACD ();
+                               cad.d = obj;
+                               cad.c = ctx;
+                               obj = cad;
+                       }
+                       
+                       if (obj == null) return null;
+                       MemoryStream ms = new MemoryStream ();
+                       _serializationFormatter.Serialize (ms, obj);
+                       return ms.ToArray ();
+               }
+
+               // This method is called by the runtime
+               internal static object DeserializeCallData (byte[] array)
+               {
+                       if (array == null) return null;
+                       
+                       MemoryStream ms = new MemoryStream (array);
+                       object obj = _deserializationFormatter.Deserialize (ms);
+                       
+                       if (obj is CACD) {
+                               CACD cad = (CACD) obj;
+                               obj = cad.d;
+                               CallContext.UpdateCurrentCallContext ((LogicalCallContext) cad.c);
+                       }
+                       return obj;
+               }
+               
+               // This method is called by the runtime
+               internal static byte[] SerializeExceptionData (Exception ex)
+               {
+                       try {
+                               int retry = 4;
+                               
+                               do {
+                                       try {
+                                               MemoryStream ms = new MemoryStream ();
+                                               _serializationFormatter.Serialize (ms, ex);
+                                               return ms.ToArray ();
+                                       }
+                                       catch (Exception e) {
+                                               if (e is ThreadAbortException) {
+                                                       Thread.ResetAbort ();
+                                                       retry = 5;
+                                                       ex = e;
+                                               }
+                                               else if (retry == 2) {
+                                                       ex = new Exception ();
+                                                       ex.SetMessage (e.Message);
+                                                       ex.SetStackTrace (e.StackTrace);
+                                               }
+                                               else
+                                                       ex = e;
+                                       }
+                                       retry--;
+                               }
+                               while (retry > 0);
+                               
+                               return null;
+                       }
+                       catch (Exception tex)
+                       {
+                               byte[] data = SerializeExceptionData (tex);
+                               Thread.ResetAbort ();
+                               return data;
+                       }
+               }
+               
                internal static object GetDomainProxy(AppDomain domain) 
                {
                        byte[] data = null;