コード例 #1
0
        internal static ServerIdentity FindOrCreateServerIdentity(MarshalByRefObject obj, string objURI, int flags)
        {
            ServerIdentity idObj = null;
            bool           flag;

            idObj = (ServerIdentity)MarshalByRefObject.GetIdentity(obj, out flag);
            if (idObj == null)
            {
                Context serverCtx = null;
                if (obj is ContextBoundObject)
                {
                    serverCtx = Thread.CurrentContext;
                }
                else
                {
                    serverCtx = DefaultContext;
                }
                ServerIdentity id = new ServerIdentity(obj, serverCtx);
                if (flag)
                {
                    idObj = obj.__RaceSetServerIdentity(id);
                }
                else
                {
                    RealProxy realProxy = null;
                    realProxy = RemotingServices.GetRealProxy(obj);
                    realProxy.IdentityObject = id;
                    idObj = (ServerIdentity)realProxy.IdentityObject;
                }
            }
            if (IdOps.bStrongIdentity(flags))
            {
                ReaderWriterLock tableLock = TableLock;
                bool             flag2     = !tableLock.IsWriterLockHeld;
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    if (flag2)
                    {
                        tableLock.AcquireWriterLock(0x7fffffff);
                    }
                    if ((idObj.ObjURI == null) || !idObj.IsInIDTable())
                    {
                        SetIdentity(idObj, objURI, DuplicateIdentityOption.Unique);
                    }
                    if (idObj.IsDisconnected())
                    {
                        idObj.SetFullyConnected();
                    }
                }
                finally
                {
                    if (flag2 && tableLock.IsWriterLockHeld)
                    {
                        tableLock.ReleaseWriterLock();
                    }
                }
            }
            return(idObj);
        }
コード例 #2
0
        internal IMessageSink GetServerObjectChain(out MarshalByRefObject obj)
        {
            obj = null;
            if (!this.IsSingleCall())
            {
                if (this._serverObjectChain == null)
                {
                    bool lockTaken = false;
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try
                    {
                        Monitor.Enter(this, ref lockTaken);
                        if (this._serverObjectChain == null)
                        {
                            MarshalByRefObject tPOrObject = base.TPOrObject;
                            this._serverObjectChain = this._srvCtx.CreateServerObjectChain(tPOrObject);
                        }
                    }
                    finally
                    {
                        if (lockTaken)
                        {
                            Monitor.Exit(this);
                        }
                    }
                }
                return(this._serverObjectChain);
            }
            MarshalByRefObject serverObj = null;
            IMessageSink       sink      = null;

            if (((base._tpOrObject != null) && (this._firstCallDispatched == 0)) && (Interlocked.CompareExchange(ref this._firstCallDispatched, 1, 0) == 0))
            {
                serverObj = (MarshalByRefObject)base._tpOrObject;
                sink      = this._serverObjectChain;
                if (sink == null)
                {
                    sink = this._srvCtx.CreateServerObjectChain(serverObj);
                }
            }
            else
            {
                serverObj = (MarshalByRefObject)Activator.CreateInstance(this._srvType, true);
                if (RemotingServices.GetObjectUri(serverObj) != null)
                {
                    throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_WellKnown_CtorCantMarshal"), new object[] { base.URI }));
                }
                if (!RemotingServices.IsTransparentProxy(serverObj))
                {
                    serverObj.__RaceSetServerIdentity(this);
                }
                else
                {
                    RemotingServices.GetRealProxy(serverObj).IdentityObject = this;
                }
                sink = this._srvCtx.CreateServerObjectChain(serverObj);
            }
            obj = serverObj;
            return(sink);
        }
