2 // System.Runtime.Remoting.RemotingServices.cs
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Lluis Sanchez Gual (lluis@ideary.com)
9 // (C) 2001 Ximian, Inc. http://www.ximian.com
14 using System.Reflection;
15 using System.Threading;
16 using System.Collections;
17 using System.Runtime.Remoting.Messaging;
18 using System.Runtime.Remoting.Proxies;
19 using System.Runtime.Remoting.Channels;
20 using System.Runtime.Remoting.Contexts;
21 using System.Runtime.Remoting.Activation;
22 using System.Runtime.Remoting.Lifetime;
23 using System.Runtime.CompilerServices;
24 using System.Runtime.Serialization;
27 namespace System.Runtime.Remoting
29 public sealed class RemotingServices
31 // Holds the identities of the objects, using uri as index
32 static Hashtable uri_hash = new Hashtable ();
34 internal static string app_id;
35 static int next_id = 1;
37 static RemotingServices ()
39 RegisterInternalChannels ();
40 app_id = Guid.NewGuid().ToString().Replace('-', '_') + "/";
41 CreateWellKnownServerIdentity (typeof(RemoteActivator), "RemoteActivationService.rem", WellKnownObjectMode.Singleton);
44 private RemotingServices () {}
46 [MethodImplAttribute(MethodImplOptions.InternalCall)]
47 internal extern static object InternalExecute (MonoMethod method, Object obj,
48 Object[] parameters, out object [] out_args);
50 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51 public extern static bool IsTransparentProxy (object proxy);
53 internal static IMethodReturnMessage InternalExecuteMessage (
54 MarshalByRefObject target, IMethodCallMessage reqMsg)
58 MonoMethod method = (MonoMethod) target.GetType().GetMethod(reqMsg.MethodName, BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance, null, (Type[]) reqMsg.MethodSignature, null);
59 object oldContext = CallContext.SetCurrentCallContext (reqMsg.LogicalCallContext);
64 object rval = InternalExecute (method, target, reqMsg.Args, out out_args);
66 // Collect parameters with Out flag from the request message
67 // FIXME: This can be done in the unmanaged side and will be
70 ParameterInfo[] parameters = method.GetParameters();
71 object[] returnArgs = new object [parameters.Length];
75 foreach (ParameterInfo par in parameters)
77 if (par.IsOut && !par.ParameterType.IsByRef)
78 returnArgs [n++] = reqMsg.GetArg (par.Position);
79 else if (par.ParameterType.IsByRef)
80 returnArgs [n++] = out_args [noa++];
82 returnArgs [n++] = null;
85 result = new ReturnMessage (rval, returnArgs, n, CallContext.CreateLogicalCallContext(), reqMsg);
89 result = new ReturnMessage (e, reqMsg);
92 CallContext.RestoreCallContext (oldContext);
96 public static IMethodReturnMessage ExecuteMessage (
97 MarshalByRefObject target, IMethodCallMessage reqMsg)
99 if (IsTransparentProxy(target))
101 // Message must go through all chain of sinks
102 RealProxy rp = GetRealProxy (target);
103 return (IMethodReturnMessage) rp.Invoke (reqMsg);
106 return InternalExecuteMessage (target, reqMsg);
109 public static object Connect (Type classToProxy, string url)
111 ObjRef objRef = new ObjRef (classToProxy, url, null);
112 return GetRemoteObject (objRef, classToProxy);
115 public static object Connect (Type classToProxy, string url, object data)
117 ObjRef objRef = new ObjRef (classToProxy, url, data);
118 return GetRemoteObject (objRef, classToProxy);
121 public static bool Disconnect (MarshalByRefObject obj)
123 if (obj == null) throw new ArgumentNullException ("obj");
125 ServerIdentity identity;
127 if (IsTransparentProxy (obj))
129 // CBOs are always accessed through a proxy, even in the server, so
130 // for server CBOs it is ok to disconnect a proxy
132 RealProxy proxy = GetRealProxy(obj);
133 if (proxy.GetProxiedType().IsContextful && (proxy.ObjectIdentity is ServerIdentity))
134 identity = proxy.ObjectIdentity as ServerIdentity;
136 throw new ArgumentException ("The obj parameter is a proxy.");
139 identity = obj.ObjectIdentity;
141 if (identity == null || !identity.IsConnected)
145 LifetimeServices.StopTrackingLifetime (identity);
146 DisposeIdentity (identity);
151 public static Type GetServerTypeForUri (string uri)
153 ServerIdentity ident = GetIdentityForUri (uri) as ServerIdentity;
154 if (ident == null) return null;
155 return ident.ObjectType;
158 public static string GetObjectUri (MarshalByRefObject obj)
160 Identity ident = GetObjectIdentity(obj);
161 if (ident is ClientIdentity) return ((ClientIdentity)ident).TargetUri;
162 else if (ident != null) return ident.ObjectUri;
166 public static object Unmarshal (ObjRef objref)
168 return Unmarshal(objref, true);
171 public static object Unmarshal (ObjRef objref, bool fRefine)
173 Type classToProxy = fRefine ? objref.ServerType : typeof (MarshalByRefObject);
174 if (classToProxy == null) classToProxy = typeof (MarshalByRefObject);
176 if (objref.IsReferenceToWellKnow)
177 return GetRemoteObject(objref, classToProxy);
180 if (classToProxy.IsContextful)
182 // Look for a ProxyAttribute
183 ProxyAttribute att = (ProxyAttribute) Attribute.GetCustomAttribute (classToProxy, typeof(ProxyAttribute),true);
185 return att.CreateProxy (objref, classToProxy, null, null).GetTransparentProxy();
187 return GetProxyForRemoteObject (objref, classToProxy);
191 public static ObjRef Marshal (MarshalByRefObject obj)
193 return Marshal (obj, null, null);
196 public static ObjRef Marshal (MarshalByRefObject obj, string uri)
198 return Marshal (obj, uri, null);
201 public static ObjRef Marshal (MarshalByRefObject obj, string uri, Type requested_type)
203 if (IsTransparentProxy (obj))
205 RealProxy proxy = RemotingServices.GetRealProxy(obj);
206 Identity identity = proxy.ObjectIdentity;
208 if (identity != null)
210 if (proxy.GetProxiedType().IsContextful && !identity.IsConnected)
212 // Unregistered local contextbound object. Register now.
213 ClientActivatedIdentity cboundIdentity = (ClientActivatedIdentity)identity;
214 if (uri == null) uri = NewUri();
215 cboundIdentity.ObjectUri = uri;
216 RegisterServerIdentity (cboundIdentity);
217 cboundIdentity.StartTrackingLifetime ((ILease)obj.InitializeLifetimeService());
218 return cboundIdentity.CreateObjRef(requested_type);
220 else if (uri != null)
221 throw new RemotingException ("It is not possible marshal a proxy of a remote object.");
223 return proxy.ObjectIdentity.CreateObjRef(requested_type);
227 if (requested_type == null) requested_type = obj.GetType();
231 if (obj.ObjectIdentity == null)
234 CreateClientActivatedServerIdentity (obj, requested_type, uri);
239 ClientActivatedIdentity identity = GetIdentityForUri ("/" + uri) as ClientActivatedIdentity;
240 if (identity == null || obj != identity.GetServerObject())
241 CreateClientActivatedServerIdentity (obj, requested_type, uri);
244 if (IsTransparentProxy (obj))
245 return RemotingServices.GetRealProxy(obj).ObjectIdentity.CreateObjRef (requested_type);
247 return obj.CreateObjRef(requested_type);
250 static string NewUri ()
252 int n = Interlocked.Increment (ref next_id);
253 return app_id + Environment.TickCount + "_" + n + ".rem";
256 public static RealProxy GetRealProxy (object proxy)
258 if (!IsTransparentProxy(proxy)) throw new RemotingException("Cannot get the real proxy from an object that is not a transparent proxy.");
259 return (RealProxy)((TransparentProxy)proxy)._rp;
262 public static MethodBase GetMethodBaseFromMethodMessage(IMethodMessage msg)
264 Type type = Type.GetType (msg.TypeName);
266 throw new RemotingException ("Type '" + msg.TypeName + "' not found.");
268 BindingFlags bflags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
271 if (msg.MethodSignature == null)
272 method = type.GetMethod (msg.MethodName, bflags);
274 method = type.GetMethod (msg.MethodName, bflags, null, (Type[]) msg.MethodSignature, null);
279 if (msg.MethodSignature == null)
280 return type.GetConstructor (bflags, null, Type.EmptyTypes, null);
282 return type.GetConstructor (bflags, null, (Type[]) msg.MethodSignature, null);
285 public static void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
287 if (obj == null) throw new ArgumentNullException ("obj");
289 ObjRef oref = Marshal ((MarshalByRefObject)obj);
290 oref.GetObjectData (info, context);
293 public static ObjRef GetObjRefForProxy(MarshalByRefObject obj)
295 Identity ident = GetObjectIdentity(obj);
296 if (ident == null) return null;
297 else return ident.CreateObjRef(null);
300 public static object GetLifetimeService (MarshalByRefObject obj)
302 if (obj == null) return null;
303 return obj.GetLifetimeService ();
306 public static IMessageSink GetEnvoyChainForProxy (MarshalByRefObject obj)
308 if (IsTransparentProxy(obj))
309 return ((ClientIdentity)GetRealProxy (obj).ObjectIdentity).EnvoySink;
311 throw new ArgumentException ("obj must be a proxy.","obj");
314 public static void LogRemotingStage (int stage)
316 throw new NotImplementedException ();
319 public static string GetSessionIdForMethodMessage(IMethodMessage msg)
321 // It seems that this it what MS returns.
325 public static bool IsMethodOverloaded(IMethodMessage msg)
327 // TODO: use internal call for better performance
328 Type type = msg.MethodBase.DeclaringType;
329 MemberInfo[] members = type.GetMember (msg.MethodName, MemberTypes.Method, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
330 return members.Length > 1;
333 public static bool IsObjectOutOfAppDomain(object tp)
335 // TODO: use internal call for better performance
336 Identity ident = GetObjectIdentity((MarshalByRefObject)tp);
337 if (ident != null) return !ident.IsFromThisAppDomain;
341 public static bool IsObjectOutOfContext(object tp)
343 // TODO: use internal call for better performance
344 ServerIdentity ident = GetObjectIdentity((MarshalByRefObject)tp) as ServerIdentity;
345 if (ident != null) return ident.Context != System.Threading.Thread.CurrentContext;
349 public static bool IsOneWay(MethodBase method)
351 // TODO: use internal call for better performance
352 object[] atts = method.GetCustomAttributes (typeof (OneWayAttribute), false);
353 return atts.Length > 0;
356 internal static bool IsAsyncMessage(IMessage msg)
358 if (! (msg is MonoMethodMessage)) return false;
359 else if (((MonoMethodMessage)msg).IsAsync) return true;
360 else if (IsOneWay (((MonoMethodMessage)msg).MethodBase)) return true;
364 public static void SetObjectUriForMarshal(MarshalByRefObject obj, string uri)
366 if (IsTransparentProxy (obj)) throw new RemotingException ("SetObjectUriForMarshal method should only be called for MarshalByRefObjects that exist in the current AppDomain.");
370 #region Internal Methods
372 internal static object CreateClientProxy (ActivatedClientTypeEntry entry, object[] activationAttributes)
374 if (entry.ContextAttributes != null || activationAttributes != null)
376 ArrayList props = new ArrayList ();
377 if (entry.ContextAttributes != null) props.AddRange (entry.ContextAttributes);
378 if (activationAttributes != null) props.AddRange (activationAttributes);
379 return CreateClientProxy (entry.ObjectType, entry.ApplicationUrl, props.ToArray ());
382 return CreateClientProxy (entry.ObjectType, entry.ApplicationUrl, null);
385 internal static object CreateClientProxy (Type objectType, string url, object[] activationAttributes)
387 string activationUrl = url + "/RemoteActivationService.rem";
390 IMessageSink sink = GetClientChannelSinkChain (activationUrl, null, out objectUri);
392 RemotingProxy proxy = new RemotingProxy (objectType, activationUrl, activationAttributes);
393 return proxy.GetTransparentProxy();
396 internal static object CreateClientProxy (WellKnownClientTypeEntry entry)
398 return Connect (entry.ObjectType, entry.ObjectUrl, null);
401 internal static object CreateClientProxyForContextBound (Type type, object[] activationAttributes)
403 if (type.IsContextful)
405 // Look for a ProxyAttribute
406 ProxyAttribute att = (ProxyAttribute) Attribute.GetCustomAttribute (type, typeof(ProxyAttribute), true);
408 return att.CreateInstance (type);
410 RemotingProxy proxy = new RemotingProxy (type, ChannelServices.CrossContextUrl, activationAttributes);
411 return proxy.GetTransparentProxy();
414 internal static Identity GetIdentityForUri (string uri)
418 return (Identity)uri_hash [GetNormalizedUri(uri)];
422 internal static Identity GetObjectIdentity (MarshalByRefObject obj)
424 if (IsTransparentProxy(obj))
425 return GetRealProxy (obj).ObjectIdentity;
427 return obj.ObjectIdentity;
430 internal static ClientIdentity GetOrCreateClientIdentity(ObjRef objRef, Type proxyType, out object clientProxy)
432 // This method looks for an identity for the given url.
433 // If an identity is not found, it creates the identity and
434 // assigns it a proxy to the remote object.
436 // Creates the client sink chain for the given url or channelData.
437 // It will also get the object uri from the url.
439 object channelData = objRef.ChannelInfo != null ? objRef.ChannelInfo.ChannelData : null;
440 string url = (channelData == null) ? objRef.URI : null;
443 IMessageSink sink = GetClientChannelSinkChain (url, channelData, out objectUri);
445 if (objectUri == null) objectUri = objRef.URI;
450 string uri = GetNormalizedUri (objRef.URI);
452 ClientIdentity identity = uri_hash [uri] as ClientIdentity;
453 if (identity != null)
455 // Object already registered
456 clientProxy = identity.ClientProxy;
457 if (clientProxy != null) return identity;
459 // The proxy has just been GCed, so its identity cannot
460 // be reused. Just dispose it.
461 DisposeIdentity (identity);
464 // Creates an identity and a proxy for the remote object
466 identity = new ClientIdentity (objectUri, objRef);
467 identity.ChannelSink = sink;
469 // Registers the identity
470 uri_hash [uri] = identity;
472 if (proxyType != null)
474 RemotingProxy proxy = new RemotingProxy (proxyType, identity);
475 clientProxy = proxy.GetTransparentProxy();
476 identity.ClientProxy = (MarshalByRefObject) clientProxy;
483 static IMessageSink GetClientChannelSinkChain(string url, object channelData, out string objectUri)
485 IMessageSink sink = ChannelServices.CreateClientChannelSinkChain (url, channelData, out objectUri);
490 string msg = String.Format ("Cannot create channel sink to connect to URL {0}. An appropriate channel has probably not been registered.", url);
491 throw new RemotingException (msg);
495 string msg = String.Format ("Cannot create channel sink to connect to the remote object. An appropriate channel has probably not been registered.", url);
496 throw new RemotingException (msg);
502 internal static ClientActivatedIdentity CreateContextBoundObjectIdentity(Type objectType)
504 ClientActivatedIdentity identity = new ClientActivatedIdentity (null, objectType);
505 identity.ChannelSink = ChannelServices.CrossContextChannel;
509 internal static ClientActivatedIdentity CreateClientActivatedServerIdentity(MarshalByRefObject realObject, Type objectType, string objectUri)
511 ClientActivatedIdentity identity = new ClientActivatedIdentity (objectUri, objectType);
512 identity.AttachServerObject (realObject, Context.DefaultContext);
513 RegisterServerIdentity (identity);
514 identity.StartTrackingLifetime ((ILease)realObject.InitializeLifetimeService ());
518 internal static ServerIdentity CreateWellKnownServerIdentity(Type objectType, string objectUri, WellKnownObjectMode mode)
520 ServerIdentity identity;
522 if (mode == WellKnownObjectMode.SingleCall)
523 identity = new SingleCallIdentity(objectUri, Context.DefaultContext, objectType);
525 identity = new SingletonIdentity(objectUri, Context.DefaultContext, objectType);
527 RegisterServerIdentity (identity);
531 private static void RegisterServerIdentity(ServerIdentity identity)
535 if (uri_hash.ContainsKey (identity.ObjectUri))
536 throw new RemotingException ("Uri already in use: " + identity.ObjectUri + ".");
538 uri_hash[identity.ObjectUri] = identity;
542 internal static object GetProxyForRemoteObject (ObjRef objref, Type classToProxy)
544 ClientActivatedIdentity identity = GetIdentityForUri (objref.URI) as ClientActivatedIdentity;
545 if (identity != null) return identity.GetServerObject ();
546 else return GetRemoteObject (objref, classToProxy);
549 internal static object GetRemoteObject(ObjRef objRef, Type proxyType)
552 GetOrCreateClientIdentity (objRef, proxyType, out proxy);
556 internal static object GetDomainProxy(AppDomain domain)
560 Context currentContext = Thread.CurrentContext;
564 data = (byte[])AppDomain.InvokeInDomain (domain, typeof (AppDomain).GetMethod ("GetMarshalledDomainObjRef", BindingFlags.Instance|BindingFlags.NonPublic), domain, null);
568 AppDomain.InternalSetContext (currentContext);
571 MemoryStream stream = new MemoryStream (data);
572 ObjRef appref = (ObjRef) CADSerializer.DeserializeObject (stream);
573 return (AppDomain) RemotingServices.Unmarshal(appref);
576 private static void RegisterInternalChannels()
578 CrossAppDomainChannel.RegisterCrossAppDomainChannel();
581 internal static void DisposeIdentity (Identity ident)
585 if (!ident.Disposed) {
586 ClientIdentity clientId = ident as ClientIdentity;
587 if (clientId != null)
588 uri_hash.Remove (GetNormalizedUri (clientId.TargetUri));
590 uri_hash.Remove (ident.ObjectUri);
592 ident.Disposed = true;
597 internal static Identity GetMessageTargetIdentity (IMessage msg)
599 // Returns the identity where the message is sent
601 if (msg is IInternalMessage)
602 return ((IInternalMessage)msg).TargetIdentity;
606 string uri = GetNormalizedUri (((IMethodMessage)msg).Uri);
607 return uri_hash [uri] as ServerIdentity;
611 internal static void SetMessageTargetIdentity (IMessage msg, Identity ident)
613 if (msg is IInternalMessage)
614 ((IInternalMessage)msg).TargetIdentity = ident;
617 internal static bool UpdateOutArgObject (ParameterInfo pi, object local, object remote)
619 if (local is StringBuilder)
621 StringBuilder sb = local as StringBuilder;
622 sb.Remove (0, sb.Length);
623 sb.Append (remote.ToString());
626 else if (pi.ParameterType.IsArray && ((Array)local).Rank == 1)
628 Array alocal = (Array) local;
629 if (alocal.Rank == 1)
631 Array.Copy ((Array) remote, alocal, alocal.Length);
642 static string GetNormalizedUri (string uri)
644 if (uri.StartsWith ("/")) return uri.Substring (1);