internal TransitionCall(IntPtr targetCtxID, CrossContextDelegate deleg)
 {
     this._sourceCtxID = Thread.CurrentContext.InternalContextID;
     this._targetCtxID = targetCtxID;
     this._delegate = deleg;
     this._targetDomainID = 0;
     this._eeData = IntPtr.Zero;
     this._srvID = new ServerIdentity(null, Thread.GetContextInternal(this._targetCtxID));
     this._ID = this._srvID;
     this._ID.RaceSetChannelSink(CrossContextChannel.MessageSink);
     this._srvID.RaceSetServerObjectChain(this);
 }
 internal TransitionCall(IntPtr targetCtxID, IntPtr eeData, int targetDomainID)
 {
     string str;
     this._sourceCtxID = Thread.CurrentContext.InternalContextID;
     this._targetCtxID = targetCtxID;
     this._delegate = null;
     this._targetDomainID = targetDomainID;
     this._eeData = eeData;
     this._srvID = null;
     this._ID = new Identity("TransitionCallURI", null);
     CrossAppDomainData data = new CrossAppDomainData(this._targetCtxID, this._targetDomainID, Identity.ProcessGuid);
     IMessageSink channelSink = CrossAppDomainChannel.AppDomainChannel.CreateMessageSink(null, data, out str);
     this._ID.RaceSetChannelSink(channelSink);
 }
示例#3
0
 internal static System.Runtime.Remoting.Identity GetIdentity(MarshalByRefObject obj, out bool fServer)
 {
     fServer = true;
     System.Runtime.Remoting.Identity identity = null;
     if (obj == null)
     {
         return(identity);
     }
     if (!RemotingServices.IsTransparentProxy(obj))
     {
         return((System.Runtime.Remoting.Identity)obj.Identity);
     }
     fServer = false;
     return(RemotingServices.GetRealProxy(obj).IdentityObject);
 }
示例#4
0
 internal static System.Runtime.Remoting.Identity GetIdentity(MarshalByRefObject obj, out bool fServer)
 {
     fServer = true;
     System.Runtime.Remoting.Identity identity = (System.Runtime.Remoting.Identity)null;
     if (obj != null)
     {
         if (!RemotingServices.IsTransparentProxy((object)obj))
         {
             identity = (System.Runtime.Remoting.Identity)obj.Identity;
         }
         else
         {
             fServer  = false;
             identity = RemotingServices.GetRealProxy((object)obj).IdentityObject;
         }
     }
     return(identity);
 }
 internal void Init(object o, Identity idObj, RuntimeType requestedType)
 {
     this.uri = idObj.URI;
     MarshalByRefObject tPOrObject = idObj.TPOrObject;
     RuntimeType c = null;
     if (!RemotingServices.IsTransparentProxy(tPOrObject))
     {
         c = (RuntimeType) tPOrObject.GetType();
     }
     else
     {
         c = (RuntimeType) RemotingServices.GetRealProxy(tPOrObject).GetProxiedType();
     }
     RuntimeType typeOfObj = (null == requestedType) ? c : requestedType;
     if (((null != requestedType) && !requestedType.IsAssignableFrom(c)) && !typeof(IMessageSink).IsAssignableFrom(c))
     {
         throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_InvalidRequestedType"), new object[] { requestedType.ToString() }));
     }
     if (c.IsCOMObject)
     {
         DynamicTypeInfo info = new DynamicTypeInfo(typeOfObj);
         this.TypeInfo = info;
     }
     else
     {
         RemotingTypeCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(typeOfObj);
         this.TypeInfo = reflectionCachedData.TypeInfo;
     }
     if (!idObj.IsWellKnown())
     {
         this.EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);
         IChannelInfo info2 = new System.Runtime.Remoting.ChannelInfo();
         if (o is AppDomain)
         {
             object[] channelData = info2.ChannelData;
             int length = channelData.Length;
             object[] destinationArray = new object[length];
             Array.Copy(channelData, destinationArray, length);
             for (int i = 0; i < length; i++)
             {
                 if (!(destinationArray[i] is CrossAppDomainData))
                 {
                     destinationArray[i] = null;
                 }
             }
             info2.ChannelData = destinationArray;
         }
         this.ChannelInfo = info2;
         if (c.HasProxyAttribute)
         {
             this.SetHasProxyAttribute();
         }
     }
     else
     {
         this.SetWellKnown();
     }
     if (ShouldUseUrlObjRef())
     {
         if (this.IsWellKnown())
         {
             this.SetObjRefLite();
         }
         else
         {
             string str = ChannelServices.FindFirstHttpUrlForObject(this.URI);
             if (str != null)
             {
                 this.URI = str;
                 this.SetObjRefLite();
             }
         }
     }
 }
示例#6
0
文件: message.cs 项目: ArildF/masters
        IntPtr _privateData; // Used for DoCallbackInEE

        // The _delegate should really be on an agile object otherwise
        // the whole point of doing a callBack is moot. However, even if it
        // is not, remoting and serialization together will ensure that
        // everything happens as expected and there is no smuggling.

        internal TransitionCall(
            IntPtr targetCtxID, 
            CrossContextDelegate deleg)
        {
            BCLDebug.Assert(targetCtxID!=IntPtr.Zero, "bad target ctx for call back");
            _sourceCtxID = Thread.CurrentContext.InternalContextID;
            _targetCtxID = targetCtxID;
            _delegate = deleg;
            _targetDomainID = 0;
            _privateData = IntPtr.Zero;

                // We are going to another context in the same app domain
                _srvID = new ServerIdentity(
                                null, 
                                Thread.GetContextInternal(_targetCtxID));
                _ID = _srvID;
                _ID.RaceSetChannelSink(CrossContextChannel.MessageSink);
                _srvID.RaceSetServerObjectChain(this);
                
            //DBG Console.WriteLine("### TransitionCall ctor: " + Int32.Format(_sourceCtxID,"x") + ":" + Int32.Format(_targetCtxID,"x"));
        } // TransitionCall
示例#7
0
        public static ObjRef Marshal(MarshalByRefObject Obj, string ObjURI, Type RequestedType)
        {
            if (IsTransparentProxy(Obj))
            {
                RealProxy proxy    = RemotingServices.GetRealProxy(Obj);
                Identity  identity = proxy.ObjectIdentity;

                if (identity != null)
                {
                    if (proxy.GetProxiedType().IsContextful&& !identity.IsConnected)
                    {
                        // Unregistered local contextbound object. Register now.
                        ClientActivatedIdentity cboundIdentity = (ClientActivatedIdentity)identity;
                        if (ObjURI == null)
                        {
                            ObjURI = NewUri();
                        }
                        cboundIdentity.ObjectUri = ObjURI;
                        RegisterServerIdentity(cboundIdentity);
                        cboundIdentity.StartTrackingLifetime((ILease)Obj.InitializeLifetimeService());
                        return(cboundIdentity.CreateObjRef(RequestedType));
                    }
                    else if (ObjURI != null)
                    {
                        throw new RemotingException("It is not possible marshal a proxy of a remote object.");
                    }

                    ObjRef or = proxy.ObjectIdentity.CreateObjRef(RequestedType);
                    TrackingServices.NotifyMarshaledObject(Obj, or);
                    return(or);
                }
            }

            if (RequestedType == null)
            {
                RequestedType = Obj.GetType();
            }

            if (ObjURI == null)
            {
                if (Obj.ObjectIdentity == null)
                {
                    ObjURI = NewUri();
                    CreateClientActivatedServerIdentity(Obj, RequestedType, ObjURI);
                }
            }
            else
            {
                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(RequestedType);
            }
            else
            {
                oref = Obj.CreateObjRef(RequestedType);
            }

            TrackingServices.NotifyMarshaledObject(Obj, oref);
            return(oref);
        }
 internal static Identity FindOrCreateIdentity(string objURI, string URL, ObjRef objectRef)
 {
     Identity idObj = null;
     bool flag = URL != null;
     idObj = ResolveIdentity(flag ? URL : objURI);
     if ((flag && (idObj != null)) && (idObj is ServerIdentity))
     {
         throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_WellKnown_CantDirectlyConnect"), new object[] { URL }));
     }
     if (idObj == null)
     {
         idObj = new Identity(objURI, URL);
         ReaderWriterLock tableLock = TableLock;
         bool flag2 = !tableLock.IsWriterLockHeld;
         RuntimeHelpers.PrepareConstrainedRegions();
         try
         {
             if (flag2)
             {
                 tableLock.AcquireWriterLock(0x7fffffff);
             }
             idObj = SetIdentity(idObj, null, DuplicateIdentityOption.UseExisting);
             idObj.RaceSetObjRef(objectRef);
         }
         finally
         {
             if (flag2 && tableLock.IsWriterLockHeld)
             {
                 tableLock.ReleaseWriterLock();
             }
         }
     }
     return idObj;
 }
        [System.Security.SecurityCritical]  // auto-generated
        internal static Identity FindOrCreateIdentity(
            String objURI, String URL, ObjRef objectRef)
        {
            Identity idObj = null;

            Contract.Assert(null != objURI,"null != objURI");

            bool bWellKnown = (URL != null);

            // Lookup the object in the identity table
            // for well-known objects we user the URL
            // as the hash-key (instead of just the objUri)
            idObj = ResolveIdentity(bWellKnown ? URL : objURI);
            if (bWellKnown &&
                (idObj != null) &&
                (idObj is ServerIdentity))
            {
                // We are trying to do a connect to a server wellknown object.
                throw new RemotingException(
                    String.Format(
                        CultureInfo.CurrentCulture, Environment.GetResourceString(
                            "Remoting_WellKnown_CantDirectlyConnect"),
                        URL));                            
            }
                 
            if (null == idObj)
            {
                // There is no entry for this uri in the IdTable.
                Message.DebugOut("RemotingService::FindOrCreateIdentity: Creating Identity\n");

                // This identity is being encountered for the first time.
                // We have to do the following things
                // (1) Create an identity object for the proxy
                // (2) Add the identity to the identity table
                // (3) Create a proxy for the object represented by the objref      
                
                // Create a new identity
                // <EMAIL>GopalK:</EMAIL> Identity should get only one string that is used for everything
                idObj = new Identity(objURI, URL);                         

                // We need to guarantee that finally is not interrupted so that the lock is released.
                // TableLock has a long path without reliability contract.  To avoid adding contract on
                // the path, we will use ReaderWriterLock directly.
                ReaderWriterLock rwlock = TableLock;
                bool takeAndRelease = !rwlock.IsWriterLockHeld;

                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {                
                    // Add it to the identity table
                    if (takeAndRelease)
                        rwlock.AcquireWriterLock(INFINITE);

                    // SetIdentity will give the correct Id if we ----d
                    // between the ResolveIdentity call above and now.
                    //   (we are unmarshaling, and the server should guarantee
                    //    that the uri is unique, so we will use an existing identity
                    //    in case of a ----)
                    idObj = SetIdentity(idObj, null, DuplicateIdentityOption.UseExisting);

                    idObj.RaceSetObjRef(objectRef);
                }
                finally
                {
                    if (takeAndRelease && rwlock.IsWriterLockHeld)
                    {
                        rwlock.ReleaseWriterLock();
                    }                
                }
            }
            else
            {
                Message.DebugOut("RemotingService::FindOrCreateIdentity: Found Identity!\n");
            }
            Contract.Assert(null != idObj,"null != idObj");
            return idObj;                
        }
