static RemotingServices ()
{
RegisterInternalChannels ();
- app_id = "/" + Guid.NewGuid().ToString().Replace('-', '_') + "/";
+ app_id = Guid.NewGuid().ToString().Replace('-', '_') + "/";
CreateWellKnownServerIdentity (typeof(RemoteActivator), "RemoteActivationService.rem", WellKnownObjectMode.Singleton);
}
if (proxy.GetProxiedType().IsContextful && (proxy.ObjectIdentity is ServerIdentity))
identity = proxy.ObjectIdentity as ServerIdentity;
else
- throw new ArgumentException ("The obj parameter is a proxy");
+ throw new ArgumentException ("The obj parameter is a proxy.");
}
else
identity = obj.ObjectIdentity;
public static Type GetServerTypeForUri (string uri)
{
- Identity ident = GetIdentityForUri (uri);
+ ServerIdentity ident = GetIdentityForUri (uri) as ServerIdentity;
if (ident == null) return null;
return ident.ObjectType;
}
public static object Unmarshal (ObjRef objref, bool fRefine)
{
- // FIXME: use type name when fRefine==true
-
Type classToProxy = fRefine ? objref.ServerType : typeof (MarshalByRefObject);
if (classToProxy == null) classToProxy = typeof (MarshalByRefObject);
if (identity != null)
{
- if (identity.ObjectType.IsContextful && !identity.IsConnected)
+ if (proxy.GetProxiedType().IsContextful && !identity.IsConnected)
{
// Unregistered local contextbound object. Register now.
ClientActivatedIdentity cboundIdentity = (ClientActivatedIdentity)identity;
return cboundIdentity.CreateObjRef(requested_type);
}
else if (uri != null)
- throw new RemotingException ("It is not possible marshal a proxy of a remote object");
+ throw new RemotingException ("It is not possible marshal a proxy of a remote object.");
return proxy.ObjectIdentity.CreateObjRef(requested_type);
}
if (uri == null)
{
- uri = NewUri();
- CreateClientActivatedServerIdentity (obj, requested_type, uri);
+ if (obj.ObjectIdentity == null)
+ {
+ uri = NewUri();
+ CreateClientActivatedServerIdentity (obj, requested_type, uri);
+ }
}
else
{
- ClientActivatedIdentity identity = GetIdentityForUri (uri) as ClientActivatedIdentity;
+ ClientActivatedIdentity identity = GetIdentityForUri ("/" + uri) as ClientActivatedIdentity;
if (identity == null || obj != identity.GetServerObject())
CreateClientActivatedServerIdentity (obj, requested_type, uri);
}
- return obj.CreateObjRef(requested_type);
+ if (IsTransparentProxy (obj))
+ return RemotingServices.GetRealProxy(obj).ObjectIdentity.CreateObjRef (requested_type);
+ else
+ return obj.CreateObjRef(requested_type);
}
static string NewUri ()
{
- return app_id + Environment.TickCount + "_" + next_id++;
+ int n = Interlocked.Increment (ref next_id);
+ return app_id + Environment.TickCount + "_" + n + ".rem";
}
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");
+ if (!IsTransparentProxy(proxy)) throw new RemotingException("Cannot get the real proxy from an object that is not a transparent proxy.");
return (RealProxy)((TransparentProxy)proxy)._rp;
}
{
Type type = Type.GetType (msg.TypeName);
if (type == null)
- throw new RemotingException ("Type '" + msg.TypeName + "' not found!");
+ throw new RemotingException ("Type '" + msg.TypeName + "' not found.");
BindingFlags bflags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
if (msg.MethodSignature == null)
if (IsTransparentProxy(obj))
return ((ClientIdentity)GetRealProxy (obj).ObjectIdentity).EnvoySink;
else
- throw new ArgumentException ("obj must be a proxy","obj");
+ throw new ArgumentException ("obj must be a proxy.","obj");
}
public static void LogRemotingStage (int stage)
throw new NotImplementedException ();
}
- [MonoTODO]
public static string GetSessionIdForMethodMessage(IMethodMessage msg)
{
- throw new NotImplementedException ();
+ // It seems that this it what MS returns.
+ return msg.Uri;
}
public static bool IsMethodOverloaded(IMethodMessage msg)
{
+ // TODO: use internal call for better performance
Type type = msg.MethodBase.DeclaringType;
MemberInfo[] members = type.GetMember (msg.MethodName, MemberTypes.Method, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
return members.Length > 1;
public static bool IsObjectOutOfAppDomain(object tp)
{
+ // TODO: use internal call for better performance
Identity ident = GetObjectIdentity((MarshalByRefObject)tp);
if (ident != null) return !ident.IsFromThisAppDomain;
else return false;
public static bool IsObjectOutOfContext(object tp)
{
+ // TODO: use internal call for better performance
ServerIdentity ident = GetObjectIdentity((MarshalByRefObject)tp) as ServerIdentity;
if (ident != null) return ident.Context != System.Threading.Thread.CurrentContext;
else return false;
return atts.Length > 0;
}
- public static bool IsAsyncMessage(IMessage msg)
+ internal static bool IsAsyncMessage(IMessage msg)
{
if (! (msg is MonoMethodMessage)) return false;
else if (((MonoMethodMessage)msg).IsAsync) return true;
{
lock (uri_hash)
{
- return (Identity)uri_hash [uri];
+ return (Identity)uri_hash [GetNormalizedUri(uri)];
}
}
return obj.ObjectIdentity;
}
- internal static ClientIdentity GetOrCreateClientIdentity(ObjRef objRef, Type proxyType)
+ internal static ClientIdentity GetOrCreateClientIdentity(ObjRef objRef, Type proxyType, out object clientProxy)
{
// This method looks for an identity for the given url.
// If an identity is not found, it creates the identity and
lock (uri_hash)
{
- ClientIdentity identity = uri_hash [objRef.URI] as ClientIdentity;
- if (identity != null)
- return identity; // Object already registered
+ clientProxy = null;
+ string uri = GetNormalizedUri (objRef.URI);
+
+ ClientIdentity identity = uri_hash [uri] as ClientIdentity;
+ if (identity != null)
+ {
+ // Object already registered
+ clientProxy = identity.ClientProxy;
+ if (clientProxy != null) return identity;
+
+ // The proxy has just been GCed, so its identity cannot
+ // be reused. Just dispose it.
+ DisposeIdentity (identity);
+ }
// Creates an identity and a proxy for the remote object
identity = new ClientIdentity (objectUri, objRef);
identity.ChannelSink = sink;
+ // Registers the identity
+ uri_hash [uri] = identity;
+
if (proxyType != null)
{
RemotingProxy proxy = new RemotingProxy (proxyType, identity);
- identity.ClientProxy = (MarshalByRefObject) proxy.GetTransparentProxy();
+ clientProxy = proxy.GetTransparentProxy();
+ identity.ClientProxy = (MarshalByRefObject) clientProxy;
}
- // Registers the identity
- uri_hash [objRef.URI] = identity;
return identity;
}
}
lock (uri_hash)
{
if (uri_hash.ContainsKey (identity.ObjectUri))
- throw new RemotingException ("Uri already in use: " + identity.ObjectUri);
+ throw new RemotingException ("Uri already in use: " + identity.ObjectUri + ".");
uri_hash[identity.ObjectUri] = identity;
}
internal static object GetRemoteObject(ObjRef objRef, Type proxyType)
{
- ClientIdentity id = GetOrCreateClientIdentity (objRef, proxyType);
- return id.ClientProxy;
+ object proxy;
+ GetOrCreateClientIdentity (objRef, proxyType, out proxy);
+ return proxy;
}
internal static object GetDomainProxy(AppDomain domain)
byte[] data = null;
Context currentContext = Thread.CurrentContext;
- AppDomain currentDomain = AppDomain.InternalSetDomain (domain);
- try
+
+ try
{
- data = domain.GetMarshalledDomainObjRef ();
+ data = (byte[])AppDomain.InvokeInDomain (domain, typeof (AppDomain).GetMethod ("GetMarshalledDomainObjRef", BindingFlags.Instance|BindingFlags.NonPublic), domain, null);
}
- finally
+ finally
{
- AppDomain.InternalSetDomain (currentDomain);
AppDomain.InternalSetContext (currentContext);
- }
+ }
MemoryStream stream = new MemoryStream (data);
ObjRef appref = (ObjRef) CADSerializer.DeserializeObject (stream);
CrossAppDomainChannel.RegisterCrossAppDomainChannel();
}
- internal static void DisposeIdentity (ServerIdentity ident)
+ internal static void DisposeIdentity (Identity ident)
{
lock (uri_hash)
{
- uri_hash.Remove (ident.ObjectUri);
+ if (!ident.Disposed) {
+ ClientIdentity clientId = ident as ClientIdentity;
+ if (clientId != null)
+ uri_hash.Remove (GetNormalizedUri (clientId.TargetUri));
+ else
+ uri_hash.Remove (ident.ObjectUri);
+
+ ident.Disposed = true;
+ }
}
}
lock (uri_hash)
{
- return uri_hash [((IMethodMessage)msg).Uri] as ServerIdentity;
+ string uri = GetNormalizedUri (((IMethodMessage)msg).Uri);
+ return uri_hash [uri] as ServerIdentity;
}
}
}
return false;
}
+
+ static string GetNormalizedUri (string uri)
+ {
+ if (uri.StartsWith ("/")) return uri.Substring (1);
+ else return uri;
+ }
#endregion
}