WhereaboutsReader.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 / Transactions / WhereaboutsReader.cs / 1 / WhereaboutsReader.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------
namespace System.ServiceModel.Transactions
{ 
    using System;
    using System.IO; 
    using System.Net; 
    using System.Text;
 
    using Microsoft.Transactions.Wsat.Protocol;
    using Microsoft.Transactions.Wsat.Recovery;

    class WhereaboutsReader 
    {
        string hostName; 
        ProtocolInformationReader protocolInfo; 

        // Whereabouts internals 
        static Guid GuidWhereaboutsInfo = new Guid("{2adb4462-bd41-11d0-b12e-00c04fc2f3ef}");
        const long STmToTmProtocolSize = 4 + 4;

        enum TmProtocol 
        {
            TmProtocolNone     = 0, 
            TmProtocolTip      = 1, 
            TmProtocolMsdtcV1  = 2,
            TmProtocolMsdtcV2  = 3, // unicode host names in nameobject blobs etc 
            TmProtocolExtended = 4  // other stuff (e.g., WS-AT)	
        }

        public WhereaboutsReader(byte[] whereabouts) 
        {
            MemoryStream mem = new MemoryStream(whereabouts, 
                                                0, 
                                                whereabouts.Length,
                                                false, 
                                                true); // Enable calls to GetBuffer()
            DeserializeWhereabouts(mem);
        }
 
        public string HostName
        { 
            get { return this.hostName; } 
        }
 
        public ProtocolInformationReader ProtocolInformation
        {
            get { return this.protocolInfo; }
        } 

        void DeserializeWhereabouts(MemoryStream mem) 
        { 
            // guidSignature
            Guid signature = SerializationUtils.ReadGuid(mem); 
            if (signature != GuidWhereaboutsInfo)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new SerializationException(SR.GetString(SR.WhereaboutsSignatureMissing))); 
            }
 
            // cTmToTmProtocols 
            uint cTmToTmProtocols = SerializationUtils.ReadUInt(mem);
 
            // Make sure that cTmToTmProtocols is at least plausible
            if (cTmToTmProtocols * STmToTmProtocolSize > mem.Length - mem.Position)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                    new SerializationException(SR.GetString(SR.WhereaboutsImplausibleProtocolCount)));
            } 
 
            // Loop through each protocol
            for (uint i = 0; i < cTmToTmProtocols; i++) 
            {
                DeserializeWhereaboutsProtocol(mem);
            }
 
            // Require a host name
            if (string.IsNullOrEmpty(this.hostName)) 
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new SerializationException(SR.GetString(SR.WhereaboutsNoHostName))); 
            }
        }

        void DeserializeWhereaboutsProtocol(MemoryStream mem) 
        {
            // tmprotDescribed 
            TmProtocol tmprotDescribed = (TmProtocol)SerializationUtils.ReadInt(mem); 

            // cbTmProtocolData 
            uint cbTmProtocolData = SerializationUtils.ReadUInt(mem);

            switch (tmprotDescribed)
            { 
                case TmProtocol.TmProtocolMsdtcV2:
                    ReadMsdtcV2Protocol(mem, cbTmProtocolData); 
                    break; 

                case TmProtocol.TmProtocolExtended: 
                    ReadExtendedProtocol(mem, cbTmProtocolData);
                    break;

                default: 
                    // We don't care about this protocol
                    SerializationUtils.IncrementPosition(mem, cbTmProtocolData); 
                    break; 
            }
 
            // Align the cursor to a 4-byte boundary
            SerializationUtils.AlignPosition(mem, 4);
        }
 
        void ReadMsdtcV2Protocol(MemoryStream mem, uint cbTmProtocolData)
        { 
            const int MaxComputerName = 15; 

            // 
            // The host name is encoded in unicode format
            // It is followed by a null-terminating unicode character,
            // plus some padding to align on 4
            // 

            // Reject host names of disproportionate size 
            if (cbTmProtocolData > (MaxComputerName + 1) * 2) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                    new SerializationException(SR.GetString(SR.WhereaboutsImplausibleHostNameByteCount)));
            }

            byte[] chars = SerializationUtils.ReadBytes(mem, (int)cbTmProtocolData); 

            // Count the bytes until the first null terminating character 
            int cbString = 0; 
            while (cbString < cbTmProtocolData - 1 &&
                  (chars[cbString] != 0 || chars[cbString + 1] != 0)) 
            {
                cbString += 2;
            }
 
            if (cbString == 0)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                    new SerializationException(SR.GetString(SR.WhereaboutsInvalidHostName)));
            } 

            try
            {
                this.hostName = Encoding.Unicode.GetString(chars, 0, cbString); 
            }
            catch (ArgumentException e) 
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new SerializationException(SR.GetString(SR.WhereaboutsInvalidHostName), e)); 
            }
        }

        void ReadExtendedProtocol(MemoryStream mem, uint cbTmProtocolData) 
        {
            // Read the WSAT1.0 protoocol identifier 
            Guid guid = SerializationUtils.ReadGuid(mem); 
            if ( guid == PluggableProtocol10.ProtocolGuid || guid == PluggableProtocol11.ProtocolGuid)
            { 
                // This is the WS-AT extended whereabouts blob
                this.protocolInfo = new ProtocolInformationReader(mem);
            }
            else 
            {
                // Some other gateway protocol... Skip the rest of the data 
                SerializationUtils.IncrementPosition(mem, cbTmProtocolData - 16); 
            }
        } 
    }
}

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