StreamedFramingRequestChannel.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 / Channels / StreamedFramingRequestChannel.cs / 1 / StreamedFramingRequestChannel.cs

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

namespace System.ServiceModel.Channels 
{
    using System.Collections.Generic; 
    using System.ServiceModel; 
    using System.Diagnostics;
    using System.IO; 
    using System.ServiceModel.Diagnostics;
    using System.ServiceModel.Security;
    using System.Threading;
 
    class StreamedFramingRequestChannel : RequestChannel
    { 
        IConnectionInitiator connectionInitiator; 
        ConnectionPool connectionPool;
        MessageEncoder messageEncoder; 
        IConnectionOrientedTransportFactorySettings settings;
        byte[] startBytes;
        StreamUpgradeProvider upgrade;
 
        public StreamedFramingRequestChannel(ChannelManagerBase factory, IConnectionOrientedTransportChannelFactorySettings settings,
            EndpointAddress remoteAddresss, Uri via, IConnectionInitiator connectionInitiator, ConnectionPool connectionPool) 
            : base(factory, remoteAddresss, via, settings.ManualAddressing) 
        {
            this.settings = settings; 
            this.connectionInitiator = connectionInitiator;
            this.connectionPool = connectionPool;

            this.messageEncoder = settings.MessageEncoderFactory.Encoder; 
            this.upgrade = settings.Upgrade;
        } 
 
        byte[] Preamble
        { 
            get { return this.startBytes; }
        }

        protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state) 
        {
            return new CompletedAsyncResult(callback, state); 
        } 

        protected override void OnEndOpen(IAsyncResult result) 
        {
            CompletedAsyncResult.End(result);
        }
 
        protected override void OnOpen(TimeSpan timeout)
        { 
        } 

        protected override void OnOpened() 
        {
            // setup our preamble which we'll use for all connections we establish
            EncodedVia encodedVia = new EncodedVia(this.Via.AbsoluteUri);
            EncodedContentType encodedContentType = EncodedContentType.Create(settings.MessageEncoderFactory.Encoder.ContentType); 
            int startSize = ClientSingletonEncoder.ModeBytes.Length + ClientSingletonEncoder.CalcStartSize(encodedVia, encodedContentType);
            int preambleEndOffset = 0; 
            if (this.upgrade == null) 
            {
                preambleEndOffset = startSize; 
                startSize += ClientDuplexEncoder.PreambleEndBytes.Length;
            }
            this.startBytes = DiagnosticUtility.Utility.AllocateByteArray(startSize);
            Buffer.BlockCopy(ClientSingletonEncoder.ModeBytes, 0, startBytes, 0, ClientSingletonEncoder.ModeBytes.Length); 
            ClientSingletonEncoder.EncodeStart(this.startBytes, ClientSingletonEncoder.ModeBytes.Length, encodedVia, encodedContentType);
            if (preambleEndOffset > 0) 
            { 
                Buffer.BlockCopy(ClientSingletonEncoder.PreambleEndBytes, 0, startBytes, preambleEndOffset, ClientSingletonEncoder.PreambleEndBytes.Length);
            } 

            // and then transition to the Opened state
            base.OnOpened();
        } 

        protected override IAsyncRequest CreateAsyncRequest(Message message, AsyncCallback callback, object state) 
        { 
            return new StreamedFramingAsyncRequest(this, callback, state);
        } 

        protected override IRequest CreateRequest(Message message)
        {
            return new StreamedFramingRequest(this); 
        }
 
        IConnection SendPreamble(IConnection connection, ref TimeoutHelper timeoutHelper, 
            ClientFramingDecoder decoder, out SecurityMessageProperty remoteSecurity)
        { 
            connection.Write(Preamble, 0, Preamble.Length, true, timeoutHelper.RemainingTime());

            if (upgrade != null)
            { 
                StreamUpgradeInitiator upgradeInitiator = upgrade.CreateUpgradeInitiator(this.RemoteAddress, this.Via);
 
                if (!ConnectionUpgradeHelper.InitiateUpgrade(upgradeInitiator, ref connection, decoder, 
                    this, ref timeoutHelper))
                { 
                    ConnectionUpgradeHelper.DecodeFramingFault(decoder, connection, Via, messageEncoder.ContentType, ref timeoutHelper);
                }

                remoteSecurity = StreamSecurityUpgradeInitiator.GetRemoteSecurity(upgradeInitiator); 

                connection.Write(ClientSingletonEncoder.PreambleEndBytes, 0, 
                    ClientSingletonEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime()); 
            }
            else 
            {
                remoteSecurity = null;
            }
 
            // read ACK
            byte[] ackBuffer = new byte[1]; 
            int ackBytesRead = connection.Read(ackBuffer, 0, ackBuffer.Length, timeoutHelper.RemainingTime()); 
            if (!ConnectionUpgradeHelper.ValidatePreambleResponse(ackBuffer, ackBytesRead, decoder, this.Via))
            { 
                ConnectionUpgradeHelper.DecodeFramingFault(decoder, connection, Via, messageEncoder.ContentType, ref timeoutHelper);
            }

            return connection; 
        }
 
        protected override void OnClose(TimeSpan timeout) 
        {
            base.WaitForPendingRequests(timeout); 
        }

        protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
        { 
            return base.BeginWaitForPendingRequests(timeout, callback, state);
        } 
 
        protected override void OnEndClose(IAsyncResult result)
        { 
            base.EndWaitForPendingRequests(result);
        }

        internal class StreamedConnectionPoolHelper : ConnectionPoolHelper 
        {
            StreamedFramingRequestChannel channel; 
            ClientSingletonDecoder decoder; 
            SecurityMessageProperty remoteSecurity;
 
            public StreamedConnectionPoolHelper(StreamedFramingRequestChannel channel)
                : base(channel.connectionPool, channel.connectionInitiator, channel.Via)
            {
                this.channel = channel; 
            }
 
            public ClientSingletonDecoder Decoder 
            {
                get { return this.decoder; } 
            }

            public SecurityMessageProperty RemoteSecurity
            { 
                get { return this.remoteSecurity; }
            } 
 
            protected override TimeoutException CreateNewConnectionTimeoutException(TimeSpan timeout, TimeoutException innerException)
            { 
                return new TimeoutException(SR.GetString(SR.RequestTimedOutEstablishingTransportSession,
                        timeout, channel.Via.AbsoluteUri), innerException);
            }
 
            protected override IConnection AcceptPooledConnection(IConnection connection, ref TimeoutHelper timeoutHelper)
            { 
                this.decoder = new ClientSingletonDecoder(0); 
                return channel.SendPreamble(connection, ref timeoutHelper, this.decoder, out this.remoteSecurity);
            } 

            protected override IAsyncResult BeginAcceptPooledConnection(IConnection connection, ref TimeoutHelper timeoutHelper, AsyncCallback callback, object state)
            {
                this.decoder = new ClientSingletonDecoder(0); 
                return new SendPreambleAsyncResult(channel, connection, ref timeoutHelper, decoder, callback, state);
            } 
 
            protected override IConnection EndAcceptPooledConnection(IAsyncResult result)
            { 
                return SendPreambleAsyncResult.End(result, out this.remoteSecurity);
            }

            class SendPreambleAsyncResult : AsyncResult 
            {
                StreamedFramingRequestChannel channel; 
                IConnection connection; 
                ClientFramingDecoder decoder;
                StreamUpgradeInitiator upgradeInitiator; 
                SecurityMessageProperty remoteSecurity;
                TimeoutHelper timeoutHelper;
                static AsyncCallback onWritePreamble = DiagnosticUtility.ThunkAsyncCallback(new AsyncCallback(OnWritePreamble));
                static AsyncCallback onWritePreambleEnd; 
                static WaitCallback onReadPreambleAck = new WaitCallback(OnReadPreambleAck);
                static AsyncCallback onUpgrade; 
                static AsyncCallback onFailedUpgrade; 

                public SendPreambleAsyncResult(StreamedFramingRequestChannel channel, IConnection connection, 
                    ref TimeoutHelper timeoutHelper, ClientFramingDecoder decoder, AsyncCallback callback, object state)
                    : base(callback, state)
                {
                    this.channel = channel; 
                    this.connection = connection;
                    this.timeoutHelper = timeoutHelper; 
                    this.decoder = decoder; 

                    IAsyncResult writePreambleResult = connection.BeginWrite(channel.Preamble, 0, channel.Preamble.Length, 
                        true, timeoutHelper.RemainingTime(), onWritePreamble, this);

                    if (!writePreambleResult.CompletedSynchronously)
                    { 
                        return;
                    } 
 
                    if (HandleWritePreamble(writePreambleResult))
                    { 
                        base.Complete(true);
                    }
                }
 
