SecurityListenerSettingsLifetimeManager.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Security / SecurityListenerSettingsLifetimeManager.cs / 1 / SecurityListenerSettingsLifetimeManager.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

using System; 
using System.Threading;
using System.ServiceModel; 
using System.ServiceModel.Channels; 
using System.Collections.Generic;
 
namespace System.ServiceModel.Security
{
    class SecurityListenerSettingsLifetimeManager
    { 
        SecurityProtocolFactory securityProtocolFactory;
        SecuritySessionServerSettings sessionSettings; 
        bool sessionMode; 
        IChannelListener innerListener;
        int referenceCount; 

        public SecurityListenerSettingsLifetimeManager(SecurityProtocolFactory securityProtocolFactory, SecuritySessionServerSettings sessionSettings, bool sessionMode, IChannelListener innerListener)
        {
            this.securityProtocolFactory = securityProtocolFactory; 
            this.sessionSettings = sessionSettings;
            this.sessionMode = sessionMode; 
            this.innerListener = innerListener; 
            // have a reference right from the start so that the state can be aborted before open
            referenceCount = 1; 
        }

        public void Abort()
        { 
            if (Interlocked.Decrement(ref this.referenceCount) == 0)
            { 
                AbortCore(); 
            }
        } 

        public void AddReference()
        {
            Interlocked.Increment(ref this.referenceCount); 
        }
 
        public void Open(TimeSpan timeout) 
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); 
            if (this.securityProtocolFactory != null)
            {
                this.securityProtocolFactory.Open(false, timeoutHelper.RemainingTime());
            } 
            if (this.sessionMode && this.sessionSettings != null)
            { 
                this.sessionSettings.Open(timeoutHelper.RemainingTime()); 
            }
            this.innerListener.Open(timeoutHelper.RemainingTime()); 
        }

        public IAsyncResult BeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
        { 
            List beginOperations = new List(3);
            List endOperations = new List(3); 
            if (this.securityProtocolFactory != null) 
            {
                beginOperations.Add(new OperationWithTimeoutBeginCallback(this.BeginOpenSecurityProtocolFactory)); 
                endOperations.Add(new OperationEndCallback(this.EndOpenSecurityProtocolFactory));
            }
            if (this.sessionMode && this.sessionSettings != null)
            { 
                beginOperations.Add(new OperationWithTimeoutBeginCallback(this.sessionSettings.BeginOpen));
                endOperations.Add(new OperationEndCallback(this.sessionSettings.EndOpen)); 
            } 
            beginOperations.Add(new OperationWithTimeoutBeginCallback(this.innerListener.BeginOpen));
            endOperations.Add(new OperationEndCallback(this.innerListener.EndOpen)); 
            return OperationWithTimeoutComposer.BeginComposeAsyncOperations(timeout, beginOperations.ToArray(), endOperations.ToArray(), callback, state);
        }

        public void EndOpen(IAsyncResult result) 
        {
            OperationWithTimeoutComposer.EndComposeAsyncOperations(result); 
        } 

        IAsyncResult BeginOpenSecurityProtocolFactory(TimeSpan timeout, AsyncCallback callback, object state) 
        {
            return this.securityProtocolFactory.BeginOpen(false, timeout, callback, state);
        }
 
        void EndOpenSecurityProtocolFactory(IAsyncResult result)
        { 
            this.securityProtocolFactory.EndOpen(result); 
        }
 
        public void Close(TimeSpan timeout)
        {
            if (Interlocked.Decrement(ref this.referenceCount) == 0)
            { 
                TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
                bool throwing = true; 
                try 
                {
                    if (this.securityProtocolFactory != null) 
                    {
                        this.securityProtocolFactory.Close(false, timeoutHelper.RemainingTime());
                    }
                    if (sessionMode && sessionSettings != null) 
                    {
                        this.sessionSettings.Close(timeoutHelper.RemainingTime()); 
                    } 
                    this.innerListener.Close(timeoutHelper.RemainingTime());
                    throwing = false; 
                }
                finally
                {
                    if (throwing) 
                    {
                        AbortCore(); 
                    } 
                }
            } 
        }

        public IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state)
        { 
            if (Interlocked.Decrement(ref this.referenceCount) == 0)
            { 
                bool throwing = true; 
                try
                { 
                    List beginOperations = new List(3);
                    List endOperations = new List(3);
                    if (this.securityProtocolFactory != null)
                    { 
                        beginOperations.Add(new OperationWithTimeoutBeginCallback(this.securityProtocolFactory.BeginClose));
                        endOperations.Add(new OperationEndCallback(this.securityProtocolFactory.EndClose)); 
                    } 
                    if (this.sessionMode && this.sessionSettings != null)
                    { 
                        beginOperations.Add(new OperationWithTimeoutBeginCallback(this.sessionSettings.BeginClose));
                        endOperations.Add(new OperationEndCallback(this.sessionSettings.EndClose));
                    }
                    beginOperations.Add(new OperationWithTimeoutBeginCallback(this.innerListener.BeginClose)); 
                    endOperations.Add(new OperationEndCallback(this.innerListener.EndClose));
                    IAsyncResult result = OperationWithTimeoutComposer.BeginComposeAsyncOperations(timeout, beginOperations.ToArray(), endOperations.ToArray(), callback, state); 
                    throwing = false; 
                    return result;
                } 
                finally
                {
                    if (throwing)
                    { 
                        AbortCore();
                    } 
                } 
            }
            else 
            {
                return new DummyCloseAsyncResult(callback, state);
            }
        } 

        public void EndClose(IAsyncResult result) 
        { 
            if (result is DummyCloseAsyncResult)
            { 
                DummyCloseAsyncResult.End(result);
            }
            else
            { 
                bool throwing = true;
                try 
                { 
                    OperationWithTimeoutComposer.EndComposeAsyncOperations(result);
                    throwing = false; 
                }
                finally
                {
                    if (throwing) 
                    {
                        AbortCore(); 
                    } 
                }
            } 
        }

        void AbortCore()
        { 
            if (this.securityProtocolFactory != null)
            { 
                this.securityProtocolFactory.Close(true, TimeSpan.Zero); 
            }
            if (sessionMode && this.sessionSettings != null) 
            {
                this.sessionSettings.Abort();
            }
            this.innerListener.Abort(); 
        }
 
        class DummyCloseAsyncResult : CompletedAsyncResult 
        {
            public DummyCloseAsyncResult(AsyncCallback callback, object state) 
                : base(callback, state)
            {
            }
 
            new public static void End(IAsyncResult result)
            { 
                AsyncResult.End(result); 
            }
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK