We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Runtime.Remoting
- {
-
- using System;
- using System.Threading;
- using System.Runtime.InteropServices;
- using System.Runtime.Remoting;
- using System.Runtime.Remoting.Channels;
- using System.Runtime.Remoting.Contexts;
- using System.Runtime.Remoting.Messaging;
- using System.Runtime.Remoting.Metadata;
- using System.Runtime.Serialization;
- using System.Reflection;
- using System.Security.Permissions;
- using Win32Native = Microsoft.Win32.Win32Native;
- using System.Runtime.ConstrainedExecution;
- using System.Globalization;
-
-
-
-
-
-
- [System.Runtime.InteropServices.ComVisible(true)]
- public interface IRemotingTypeInfo
- {
-
- string TypeName {
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
- get;
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
- set;
- }
-
-
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
- bool CanCastTo(Type fromType, object o);
- }
-
-
-
-
- [System.Runtime.InteropServices.ComVisible(true)]
- public interface IChannelInfo
- {
-
- object[] ChannelData {
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
- get;
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
- set;
- }
- }
-
-
-
-
- [System.Runtime.InteropServices.ComVisible(true)]
- public interface IEnvoyInfo
- {
-
- IMessageSink EnvoySinks {
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
- get;
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
- set;
- }
- }
-
-
-
- [Serializable()]
- internal class TypeInfo : IRemotingTypeInfo
- {
- private string serverType;
- private string[] serverHierarchy;
- private string[] interfacesImplemented;
-
-
- public virtual string TypeName {
- get { return serverType; }
- set { serverType = value; }
- }
-
-
-
- public virtual bool CanCastTo(Type castType, object o)
- {
- if (null != castType) {
-
-
- if ((castType == typeof(MarshalByRefObject)) || (castType == typeof(object))) {
- return true;
- }
- else if (castType.IsInterface) {
- if (interfacesImplemented != null)
- return CanCastTo(castType, InterfacesImplemented);
- else
- return false;
- }
- else if (castType.IsMarshalByRef) {
- if (CompareTypes(castType, serverType))
- return true;
-
- if ((serverHierarchy != null) && CanCastTo(castType, ServerHierarchy))
- return true;
- }
- }
-
- return false;
- }
-
- static internal string GetQualifiedTypeName(Type type)
- {
- if (type == null)
- return null;
-
- return RemotingServices.GetDefaultQualifiedTypeName(type);
- }
-
- static internal bool ParseTypeAndAssembly(string typeAndAssembly, out string typeName, out string assemName)
- {
- if (typeAndAssembly == null) {
- typeName = null;
- assemName = null;
- return false;
- }
-
- int index = typeAndAssembly.IndexOf(',');
- if (index == -1) {
- typeName = typeAndAssembly;
- assemName = null;
- return true;
- }
-
-
- typeName = typeAndAssembly.Substring(0, index);
-
-
- assemName = typeAndAssembly.Substring(index + 1).Trim();
-
- return true;
- }
-
-
- internal TypeInfo(Type typeOfObj)
- {
- ServerType = GetQualifiedTypeName(typeOfObj);
-
-
- Type currType = typeOfObj.BaseType;
-
- Message.DebugOut("RemotingServices::TypeInfo: Determining length of server heirarchy\n");
- int hierarchyLen = 0;
- while ((currType != typeof(MarshalByRefObject)) && (currType != null)) {
- currType = currType.BaseType;
- hierarchyLen++;
- }
-
-
- Message.DebugOut("RemotingServices::TypeInfo: Determined length of server heirarchy\n");
- string[] serverHierarchy = null;
- if (hierarchyLen > 0) {
- serverHierarchy = new string[hierarchyLen];
-
- currType = typeOfObj.BaseType;
- for (int i = 0; i < hierarchyLen; i++) {
- serverHierarchy[i] = GetQualifiedTypeName(currType);
- currType = currType.BaseType;
- }
- }
-
- this.ServerHierarchy = serverHierarchy;
-
- Message.DebugOut("RemotingServices::TypeInfo: Getting implemented interfaces\n");
-
- Type[] interfaces = typeOfObj.GetInterfaces();
- string[] interfaceNames = null;
-
-
- bool isInterface = typeOfObj.IsInterface;
- if (interfaces.Length > 0 || isInterface) {
- interfaceNames = new string[interfaces.Length + (isInterface ? 1 : 0)];
- for (int i = 0; i < interfaces.Length; i++) {
- interfaceNames[i] = GetQualifiedTypeName(interfaces[i]);
- }
- if (isInterface)
- interfaceNames[interfaceNames.Length - 1] = GetQualifiedTypeName(typeOfObj);
- }
-
- this.InterfacesImplemented = interfaceNames;
- }
-
- internal string ServerType {
- get { return serverType; }
- set { serverType = value; }
- }
-
- private string[] ServerHierarchy {
- get { return serverHierarchy; }
- set { serverHierarchy = value; }
- }
-
- private string[] InterfacesImplemented {
- get { return interfacesImplemented; }
- set { interfacesImplemented = value; }
- }
-
- private bool CompareTypes(Type type1, string type2)
- {
- Type type = RemotingServices.InternalGetTypeFromQualifiedTypeName(type2);
-
- return type1 == type;
- }
-
- private bool CanCastTo(Type castType, string[] types)
- {
- bool fCastOK = false;
-
-
-
-
- if (null != castType) {
- for (int i = 0; i < types.Length; i++) {
- if (CompareTypes(castType, types[i])) {
- fCastOK = true;
- break;
- }
- }
- }
-
- Message.DebugOut("CanCastTo returning " + fCastOK + " for type " + castType.FullName + "\n");
- return fCastOK;
- }
- }
-
- [Serializable()]
- internal class DynamicTypeInfo : TypeInfo
- {
- internal DynamicTypeInfo(Type typeOfObj) : base(typeOfObj)
- {
- }
- public override bool CanCastTo(Type castType, object o)
- {
- return ((MarshalByRefObject)o).IsInstanceOfType(castType);
- }
- }
-
- [Serializable()]
- internal sealed class ChannelInfo : IChannelInfo
- {
- private object[] channelData;
-
- internal ChannelInfo()
- {
- ChannelData = ChannelServices.CurrentChannelData;
- }
-
- public object[] ChannelData {
- get { return channelData; }
- set { channelData = value; }
- }
-
- }
-
- [Serializable()]
- internal sealed class EnvoyInfo : IEnvoyInfo
- {
- private IMessageSink envoySinks;
-
- static internal IEnvoyInfo CreateEnvoyInfo(ServerIdentity serverID)
- {
- IEnvoyInfo info = null;
- if (null != serverID) {
-
- if (serverID.EnvoyChain == null) {
- serverID.RaceSetEnvoyChain(serverID.ServerContext.CreateEnvoyChain(serverID.TPOrObject));
- }
-
-
- IMessageSink sink = serverID.EnvoyChain as EnvoyTerminatorSink;
- if (null == sink) {
-
-
-
-
- info = new EnvoyInfo(serverID.EnvoyChain);
- }
- }
-
- return info;
- }
-
- private EnvoyInfo(IMessageSink sinks)
- {
- BCLDebug.Assert(null != sinks, "null != sinks");
- EnvoySinks = sinks;
- }
-
- public IMessageSink EnvoySinks {
- get { return envoySinks; }
- set { envoySinks = value; }
- }
- }
-
- [Serializable()]
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
- [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.Infrastructure)]
- [System.Runtime.InteropServices.ComVisible(true)]
- public class ObjRef : IObjectReference, ISerializable
- {
-
-
-
- internal const int FLG_MARSHALED_OBJECT = 1;
-
-
-
- internal const int FLG_WELLKNOWN_OBJREF = 2;
-
-
-
-
-
-
- internal const int FLG_LITE_OBJREF = 4;
-
- internal const int FLG_PROXY_ATTRIBUTE = 8;
-
-
-
-
- internal string uri;
- internal IRemotingTypeInfo typeInfo;
- internal IEnvoyInfo envoyInfo;
- internal IChannelInfo channelInfo;
- internal int objrefFlags;
- internal GCHandle srvIdentity;
- internal int domainID;
-
- internal void SetServerIdentity(GCHandle hndSrvIdentity)
- {
- srvIdentity = hndSrvIdentity;
- }
-
- internal GCHandle GetServerIdentity()
- {
- return srvIdentity;
- }
-
- internal void SetDomainID(int id)
- {
- domainID = id;
- }
-
- internal int GetDomainID()
- {
- return domainID;
- }
-
-
- private static Type orType = typeof(ObjRef);
-
-
-
- private ObjRef(ObjRef o)
- {
- BCLDebug.Assert(o.GetType() == typeof(ObjRef), "this should be just an ObjRef");
-
- uri = o.uri;
- typeInfo = o.typeInfo;
- envoyInfo = o.envoyInfo;
- channelInfo = o.channelInfo;
- objrefFlags = o.objrefFlags;
- SetServerIdentity(o.GetServerIdentity());
- SetDomainID(o.GetDomainID());
- }
-
- public ObjRef(MarshalByRefObject o, Type requestedType)
- {
- bool fServer;
- Identity id = MarshalByRefObject.GetIdentity(o, out fServer);
- Init(o, id, requestedType);
- }
-
- protected ObjRef(SerializationInfo info, StreamingContext context)
- {
- string url = null;
-
- bool bFoundFIsMarshalled = false;
-
- SerializationInfoEnumerator e = info.GetEnumerator();
- while (e.MoveNext()) {
- if (e.Name.Equals("uri")) {
- uri = (string)e.Value;
- }
- else if (e.Name.Equals("typeInfo")) {
- typeInfo = (IRemotingTypeInfo)e.Value;
- }
- else if (e.Name.Equals("envoyInfo")) {
- envoyInfo = (IEnvoyInfo)e.Value;
- }
- else if (e.Name.Equals("channelInfo")) {
- channelInfo = (IChannelInfo)e.Value;
- }
- else if (e.Name.Equals("objrefFlags")) {
- object o = e.Value;
- if (o.GetType() == typeof(string)) {
- objrefFlags = ((IConvertible)o).ToInt32(null);
- }
- else {
- objrefFlags = (int)o;
- }
- }
- else if (e.Name.Equals("fIsMarshalled")) {
- int value;
- object o = e.Value;
- if (o.GetType() == typeof(string))
- value = ((IConvertible)o).ToInt32(null);
- else
- value = (int)o;
-
- if (value == 0)
- bFoundFIsMarshalled = true;
- }
- else if (e.Name.Equals("url")) {
- url = (string)e.Value;
- }
- else if (e.Name.Equals("SrvIdentity")) {
- SetServerIdentity((GCHandle)e.Value);
- }
- else if (e.Name.Equals("DomainId")) {
- SetDomainID((int)e.Value);
- }
- }
-
- if (!bFoundFIsMarshalled) {
-
- objrefFlags |= FLG_MARSHALED_OBJECT;
- }
- else
- objrefFlags &= ~FLG_MARSHALED_OBJECT;
-
-
- if (url != null) {
- uri = url;
- objrefFlags |= FLG_LITE_OBJREF;
- }
-
- }
-
-
- internal bool CanSmuggle()
- {
-
- if ((this.GetType() != typeof(ObjRef)) || IsObjRefLite())
- return false;
-
- Type typeOfTypeInfo = null;
- if (typeInfo != null)
- typeOfTypeInfo = typeInfo.GetType();
-
- Type typeOfChannelInfo = null;
- if (channelInfo != null)
- typeOfChannelInfo = channelInfo.GetType();
-
- if (((typeOfTypeInfo == null) || (typeOfTypeInfo == typeof(TypeInfo)) || (typeOfTypeInfo == typeof(DynamicTypeInfo))) && (envoyInfo == null) && ((typeOfChannelInfo == null) || (typeOfChannelInfo == typeof(ChannelInfo)))) {
- if (channelInfo != null) {
- foreach (object channelData in channelInfo.ChannelData) {
- if (!(channelData is CrossAppDomainData)) {
- return false;
- }
- }
- }
-
- return true;
- }
- else {
- return false;
- }
- }
-
- internal ObjRef CreateSmuggleableCopy()
- {
- BCLDebug.Assert(CanSmuggle(), "Caller should have made sure that CanSmuggle() was true first.");
-
- return new ObjRef(this);
- }
-
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
- public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
- {
- if (info == null) {
- throw new ArgumentNullException("info");
- }
-
- info.SetType(orType);
-
- if (!IsObjRefLite()) {
- info.AddValue("uri", uri, typeof(string));
- info.AddValue("objrefFlags", (int)objrefFlags);
- info.AddValue("typeInfo", typeInfo, typeof(IRemotingTypeInfo));
- info.AddValue("envoyInfo", envoyInfo, typeof(IEnvoyInfo));
- info.AddValue("channelInfo", GetChannelInfoHelper(), typeof(IChannelInfo));
- }
- else {
- info.AddValue("url", uri, typeof(string));
- }
- }
-
-
-
-
-
- private IChannelInfo GetChannelInfoHelper()
- {
- ChannelInfo oldChannelInfo = channelInfo as ChannelInfo;
- if (oldChannelInfo == null)
- return channelInfo;
-
- object[] oldChannelData = oldChannelInfo.ChannelData;
- if (oldChannelData == null)
- return oldChannelInfo;
-
- string[] bashInfo = (string[])CallContext.GetData("__bashChannelUrl");
- if (bashInfo == null)
- return oldChannelInfo;
-
- string urlToBash = bashInfo[0];
- string replacementUrl = bashInfo[1];
-
- ChannelInfo newChInfo = new ChannelInfo();
- newChInfo.ChannelData = new object[oldChannelData.Length];
- for (int co = 0; co < oldChannelData.Length; co++) {
- newChInfo.ChannelData[co] = oldChannelData[co];
-
- ChannelDataStore channelDataStore = newChInfo.ChannelData[co] as ChannelDataStore;
- if (channelDataStore != null) {
- string[] urls = channelDataStore.ChannelUris;
- if ((urls != null) && (urls.Length == 1) && urls[0].Equals(urlToBash)) {
- ChannelDataStore newChannelDataStore = channelDataStore.InternalShallowCopy();
- newChannelDataStore.ChannelUris = new string[1];
- newChannelDataStore.ChannelUris[0] = replacementUrl;
-
- newChInfo.ChannelData[co] = newChannelDataStore;
- }
- }
- }
-
- return newChInfo;
- }
-
-
-
-
-
-
-
- public virtual string URI {
- get { return uri; }
- set { uri = value; }
- }
-
- public virtual IRemotingTypeInfo TypeInfo {
- get { return typeInfo; }
- set { typeInfo = value; }
- }
-
- public virtual IEnvoyInfo EnvoyInfo {
- get { return envoyInfo; }
- set { envoyInfo = value; }
- }
-
- public virtual IChannelInfo ChannelInfo {
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- get { return channelInfo; }
- set { channelInfo = value; }
- }
-
-
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
- public virtual object GetRealObject(StreamingContext context)
- {
- return GetRealObjectHelper();
- }
-
-
- internal object GetRealObjectHelper()
- {
-
-
- if (!IsMarshaledObject()) {
- BCLDebug.Trace("REMOTE", "ObjRef.GetRealObject: Returning *this*\n");
- return this;
- }
- else {
-
- if (IsObjRefLite()) {
- BCLDebug.Assert(null != uri, "null != uri");
-
-
-
- int index = uri.IndexOf(RemotingConfiguration.ApplicationId);
-
-
-
- if (index > 0)
- uri = uri.Substring(index - 1);
- }
-
-
-
-
-
-
-
-
-
- bool fRefine = !(GetType() == typeof(ObjRef));
- object ret = RemotingServices.Unmarshal(this, fRefine);
-
-
- ret = GetCustomMarshaledCOMObject(ret);
-
- return ret;
- }
-
- }
-
- private object GetCustomMarshaledCOMObject(object ret)
- {
- return ret;
- }
-
- public ObjRef()
- {
- objrefFlags = 0;
- }
-
- internal bool IsMarshaledObject()
- {
- return (objrefFlags & FLG_MARSHALED_OBJECT) == FLG_MARSHALED_OBJECT;
- }
-
- internal void SetMarshaledObject()
- {
- objrefFlags |= FLG_MARSHALED_OBJECT;
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- internal bool IsWellKnown()
- {
- return (objrefFlags & FLG_WELLKNOWN_OBJREF) == FLG_WELLKNOWN_OBJREF;
- }
-
- internal void SetWellKnown()
- {
- objrefFlags |= FLG_WELLKNOWN_OBJREF;
- }
-
- internal bool HasProxyAttribute()
- {
- return (objrefFlags & FLG_PROXY_ATTRIBUTE) == FLG_PROXY_ATTRIBUTE;
- }
-
- internal void SetHasProxyAttribute()
- {
- objrefFlags |= FLG_PROXY_ATTRIBUTE;
- }
-
- internal bool IsObjRefLite()
- {
- return (objrefFlags & FLG_LITE_OBJREF) == FLG_LITE_OBJREF;
- }
-
- internal void SetObjRefLite()
- {
- objrefFlags |= FLG_LITE_OBJREF;
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- private CrossAppDomainData GetAppDomainChannelData()
- {
- BCLDebug.Assert(ObjRef.IsWellFormed(this), "ObjRef.IsWellFormed()");
-
-
- int i = 0;
- CrossAppDomainData xadData = null;
- while (i < ChannelInfo.ChannelData.Length) {
- xadData = ChannelInfo.ChannelData[i] as CrossAppDomainData;
- if (null != xadData) {
- return xadData;
- }
- i++;
- }
-
-
- return null;
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- public bool IsFromThisProcess()
- {
-
-
- if (IsWellKnown())
- return false;
-
- CrossAppDomainData xadData = GetAppDomainChannelData();
- if (xadData != null) {
- return xadData.IsFromThisProcess();
- }
- return false;
- }
-
- public bool IsFromThisAppDomain()
- {
- CrossAppDomainData xadData = GetAppDomainChannelData();
- if (xadData != null) {
- return xadData.IsFromThisAppDomain();
- }
- return false;
- }
-
-
-
-
-
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- internal Int32 GetServerDomainId()
- {
- if (!IsFromThisProcess())
- return 0;
- CrossAppDomainData xadData = GetAppDomainChannelData();
- BCLDebug.Assert(xadData != null, "bad objRef?");
- return xadData.DomainID;
-
- }
-
- internal IntPtr GetServerContext(out int domainId)
- {
- IntPtr contextId = IntPtr.Zero;
- domainId = 0;
- if (IsFromThisProcess()) {
- CrossAppDomainData xadData = GetAppDomainChannelData();
- BCLDebug.Assert(xadData != null, "bad objRef?");
- domainId = xadData.DomainID;
- if (AppDomain.IsDomainIdValid(xadData.DomainID)) {
- contextId = xadData.ContextID;
- }
- }
- return contextId;
- }
-
-
-
- internal void Init(object o, Identity idObj, Type requestedType)
- {
- Message.DebugOut("RemotingServices::FillObjRef: IN");
- BCLDebug.Assert(idObj != null, "idObj != null");
-
-
- uri = idObj.URI;
-
-
- MarshalByRefObject obj = idObj.TPOrObject;
- BCLDebug.Assert(null != obj, "Identity not setup correctly");
-
-
- Type serverType = null;
- if (!RemotingServices.IsTransparentProxy(obj)) {
- serverType = obj.GetType();
- }
- else {
- serverType = RemotingServices.GetRealProxy(obj).GetProxiedType();
- }
-
- Type typeOfObj = (null == requestedType ? serverType : requestedType);
-
-
-
-
- if ((null != requestedType) && !requestedType.IsAssignableFrom(serverType) && (!typeof(IMessageSink).IsAssignableFrom(serverType))) {
- throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_InvalidRequestedType"), requestedType.ToString()));
- ;
- }
-
- {
- RemotingTypeCachedData cache = (RemotingTypeCachedData)InternalRemotingServices.GetReflectionCachedData(typeOfObj);
-
- TypeInfo = (IRemotingTypeInfo)cache.TypeInfo;
- }
-
- if (!idObj.IsWellKnown()) {
-
- EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);
-
-
- IChannelInfo chan = (IChannelInfo)new ChannelInfo();
-
-
- if (o is AppDomain) {
- object[] channelData = chan.ChannelData;
- int channelDataLength = channelData.Length;
- object[] newChannelData = new object[channelDataLength];
- Array.Copy(channelData, newChannelData, channelDataLength);
- for (int i = 0; i < channelDataLength; i++) {
- if (!(newChannelData[i] is CrossAppDomainData))
- newChannelData[i] = null;
- }
- chan.ChannelData = newChannelData;
- }
- ChannelInfo = chan;
-
- if (serverType.HasProxyAttribute) {
- SetHasProxyAttribute();
- }
- }
- else {
- SetWellKnown();
- }
-
-
- if (ShouldUseUrlObjRef()) {
- if (IsWellKnown()) {
-
- SetObjRefLite();
- &nb