                public static IConnection End(IAsyncResult result, out SecurityMessageProperty remoteSecurity)
                { 
                    SendPreambleAsyncResult thisPtr = AsyncResult.End(result); 
                    remoteSecurity = thisPtr.remoteSecurity;
                    return thisPtr.connection; 
                }

                bool HandleWritePreamble(IAsyncResult result)
                { 
                    connection.EndWrite(result);
 
                    if (channel.upgrade == null) 
                    {
                        return ReadPreambleAck(); 
                    }
                    else
                    {
                        this.upgradeInitiator = channel.upgrade.CreateUpgradeInitiator(channel.RemoteAddress, channel.Via); 
                        if (onUpgrade == null)
                        { 
                            onUpgrade = DiagnosticUtility.ThunkAsyncCallback(new AsyncCallback(OnUpgrade)); 
                        }
 
                        IAsyncResult initiateUpgradeResult = ConnectionUpgradeHelper.BeginInitiateUpgrade(channel.settings, channel.RemoteAddress,
                            connection, decoder, this.upgradeInitiator, channel.messageEncoder.ContentType,
                            this.timeoutHelper, onUpgrade, this);
 
                        if (!initiateUpgradeResult.CompletedSynchronously)
                        { 
                            return false; 
                        }
                        return HandleUpgrade(initiateUpgradeResult); 
                    }
                }

                bool HandleUpgrade(IAsyncResult result) 
                {
                    connection = ConnectionUpgradeHelper.EndInitiateUpgrade(result); 
                    this.remoteSecurity = StreamSecurityUpgradeInitiator.GetRemoteSecurity(this.upgradeInitiator); 
                    this.upgradeInitiator = null; // we're done with the initiator
                    if (onWritePreambleEnd == null) 
                    {
                        onWritePreambleEnd = DiagnosticUtility.ThunkAsyncCallback(new AsyncCallback(OnWritePreambleEnd));
                    }
 
                    IAsyncResult writePreambleResult = connection.BeginWrite(
                        ClientSingletonEncoder.PreambleEndBytes, 0, ClientSingletonEncoder.PreambleEndBytes.Length, true, 
                        timeoutHelper.RemainingTime(), onWritePreambleEnd, this); 

                    if (!writePreambleResult.CompletedSynchronously) 
                    {
                        return false;
                    }
 
                    connection.EndWrite(writePreambleResult);
                    return ReadPreambleAck(); 
                } 

                bool ReadPreambleAck() 
                {
                    AsyncReadResult readAckResult = connection.BeginRead(0, 1,
                        timeoutHelper.RemainingTime(), onReadPreambleAck, this);
 
                    if (readAckResult == AsyncReadResult.Queued)
                    { 
                        return false; 
                    }
 
                    return HandlePreambleAck();
                }

                bool HandlePreambleAck() 
                {
                    int ackBytesRead = connection.EndRead(); 
                    if (!ConnectionUpgradeHelper.ValidatePreambleResponse( 
                        connection.AsyncReadBuffer, ackBytesRead, decoder, channel.Via))
                    { 
                        if (onFailedUpgrade == null)
                        {
                            onFailedUpgrade = DiagnosticUtility.ThunkAsyncCallback(new AsyncCallback(OnFailedUpgrade));
                        } 
                        IAsyncResult decodeFaultResult = ConnectionUpgradeHelper.BeginDecodeFramingFault(decoder,
                            connection, channel.Via, channel.messageEncoder.ContentType, ref timeoutHelper, 
                            onFailedUpgrade, this); 

                        if (!decodeFaultResult.CompletedSynchronously) 
                        {
                            return false;
                        }
 
                        ConnectionUpgradeHelper.EndDecodeFramingFault(decodeFaultResult);
                        return true; 
                    } 

                    return true; 
                }

                static void OnWritePreamble(IAsyncResult result)
                { 
                    if (result.CompletedSynchronously)
                    { 
                        return; 
                    }
 
                    SendPreambleAsyncResult thisPtr = (SendPreambleAsyncResult)result.AsyncState;

                    Exception completionException = null;
                    bool completeSelf; 
                    try
                    { 
                        completeSelf = thisPtr.HandleWritePreamble(result); 
                    }
#pragma warning suppress 56500 // [....], transferring exception to another thread 
                    catch (Exception e)
                    {
                        if (DiagnosticUtility.IsFatal(e))
                        { 
                            throw;
                        } 
 
                        completeSelf = true;
                        completionException = e; 
                    }

                    if (completeSelf)
                    { 
                        thisPtr.Complete(false, completionException);
                    } 
                } 

                static void OnWritePreambleEnd(IAsyncResult result) 
                {
                    if (result.CompletedSynchronously)
                    {
                        return; 
                    }
 
                    SendPreambleAsyncResult thisPtr = (SendPreambleAsyncResult)result.AsyncState; 

                    Exception completionException = null; 
                    bool completeSelf;
                    try
                    {
                        thisPtr.connection.EndWrite(result); 
                        completeSelf = thisPtr.ReadPreambleAck();
                    } 
#pragma warning suppress 56500 // [....], transferring exception to another thread 
                    catch (Exception e)
                    { 
                        if (DiagnosticUtility.IsFatal(e))
                        {
                            throw;
                        } 

                        completeSelf = true; 
                        completionException = e; 
                    }
 
                    if (completeSelf)
                    {
                        thisPtr.Complete(false, completionException);
                    } 
                }
 
                static void OnReadPreambleAck(object state) 
                {
                    SendPreambleAsyncResult thisPtr = (SendPreambleAsyncResult)state; 

                    Exception completionException = null;
                    bool completeSelf;
                    try 
                    {
                        completeSelf = thisPtr.HandlePreambleAck(); 
                    } 
#pragma warning suppress 56500 // [....], transferring exception to another thread
                    catch (Exception e) 
                    {
                        if (DiagnosticUtility.IsFatal(e))
                        {
                            throw; 
                        }
 
                        completeSelf = true; 
                        completionException = e;
                    } 

                    if (completeSelf)
                    {
                        thisPtr.Complete(false, completionException); 
                    }
                } 
 
                static void OnUpgrade(IAsyncResult result)
                { 
                    if (result.CompletedSynchronously)
                    {
                        return;
                    } 

                    SendPreambleAsyncResult thisPtr = (SendPreambleAsyncResult)result.AsyncState; 
 
                    Exception completionException = null;
                    bool completeSelf; 
                    try
                    {
                        completeSelf = thisPtr.HandleUpgrade(result);
                    } 
#pragma warning suppress 56500 // [....], transferring exception to another thread
                    catch (Exception e) 
                    { 
                        if (DiagnosticUtility.IsFatal(e))
                        { 
                            throw;
                        }

                        completeSelf = true; 
                        completionException = e;
                    } 
 
                    if (completeSelf)
                    { 
                        thisPtr.Complete(false, completionException);
                    }
                }
 
                static void OnFailedUpgrade(IAsyncResult result)
                { 
                    if (result.CompletedSynchronously) 
                    {
                        return; 
                    }

                    SendPreambleAsyncResult thisPtr = (SendPreambleAsyncResult)result.AsyncState;
 
                    Exception completionException = null;
                    try 
                    { 
                        ConnectionUpgradeHelper.EndDecodeFramingFault(result);
                    } 
#pragma warning suppress 56500 // [....], transferring exception to another thread
                    catch (Exception e)
                    {
                        if (DiagnosticUtility.IsFatal(e)) 
                        {
                            throw; 
                        } 

                        completionException = e; 
                    }

                    thisPtr.Complete(false, completionException);
                } 
            }
        } 
 
        class ClientSingletonConnectionReader : SingletonConnectionReader
        { 
            StreamedConnectionPoolHelper connectionPoolHelper;

            public ClientSingletonConnectionReader(IConnection connection, StreamedConnectionPoolHelper connectionPoolHelper,
                IConnectionOrientedTransportFactorySettings settings) 
                : base(connection, 0, 0, connectionPoolHelper.RemoteSecurity, settings, null)
            { 
                this.connectionPoolHelper = connectionPoolHelper; 
            }
 
            protected override long StreamPosition
            {
                get { return connectionPoolHelper.Decoder.StreamPosition; }
            } 

            protected override bool DecodeBytes(byte[] buffer, ref int offset, ref int size, ref bool isAtEof) 
            { 
                while (size > 0)
                { 
                    int bytesRead = connectionPoolHelper.Decoder.Decode(buffer, offset, size);
                    if (bytesRead > 0)
                    {
                        offset += bytesRead; 
                        size -= bytesRead;
                    } 
 
                    switch (connectionPoolHelper.Decoder.CurrentState)
                    { 
                        case ClientFramingDecoderState.EnvelopeStart:
                            // we're at the envelope
                            return true;
 
                        case ClientFramingDecoderState.End:
                            isAtEof = true; 
                            return false; 
                    }
                } 

                return false;
            }
 
