PluggableProtocol.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 / TransactionBridge / Microsoft / Transactions / Wsat / Protocol / PluggableProtocol.cs / 1 / PluggableProtocol.cs

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

// This file implements the class activated by the TM bridge in representation 
// of the WS-AT protocol
 
using System; 
using System.Diagnostics;
 
using Microsoft.Transactions.Bridge;
using Microsoft.Transactions.Wsat.InputOutput;
using Microsoft.Transactions.Wsat.Messaging;
 
namespace Microsoft.Transactions.Wsat.Protocol
{ 
    [Flags] 
    enum ProtocolVersion : ushort
    { 
        Version10 = 0x01,
        Version11 = 0x02,
    }
 
    static class ProtocolVersionHelper
    { 
        public static void AssertProtocolVersion(ProtocolVersion protocolVersion, Type type, string method) 
        {
            if (!Enum.IsDefined(typeof(ProtocolVersion), protocolVersion)) 
            {
                // We need to failfast if protocolVersion is not a defined ProtocolVersion
                // value because this indicate a serious incosistency
 
                DiagnosticUtility.FailFast("An invalid protocol version value was used in " + type + '.' + method);
            } 
        } 

        public static void AssertProtocolVersion10(ProtocolVersion protocolVersion, Type type, string method) 
        {
            if (protocolVersion != ProtocolVersion.Version10)
            {
                // Somehow we execute 1.0 operations in the context of the 
                // protocol version 1.1. This is a serious inconsistency.
                DiagnosticUtility.FailFast("Must use the protocol version 1.0 to execute " + type + '.' + method); 
            } 
        }
 
        public static void AssertProtocolVersion11(ProtocolVersion protocolVersion, Type type, string method)
        {
            if (protocolVersion != ProtocolVersion.Version11)
            { 
                // Somehow we execute 1.1 operations in the context of the
                // protocol version 1.0. This is a serious inconsistency. 
                DiagnosticUtility.FailFast("Must use the protocol version 1.1 to execute " + type + '.' + method); 
            }
        } 
    }

    abstract class PluggableProtocol : IProtocolProvider
    { 
        ProtocolProviderState protocolProviderState;
        protected ProtocolState state; 
        protected Guid protocolId; 
        protected string name;
        protected ProtocolVersion protocolVersion; 

        //
        // Construction
        // 

        public PluggableProtocol(Guid protocolId, string name, ProtocolVersion protocolVersion) 
        { 
            protocolProviderState = ProtocolProviderState.Uninitialized;
            this.protocolId = protocolId; 
            this.name = name;
            this.protocolVersion = protocolVersion;
        }
 
        //
        // IProtocolProvider implementation 
        // 

        public void Initialize(TransactionManager transactionManager) 
        {
            DebugTrace.TraceEnter(this, "Initialize");
            try
            { 
                this.state = new ProtocolState(transactionManager, this.protocolVersion);
                this.protocolProviderState = ProtocolProviderState.Initialized; 
 
                if (ProtocolInitializedRecord.ShouldTrace)
                { 
                    ProtocolInitializedRecord.Trace(this.protocolId,
                                                    this.name);
                }
            } 
            catch (Exception e)
            { 
                DebugTrace.Trace(TraceLevel.Error, "Could not initialize protocol: {0}", e); 

                ProtocolInitializationFailureRecord.TraceAndLog(this.protocolId, 
                                                                this.name,
                                                                e);
                throw;
            } 
            finally
            { 
                DebugTrace.TraceLeave(this, "Initialize"); 
            }
        } 

        public void Start()
        {
            DebugTrace.TraceEnter(this, "Start"); 
            try
            { 
                this.protocolProviderState = ProtocolProviderState.Starting; 
                this.state.Start();
                this.protocolProviderState = ProtocolProviderState.Started; 

                if (ProtocolStartedRecord.ShouldTrace)
                {
                    ProtocolStartedRecord.Trace(this.protocolId, 
                                                this.name);
                } 
            } 
            catch (Exception e)
            { 
                DebugTrace.Trace(TraceLevel.Error, "Could not start protocol: {0}", e);

                ProtocolStartFailureRecord.TraceAndLog(this.protocolId,
                                                       this.name, 
                                                       e);
                throw; 
            } 
            finally
            { 
                DebugTrace.TraceLeave(this, "Start");
            }
        }
 