コード例 #3
0
        internal static ServerIdentity FindOrCreateServerIdentity(MarshalByRefObject obj, string objURI, int flags)
        {
            bool           fServer;
            ServerIdentity serverIdentity1 = (ServerIdentity)MarshalByRefObject.GetIdentity(obj, out fServer);

            if (serverIdentity1 == null)
            {
                Context        serverCtx = !(obj is ContextBoundObject) ? IdentityHolder.DefaultContext : Thread.CurrentContext;
                ServerIdentity id        = new ServerIdentity(obj, serverCtx);
                if (fServer)
                {
                    serverIdentity1 = obj.__RaceSetServerIdentity(id);
                }
                else
                {
                    RealProxy      realProxy       = RemotingServices.GetRealProxy((object)obj);
                    ServerIdentity serverIdentity2 = id;
                    realProxy.IdentityObject = (Identity)serverIdentity2;
                    serverIdentity1          = (ServerIdentity)realProxy.IdentityObject;
                }
                if (IdOps.bIsInitializing(flags))
                {
                    serverIdentity1.IsInitializing = true;
                }
            }
            if (IdOps.bStrongIdentity(flags))
            {
                ReaderWriterLock tableLock = IdentityHolder.TableLock;
                bool             flag      = !tableLock.IsWriterLockHeld;
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    if (flag)
                    {
                        tableLock.AcquireWriterLock(int.MaxValue);
                    }
                    if (serverIdentity1.ObjURI == null || !serverIdentity1.IsInIDTable())
                    {
                        IdentityHolder.SetIdentity((Identity)serverIdentity1, objURI, DuplicateIdentityOption.Unique);
                    }
                    if (serverIdentity1.IsDisconnected())
                    {
                        serverIdentity1.SetFullyConnected();
                    }
                }
                finally
                {
                    if (flag && tableLock.IsWriterLockHeld)
                    {
                        tableLock.ReleaseWriterLock();
                    }
                }
            }
            return(serverIdentity1);
        }
コード例 #4
0
        } // CasualResolveReference

        //
        //
        // This is typically called when we need to create/establish
        // an identity for a serverObject.
        internal static ServerIdentity FindOrCreateServerIdentity(
            MarshalByRefObject obj, String objURI, int flags)
        {
            Message.DebugOut("Entered FindOrCreateServerIdentity \n");

            ServerIdentity srvID = null;
            bool           bLock = false;

            try
            {
                bool fServer;
                srvID = (ServerIdentity)MarshalByRefObject.GetIdentity(obj, out fServer);

                if (srvID == null)
                {
                    // Create a new server identity and add it to the
                    // table. IdentityHolder will take care of races
                    Context serverCtx = null;

                    if (obj is ContextBoundObject)
                    {
                        serverCtx = Thread.CurrentContext;
                    }
                    else
                    {
                        serverCtx = DefaultContext;
                    }
                    BCLDebug.Assert(null != serverCtx, "null != serverCtx");

                    ServerIdentity serverID = new ServerIdentity(obj, serverCtx);

                    // Set the identity depending on whether we have the server or proxy
                    if (fServer)
                    {
                        srvID = obj.__RaceSetServerIdentity(serverID);
                        BCLDebug.Assert(srvID == MarshalByRefObject.GetIdentity(obj), "Bad ID state!");
                    }
                    else
                    {
                        RealProxy rp = null;
                        rp = RemotingServices.GetRealProxy(obj);
                        BCLDebug.Assert(null != rp, "null != rp");

                        rp.IdentityObject = serverID;
                        srvID             = (ServerIdentity)rp.IdentityObject;
                    }

                    Message.DebugOut("Created ServerIdentity \n");
                }


                // NOTE: for purely x-context cases we never execute this ...
                // the server ID is not put in the ID table.
                if (IdOps.bStrongIdentity(flags))
                {
                    TableLock.AcquireWriterLock(INFINITE);
                    bLock = true;

                    // It is possible that we are marshaling out of this app-domain
                    // for the first time. We need to do two things
                    // (1) If there is no URI associated with the identity then go ahead
                    // and generate one.
                    // (2) Add the identity to the URI -> Identity map if not already present
                    // (For purely x-context cases we don't need the URI)
                    // (3) If the object ref is null, then this object hasn't been
                    // marshalled yet.
                    // (4) if id was created through SetObjectUriForMarshal, it would be
                    // in the ID table
                    if ((srvID.ObjURI == null) ||
                        (srvID.IsInIDTable() == false))
                    {
                        // we are marshalling a server object, so there should not be a
                        //   a different identity at this location.
                        SetIdentity(srvID, objURI, DuplicateIdentityOption.Unique);
                    }

                    // If the object is marked as disconnect, mark it as connected
                    if (srvID.IsDisconnected())
                    {
                        srvID.SetFullyConnected();
                    }
                }
            }
            finally
            {
                if (bLock)
                {
                    TableLock.ReleaseWriterLock();
                }
            }

            Message.DebugOut("Leaving FindOrCreateServerIdentity \n");
            BCLDebug.Assert(null != srvID, "null != srvID");
            return(srvID);
        }