示例#10
0
        [System.Security.SecurityCritical]  // auto-generated
        private static MarshalByRefObject SetOrCreateProxy( 
            Identity idObj, Type classToProxy, Object proxy)
 
        { 
            Message.DebugOut("Entering SetOrCreateProxy for type \n");
 
            RealProxy realProxy = null;
            // If a proxy has not been supplied create one
            if (null == proxy)
            { 
                // Create a remoting proxy
                Message.DebugOut("SetOrCreateProxy::Creating Proxy for " + 
                                 classToProxy.FullName + "\n"); 

                ServerIdentity srvID = idObj as ServerIdentity; 
                if (idObj.ObjectRef != null)
                {
                    ProxyAttribute pa = ActivationServices.GetProxyAttribute(classToProxy);
                    realProxy = pa.CreateProxy(idObj.ObjectRef, 
                                           classToProxy,
                                           null,  // 
                                           null); // 
                }
                if(null == realProxy) 
                {
                    // The custom proxy attribute does not want to create a proxy. We create a default
                    // proxy in this case.
                    ProxyAttribute defaultProxyAttribute = ActivationServices.DefaultProxyAttribute; 

                    realProxy = defaultProxyAttribute.CreateProxy(idObj.ObjectRef, 
                                                                  classToProxy, 
                                                                  null,
                                                                  (null == srvID ? null : 
                                                                                   srvID.ServerContext));
                }
            }
            else 
            {
                BCLDebug.Assert(IsTransparentProxy(proxy),"IsTransparentProxy(proxy)"); 
 
                // Extract the remoting proxy from the transparent proxy
                Message.DebugOut("SetOrCreateProxy::Proxy already created \n"); 
                realProxy = GetRealProxy(proxy);
            }

            BCLDebug.Assert(null != realProxy,"null != realProxy"); 

            // Set the back reference to the identity in the proxy object 
            realProxy.IdentityObject = idObj; 

            // Set the reference to the proxy in the identity object 
            proxy = realProxy.GetTransparentProxy();
            proxy = idObj.RaceSetTransparentProxy(proxy);

            Message.DebugOut("Leaving SetOrCreateProxy\n"); 
            // return the transparent proxy
            return (MarshalByRefObject)proxy; 
        } 
示例#11
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static Identity FindOrCreateIdentity(
            String objURI, String URL, ObjRef objectRef)
        {
            Identity idObj = null;

            Contract.Assert(null != objURI, "null != objURI");

            bool bWellKnown = (URL != null);

            // Lookup the object in the identity table
            // for well-known objects we user the URL
            // as the hash-key (instead of just the objUri)
            idObj = ResolveIdentity(bWellKnown ? URL : objURI);
            if (bWellKnown &&
                (idObj != null) &&
                (idObj is ServerIdentity))
            {
                // We are trying to do a connect to a server wellknown object.
                throw new RemotingException(
                          String.Format(
                              CultureInfo.CurrentCulture, Environment.GetResourceString(
                                  "Remoting_WellKnown_CantDirectlyConnect"),
                              URL));
            }

            if (null == idObj)
            {
                // There is no entry for this uri in the IdTable.
                Message.DebugOut("RemotingService::FindOrCreateIdentity: Creating Identity\n");

                // This identity is being encountered for the first time.
                // We have to do the following things
                // (1) Create an identity object for the proxy
                // (2) Add the identity to the identity table
                // (3) Create a proxy for the object represented by the objref

                // Create a new identity
                // <EMAIL>GopalK:</EMAIL> Identity should get only one string that is used for everything
                idObj = new Identity(objURI, URL);

                // We need to guarantee that finally is not interrupted so that the lock is released.
                // TableLock has a long path without reliability contract.  To avoid adding contract on
                // the path, we will use ReaderWriterLock directly.
                ReaderWriterLock rwlock         = TableLock;
                bool             takeAndRelease = !rwlock.IsWriterLockHeld;

                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    // Add it to the identity table
                    if (takeAndRelease)
                    {
                        rwlock.AcquireWriterLock(INFINITE);
                    }

                    // SetIdentity will give the correct Id if we ----d
                    // between the ResolveIdentity call above and now.
                    //   (we are unmarshaling, and the server should guarantee
                    //    that the uri is unique, so we will use an existing identity
                    //    in case of a ----)
                    idObj = SetIdentity(idObj, null, DuplicateIdentityOption.UseExisting);

                    idObj.RaceSetObjRef(objectRef);
                }
                finally
                {
                    if (takeAndRelease && rwlock.IsWriterLockHeld)
                    {
                        rwlock.ReleaseWriterLock();
                    }
                }
            }
            else
            {
                Message.DebugOut("RemotingService::FindOrCreateIdentity: Found Identity!\n");
            }
            Contract.Assert(null != idObj, "null != idObj");
            return(idObj);
        }
示例#12
0
        //
        //
        internal void Init(Identity idObj, Type requestedType)
        {
            Message.DebugOut("RemotingServices::FillObjRef: IN");
            BCLDebug.Assert(idObj != null, "idObj != null");

            // Set the URI of the object to be marshaled
            URI = idObj.URI;

            // Figure out the type
            MarshalByRefObject obj = idObj.TPOrObject;

            BCLDebug.Assert(null != obj, "Identity not setup correctly");

            // Get the type of the object
            Type serverType = null;

            if (!RemotingServices.IsTransparentProxy(obj))
            {
                serverType = obj.GetType();
            }
            else
            {
                serverType = RemotingServices.GetRealProxy(obj).GetProxiedType();
            }

            Type typeOfObj = (null == requestedType ? serverType : requestedType);

            // Make sure that the server and requested types are compatible
            //  (except for objects that implement IMessageSink, since we
            //   just hand off the message instead of invoking the proxy)
            if ((null != requestedType) &&
                !requestedType.IsAssignableFrom(serverType) &&
                (!typeof(IMessageSink).IsAssignableFrom(serverType)))
            {
                throw new RemotingException(
                          String.Format(
                              Environment.GetResourceString(
                                  "Remoting_InvalidRequestedType"),
                              requestedType.ToString()));;
            }

            // Create the type info
            if (serverType.IsCOMObject)
            {
                // __ComObjects need dynamic TypeInfo
                DynamicTypeInfo dt = new DynamicTypeInfo(typeOfObj);
                TypeInfo = (IRemotingTypeInfo)dt;
            }
            else
            {
                RemotingTypeCachedData cache = (RemotingTypeCachedData)
                                               InternalRemotingServices.GetReflectionCachedData(typeOfObj);

                TypeInfo = (IRemotingTypeInfo)cache.TypeInfo;
            }

            if (!idObj.IsWellKnown())
            {
                // Create the envoy info
                EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);

                // Create the channel info
                ChannelInfo = (IChannelInfo) new ChannelInfo();

                if (serverType.HasProxyAttribute)
                {
                    SetHasProxyAttribute();
                }
            }
            else
            {
                SetWellKnown();
            }

            // See if we should and can use a url obj ref?
            if (ShouldUseUrlObjRef())
            {
                if (IsWellKnown())
                {
                    // full uri already supplied.
                    SetObjRefLite();
                }
                else
                {
                    String httpUri = ChannelServices.FindFirstHttpUrlForObject(URI);
                    if (httpUri != null)
                    {
                        URI = httpUri;
                        SetObjRefLite();
                    }
                }
            }
        } // Init
