upnp/upnpstack/dlnawebserver/src/upnptcpsessionreader.cpp
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:  CUpnpTcpSessionReader is a class responsible for 
       
    15 *                   asynchronous reading from socket owned by CUpnpTcpSession.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include "upnptcpsession.h"
       
    22 #include "upnptcpsessionreader.h"
       
    23 #define KLogFile _L("DLNAWebServer.txt")
       
    24 #include "upnpcustomlog.h"
       
    25 #include "inet6err.h"
       
    26 
       
    27 static const TInt KMaxRetryErrors = 5;
       
    28 static const TInt KRetryWaitTime = 1000000;
       
    29 static const TInt KTwoSec = 2000000;
       
    30 static const TInt KOneSec = 1000000;
       
    31 
       
    32 
       
    33 // ======== MEMBER FUNCTIONS ========
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // CUpnpTcpSessionReader::NewL
       
    37 // Factory method.
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CUpnpTcpSessionReader* CUpnpTcpSessionReader::NewL( CUpnpTcpSession& aSession,
       
    41                                                     RSocket& aSocket,
       
    42                                                     TThreadPriority aPriority,
       
    43                                                     TInt aBufferSize )
       
    44     {
       
    45     CUpnpTcpSessionReader* self = new ( ELeave ) 
       
    46                 CUpnpTcpSessionReader( aSession, aSocket, aPriority, aBufferSize );
       
    47     CleanupStack::PushL( self );
       
    48     self->ConstructL();
       
    49     CleanupStack::Pop( self );
       
    50     return self;
       
    51     }
       
    52     
       
    53 // ---------------------------------------------------------------------------
       
    54 // CUpnpTcpSessionReader::CUpnpTcpSessionReader
       
    55 // C++ constructor.
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CUpnpTcpSessionReader::CUpnpTcpSessionReader( CUpnpTcpSession& aSession,
       
    59                                               RSocket& aSocket, 
       
    60                                               TThreadPriority aPriority,
       
    61                                               TInt aBufferSize )
       
    62     :CActive( aPriority ),
       
    63     iSocket( aSocket ),
       
    64     iSession( aSession ),
       
    65     iBufferSize( aBufferSize )
       
    66     {
       
    67     LOGS1( "%i, CUpnpTcpSessionReader::CUpnpTcpSessionReader", iSession.Id() );
       
    68     
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CUpnpTcpSessionReader::TcpConstructL
       
    73 // Two-phased constructor
       
    74 // constructor that can leave. Used from derived classes.
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 void CUpnpTcpSessionReader::ConstructL()
       
    78     {
       
    79     CActiveScheduler::Add( this );
       
    80     
       
    81     iReceiveBuffer.CreateL( iBufferSize );
       
    82         
       
    83     iRetryTimer = CUpnpNotifyTimer::NewL( this );
       
    84     iCancelTimer = CUpnpNotifyTimer::NewL( this );        
       
    85     }
       
    86     
       
    87 // ---------------------------------------------------------------------------
       
    88 // CUpnpTcpSessionReader::~CUpnpTcpSessionReader
       
    89 // Destructor.
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 CUpnpTcpSessionReader::~CUpnpTcpSessionReader()
       
    93     {
       
    94     LOGS1( "%i, CUpnpTcpSessionReader::~CUpnpTcpSessionReader", iSession.Id() );
       
    95     Cancel();
       
    96     
       
    97     iReceiveBuffer.Close();  
       
    98     
       
    99     delete iRetryTimer;
       
   100     delete iCancelTimer;   
       
   101     }
       
   102     
       
   103 // ---------------------------------------------------------------------------
       
   104 // CUpnpTcpSessionReader::Start
       
   105 // Starts reading.
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 void CUpnpTcpSessionReader::Start()
       
   109     {
       
   110     LOGS1( "%i, CUpnpTcpSessionReader::Start", iSession.Id() );
       
   111     // Initiate a new read from socket into iBuffer
       
   112     if ( !IsActive() )
       
   113         {
       
   114         IssueRead();
       
   115         }
       
   116     }    
       
   117     
       
   118 // ---------------------------------------------------------------------------
       
   119 // CUpnpTcpSessionReader::DoCancel
       
   120 // From class CActive.
       
   121 // Cancels issued reading.
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 void CUpnpTcpSessionReader::DoCancel()
       
   125     {
       
   126     LOGS1( "%i, CUpnpTcpSessionReader::DoCancel", iSession.Id() );
       
   127     // Cancel asychronous read request
       
   128     iSocket.CancelRead();
       
   129     }
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // CUpnpTcpSessionReader::CancelRetry
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 void CUpnpTcpSessionReader::CancelTimers()
       
   136     {
       
   137 	iRetryTimer->Cancel();
       
   138 	iCancelTimer->Cancel();
       
   139     }       
       
   140     
       
   141 // ---------------------------------------------------------------------------
       
   142 // CUpnpTcpSessionReader::RunL
       
   143 // From class CActive.
       
   144 // Function is called as a callback when the issued reading is completed.
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 void CUpnpTcpSessionReader::RunL()
       
   148     {
       
   149     LOGS2( "%i, CUpnpTcpSessionReader::RunL(), iStatus %i",
       
   150     		iSession.Id(), iStatus.Int() );
       
   151 
       
   152     // Active object request complete handler
       
   153     switch ( iStatus.Int() )
       
   154         {
       
   155         case KErrNone:
       
   156            	iRetryErrorCount = 0;
       
   157            	iCancelTimer->Cancel();
       
   158 
       
   159             // Forward read data to HTTPSession 
       
   160             iSession.ReceivingCompletedL( iReceiveBuffer );
       
   161             break;
       
   162             
       
   163          case KErrCancel:
       
   164             if(iCancelFromTimer) 
       
   165                 {          
       
   166                 IssueRead();
       
   167                 iCancelFromTimer = EFalse;
       
   168                 }
       
   169             break;
       
   170        
       
   171         case KErrNoMemory:
       
   172         case KErrNotReady:
       
   173         case KErrInet6AddressExpired:
       
   174             if( iRetryErrorCount < KMaxRetryErrors )
       
   175 	            {
       
   176 	        	iRetryTimer->Cancel();
       
   177 				iRetryTimer->After( KRetryWaitTime * ++iRetryErrorCount, EFalse );
       
   178 				break;
       
   179 	            }
       
   180             
       
   181         default:
       
   182             iSession.HandleErrorL( iStatus.Int() );
       
   183             break;
       
   184         }
       
   185     }
       
   186 // -----------------------------------------------------------------------------
       
   187 // CUpnpTcpSessionWriter::RunError
       
   188 // RunError is called when RunL leaves.
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 TInt CUpnpTcpSessionReader::RunError( TInt aError )
       
   192     {
       
   193     LOGS2( "%i, CUpnpTcpSessionReader::RunError - %d"
       
   194     		, iSession.Id(), aError);
       
   195     return KErrNone;
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CUpnpTcpSessionReader::TimerEventL
       
   200 // Retry read
       
   201 // -----------------------------------------------------------------------------
       
   202 //        
       
   203 void CUpnpTcpSessionReader::TimerEventL( CUpnpNotifyTimer* aTimer )
       
   204     {
       
   205     if( aTimer == iCancelTimer )
       
   206         {
       
   207         iCancelFromTimer = ETrue;
       
   208         iSocket.CancelRecv();
       
   209         }
       
   210     else if( aTimer == iRetryTimer )
       
   211         {
       
   212         IssueRead();
       
   213         }
       
   214     }
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CUpnpTcpSessionReader::StartCancelTimer
       
   218 // -----------------------------------------------------------------------------
       
   219 //  
       
   220 void CUpnpTcpSessionReader::StartCancelTimer()
       
   221     {
       
   222     TInt timeout = iSession.TimeoutCurrentValue();
       
   223    	iCancelTimer->Cancel();
       
   224    	
       
   225    	timeout = timeout/2 - KTwoSec;
       
   226    	if( timeout <= 0 ) timeout = KOneSec;
       
   227    	
       
   228 	iCancelTimer->After( timeout, EFalse );
       
   229     iCancelFromTimer = EFalse;
       
   230     }
       
   231    
       
   232 // ---------------------------------------------------------------------------
       
   233 // CUpnpTcpSessionReader::IssueRead
       
   234 // Issues reading.
       
   235 // ---------------------------------------------------------------------------
       
   236 //
       
   237 void CUpnpTcpSessionReader::IssueRead()
       
   238     {
       
   239     LOGS1( "%i, CUpnpTcpSessionReader::IssueRead", iSession.Id());
       
   240     // Initiate a new read from socket into iBuffer
       
   241     if( IsActive() )
       
   242         {
       
   243         LOGS1( "%i, PANIC: CUpnpTcpSessionReader::IssueRead!", iSession.Id() );
       
   244         User::Panic( _L("CUpnpTcpSessionReader is active" ), KErrGeneral );    
       
   245         } 
       
   246                                                
       
   247     if( iSession.IsConnected() )
       
   248         {
       
   249       	LOGS1( "%i, CUpnpTcpSessionReader::IssueRead"
       
   250           	, iSession.Id());
       
   251           	
       
   252         Deque();
       
   253         CActiveScheduler::Add(this);
       
   254           	
       
   255         iReceiveBuffer.Zero();
       
   256         
       
   257         StartCancelTimer();
       
   258 
       
   259         iSocket.RecvOneOrMore( iReceiveBuffer, 0, iStatus, iLen );   
       
   260         SetActive();
       
   261         }
       
   262     }
       
   263 
       
   264     
       
   265 //  End of File