using System.IO;
using System.Runtime.Remoting.Services;
using System.Security.Permissions;
-
-#if NET_2_0
using System.Runtime.ConstrainedExecution;
using System.Runtime.Serialization.Formatters;
-#endif
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 ();
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;
StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
_serializationFormatter = new BinaryFormatter (surrogateSelector, context);
_deserializationFormatter = new BinaryFormatter (null, context);
-#if NET_2_0
_serializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Full;
_deserializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Full;
-#endif
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 DISABLE_REMOTING
+ 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)
{
Type tt = target.GetType ();
MethodBase method;
if (reqMsg.MethodBase.DeclaringType == tt ||
- reqMsg.MethodBase == FieldSetterMethod ||
- reqMsg.MethodBase == FieldGetterMethod
- /*|| reqMsg.MethodBase.DeclaringType.IsInterface*/)
+ 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
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);
}
}
- 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;
}
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
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)
{
// 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.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.");
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) {
if (method != null)
return method;
- if (methodName == "FieldSetter" || methodName == "FieldGetter")
- return typeof(object).GetMethod (methodName, methodBindings);
+ if (methodName == "FieldSetter")
+ return FieldSetterMethod;
+
+ if (methodName == "FieldGetter")
+ return FieldGetterMethod;
if (signature == null)
return type.GetConstructor (methodBindings, null, Type.EmptyTypes, null);
[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 ();
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);
// Registers the identity
uri_hash [uri] = identity;
-
if (proxyType != null)
{
RemotingProxy proxy = new RemotingProxy (proxyType, identity);
clientProxy = proxy.GetTransparentProxy();
identity.ClientProxy = (MarshalByRefObject) clientProxy;
}
-
return identity;
}
}
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);
}