示例#13
0
        internal void Init(object o, Identity idObj, RuntimeType requestedType)
        {
            this.uri = idObj.URI;
            MarshalByRefObject tPOrObject = idObj.TPOrObject;
            RuntimeType        c          = null;

            if (!RemotingServices.IsTransparentProxy(tPOrObject))
            {
                c = (RuntimeType)tPOrObject.GetType();
            }
            else
            {
                c = (RuntimeType)RemotingServices.GetRealProxy(tPOrObject).GetProxiedType();
            }
            RuntimeType typeOfObj = (null == requestedType) ? c : requestedType;

            if (((null != requestedType) && !requestedType.IsAssignableFrom(c)) && !typeof(IMessageSink).IsAssignableFrom(c))
            {
                throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_InvalidRequestedType"), new object[] { requestedType.ToString() }));
            }
            if (c.IsCOMObject)
            {
                DynamicTypeInfo info = new DynamicTypeInfo(typeOfObj);
                this.TypeInfo = info;
            }
            else
            {
                RemotingTypeCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(typeOfObj);
                this.TypeInfo = reflectionCachedData.TypeInfo;
            }
            if (!idObj.IsWellKnown())
            {
                this.EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);
                IChannelInfo info2 = new System.Runtime.Remoting.ChannelInfo();
                if (o is AppDomain)
                {
                    object[] channelData      = info2.ChannelData;
                    int      length           = channelData.Length;
                    object[] destinationArray = new object[length];
                    Array.Copy(channelData, destinationArray, length);
                    for (int i = 0; i < length; i++)
                    {
                        if (!(destinationArray[i] is CrossAppDomainData))
                        {
                            destinationArray[i] = null;
                        }
                    }
                    info2.ChannelData = destinationArray;
                }
                this.ChannelInfo = info2;
                if (c.HasProxyAttribute)
                {
                    this.SetHasProxyAttribute();
                }
            }
            else
            {
                this.SetWellKnown();
            }
            if (ShouldUseUrlObjRef())
            {
                if (this.IsWellKnown())
                {
                    this.SetObjRefLite();
                }
                else
                {
                    string str = ChannelServices.FindFirstHttpUrlForObject(this.URI);
                    if (str != null)
                    {
                        this.URI = str;
                        this.SetObjRefLite();
                    }
                }
            }
        }
 internal static Type GetServerTypeForUri(string URI)
 {
     URI = Identity.RemoveAppNameOrAppGuidIfNecessary(URI);
     return(Info.GetServerTypeForUri(URI));
 }
 internal static ServerIdentity CreateWellKnownObject(string uri)
 {
     uri = Identity.RemoveAppNameOrAppGuidIfNecessary(uri);
     return(Info.StartupWellKnownObject(uri));
 }
示例#16
0
		internal static void DisposeIdentity (Identity ident)
		{
			lock (uri_hash)
			{
				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;
				}
			}
		}
示例#17
0
        [System.Security.SecurityCritical]  // auto-generated
        private static Object GetOrCreateProxy(
            Identity idObj, Object proxy, bool fRefine) 
        {
            Message.DebugOut("Entering GetOrCreateProxy for given proxy\n"); 
 
            // If we are not supplied a proxy then we have to find an existing
            // proxy or create one 
            if (null == proxy)
            {
                // Get the type of the server object
                Type serverType; 
                ServerIdentity serverID = idObj as ServerIdentity;
                if (null != serverID) 
                { 
                    serverType = serverID.ServerType; // ServerObject.GetType();
                } 
                else
                {
                    BCLDebug.Assert(ObjRef.IsWellFormed(idObj.ObjectRef),
                                    "ObjRef.IsWellFormed(idObj.ObjectRef)"); 
                    IRemotingTypeInfo serverTypeInfo = idObj.ObjectRef.TypeInfo;
 
                    // For system generated type info we create the proxy for 
                    // object type. Later, when the proxy is cast to the appropriate
                    // type we will update its internal state to refer to the cast type 
                    // This way we avoid loading the server type till cast time.
                    //
                    // For type info generated by others we have no choice but to
                    // load the type provided by the typeinfo. Sometimes, we 
                    // use this second approach even if the typeinfo has been
                    // generated by us because this saves us an extra checkcast. 
                    // A typical example of this usage will be when we are 
                    // unmarshaling in parameters. We know that the unmarshal will
                    // be followed by a check cast to ensure that the parameter type 
                    // matches the signature type, so we do both in one step.
                    serverType = null;
                    if (((serverTypeInfo is TypeInfo) && !fRefine) ||
                        (serverTypeInfo == null)) 
                    {
                        serverType = typeof(System.MarshalByRefObject); 
                    } 
                    else
                    { 
                        String typeName = serverTypeInfo.TypeName;
                        if (typeName != null)
                        {
                            String typeNamespace = null; 
                            String assemNamespace = null;
                            TypeInfo.ParseTypeAndAssembly(typeName, out typeNamespace, out assemNamespace); 
 

                            Assembly assem = FormatterServices.LoadAssemblyFromStringNoThrow(assemNamespace); 

                            if (assem != null)
                            {
                                serverType = assem.GetType(typeNamespace, false, false); 
                            }
 
                        } 

                    } 

                    if (null == serverType)
                    {
                        throw new RemotingException( 
                            String.Format(
                                CultureInfo.CurrentCulture, Environment.GetResourceString( 
                                    "Remoting_BadType"), 
                                serverTypeInfo.TypeName));
                    } 
                }
                Message.DebugOut("Creating Proxy for type " + serverType.FullName + "\n");
                proxy = SetOrCreateProxy(idObj, serverType, null);
            } 
            else
            { 
                // We have been supplied with a proxy. Set that proxy in 
                // the identity object
                // Assert that this the activation case only as this code path is 
                // not thread safe otherwise! (We call Wrap to associate an object
                // with its proxy during activation).
                BCLDebug.Assert(
                    ((RemotingProxy)GetRealProxy(proxy)).ConstructorMessage!=null, 
                    "Only expect to be here during activation!");
                proxy = SetOrCreateProxy(idObj, null, proxy); 
            } 

            // At this point we should have a non-null transparent proxy 
            if (proxy == null)
                throw new RemotingException(Environment.GetResourceString("Remoting_UnexpectedNullTP"));

            BCLDebug.Assert(IsTransparentProxy(proxy),"IsTransparentProxy(proxy)"); 

            Message.DebugOut("Leaving GetOrCreateProxy for given proxy\n"); 
            // Return the underlying transparent proxy 
            return proxy;
        } 
