upnp/upnpstack/dlnawebserver/inc/upnphttpfiletransferreader.h
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2005-2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies  this distribution, and is available 
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  CUpnpHttpFileTransferReader is a class responsible for 
       
    15 *                   asynchronous reading from socket owned by CUpnpTcpSession.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #ifndef C_CUPNPHTTPFILETRANSFERREADER_H
       
    22 #define C_CUPNPHTTPFILETRANSFERREADER_H
       
    23 
       
    24 
       
    25 #include <e32base.h>
       
    26 #include <in_sock.h>
       
    27 #include "upnpnotifytimer.h"
       
    28 
       
    29 //constants
       
    30 static const TInt KMaxRetryErrors = 5;
       
    31 static const TInt KRetryWaitTime = 1000000;
       
    32 static const TInt KTwoSec = 2000000;
       
    33 static const TInt KOneSec = 1000000;
       
    34 
       
    35 //FORWARD DECLARATIONS
       
    36 class CUpnpTcpSession;
       
    37 class CUpnpHttpFileTransferBuffer;
       
    38 class CUpnpHttpFileAccess;
       
    39 
       
    40 
       
    41 /**
       
    42  *  CUpnpHttpFileTransferReader is responsible for asynchronous reading.
       
    43  *  Reader starta reading in asynchronous mode. Issues reading and then waits 
       
    44  *  for the operation to be completed. When it is completed then received
       
    45  *  data is forwarded to upper layer - CUpnpTcpSession and the new read 
       
    46  *  operation is issues. This continues in loop until the Cancel() is invoked
       
    47  *  or object is destroyed.
       
    48  *  @lib DLNAWebServer.lib
       
    49  *  @since Series60 3.2
       
    50  */
       
    51 NONSHARABLE_CLASS (CUpnpHttpFileTransferReader) : public CActive, MUpnpNotifyTimerObserver
       
    52     {
       
    53 
       
    54 public:
       
    55     
       
    56     /**
       
    57     * CUpnpHttpFileTransferReader factory method.
       
    58     * @since Series60 3.2
       
    59     * @param aSession session that runs and supervises reader.
       
    60     * @param aSocket socket from which data will be read.
       
    61     * @param aPriority priority with which the reader will be working.
       
    62     * @param aReadPortion - size of the buffer to read from the socket
       
    63     * @param aBufferSize - the buffer size
       
    64     * @return new CUpnpHttpFileTransferReader object's instance.
       
    65     */
       
    66     static CUpnpHttpFileTransferReader* NewL( CUpnpTcpSession& aSession,
       
    67                                         RSocket& aSocket,
       
    68                                         TThreadPriority aPriority,                                        
       
    69                                         TInt aReadPortion,
       
    70                                         TInt aBufferSize);
       
    71     
       
    72     /**
       
    73     * Virtual destructor.
       
    74     * @since Series60 3.2
       
    75     */                             
       
    76     virtual ~CUpnpHttpFileTransferReader();
       
    77     
       
    78     /**
       
    79     * Appends first portion of data to the buffer.
       
    80     * @since Series60 3.2
       
    81     */    
       
    82     void AppendL(const TDesC8& aBeginning);
       
    83     
       
    84     /**
       
    85     * Calculates the buffer size as exponent of 2
       
    86     * and proper for read portions
       
    87     * @since Series60 3.2
       
    88     */    
       
    89     static TInt CalculateBufferSize(TInt aReadPortion, TInt aBufferSize);            
       
    90     /**
       
    91     * Starts reading in asynchronous mode.
       
    92     * @since Series60 3.2
       
    93     */
       
    94     virtual void StartL();
       
    95 
       
    96     /**
       
    97     * Cancel the timers
       
    98     * @since Series60 3.2
       
    99     */
       
   100     void CancelTimers();
       
   101 
       
   102     /**
       
   103     * Resets some member values to start reading data
       
   104     * @since Series60 3.2
       
   105     */
       
   106     
       
   107     virtual void Reset();
       
   108     
       
   109     /**
       
   110     * Checks if the reader is started meaning in use
       
   111     * @since Series60 3.2
       
   112     * @return TBool
       
   113     */
       
   114     TBool Activated();
       
   115      
       
   116     /**
       
   117     * Sets the state of the reader to activated
       
   118     * @since Series60 3.2    
       
   119     * @param state - ETrue/EFalse
       
   120     */
       
   121     void SetActivated(TBool aValue);
       
   122 
       
   123   
       
   124 
       
   125 protected: // Functions from CActive
       
   126    
       
   127     /**
       
   128     * Cancels issued reading request.
       
   129     * Standard active object function
       
   130     * @since Series60 3.2
       
   131     */
       
   132     void DoCancel();
       
   133     
       
   134      /**
       
   135     * Function called when the read request is finished.
       
   136     * Standard active object function
       
   137     * @since Series60 3.2
       
   138     */
       
   139     virtual void RunL();
       
   140     
       
   141     /**
       
   142     * Trap RunL leaves
       
   143     * Standard active object function
       
   144     * @since Series60 3.2
       
   145     */
       
   146     TInt RunError( TInt aError );
       
   147 
       
   148 	// Constructors and destructors
       
   149 
       
   150     /**
       
   151     * CUpnpHttpFileTransferReader constructor.
       
   152     * @since Series60 3.2
       
   153     * @param aSession session that runs and supervises reader.
       
   154     * @param aSocket socket from which data will be read.
       
   155     * @param aPriority priority with which the reader will be working.
       
   156     * @param aReadPortion - size of the buffer to read from the socket
       
   157     * @param aBufferSize - the buffer size
       
   158     */
       
   159     CUpnpHttpFileTransferReader( CUpnpTcpSession& aSession,
       
   160                            RSocket& aSocket,
       
   161                            TThreadPriority aPriority,
       
   162                            TInt aReadPortion,
       
   163                            TInt aBufferSize);
       
   164     
       
   165 
       
   166         
       
   167 
       
   168 protected: // New functions
       
   169 
       
   170     /**
       
   171     * Issues reading in asynchronous mode.
       
   172     * @since Series60 3.2
       
   173     */
       
   174     void IssueRead();
       
   175     
       
   176     /**
       
   177     * Callback function for timer expirations.
       
   178     * @since Series60 2.6
       
   179     * @param aTimer Timer that has expired.
       
   180     **/
       
   181 	void TimerEventL( CUpnpNotifyTimer* /*aTimer*/ );
       
   182 
       
   183     /**
       
   184     * Starts the cancel timer.
       
   185     * @since Series60 3.2
       
   186     */
       
   187     void StartCancelTimer();
       
   188 
       
   189 	/**
       
   190     * Base constructor to be used also in derived classes.
       
   191     * @since Series60 3.2
       
   192     */
       
   193     void BaseConstructL();
       
   194     
       
   195     /**
       
   196     * Initiates the buffer
       
   197     * @since Series60 3.2      
       
   198     */
       
   199     void InitiateBufferL();
       
   200 	
       
   201 	/**
       
   202     * Remaing number of bytes to read.
       
   203     * @since Series60 3.2    
       
   204     * @return TInt
       
   205     */
       
   206     virtual TInt ReadBufferSize();   
       
   207     
       
   208     /**
       
   209     * Handles buffer of data
       
   210     * @since Series60 3.2    
       
   211     * @return TBool
       
   212     */
       
   213     virtual TBool HandleL();   
       
   214     
       
   215     /**
       
   216     * Handles leave from RunL
       
   217     * @since Series60 3.2    
       
   218     * @param aError
       
   219     */
       
   220     void HandleError();   
       
   221     
       
   222     /**
       
   223     * Finish the session
       
   224     * @since Series60 3.2
       
   225     */
       
   226     virtual void Finish();  
       
   227     
       
   228     
       
   229     /**
       
   230     * Reads from socket
       
   231     * @since Series60 3.2    
       
   232     */
       
   233     virtual void ReadFromSocket();
       
   234     
       
   235     /**
       
   236     * Remaing number of bytes to read.
       
   237     * @since Series60 3.2    
       
   238     * @return TInt
       
   239     */
       
   240     virtual TInt RemainingBytes();
       
   241     
       
   242     /**
       
   243     * Returns bytes to be read unless FIN received, then returns 0
       
   244     * @since Series60 3.2    
       
   245     * @return TInt
       
   246     */
       
   247     TInt TcpFinFoundRemainingBytes();
       
   248     
       
   249     /**
       
   250     * Returns how many bytes will still fit into the buffer
       
   251     * @since Series60 3.2    
       
   252     * @return TInt
       
   253     */
       
   254     TInt AvailableSpace();
       
   255     
       
   256     /**
       
   257     * If content length is less than single buffer size
       
   258     * transaction is completed and session is closed
       
   259     * @since Series60 3.2    
       
   260     * @return TInt
       
   261     */    
       
   262     void HandleOneBufferShortTransferL();
       
   263     
       
   264      
       
   265     
       
   266 protected: // Data
       
   267       
       
   268     //Socket from which the data will be read, not owned.    
       
   269     RSocket& iSocket;   
       
   270     
       
   271     // Reference to session that owns this reader.   
       
   272     CUpnpTcpSession& iSession;
       
   273     
       
   274     //Pointer that points to receiving buffer.
       
   275     TPtr8 iReceivePtr;       
       
   276     
       
   277     // Buffer that is used to receive data to session.   
       
   278     RBuf8 iCacheBuffer;
       
   279         	
       
   280     // Read buffer size
       
   281     TInt iReadBufferSize;
       
   282 
       
   283     //size of buffered data
       
   284     TInt iBufferSize;  	
       
   285 
       
   286     //processing has finished
       
   287     TInt iIsFinished;
       
   288 
       
   289     //whether upload reader is activated
       
   290     TBool iIsActivated;
       
   291 
       
   292     /**
       
   293     * Timer used for retry process.
       
   294     */
       
   295     CUpnpNotifyTimer *iRetryTimer;
       
   296 
       
   297     /**
       
   298     * Timer used for cancel reciving process.
       
   299     */
       
   300     CUpnpNotifyTimer *iCancelTimer;
       
   301 
       
   302     /**
       
   303     * Variable indicating when the cancel was called from cancel timer.
       
   304     */
       
   305     TBool iCancelFromTimer;
       
   306 
       
   307     /**
       
   308     * Count of reading errors.
       
   309     */
       
   310     TInt iRetryErrorCount;
       
   311 
       
   312     //length of data read from socket
       
   313     TSockXfrLength iLen;
       
   314 
       
   315     };
       
   316 
       
   317 
       
   318 #endif // C_CUPNPHTTPFILETRANSFERREADER_H
       
   319 
       
   320 // End Of File