Code:
                         / Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / Net / System / Net / SecureProtocols / _FixedSizeReader.cs / 2 / _FixedSizeReader.cs
                        
                        
                            /*++ 
Copyright (c) 2000 Microsoft Corporation
Module Name:
 
    _FixedSizeReader.cs
 
Abstract: 
    The class is a simple wrapper on top of a read stream.
    It will read the exact number of bytes requested. 
    It operates either [....] or async.
Author:
 
    Alexei Vopilov  Aug 18 2003
 
Revision History: 
--*/ 
namespace System.Net {
    using System;
    using System.IO; 
    using System.Threading;
 
 
    //
    // Reads a fixed size packet from a stream, can disover EOF as 0 bytes read from stream 
    //
    internal class FixedSizeReader {
        private static readonly AsyncCallback _ReadCallback  = new AsyncCallback(ReadCallback);
 
        private readonly Stream      _Transport;
        private AsyncProtocolRequest _Request; 
        private int                  _TotalRead; 
        public FixedSizeReader(Stream transport) 
        {
            _Transport = transport;
        }
 
        //
        // Returns 0 on legitimate EOF or if 0 bytes was requested, otheriwse reads as directed or throws 
        // Returns count on success 
        //
        public int ReadPacket(byte[] buffer, int offset, int count) 
        {
            int tempCount = count;
            do {
                int bytes = _Transport.Read(buffer, offset, tempCount); 
                if (bytes == 0) 
                { 
                     if (tempCount != count)
                     { 
                         throw new IOException(SR.GetString(SR.net_io_eof));
                     }
                     return 0;
                } 
                tempCount -= bytes; 
                offset += bytes; 
            }while (tempCount != 0);
 
            return count;
        }
        // 
        // Completes "_Request" with 0 if 0 bytes was requested or legitimate EOF received
        // Otheriwse, reads as directed or completes "_Request" with an Exception or throws right here 
        // 
        public void AsyncReadPacket(AsyncProtocolRequest request)
        { 
            _Request = request;
            _TotalRead = 0;
            StartReading();
        } 
        //
        // Loops while subsequest completions are [....] 
        // 
        private void StartReading()
        { 
            while (true)
            {
                IAsyncResult ar = _Transport.BeginRead(_Request.Buffer, _Request.Offset+_TotalRead, _Request.Count-_TotalRead, _ReadCallback, this);
                if (!ar.CompletedSynchronously) 
                {
#if DEBUG 
                    _Request._DebugAsyncChain = ar; 
#endif
                    break; 
                }
                int bytes = _Transport.EndRead(ar);
 
                if (CheckCompletionBeforeNextRead(bytes))
                { 
                    break; 
                }
            } 
        }
        //
        // 
        //
        private bool CheckCompletionBeforeNextRead(int bytes) 
        { 
            if (bytes == 0)
            { 
                // 0 bytes was requested or EOF in the beginning of a frame, the caller should decide whether it's OK
                if(_TotalRead == 0)
                {
                    _Request.CompleteRequest(0); 
                    return true;
                } 
                // EOF in the middle of a frame, bummer! 
                throw new IOException(SR.GetString(SR.net_io_eof));
            } 
            GlobalLog.Assert(_TotalRead + bytes <= _Request.Count, "FixedSizeReader::CheckCompletion()|State got out of range. Total:{0} Count:{1}", _TotalRead + bytes, _Request.Count);
            if ((_TotalRead+=bytes) == _Request.Count) 
            {
                _Request.CompleteRequest(_Request.Count); 
                return true; 
            }
            return false; 
        }
        // 
        //
        // 
        private static void ReadCallback(IAsyncResult transportResult) 
        {
            GlobalLog.Assert(transportResult.AsyncState is FixedSizeReader, "ReadCallback|State type is wrong, expected FixedSizeReader."); 
            if (transportResult.CompletedSynchronously)
            {
                return;
            } 
            FixedSizeReader reader = (FixedSizeReader)transportResult.AsyncState; 
            AsyncProtocolRequest request = reader._Request; 
            // Async completion 
            try
            {
                int bytes = reader._Transport.EndRead(transportResult);
 
                if (reader.CheckCompletionBeforeNextRead(bytes))
                { 
                    return; 
                }
                reader.StartReading(); 
            }
            catch (Exception e)
            {
                if (request.IsUserCompleted) 
                    throw;
                request.CompleteWithError(e); 
            } 
        }
    } 
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
/*++ 
Copyright (c) 2000 Microsoft Corporation
Module Name:
 
    _FixedSizeReader.cs
 
Abstract: 
    The class is a simple wrapper on top of a read stream.
    It will read the exact number of bytes requested. 
    It operates either [....] or async.
Author:
 
    Alexei Vopilov  Aug 18 2003
 
Revision History: 
--*/ 
namespace System.Net {
    using System;
    using System.IO; 
    using System.Threading;
 
 
    //
    // Reads a fixed size packet from a stream, can disover EOF as 0 bytes read from stream 
    //
    internal class FixedSizeReader {
        private static readonly AsyncCallback _ReadCallback  = new AsyncCallback(ReadCallback);
 