示例#18
0
        [System.Security.SecurityCritical]  // auto-generated
        private static Identity SetIdentity(
            Identity idObj, String URI, DuplicateIdentityOption duplicateOption)
        {
            // NOTE: This function assumes that a lock has been taken
            // by the calling function
            // idObj could be for a transparent proxy or a server object
            Message.DebugOut("SetIdentity:: domainid: " + Thread.GetDomainID() + "\n");
            Contract.Assert(null != idObj, "null != idObj");

            // WriterLock must already be taken when SetIdentity is called!
            Contract.Assert(
                TableLock.IsWriterLockHeld,
                "Should have write-locked the ID Table!");

            // flag to denote that the id being set is a ServerIdentity
            bool bServerIDSet = idObj is ServerIdentity;

            if (null == idObj.URI)
            {
                // No URI has been associated with this identity. It must be a
                // server identity getting marshaled out of the app domain for
                // the first time.
                Contract.Assert(bServerIDSet, "idObj should be ServerIdentity");

                // Set the URI on the idObj (generating one if needed)
                idObj.SetOrCreateURI(URI);

                // If objectref is non-null make sure both have same URIs
                // (the URI in the objectRef could have potentially been reset
                // in a past external call to Disconnect()
                if (idObj.ObjectRef != null)
                {
                    idObj.ObjectRef.URI = idObj.URI;
                }
                Message.DebugOut("SetIdentity: Generated URI " + URI + " for identity");
            }

            // If we have come this far then there is no URI to identity
            // mapping present. Go ahead and create one.

            // ID should have a URI by now.
            Contract.Assert(null != idObj.URI, "null != idObj.URI");

            // See if this identity is already present in the Uri table
            String uriKey = MakeURIKey(idObj.URI);
            Object o      = URITable[uriKey];

            // flag to denote that the id found in the table is a ServerIdentity
            bool bServerID;

            if (null != o)
            {
                // We found an identity (or a WeakRef to one) for the URI provided
                WeakReference wr        = o as WeakReference;
                Identity      idInTable = null;
                if (wr != null)
                {
                    // The object we found is a weak referece to an identity

                    // This could be an identity for a client side
                    // proxy
                    // OR
                    // a server identity which has been weakened since its life
                    // is over.
                    idInTable = (Identity)wr.Target;

                    bServerID = idInTable is ServerIdentity;

                    // If we find a weakRef for a ServerId we will be converting
                    // it to a strong one before releasing the IdTable lock.
                    Contract.Assert(
                        (idInTable == null) ||
                        (!bServerID || idInTable.IsRemoteDisconnected()),
                        "Expect to find WeakRef only for remotely disconnected ids");
                    // We could find a weakRef to a client ID that does not
                    // match the idObj .. but that is a handled ---- case
                    // during Unmarshaling .. SetIdentity() will return the ID
                    // from the table to the caller.
                }
                else
                {
                    // We found a non-weak (strong) Identity for the URI
                    idInTable = (Identity)o;
                    bServerID = idInTable is ServerIdentity;

                    //We dont put strong refs to client "Identity"s in the table
                    Contract.Assert(
                        bServerID,
                        "Found client side strong ID in the table");
                }

                if ((idInTable != null) && (idInTable != idObj))
                {
                    // We are trying to add another identity for the same URI
                    switch (duplicateOption)
                    {
                    case DuplicateIdentityOption.Unique:
                    {
                        String tempURI = idObj.URI;

                        // Throw an exception to indicate the error since this could
                        // be caused by a user trying to marshal two objects with the same
                        // URI
                        throw new RemotingException(
                                  Environment.GetResourceString("Remoting_URIClash",
                                                                tempURI));
                    } // case DuplicateIdentityOption.Unique

                    case DuplicateIdentityOption.UseExisting:
                    {
                        // This would be a case where our thread lost the ----
                        // we will return the one found in the table
                        idObj = idInTable;
                        break;
                    } // case DuplicateIdentityOption.UseExisting:

                    default:
                    {
                        Contract.Assert(false, "Invalid DuplicateIdentityOption");
                        break;
                    }
                    } // switch (duplicateOption)
                }
                else
                if (wr != null)
                {
                    // We come here if we found a weakRef in the table but
                    // the target object had been cleaned up
                    // OR
                    // If there was a weakRef in the table and the target
                    // object matches the idObj just passed in

                    // Strengthen the entry if it a ServerIdentity.
                    if (bServerID)
                    {
                        URITable[uriKey] = idObj;
                    }
                    else
                    {
                        // For client IDs associate the table entry
                        // with the one passed in.
                        // (If target was null we would set it ...
                        // if was non-null then it matches idObj anyway)
                        wr.Target = idObj;
                    }
                }
            }
            else
            {
                // We did not find an identity entry for the URI
                Object addMe = null;
                if (bServerIDSet)
                {
                    addMe = idObj;
                    ((ServerIdentity)idObj).SetHandle();
                }
                else
                {
                    addMe = new WeakReference(idObj);
                }

                // Add the entry into the table
                URITable.Add(uriKey, addMe);
                idObj.SetInIDTable();

                // After every fixed number of set-id calls we run through
                // the table and cleanup if needed.
                SetIDCount++;
                if (SetIDCount % CleanUpCountInterval == 0)
                {
                    // This should be called with the write lock held!
                    //   (which is why we assert that at the beginning of this
                    //    method)
                    CleanupIdentities(null);
                }
            }

            Message.DebugOut("SetIdentity:: Identity::URI: " + idObj.URI + "\n");
            return(idObj);
        }
示例#19
0
文件: objref.cs 项目: ArildF/masters
       //
       //
        internal void Init(Identity idObj, Type requestedType)
        {        
            Message.DebugOut("RemotingServices::FillObjRef: IN");
            BCLDebug.Assert(idObj != null,"idObj != null");

            // Set the URI of the object to be marshaled            
            URI = idObj.URI;

            // Figure out the type 
            MarshalByRefObject obj = idObj.TPOrObject;
            BCLDebug.Assert(null != obj, "Identity not setup correctly");

            // Get the type of the object
            Type serverType = null;
            if(!RemotingServices.IsTransparentProxy(obj))
            {
                serverType = obj.GetType();
            }
            else
            {
                serverType = RemotingServices.GetRealProxy(obj).GetProxiedType();
            }

            Type typeOfObj = (null == requestedType ? serverType : requestedType);

            // Make sure that the server and requested types are compatible
            //  (except for objects that implement IMessageSink, since we 
            //   just hand off the message instead of invoking the proxy)
            if ((null != requestedType) &&
                !requestedType.IsAssignableFrom(serverType) &&
                (!typeof(IMessageSink).IsAssignableFrom(serverType)))
            {
                throw new RemotingException(
                    String.Format(
                        Environment.GetResourceString(
                            "Remoting_InvalidRequestedType"), 
                        requestedType.ToString())); ;
            }

            {
                RemotingTypeCachedData cache = (RemotingTypeCachedData)
                    InternalRemotingServices.GetReflectionCachedData(typeOfObj);
            
                TypeInfo = (IRemotingTypeInfo)cache.TypeInfo;
            }
    
            if (!idObj.IsWellKnown())
            {
                // Create the envoy info
                EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);

                // Create the channel info 
                ChannelInfo = (IChannelInfo)new ChannelInfo();

                if (serverType.HasProxyAttribute)
                {
                    SetHasProxyAttribute();
                }
            }
            else
            {
                SetWellKnown();
            }

            // See if we should and can use a url obj ref?
            if (ShouldUseUrlObjRef())
            {
                if (IsWellKnown())
                {
                    // full uri already supplied.
                    SetObjRefLite();
                }
                else
                {
                    String httpUri = ChannelServices.FindFirstHttpUrlForObject(URI);
                    if (httpUri != null)
                    {
                        URI = httpUri;
                        SetObjRefLite();
                    }
                }
            }
        } // Init
 internal static void SetEnvoyAndChannelSinks(Identity idObj, IMessageSink chnlSink, IMessageSink envoySink)
 {
     if ((idObj.ChannelSink == null) && (chnlSink != null))
     {
         idObj.RaceSetChannelSink(chnlSink);
     }
     if (idObj.EnvoyChain == null)
     {
         if (envoySink == null)
         {
             throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadInternalState_FailEnvoySink"), new object[0]));
         }
         idObj.RaceSetEnvoyChain(envoySink);
     }
 }
示例#21
0
        private static Identity SetIdentity(Identity idObj, string URI, DuplicateIdentityOption duplicateOption)
        {
            bool flag1 = idObj is ServerIdentity;

            if (idObj.URI == null)
            {
                idObj.SetOrCreateURI(URI);
                if (idObj.ObjectRef != null)
                {
                    idObj.ObjectRef.URI = idObj.URI;
                }
            }
            string str  = IdentityHolder.MakeURIKey(idObj.URI);
            object obj1 = IdentityHolder.URITable[(object)str];

            if (obj1 != null)
            {
                WeakReference weakReference = obj1 as WeakReference;
                Identity      identity;
                bool          flag2;
                if (weakReference != null)
                {
                    identity = (Identity)weakReference.Target;
                    flag2    = identity is ServerIdentity;
                }
                else
                {
                    identity = (Identity)obj1;
                    flag2    = identity is ServerIdentity;
                }
                if (identity != null && identity != idObj)
                {
                    if (duplicateOption != DuplicateIdentityOption.Unique)
                    {
                        if (duplicateOption == DuplicateIdentityOption.UseExisting)
                        {
                            idObj = identity;
                        }
                    }
                    else
                    {
                        throw new RemotingException(Environment.GetResourceString("Remoting_URIClash", (object)idObj.URI));
                    }
                }
                else if (weakReference != null)
                {
                    if (flag2)
                    {
                        IdentityHolder.URITable[(object)str] = (object)idObj;
                    }
                    else
                    {
                        weakReference.Target = (object)idObj;
                    }
                }
            }
            else
            {
                object obj2;
                if (flag1)
                {
                    obj2 = (object)idObj;
                    ((ServerIdentity)idObj).SetHandle();
                }
                else
                {
                    obj2 = (object)new WeakReference((object)idObj);
                }
                IdentityHolder.URITable.Add((object)str, obj2);
                idObj.SetInIDTable();
                ++IdentityHolder.SetIDCount;
                if (IdentityHolder.SetIDCount % 64 == 0)
                {
                    IdentityHolder.CleanupIdentities((object)null);
                }
            }
            return(idObj);
        }
 private static MarshalByRefObject SetOrCreateProxy(Identity idObj, Type classToProxy, object proxy)
 {
     RealProxy realProxy = null;
     if (proxy == null)
     {
         ServerIdentity identity = idObj as ServerIdentity;
         if (idObj.ObjectRef != null)
         {
             realProxy = ActivationServices.GetProxyAttribute(classToProxy).CreateProxy(idObj.ObjectRef, classToProxy, null, null);
         }
         if (realProxy == null)
         {
             realProxy = ActivationServices.DefaultProxyAttribute.CreateProxy(idObj.ObjectRef, classToProxy, null, (identity == null) ? null : identity.ServerContext);
         }
     }
     else
     {
         realProxy = GetRealProxy(proxy);
     }
     realProxy.IdentityObject = idObj;
     proxy = realProxy.GetTransparentProxy();
     proxy = idObj.RaceSetTransparentProxy(proxy);
     return (MarshalByRefObject) proxy;
 }
示例#23
0
        internal IMessageSink GetServerObjectChain(out MarshalByRefObject obj)
        {
            obj = null;
            // NOTE: Lifetime relies on the Identity flags for
            // SingleCall and Singleton being set by the time this getter
            // is called.
            if (!this.IsSingleCall())
            {
                // This is the common case
                if (_serverObjectChain == null)
                {
                    bool fLocked = false;
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try
                    {
                        Monitor.ReliableEnter(this, ref fLocked);
                        if (_serverObjectChain == null)
                        {
                            MarshalByRefObject srvObj =
                                (MarshalByRefObject)
                                this.TPOrObject;

                            _serverObjectChain =
                                _srvCtx.CreateServerObjectChain(
                                    srvObj);
                        }
                    }
                    finally
                    {
                        if (fLocked)
                        {
                            Monitor.Exit(this);
                        }
                    }
                }
                BCLDebug.Assert(null != _serverObjectChain,
                                "null != _serverObjectChain");

                return(_serverObjectChain);
            }
            else
            {
                // ---------- SINGLE CALL WKO --------------
                // In this case, we are expected to provide
                // a fresh server object for each dispatch.
                // Since the server object chain is object
                // specific, we must create a fresh chain too.

                // We must be in the correct context for this
                // to succeed.


                BCLDebug.Assert(Thread.CurrentContext == _srvCtx,
                                "Bad context mismatch");

                MarshalByRefObject srvObj   = null;
                IMessageSink       objChain = null;
                if (_tpOrObject != null && _firstCallDispatched == 0 && Interlocked.CompareExchange(ref _firstCallDispatched, 1, 0) == 0)
                {
                    // use the instance of server object created to
                    // set up the pipeline.
                    srvObj = (MarshalByRefObject)_tpOrObject;

                    objChain = _serverObjectChain;

                    if (objChain == null)
                    {
                        objChain = _srvCtx.CreateServerObjectChain(srvObj);
                    }
                }
                else
                {
                    // For singleCall we create a fresh object & its chain
                    // on each dispatch!
                    srvObj = (MarshalByRefObject)
                             Activator.CreateInstance((Type)_srvType, true);

                    // make sure that object didn't Marshal itself.
                    // (well known objects should live up to their promise
                    // of exporting themselves through exactly one url)
                    String tempUri = RemotingServices.GetObjectUri(srvObj);
                    if (tempUri != null)
                    {
                        throw new RemotingException(
                                  String.Format(
                                      CultureInfo.CurrentCulture, Environment.GetResourceString(
                                          "Remoting_WellKnown_CtorCantMarshal"),
                                      this.URI));
                    }

                    // Set the identity depending on whether we have the server
                    // or proxy
                    if (!RemotingServices.IsTransparentProxy(srvObj))
                    {
#if _DEBUG
                        Identity idObj = srvObj.__RaceSetServerIdentity(this);
#else
                        srvObj.__RaceSetServerIdentity(this);
#endif
#if _DEBUG
                        BCLDebug.Assert(idObj == this, "Bad ID state!");
                        BCLDebug.Assert(idObj == MarshalByRefObject.GetIdentity(srvObj), "Bad ID state!");
#endif
                    }
                    else
                    {
                        RealProxy rp = null;
                        rp = RemotingServices.GetRealProxy(srvObj);
                        BCLDebug.Assert(null != rp, "null != rp");
                        //  #if _DEBUG
                        //                      Identity idObj = (ServerIdentity) rp.SetIdentity(this);
                        // #else
                        rp.IdentityObject = this;
                        // #endif
                    }
                    // Create the object chain and return it
                    objChain = _srvCtx.CreateServerObjectChain(srvObj);
                }

                // This is passed out to the caller so that for single-call
                // case we can call Dispose when the incoming call is done
                obj = srvObj;
                return(objChain);
            }
        }
 private static object GetOrCreateProxy(Type classToProxy, Identity idObj)
 {
     object tPOrObject = idObj.TPOrObject;
     if (tPOrObject == null)
     {
         tPOrObject = SetOrCreateProxy(idObj, classToProxy, null);
     }
     ServerIdentity identity = idObj as ServerIdentity;
     if (identity != null)
     {
         Type serverType = identity.ServerType;
         if (!classToProxy.IsAssignableFrom(serverType))
         {
             throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidCast_FromTo"), new object[] { serverType.FullName, classToProxy.FullName }));
         }
     }
     return tPOrObject;
 }
示例#25
0
文件: message.cs 项目: ArildF/masters
        // NOTE: This method is called multiple times as we reuse the
        // message object. Make sure that you reset any fields that you
        // add to the message object to the default values. This will
        // ensure that the reused message object starts with the correct
        // values.
        internal void InitFields(MessageData msgData)
        {
            _frame = msgData.pFrame;
            _delegateMD = msgData.pDelegateMD;
            _methodDesc = msgData.pMethodDesc;
            _last = -1;
            _flags = msgData.iFlags;
            _initDone = true;
            _metaSigHolder = msgData.pSig;

            _MethodName = null;
            _MethodSignature = null;
            _MethodBase = null;
            _URI = null;
            _Fault = null;
            _ID = null;
            _srvID = null;
            _callContext = null;

            if (_properties != null)
            {
                // A dictionary object already exists. This case occurs
                // when we reuse the message object. Just remove all the
                // entries from the dictionary object and reuse it.
                ((IDictionary)_properties).Clear();
            }
            
        }
 private static object GetOrCreateProxy(Identity idObj, object proxy, bool fRefine)
 {
     if (proxy == null)
     {
         Type serverType;
         ServerIdentity identity = idObj as ServerIdentity;
         if (identity != null)
         {
             serverType = identity.ServerType;
         }
         else
         {
             IRemotingTypeInfo typeInfo = idObj.ObjectRef.TypeInfo;
             serverType = null;
             if (((typeInfo is TypeInfo) && !fRefine) || (typeInfo == null))
             {
                 serverType = typeof(MarshalByRefObject);
             }
             else
             {
                 string typeName = typeInfo.TypeName;
                 if (typeName != null)
                 {
                     string str2 = null;
                     string assemName = null;
                     TypeInfo.ParseTypeAndAssembly(typeName, out str2, out assemName);
                     Assembly assembly = FormatterServices.LoadAssemblyFromStringNoThrow(assemName);
                     if (assembly != null)
                     {
                         serverType = assembly.GetType(str2, false, false);
                     }
                 }
             }
             if (null == serverType)
             {
                 throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), new object[] { typeInfo.TypeName }));
             }
         }
         proxy = SetOrCreateProxy(idObj, serverType, null);
     }
     else
     {
         proxy = SetOrCreateProxy(idObj, null, proxy);
     }
     if (proxy == null)
     {
         throw new RemotingException(Environment.GetResourceString("Remoting_UnexpectedNullTP"));
     }
     return proxy;
 }
示例#27
0
文件: message.cs 项目: ArildF/masters
        } // TransitionCall


        // This constructor should be used for cross appdomain case.
        internal TransitionCall(IntPtr targetCtxID, IntPtr privateData, int targetDomainID)
            {
            BCLDebug.Assert(targetCtxID != IntPtr.Zero, "bad target ctx for call back");
            BCLDebug.Assert(targetDomainID != 0, "bad target ctx for call back");

            _sourceCtxID = Thread.CurrentContext.InternalContextID;
            _targetCtxID = targetCtxID;
            _delegate = null;
            _targetDomainID = targetDomainID;
            _privateData = privateData;
            

            // In the cross domain case, the client side just has a base Identity
            // and the server domain has the Server identity. We fault in the latter
            // when requested later.

                // We are going to a context in another app domain
                _srvID = null;
                _ID = new Identity("TransitionCallURI", null);

                // Create the data needed for the channel sink creation
            CrossAppDomainData data = 
                new CrossAppDomainData(_targetCtxID,
                                       _targetDomainID,
                                       Identity.ProcessGuid);
                String unUsed;
                IMessageSink channelSink =
                CrossAppDomainChannel.AppDomainChannel.CreateMessageSink(
                                                        null, //uri
                                                        data, //channelData
                                                        out unUsed);//out objURI

                BCLDebug.Assert(channelSink != null, "X-domain transition failure");
                _ID.RaceSetChannelSink(channelSink);
        } // TransitionCall
 internal void InitFields(MessageData msgData)
 {
     this._frame = msgData.pFrame;
     this._delegateMD = msgData.pDelegateMD;
     this._methodDesc = msgData.pMethodDesc;
     this._flags = msgData.iFlags;
     this._initDone = true;
     this._metaSigHolder = msgData.pSig;
     this._governingType = msgData.thGoverningType;
     this._MethodName = null;
     this._MethodSignature = null;
     this._MethodBase = null;
     this._URI = null;
     this._Fault = null;
     this._ID = null;
     this._srvID = null;
     this._callContext = null;
     if (this._properties != null)
     {
         ((IDictionary) this._properties).Clear();
     }
 }