            protected override void OnClose(TimeSpan timeout)
            { 
                connectionPoolHelper.Close(timeout); 
            }
        } 

        class StreamedFramingRequest : IRequest
        {
            StreamedFramingRequestChannel channel; 
            StreamedConnectionPoolHelper connectionPoolHelper;
            IConnection connection; 
 
            public StreamedFramingRequest(StreamedFramingRequestChannel channel)
            { 
                this.channel = channel;
                this.connectionPoolHelper = new StreamedConnectionPoolHelper(channel);
            }
 
            public void SendRequest(Message message, TimeSpan timeout)
            { 
                TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); 

                try 
                {
                    this.connection = connectionPoolHelper.EstablishConnection(timeoutHelper.RemainingTime());

                    bool success = false; 
                    try
                    { 
                        StreamingConnectionHelper.WriteMessage(message, this.connection, true, channel.settings, ref timeoutHelper); 
                        success = true;
                    } 
                    finally
                    {
                        if (!success)
                        { 
                            connectionPoolHelper.Abort();
                        } 
                    } 
                }
                catch (TimeoutException exception) 
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                        new TimeoutException(SR.GetString(SR.TimeoutOnRequest, timeout), exception));
                } 
            }
 
            public Message WaitForReply(TimeSpan timeout) 
            {
                ClientSingletonConnectionReader connectionReader = new ClientSingletonConnectionReader( 
                    connection, connectionPoolHelper, channel.settings);

                connectionReader.DoneSending(TimeSpan.Zero); // we still need to receive
                return connectionReader.Receive(timeout); 
            }
 
            void Cleanup() 
            {
                this.connectionPoolHelper.Abort(); 
            }

            public void Abort(RequestChannel requestChannel)
            { 
                Cleanup();
            } 
 
            public void Fault(RequestChannel requestChannel)
            { 
                Cleanup();
            }
        }
 
        class StreamedFramingAsyncRequest : AsyncResult, IAsyncRequest
        { 
            StreamedFramingRequestChannel channel; 
            IConnection connection;
            StreamedConnectionPoolHelper connectionPoolHelper; 
            Message message;
            Message replyMessage;
            TimeoutHelper timeoutHelper;
            static AsyncCallback onEstablishConnection = DiagnosticUtility.ThunkAsyncCallback(new AsyncCallback(OnEstablishConnection)); 
            static AsyncCallback onWriteMessage = DiagnosticUtility.ThunkAsyncCallback(new AsyncCallback(OnWriteMessage));
            static AsyncCallback onReceiveReply = DiagnosticUtility.ThunkAsyncCallback(new AsyncCallback(OnReceiveReply)); 
            ClientSingletonConnectionReader connectionReader; 

            public StreamedFramingAsyncRequest(StreamedFramingRequestChannel channel, AsyncCallback callback, object state) 
                : base(callback, state)
            {
                this.channel = channel;
                this.connectionPoolHelper = new StreamedConnectionPoolHelper(channel); 
            }
 
            public void BeginSendRequest(Message message, TimeSpan timeout) 
            {
                this.timeoutHelper = new TimeoutHelper(timeout); 
                this.message = message;

                bool completeSelf = false;
                bool success = false; 
                try
                { 
                    try 
                    {
                        IAsyncResult result = connectionPoolHelper.BeginEstablishConnection(timeoutHelper.RemainingTime(), onEstablishConnection, this); 
                        if (result.CompletedSynchronously)
                        {
                            completeSelf = HandleEstablishConnection(result);
                        } 
                    }
                    catch (TimeoutException exception) 
                    { 
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                            new TimeoutException(SR.GetString(SR.TimeoutOnRequest, timeout), exception)); 
                    }

                    success = true;
                } 
                finally
                { 
                    if (!success) 
                    {
                        Cleanup(); 
                    }
                }

                if (completeSelf) 
                {
                    base.Complete(true); 
                } 
            }
 
            bool HandleEstablishConnection(IAsyncResult result)
            {
                this.connection = connectionPoolHelper.EndEstablishConnection(result);
 
                IAsyncResult writeResult = StreamingConnectionHelper.BeginWriteMessage(this.message, this.connection, true, this.channel.settings, ref timeoutHelper, onWriteMessage, this);
                if (!writeResult.CompletedSynchronously) 
                { 
                    return false;
                } 

                return HandleWriteMessage(writeResult);
            }
 
            public Message End()
            { 
                try 
                {
                    AsyncResult.End(this); 
                }
                catch (TimeoutException exception)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                        new TimeoutException(SR.GetString(SR.TimeoutOnRequest, this.timeoutHelper.OriginalTimeout), exception));
                } 
                return replyMessage; 
            }
 
            public void Abort(RequestChannel requestChannel)
            {
                Cleanup();
            } 

            public void Fault(RequestChannel requestChannel) 
            { 
                Cleanup();
            } 

            void Cleanup()
            {
                connectionPoolHelper.Abort(); 
                message.Close();
            } 
 
            bool HandleWriteMessage(IAsyncResult result)
            { 
                // write out the streamed message
                StreamingConnectionHelper.EndWriteMessage(result);

                connectionReader = new ClientSingletonConnectionReader(connection, connectionPoolHelper, channel.settings); 
                connectionReader.DoneSending(TimeSpan.Zero); // we still need to receive
 
                IAsyncResult receiveResult = connectionReader.BeginReceive(timeoutHelper.RemainingTime(), onReceiveReply, this); 

                if (!receiveResult.CompletedSynchronously) 
                {
                    return false;
                }
 
                return CompleteReceiveReply(receiveResult);
            } 
 
            bool CompleteReceiveReply(IAsyncResult result)
            { 
                this.replyMessage = connectionReader.EndReceive(result);
                return true;
            }
 
            static void OnEstablishConnection(IAsyncResult result)
            { 
                if (result.CompletedSynchronously) 
                {
                    return; 
                }

                StreamedFramingAsyncRequest thisPtr = (StreamedFramingAsyncRequest)result.AsyncState;
 
                Exception completionException = null;
                bool completeSelf; 
                bool throwing = true; 
                try
                { 
                    completeSelf = thisPtr.HandleEstablishConnection(result);
                    throwing = false;
                }
#pragma warning suppress 56500 // [....], transferring exception to another thread 
                catch (Exception e)
                { 
                    if (DiagnosticUtility.IsFatal(e)) 
                    {
                        throw; 
                    }

                    completeSelf = true;
                    completionException = e; 
                }
                finally 
                { 
                    if (throwing)
                    { 
                        thisPtr.Cleanup();
                    }
                }
 
                if (completeSelf)
                { 
                    thisPtr.Complete(false, completionException); 
                }
            } 

            static void OnWriteMessage(IAsyncResult result)
            {
                if (result.CompletedSynchronously) 
                {
                    return; 
                } 

                StreamedFramingAsyncRequest thisPtr = (StreamedFramingAsyncRequest) result.AsyncState; 

                Exception completionException = null;
                bool completeSelf;
                bool throwing = true; 
                try
                { 
                    completeSelf = thisPtr.HandleWriteMessage(result); 
                    throwing = false;
                } 
#pragma warning suppress 56500 // [....], transferring exception to another thread
                catch (Exception e)
                {
                    if (DiagnosticUtility.IsFatal(e)) 
                    {
                        throw; 
                    } 

                    completeSelf = true; 
                    completionException = e;
                }
                finally
                { 
                    if (throwing)
                    { 
                        thisPtr.Cleanup(); 
                    }
                } 

                if (completeSelf)
                {
                    thisPtr.Complete(false, completionException); 
                }
            } 
 
            static void OnReceiveReply(IAsyncResult result)
            { 
                StreamedFramingAsyncRequest thisPtr = (StreamedFramingAsyncRequest)result.AsyncState;

                Exception completionException = null;
                bool completeSelf; 
                bool throwing = true;
                try 
                { 
                    completeSelf = thisPtr.CompleteReceiveReply(result);
                    throwing = false; 
                }
#pragma warning suppress 56500 // [....], transferring exception to another thread
                catch (Exception e)
                { 
                    if (DiagnosticUtility.IsFatal(e))
                    { 
                        throw; 
                    }
 
                    completeSelf = true;
                    completionException = e;
                }
                finally 
                {
                    if (throwing) 
                    { 
                        thisPtr.Cleanup();
                    } 
                }

                if (completeSelf)
                { 
                    thisPtr.Complete(false, completionException);
                } 
            } 
        }
    } 
}

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