        private readonly Stream      _Transport;
        private AsyncProtocolRequest _Request; 
        private int                  _TotalRead; 
        public FixedSizeReader(Stream transport) 
        {
            _Transport = transport;
        }
 
        //
        // Returns 0 on legitimate EOF or if 0 bytes was requested, otheriwse reads as directed or throws 
        // Returns count on success 
        //
        public int ReadPacket(byte[] buffer, int offset, int count) 
        {
            int tempCount = count;
            do {
                int bytes = _Transport.Read(buffer, offset, tempCount); 
                if (bytes == 0) 
                { 
                     if (tempCount != count)
                     { 
                         throw new IOException(SR.GetString(SR.net_io_eof));
                     }
                     return 0;
                } 
                tempCount -= bytes; 
                offset += bytes; 
            }while (tempCount != 0);
 
            return count;
        }
        // 
        // Completes "_Request" with 0 if 0 bytes was requested or legitimate EOF received
        // Otheriwse, reads as directed or completes "_Request" with an Exception or throws right here 
        // 
        public void AsyncReadPacket(AsyncProtocolRequest request)
        { 
            _Request = request;
            _TotalRead = 0;
            StartReading();
        } 
        //
        // Loops while subsequest completions are [....] 
        // 
        private void StartReading()
        { 
            while (true)
            {
                IAsyncResult ar = _Transport.BeginRead(_Request.Buffer, _Request.Offset+_TotalRead, _Request.Count-_TotalRead, _ReadCallback, this);
                if (!ar.CompletedSynchronously) 
                {
#if DEBUG 
                    _Request._DebugAsyncChain = ar; 
#endif
                    break; 
                }
                int bytes = _Transport.EndRead(ar);
 
                if (CheckCompletionBeforeNextRead(bytes))
                { 
                    break; 
                }
            } 
        }
        //
        // 
        //
        private bool CheckCompletionBeforeNextRead(int bytes) 
        { 
            if (bytes == 0)
            { 
                // 0 bytes was requested or EOF in the beginning of a frame, the caller should decide whether it's OK
                if(_TotalRead == 0)
                {
                    _Request.CompleteRequest(0); 
                    return true;
                } 
                // EOF in the middle of a frame, bummer! 
                throw new IOException(SR.GetString(SR.net_io_eof));
            } 
            GlobalLog.Assert(_TotalRead + bytes <= _Request.Count, "FixedSizeReader::CheckCompletion()|State got out of range. Total:{0} Count:{1}", _TotalRead + bytes, _Request.Count);
            if ((_TotalRead+=bytes) == _Request.Count) 
            {
                _Request.CompleteRequest(_Request.Count); 
                return true; 
            }
            return false; 
        }
        // 
        //
        // 
        private static void ReadCallback(IAsyncResult transportResult) 
        {
            GlobalLog.Assert(transportResult.AsyncState is FixedSizeReader, "ReadCallback|State type is wrong, expected FixedSizeReader."); 
            if (transportResult.CompletedSynchronously)
            {
                return;
            } 
            FixedSizeReader reader = (FixedSizeReader)transportResult.AsyncState; 
            AsyncProtocolRequest request = reader._Request; 
            // Async completion 
            try
            {
                int bytes = reader._Transport.EndRead(transportResult);
 
                if (reader.CheckCompletionBeforeNextRead(bytes))
                { 
                    return; 
                }
                reader.StartReading(); 
            }
            catch (Exception e)
            {
                if (request.IsUserCompleted) 
                    throw;
                request.CompleteWithError(e); 
            } 
        }
    } 
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        
                        
                        
                        
                    Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Types.cs
 - Avt.cs
 - ErrorTableItemStyle.cs
 - StructuredProperty.cs
 - SchemaMapping.cs
 - WebConfigurationHostFileChange.cs
 - SecurityProtocol.cs
 - OLEDB_Enum.cs
 - VisualBrush.cs
 - ErrorTableItemStyle.cs
 - CodeDomComponentSerializationService.cs
 - AuthenticationConfig.cs
 - MailDefinition.cs
 - HyperLinkColumn.cs
 - SurrogateEncoder.cs
 - SchemaObjectWriter.cs
 - ProgressBar.cs
 - DesignSurfaceServiceContainer.cs
 - WebPart.cs
 - HttpListenerPrefixCollection.cs
 - HttpClientCredentialType.cs
 - HTTPNotFoundHandler.cs
 - VideoDrawing.cs
 - StagingAreaInputItem.cs
 - StateMachineWorkflowInstance.cs
 - LocatorBase.cs
 - Visitors.cs
 - clipboard.cs
 - SqlDataSource.cs
 - DataColumnMappingCollection.cs
 - PnrpPeerResolver.cs
 - CompilationSection.cs
 - WebPartZoneCollection.cs
 - TreeViewAutomationPeer.cs
 - UrlPath.cs
 - XmlWrappingReader.cs
 - _FtpControlStream.cs
 - ParseHttpDate.cs
 - Delay.cs
 - LicenseContext.cs
 - CompareInfo.cs
 - AspCompat.cs
 - MethodImplAttribute.cs
 - TcpTransportSecurity.cs
 - UnsafeNetInfoNativeMethods.cs
 - documentsequencetextcontainer.cs
 - SystemResources.cs
 - AccessDataSourceView.cs
 - PolyQuadraticBezierSegment.cs
 - TreeNodeEventArgs.cs
 - GeneralTransformGroup.cs
 - XamlVector3DCollectionSerializer.cs
 - GradientStop.cs
 - PointAnimationUsingPath.cs
 - Converter.cs
 - OutputScopeManager.cs
 - RequestBringIntoViewEventArgs.cs
 - NameValueCollection.cs
 - PackageRelationshipSelector.cs
 - StandardTransformFactory.cs
 - ScriptMethodAttribute.cs
 - CapabilitiesSection.cs
 - SqlRowUpdatedEvent.cs
 - DbFunctionCommandTree.cs
 - DataSvcMapFile.cs
 - CommonDialog.cs
 - WorkBatch.cs
 - EditorBrowsableAttribute.cs
 - TextTrailingCharacterEllipsis.cs
 - ParallelTimeline.cs
 - BindUriHelper.cs
 - CharacterString.cs
 - ContainerParagraph.cs
 - WindowsComboBox.cs
 - CodeNamespaceImport.cs
 - EditBehavior.cs
 - SuppressMessageAttribute.cs
 - HandlerMappingMemo.cs
 - RowSpanVector.cs
 - XamlValidatingReader.cs
 - FontDriver.cs
 - PropertyInfo.cs
 - ItemType.cs
 - ServiceOperationParameter.cs
 - ParserOptions.cs
 - SymbolTable.cs
 - TemplateField.cs
 - FixedBufferAttribute.cs
 - IdentityNotMappedException.cs
 - DataControlReference.cs
 - TreeViewBindingsEditor.cs
 - TreeNodeSelectionProcessor.cs
 - SafeNativeMethodsMilCoreApi.cs
 - AlternationConverter.cs
 - LinqExpressionNormalizer.cs
 - EndEvent.cs
 - ParseElement.cs
 - PanelDesigner.cs
 - UInt16Storage.cs
 - MenuItem.cs