示例#29
0
        //
        //
        // This is typically called when we are unmarshaling an objectref
        // in order to create a client side identity for a remote server
        // object.
        internal static Identity FindOrCreateIdentity(
            String objURI, String URL, ObjRef objectRef)
        {
            Identity idObj = null;
            bool bLock = false;

            BCLDebug.Assert(null != objURI,"null != objURI");

            try
            {                
                bool bWellKnown = (URL != null);

                // Lookup the object in the identity table
                // for well-known objects we user the URL
                // as the hash-key (instead of just the objUri)
                idObj = ResolveIdentity(bWellKnown ? URL : objURI);
                if (bWellKnown &&
                    (idObj != null) &&
                    (idObj is ServerIdentity))
                {
                    // We are trying to do a connect to a server wellknown object.
                    throw new RemotingException(
                        String.Format(
                            Environment.GetResourceString(
                                "Remoting_WellKnown_CantDirectlyConnect"),
                            URL));                            
                }
                     
                if (null == idObj)
                {
                    // There is no entry for this uri in the IdTable.
                    Message.DebugOut("RemotingService::FindOrCreateIdentity: Creating Identity\n");

                    // This identity is being encountered for the first time.
                    // We have to do the following things
                    // (1) Create an identity object for the proxy
                    // (2) Add the identity to the identity table
                    // (3) Create a proxy for the object represented by the objref      
                    
                    // Create a new identity
                    //                        Identity should get only one string that is used for everything
                    idObj = new Identity(objURI, URL);                         

                    // Add it to the identity table
                    TableLock.AcquireWriterLock(INFINITE);
                    bLock = true; 

                    // SetIdentity will give the correct Id if we raced
                    // between the ResolveIdentity call above and now.
                    //   (we are unmarshaling, and the server should guarantee
                    //    that the uri is unique, so we will use an existing identity
                    //    in case of a race)
                    idObj = SetIdentity(idObj, null, DuplicateIdentityOption.UseExisting);

                    idObj.RaceSetObjRef(objectRef);
                }
                else
                {
                    Message.DebugOut("RemotingService::FindOrCreateIdentity: Found Identity!\n");
                }
            }
            finally
            {
                if (bLock)
                {
                    TableLock.ReleaseWriterLock();
                }                
            }
            BCLDebug.Assert(null != idObj,"null != idObj");
            return idObj;                
        }
示例#30
0
        [System.Security.SecurityCritical]  // auto-generated
        internal void Init(Object o, Identity idObj, RuntimeType requestedType)
        {
            Message.DebugOut("RemotingServices::FillObjRef: IN");
            BCLDebug.Assert(idObj != null, "idObj != null");

            // Set the URI of the object to be marshaled
            uri = idObj.URI;

            // Figure out the type
            MarshalByRefObject obj = idObj.TPOrObject;

            BCLDebug.Assert(null != obj, "Identity not setup correctly");

            // Get the type of the object
            RuntimeType serverType = null;

            if (!RemotingServices.IsTransparentProxy(obj))
            {
                serverType = (RuntimeType)obj.GetType();
            }
            else
            {
                serverType = (RuntimeType)RemotingServices.GetRealProxy(obj).GetProxiedType();
            }

            RuntimeType typeOfObj = (null == requestedType ? serverType : requestedType);

            // Make sure that the server and requested types are compatible
            //  (except for objects that implement IMessageSink, since we
            //   just hand off the message instead of invoking the proxy)
            if ((null != requestedType) &&
                !requestedType.IsAssignableFrom(serverType) &&
                (!typeof(IMessageSink).IsAssignableFrom(serverType)))
            {
                throw new RemotingException(
                          String.Format(
                              CultureInfo.CurrentCulture, Environment.GetResourceString(
                                  "Remoting_InvalidRequestedType"),
                              requestedType.ToString()));;
            }

            // Create the type info
            if (serverType.IsCOMObject)
            {
                // __ComObjects need dynamic TypeInfo
                DynamicTypeInfo dt = new DynamicTypeInfo(typeOfObj);
                TypeInfo = (IRemotingTypeInfo)dt;
            }
            else
            {
                RemotingTypeCachedData cache = (RemotingTypeCachedData)
                                               InternalRemotingServices.GetReflectionCachedData(typeOfObj);

                TypeInfo = (IRemotingTypeInfo)cache.TypeInfo;
            }

            if (!idObj.IsWellKnown())
            {
                // Create the envoy info
                EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);

                // Create the channel info
                IChannelInfo chan = (IChannelInfo) new ChannelInfo();
                // Make sure the channelInfo only has x-appdomain data since the objref is agile while other
                // channelData might not be and regardless this data is useless for an appdomain proxy
                if (o is AppDomain)
                {
                    Object[] channelData       = chan.ChannelData;
                    int      channelDataLength = channelData.Length;
                    Object[] newChannelData    = new Object[channelDataLength];
                    // Clone the data so that we dont Microsoft the current appdomain data which is stored
                    // as a static
                    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();
            }

            // See if we should and can use a url obj ref?
            if (ShouldUseUrlObjRef())
            {
                if (IsWellKnown())
                {
                    // full uri already supplied.
                    SetObjRefLite();
                }
                else
                {
                    String httpUri = ChannelServices.FindFirstHttpUrlForObject(URI);
                    if (httpUri != null)
                    {
                        URI = httpUri;
                        SetObjRefLite();
                    }
                }
            }
        } // Init
示例#31
0
        [System.Security.SecurityCritical]  // auto-generated
        internal void Init(Object o, Identity idObj, RuntimeType requestedType) 
        { 
            Message.DebugOut("RemotingServices::FillObjRef: IN");
            BCLDebug.Assert(idObj != null,"idObj != null"); 

            // Set the URI of the object to be marshaled
            uri = idObj.URI;
 
            // Figure out the type
            MarshalByRefObject obj = idObj.TPOrObject; 
            BCLDebug.Assert(null != obj, "Identity not setup correctly"); 

            // Get the type of the object 
            RuntimeType serverType = null;
            if(!RemotingServices.IsTransparentProxy(obj))
            {
                serverType = (RuntimeType)obj.GetType(); 
            }
            else 
            { 
                serverType = (RuntimeType)RemotingServices.GetRealProxy(obj).GetProxiedType();
            } 

            RuntimeType typeOfObj = (null == requestedType ? serverType : requestedType);

            // Make sure that the server and requested types are compatible 
            //  (except for objects that implement IMessageSink, since we
            //   just hand off the message instead of invoking the proxy) 
            if ((null != requestedType) && 
                !requestedType.IsAssignableFrom(serverType) &&
                (!typeof(IMessageSink).IsAssignableFrom(serverType))) 
            {
                throw new RemotingException(
                    String.Format(
                        CultureInfo.CurrentCulture, Environment.GetResourceString( 
                            "Remoting_InvalidRequestedType"),
                        requestedType.ToString())); ; 
            } 

#if FEATURE_COMINTEROP 
            // Create the type info
            if(serverType.IsCOMObject)
            {
                // __ComObjects need dynamic TypeInfo 
                DynamicTypeInfo dt = new DynamicTypeInfo(typeOfObj);
                TypeInfo = (IRemotingTypeInfo) dt; 
            } 
            else
#endif // FEATURE_COMINTEROP 
            {
                RemotingTypeCachedData cache = (RemotingTypeCachedData)
                    InternalRemotingServices.GetReflectionCachedData(typeOfObj);
 
                TypeInfo = (IRemotingTypeInfo)cache.TypeInfo;
            } 
 
            if (!idObj.IsWellKnown())
            { 
                // Create the envoy info
                EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);

                // Create the channel info 
                IChannelInfo chan = (IChannelInfo)new ChannelInfo();
                // Make sure the channelInfo only has x-appdomain data since the objref is agile while other 
                // channelData might not be and regardless this data is useless for an appdomain proxy 
                if (o is AppDomain){
                    Object[] channelData = chan.ChannelData; 
                    int channelDataLength = channelData.Length;
                    Object[] newChannelData = new Object[channelDataLength];
                    // Clone the data so that we dont [....] the current appdomain data which is stored
                    // as a static 
                    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(); 
            }
 
            // See if we should and can use a url obj ref? 
            if (ShouldUseUrlObjRef())
            { 
                if (IsWellKnown())
                {
                    // full uri already supplied.
                    SetObjRefLite(); 
                }
                else 
                { 
                    String httpUri = ChannelServices.FindFirstHttpUrlForObject(URI);
                    if (httpUri != null) 
                    {
                        URI = httpUri;
                        SetObjRefLite();
                    } 
                }
            } 
        } // Init 
 private static string MakeURIKey(string uri)
 {
     return(Identity.RemoveAppNameOrAppGuidIfNecessary(uri.ToLower(CultureInfo.InvariantCulture)));
 }