        public void Stop()
        { 
            DebugTrace.TraceEnter(this, "Stop"); 
            try
            { 
                this.protocolProviderState = ProtocolProviderState.Stopping;
                this.state.Stop();
                this.protocolProviderState = ProtocolProviderState.Stopped;
 
                ProtocolStoppedRecord.TraceAndLog(this.protocolId,
                                                  this.name); 
            } 
            catch (Exception e)
            { 
                DebugTrace.Trace(TraceLevel.Error, "Could not stop protocol: {0}", e);

                ProtocolStopFailureRecord.TraceAndLog(this.protocolId,
                                                      this.name, 
                                                      e);
                throw; 
            } 
            finally
            { 
                DebugTrace.TraceLeave(this, "Stop");
            }
        }
 
        public IProtocolProviderCoordinatorService CoordinatorService
        { 
            get { return this.state.TransactionManagerReceive; } 
        }
 
        public IProtocolProviderPropagationService PropagationService
        {
            get { return this.state.TransactionManagerReceive; }
        } 

        public abstract Guid ProtocolId 
        { 
            get ;
        } 

        public ProtocolProviderState State
        {
            get { return this.protocolProviderState; } 
        }
 
        public uint MarshalCapabilities 
        {
            get { return (uint) ProtocolMarshalCapabilities.UseStaticProtocolInformation; } 
        }

        public abstract byte[] GetProtocolInformation();
 
        public static Guid Id(ProtocolVersion protocolVersion)
        { 
            ProtocolVersionHelper.AssertProtocolVersion(protocolVersion, typeof(PluggableProtocol), "ProtocolGuid"); //assert valid protocol version 

            switch (protocolVersion) 
            {
                case ProtocolVersion.Version10:
                    return PluggableProtocol10.ProtocolGuid;
 
                case ProtocolVersion.Version11:
                    return PluggableProtocol11.ProtocolGuid; 
 
                default:
                    return Guid.NewGuid(); // inaccessible path because we have asserted the protocol version 
            }
        }

        public static string Name(ProtocolVersion protocolVersion) 
        {
            ProtocolVersionHelper.AssertProtocolVersion(protocolVersion, typeof(PluggableProtocol), "Name"); //assert valid protocol version 
 
            switch (protocolVersion)
            { 
                case ProtocolVersion.Version10:
                    return PluggableProtocol10.ProtocolName;

                case ProtocolVersion.Version11: 
                    return PluggableProtocol11.ProtocolName;
 
                default: 
                    return null; // inaccessible path because we have asserted the protocol version
            } 
        }

    }
 

//========================================================================================= 
//                          PluggableProtocol implementations 
//=========================================================================================
    class PluggableProtocol10 : PluggableProtocol 
    {
        internal static readonly Guid ProtocolGuid = new Guid ("cc228cf4-a9c8-43fc-8281-8565eb5889f2");
        internal const string ProtocolName = "WS-AtomicTransaction 1.0";
 
        public PluggableProtocol10() : base(ProtocolGuid, ProtocolName, ProtocolVersion.Version10) {}
 
        public override Guid ProtocolId 
        {
            get { return ProtocolGuid; } 
        }

        public override byte[] GetProtocolInformation()
        { 
            DebugTrace.TraceEnter(this, "GetProtocolInformation");
 
            byte[] info = null; 
            if (state.Config.NetworkEndpointsEnabled)
            { 
                DebugTrace.Trace(TraceLevel.Verbose, "Generating protocol information");

                ProtocolInformationWriter writer = new ProtocolInformationWriter(this.state);
                info = writer.GetProtocolInformation(); 
            }
            else 
            { 
                DebugTrace.Trace(TraceLevel.Verbose, "Generating null protocol information");
            } 

            DebugTrace.TraceLeave (this, "GetProtocolInformation");
            return info;
        } 
    }
 
    class PluggableProtocol11 : PluggableProtocol 
    {
        internal static readonly Guid ProtocolGuid = new Guid ("c05b9cad-ab24-4bb3-9440-3548fa7b4b1b"); 
        internal const string ProtocolName = "WS-AtomicTransaction 1.1";

        public PluggableProtocol11() : base(ProtocolGuid, ProtocolName, ProtocolVersion.Version11) {}
 
        public override Guid ProtocolId
        { 
            get { return ProtocolGuid; } 
        }
 
        public override byte[] GetProtocolInformation()
        {
            DebugTrace.TraceEnter(this, "GetProtocolInformation");
            // no info needs to be returned as all v1.1 info can be derived from the v1.0 info 
            DebugTrace.TraceLeave (this, "GetProtocolInformation");
            return null; 
        } 
    }
} 


// 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