コード例 #5
0
ファイル: serveridentity.cs プロジェクト: ydunk/masters
        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.
            // get
            // {
            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.


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

                // For singleCall we create a fresh object & its chain
                // on each dispatch!
                MarshalByRefObject 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
                }

                // 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;
                // Create the object chain and return it
                return(_srvCtx.CreateServerObjectChain(srvObj));
            }
            // }
        }
コード例 #6
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static ServerIdentity FindOrCreateServerIdentity(
            MarshalByRefObject obj, String objURI, int flags)
        {
            Message.DebugOut("Entered FindOrCreateServerIdentity \n");

            ServerIdentity srvID = null;

            bool fServer;

            srvID = (ServerIdentity)MarshalByRefObject.GetIdentity(obj, out fServer);

            if (srvID == null)
            {
                // Create a new server identity and add it to the
                // table. IdentityHolder will take care of ----s
                Context serverCtx = null;

                if (obj is ContextBoundObject)
                {
                    serverCtx = Thread.CurrentContext;
                }
                else
                {
                    serverCtx = DefaultContext;
                }
                Contract.Assert(null != serverCtx, "null != serverCtx");

                ServerIdentity serverID = new ServerIdentity(obj, serverCtx);

                // Set the identity depending on whether we have the server or proxy
                if (fServer)
                {
                    srvID = obj.__RaceSetServerIdentity(serverID);
                    Contract.Assert(srvID == MarshalByRefObject.GetIdentity(obj), "Bad ID state!");
                }
                else
                {
                    RealProxy rp = null;
                    rp = RemotingServices.GetRealProxy(obj);
                    Contract.Assert(null != rp, "null != rp");

                    rp.IdentityObject = serverID;
                    srvID             = (ServerIdentity)rp.IdentityObject;
                }

                // DevDiv 720951 and 911924:
                // CreateWellKnownObject creates a ServerIdentity and places it in URITable
                // before it is fully initialized.  This transient flag is set to to prevent
                // other concurrent operations from using it.  CreateWellKnownObject is the
                // only code path that sets this flag, and by default it is false.
                if (IdOps.bIsInitializing(flags))
                {
                    srvID.IsInitializing = true;
                }

                Message.DebugOut("Created ServerIdentity \n");
            }

#if false
            // Check that we are asked to create the identity for the same
            // URI as the one already associated with the server object.
            // It is an error to associate two URIs with the same server
            // object
            // GopalK: Try eliminating the test because it is also done by GetOrCreateIdentity
            if ((null != objURI) && (null != srvID.ObjURI))
            {
                if (string.Compare(objURI, srvID.ObjURI, StringComparison.OrdinalIgnoreCase) == 0) // case-insensitive compare
                {
                    Message.DebugOut("Trying to associate a URI with identity again .. throwing execption \n");
                    throw new RemotingException(
                              String.Format(
                                  Environment.GetResourceString(
                                      "Remoting_ResetURI"),
                                  srvID.ObjURI, objURI));
                }
            }
#endif

            // NOTE: for purely x-context cases we never execute this ...
            // the server ID is not put in the ID table.
            if (IdOps.bStrongIdentity(flags))
            {
                // 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
                {
                    if (takeAndRelease)
                    {
                        rwlock.AcquireWriterLock(INFINITE);
                    }

                    // It is possible that we are marshaling out of this app-domain
                    // for the first time. We need to do two things
                    // (1) If there is no URI associated with the identity then go ahead
                    // and generate one.
                    // (2) Add the identity to the URI -> Identity map if not already present
                    // (For purely x-context cases we don't need the URI)
                    // (3) If the object ref is null, then this object hasn't been
                    // marshalled yet.
                    // (4) if id was created through SetObjectUriForMarshal, it would be
                    // in the ID table
                    if ((srvID.ObjURI == null) ||
                        (srvID.IsInIDTable() == false))
                    {
                        // we are marshalling a server object, so there should not be a
                        //   a different identity at this location.
                        SetIdentity(srvID, objURI, DuplicateIdentityOption.Unique);
                    }

                    // If the object is marked as disconnect, mark it as connected
                    if (srvID.IsDisconnected())
                    {
                        srvID.SetFullyConnected();
                    }
                }
                finally
                {
                    if (takeAndRelease && rwlock.IsWriterLockHeld)
                    {
                        rwlock.ReleaseWriterLock();
                    }
                }
            }

            Message.DebugOut("Leaving FindOrCreateServerIdentity \n");
            Contract.Assert(null != srvID, "null != srvID");
            return(srvID);
        }
コード例 #7
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);
            }
        }