示例#33
0
		internal static void SetMessageTargetIdentity (IMessage msg, Identity ident)
		{
			if (msg is IInternalMessage) 
				((IInternalMessage)msg).TargetIdentity = ident;
		}
 private static string MakeURIKeyNoLower(string uri)
 {
     return(Identity.RemoveAppNameOrAppGuidIfNecessary(uri));
 }
示例#35
0
        [System.Security.SecurityCritical]  // auto-generated
        private static Object GetOrCreateProxy(Type classToProxy, Identity idObj) 
        { 
            Message.DebugOut("Entering GetOrCreateProxy for given class\n");
 
            Object proxy = idObj.TPOrObject;
            if (null == proxy)
            {
                // Create the  proxy 
                proxy = SetOrCreateProxy(idObj, classToProxy, null);
            } 
            // proxy from idObj may be non-null if we are doing a Connect 
            // under new XXX() ... also if we are connecting to a remote URL
            // which we previously connected. 

            // If we are in the same domain as the server object then we
            // can check for type compatibility of the proxy with the given
            // type. Otherwise, we will defer this check to method call time. 
            // If we do not do this now then we run the risk of returning
            // a proxy which is different from the type given. 
 
            // <
 



 
            ServerIdentity serverID = idObj as ServerIdentity;
            if (null != serverID) 
            { 
                // Check for type compatibility
                Type serverType = serverID.ServerType; 
                if (!classToProxy.IsAssignableFrom(serverType))
                {
                    throw new InvalidCastException(
                        String.Format( 
                            CultureInfo.CurrentCulture, Environment.GetResourceString(
                                "InvalidCast_FromTo"), 
                            serverType.FullName, 
                            classToProxy.FullName));
                } 

            }

            // At this point we should have a non-null transparent proxy 
            BCLDebug.Assert(null != proxy && IsTransparentProxy(proxy),"null != proxy && IsTransparentProxy(proxy)");
 
            Message.DebugOut("Leaving GetOrCreateProxy for given class\n"); 
            return proxy;
        } 
        private static Identity SetIdentity(Identity idObj, string URI, DuplicateIdentityOption duplicateOption)
        {
            bool flag = idObj is ServerIdentity;

            if (idObj.URI == null)
            {
                idObj.SetOrCreateURI(URI);
                if (idObj.ObjectRef != null)
                {
                    idObj.ObjectRef.URI = idObj.URI;
                }
            }
            string key  = MakeURIKey(idObj.URI);
            object obj2 = URITable[key];

            if (obj2 != null)
            {
                bool          flag2;
                WeakReference reference = obj2 as WeakReference;
                Identity      target    = null;
                if (reference != null)
                {
                    target = (Identity)reference.Target;
                    flag2  = target is ServerIdentity;
                }
                else
                {
                    target = (Identity)obj2;
                    flag2  = target is ServerIdentity;
                }
                if ((target != null) && (target != idObj))
                {
                    switch (duplicateOption)
                    {
                    case DuplicateIdentityOption.Unique:
                    {
                        string uRI = idObj.URI;
                        throw new RemotingException(Environment.GetResourceString("Remoting_URIClash", new object[] { uRI }));
                    }

                    case DuplicateIdentityOption.UseExisting:
                        idObj = target;
                        return(idObj);
                    }
                    return(idObj);
                }
                if (reference != null)
                {
                    if (flag2)
                    {
                        URITable[key] = idObj;
                        return(idObj);
                    }
                    reference.Target = idObj;
                }
                return(idObj);
            }
            object obj3 = null;

            if (flag)
            {
                obj3 = idObj;
                ((ServerIdentity)idObj).SetHandle();
            }
            else
            {
                obj3 = new WeakReference(idObj);
            }
            URITable.Add(key, obj3);
            idObj.SetInIDTable();
            SetIDCount++;
            if ((SetIDCount % 0x40) == 0)
            {
                CleanupIdentities(null);
            }
            return(idObj);
        }
示例#37
0
        } // CreateChannelSinks
 
        internal static void SetEnvoyAndChannelSinks(Identity idObj,
                                                    IMessageSink chnlSink, 
                                                    IMessageSink envoySink) 
        {
            Message.DebugOut("Setting up envoy and channel sinks \n"); 
            BCLDebug.Assert(null != idObj,"null != idObj");

            // Decide if we need to set the envoy sink chain
            if (null == idObj.ChannelSink) 
            {
                if (null != chnlSink) 
                { 
                    idObj.RaceSetChannelSink(chnlSink);
                } 
            }

            // Decide if we need to set the envoy sink chain
            if (null == idObj.EnvoyChain) 
            {
                if (null != envoySink) 
                { 
                    idObj.RaceSetEnvoyChain(envoySink);
                } 
                else
                {
                    throw new RemotingException(
                        String.Format( 
                            CultureInfo.CurrentCulture, Environment.GetResourceString(
                                "Remoting_BadInternalState_FailEnvoySink"))); 
                } 
            }
        } 
示例#38
0
        internal IMessageSink GetServerObjectChain(out MarshalByRefObject obj)
        {
            obj = null;
            // NOTE: Lifetime relies on the Identity flags for
            // SingleCall and Singleton being set by the time this getter
            // is called.
            if (!this.IsSingleCall())
            {
                // This is the common case
                if (_serverObjectChain == null)
                {
                    lock (this)
                    {
                        if (_serverObjectChain == null)
                        {
                            MarshalByRefObject srvObj =
                                (MarshalByRefObject)
                                this.TPOrObject;

                            _serverObjectChain =
                                _srvCtx.CreateServerObjectChain(
                                    srvObj);
                        }
                    }
                }
                BCLDebug.Assert(null != _serverObjectChain,
                                "null != _serverObjectChain");

                return(_serverObjectChain);
            }
            else
            {
                // ---------- SINGLE CALL WKO --------------
                // In this case, we are expected to provide
                // a fresh server object for each dispatch.
                // Since the server object chain is object
                // specific, we must create a fresh chain too.

                // We must be in the correct context for this
                // to succeed.

                // NOTE: It may be useful to recycle the context too
                // but for V1 we will keep the context we setup for
                // the first object around and always dispatch the call
                // through that context ... when we are here we have
                // already been through the server sink chain. We will
                // only dispatch through the object sink chain of the
                // object we will create below ... if for some reason
                // the object creates its own context we will not go
                // through the server sink chain for that context again.
                // We should fix this in V-next and always create everything
                // from scratch for single-call wellKnown objects

                BCLDebug.Assert(Thread.CurrentContext == _srvCtx,
                                "Bad context mismatch");
                MarshalByRefObject srvObj   = null;
                IMessageSink       objChain = null;
                if (_tpOrObject != null && _firstCallDispatched == 0 && Interlocked.CompareExchange(ref _firstCallDispatched, 1, 0) == 0)
                {
                    // use the instance of server object created to
                    // set up the pipeline.
                    srvObj = (MarshalByRefObject)_tpOrObject;

                    objChain = _serverObjectChain;

                    if (objChain == null)
                    {
                        objChain = _srvCtx.CreateServerObjectChain(srvObj);
                    }
                }
                else
                {
                    // For singleCall we create a fresh object & its chain
                    // on each dispatch!
                    srvObj = (MarshalByRefObject)
                             Activator.CreateInstance((Type)_srvType, true);

                    // make sure that object didn't Marshal itself.
                    // (well known objects should live up to their promise
                    // of exporting themselves through exactly one url)
                    String tempUri = RemotingServices.GetObjectUri(srvObj);
                    if (tempUri != null)
                    {
                        throw new RemotingException(
                                  String.Format(
                                      Environment.GetResourceString(
                                          "Remoting_WellKnown_CtorCantMarshal"),
                                      this.URI));
                    }

                    // Set the identity depending on whether we have the server
                    // or proxy
                    if (!RemotingServices.IsTransparentProxy(srvObj))
                    {
#if _DEBUG
                        Identity idObj = srvObj.__RaceSetServerIdentity(this);
#else
                        srvObj.__RaceSetServerIdentity(this);
#endif
#if _DEBUG
                        BCLDebug.Assert(idObj == this, "Bad ID state!");
                        BCLDebug.Assert(idObj == MarshalByRefObject.GetIdentity(srvObj), "Bad ID state!");
#endif
                    }
                    else
                    {
                        RealProxy rp = null;
                        rp = RemotingServices.GetRealProxy(srvObj);
                        BCLDebug.Assert(null != rp, "null != rp");
                        //  #if _DEBUG
                        //                      Identity idObj = (ServerIdentity) rp.SetIdentity(this);
                        // #else
                        rp.IdentityObject = this;
                        // #endif
#if _DEBUG
                        // REVIEW: turn these on after adding a clean way to
                        // reset identity-s on x-context proxies ... (id needs
                        // to be set in the proxy && on the _serverObject in
                        // the proxy)
                        // BCLDebug.Assert(idObj == this, "Bad ID state!" );
                        // BCLDebug.Assert(idObj == rp.IdentityObject, "Bad ID state!" );
#endif
                    }
                    // Create the object chain and return it
                    objChain = _srvCtx.CreateServerObjectChain(srvObj);
                }

                // This is passed out to the caller so that for single-call
                // case we can call Dispose when the incoming call is done
                obj = srvObj;
                return(objChain);
            }
        }
示例#39
0
		internal RealProxy (Type classToProxy, ClientIdentity identity) : this(classToProxy, IntPtr.Zero, null)
		{
			_objectIdentity = identity;
		}
	// Set the URI to use to marshal an object.
	public static void SetObjectUriForMarshal
				(MarshalByRefObject obj, String uri)
			{
				if(obj == null)
				{
					return;
				}
				Identity id = obj.GetIdentity();
				if(id != null)
				{
					// Update the object's current identity.
					if(id.otherAppDomain)
					{
						throw new RemotingException(_("Remoting_NotLocal"));
					}
					if(id.uri != null)
					{
						throw new RemotingException(_("Remoting_HasIdentity"));
					}
					id.uri = uri;
				}
				else
				{
					// Create a new identity for the object and set it.
					id = new Identity();
					id.uri = uri;
					id.context = Thread.CurrentContext;
					id.otherAppDomain = false;
					obj.SetIdentity(id);
				}
			}
        [System.Security.SecurityCritical]  // auto-generated
        private static Identity SetIdentity(
            Identity idObj, String URI, DuplicateIdentityOption duplicateOption)
        {
            // NOTE: This function assumes that a lock has been taken 
            // by the calling function
            // idObj could be for a transparent proxy or a server object        
            Message.DebugOut("SetIdentity:: domainid: " + Thread.GetDomainID() + "\n");
            Contract.Assert(null != idObj,"null != idObj");
            
            // WriterLock must already be taken when SetIdentity is called!
            Contract.Assert(
                TableLock.IsWriterLockHeld,
                "Should have write-locked the ID Table!");

            // flag to denote that the id being set is a ServerIdentity
            bool bServerIDSet = idObj is ServerIdentity;
                
            if (null == idObj.URI)
            {
                // No URI has been associated with this identity. It must be a 
                // server identity getting marshaled out of the app domain for 
                // the first time.
                Contract.Assert(bServerIDSet,"idObj should be ServerIdentity");

                // Set the URI on the idObj (generating one if needed)
                idObj.SetOrCreateURI(URI);

                // If objectref is non-null make sure both have same URIs
                // (the URI in the objectRef could have potentially been reset
                // in a past external call to Disconnect()
                if (idObj.ObjectRef != null)
                {
                    idObj.ObjectRef.URI = idObj.URI;
                }
                Message.DebugOut("SetIdentity: Generated URI " + URI + " for identity");
            }

            // If we have come this far then there is no URI to identity
            // mapping present. Go ahead and create one.

            // ID should have a URI by now.
            Contract.Assert(null != idObj.URI,"null != idObj.URI");

            // See if this identity is already present in the Uri table
            String uriKey = MakeURIKey(idObj.URI);
            Object o = URITable[uriKey];

            // flag to denote that the id found in the table is a ServerIdentity
            bool bServerID;
            if (null != o)
            {
                // We found an identity (or a WeakRef to one) for the URI provided
                WeakReference wr = o as WeakReference;
                Identity idInTable = null;
                if (wr != null)
                {
                    // The object we found is a weak referece to an identity
                    
                    // This could be an identity for a client side
                    // proxy 
                    // OR
                    // a server identity which has been weakened since its life
                    // is over.
                    idInTable = (Identity) wr.Target;

                    bServerID = idInTable is ServerIdentity;

                    // If we find a weakRef for a ServerId we will be converting
                    // it to a strong one before releasing the IdTable lock.
                    Contract.Assert(
                        (idInTable == null)||
                        (!bServerID || idInTable.IsRemoteDisconnected()),
                        "Expect to find WeakRef only for remotely disconnected ids");
                    // We could find a weakRef to a client ID that does not 
                    // match the idObj .. but that is a handled ---- case 
                    // during Unmarshaling .. SetIdentity() will return the ID
                    // from the table to the caller.
                }
                else
                {
                    // We found a non-weak (strong) Identity for the URI
                    idInTable = (Identity) o;
                    bServerID = idInTable is ServerIdentity;

                    //We dont put strong refs to client "Identity"s in the table                    
                    Contract.Assert(
                        bServerID, 
                        "Found client side strong ID in the table");
                }

                if ((idInTable != null) && (idInTable != idObj))
                {
                    // We are trying to add another identity for the same URI
                    switch (duplicateOption)
                    {
                    
                    case DuplicateIdentityOption.Unique:
                    {
                        
                        String tempURI = idObj.URI;  

                        // Throw an exception to indicate the error since this could
                        // be caused by a user trying to marshal two objects with the same
                        // URI
                        throw new RemotingException(
                            Environment.GetResourceString("Remoting_URIClash",
                                tempURI));
                    } // case DuplicateIdentityOption.Unique
                    
                    case DuplicateIdentityOption.UseExisting:
                    {
                        // This would be a case where our thread lost the ----
                        // we will return the one found in the table
                        idObj = idInTable;
                        break;
                    } // case DuplicateIdentityOption.UseExisting:
                    
                    default:
                    {
                        Contract.Assert(false, "Invalid DuplicateIdentityOption");
                        break;
                    }
                    
                    } // switch (duplicateOption)
                    
                }
                else
                if (wr!=null)
                {                   
                    // We come here if we found a weakRef in the table but
                    // the target object had been cleaned up 
                    // OR
                    // If there was a weakRef in the table and the target
                    // object matches the idObj just passed in
                    
                    // Strengthen the entry if it a ServerIdentity.
                    if (bServerID)
                    {                       
                        URITable[uriKey] = idObj;
                    }
                    else
                    {
                        // For client IDs associate the table entry
                        // with the one passed in.
                        // (If target was null we would set it ... 
                        // if was non-null then it matches idObj anyway)
                        wr.Target = idObj;  
                    }
                }
            }
            else
            {
                // We did not find an identity entry for the URI
                Object addMe = null;
                if (bServerIDSet)
                {
                    addMe = idObj;
                    ((ServerIdentity)idObj).SetHandle();
                }
                else
                {
                    addMe = new WeakReference(idObj);
                }                    
                
                // Add the entry into the table
                URITable.Add(uriKey, addMe);
                idObj.SetInIDTable();
                
                // After every fixed number of set-id calls we run through
                // the table and cleanup if needed.             
                SetIDCount++;
                if (SetIDCount % CleanUpCountInterval == 0)
                {
                    // This should be called with the write lock held!
                    //   (which is why we assert that at the beginning of this
                    //    method)
                    CleanupIdentities(null);
                }

            }
            
            Message.DebugOut("SetIdentity:: Identity::URI: " + idObj.URI + "\n");       
            return idObj;
        }
 private static Identity SetIdentity(Identity idObj, string URI, DuplicateIdentityOption duplicateOption)
 {
     bool flag = idObj is ServerIdentity;
     if (idObj.URI == null)
     {
         idObj.SetOrCreateURI(URI);
         if (idObj.ObjectRef != null)
         {
             idObj.ObjectRef.URI = idObj.URI;
         }
     }
     string key = MakeURIKey(idObj.URI);
     object obj2 = URITable[key];
     if (obj2 != null)
     {
         bool flag2;
         WeakReference reference = obj2 as WeakReference;
         Identity target = null;
         if (reference != null)
         {
             target = (Identity) reference.Target;
             flag2 = target is ServerIdentity;
         }
         else
         {
             target = (Identity) obj2;
             flag2 = target is ServerIdentity;
         }
         if ((target != null) && (target != idObj))
         {
             switch (duplicateOption)
             {
                 case DuplicateIdentityOption.Unique:
                 {
                     string uRI = idObj.URI;
                     throw new RemotingException(Environment.GetResourceString("Remoting_URIClash", new object[] { uRI }));
                 }
                 case DuplicateIdentityOption.UseExisting:
                     idObj = target;
                     return idObj;
             }
             return idObj;
         }
         if (reference != null)
         {
             if (flag2)
             {
                 URITable[key] = idObj;
                 return idObj;
             }
             reference.Target = idObj;
         }
         return idObj;
     }
     object obj3 = null;
     if (flag)
     {
         obj3 = idObj;
         ((ServerIdentity) idObj).SetHandle();
     }
     else
     {
         obj3 = new WeakReference(idObj);
     }
     URITable.Add(key, obj3);
     idObj.SetInIDTable();
     SetIDCount++;
     if ((SetIDCount % 0x40) == 0)
     {
         CleanupIdentities(null);
     }
     return idObj;
 }