upnp/upnpstack/dlnawebserver/src/upnpsocketshutdown.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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "upnptcpsession.h"
       
    21 #include "upnptcpsessionreader.h"
       
    22 #include "upnptcpsessionwriter.h"
       
    23 #include "upnphttpfiletransferwriter.h"
       
    24 #include "upnphttpfiletransferreader.h"
       
    25 #include "upnpsocketshutdown.h"
       
    26 #include "upnpnotifytimer.h"
       
    27 
       
    28 
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CUpnpSocketShutdown::NewL
       
    32 //
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 CUpnpSocketShutdown* CUpnpSocketShutdown::NewL( CUpnpTcpSession& aSession, RSocket& aSocket, TThreadPriority aPriority )
       
    36     {
       
    37     CUpnpSocketShutdown* self = new ( ELeave ) CUpnpSocketShutdown( aSession, aSocket, aPriority );
       
    38     CleanupStack::PushL( self );
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop( self );
       
    41     return self;
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CUpnpSocketShutdown::CUpnpSocketShutdown
       
    46 //
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CUpnpSocketShutdown::CUpnpSocketShutdown( CUpnpTcpSession& aSession, RSocket& aSocket, TThreadPriority aPriority ):CActive( aPriority ),iSession( aSession ),iSocket( aSocket ),iInternalState(EUnknown), iSessionTimeouted( EFalse )
       
    50     {
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CUpnpSocketShutdown::ConstructL
       
    55 //
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 void CUpnpSocketShutdown::ConstructL()
       
    59     {
       
    60     CActiveScheduler::Add( this );
       
    61     iInputTimeoutTimer = CUpnpNotifyTimer::NewL( this );
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CUpnpSocketShutdown::~CUpnpSocketShutdown
       
    66 //
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CUpnpSocketShutdown::~CUpnpSocketShutdown()
       
    70     {
       
    71     Cancel();
       
    72     delete iInputTimeoutTimer;
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CUpnpSocketShutdown::ShutdownImmediate
       
    77 // Disconnect connection
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 void CUpnpSocketShutdown::ShutdownImmediate()
       
    81     {
       
    82     iSession.iTcpReader->Cancel();       
       
    83     iSession.iTcpWriter->Cancel();      
       
    84     if( iSession.iFTReader )
       
    85         {
       
    86         iSession.iFTReader->Cancel();
       
    87         }
       
    88     if( iSession.iFTWriter )
       
    89         {
       
    90         iSession.iFTWriter->Cancel();
       
    91         }
       
    92     iSocket.Shutdown( iSocket.EImmediate, iStatus );
       
    93     iInternalState = ENotConnected;                
       
    94     SetActive();    
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CUpnpSocketShutdown::ShutdownStop
       
    99 // Disconnect connection
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 void CUpnpSocketShutdown::ShutdownStop()
       
   103     {
       
   104     if ( iInternalState == EUnknown )
       
   105         {
       
   106         iSession.iTcpReader->CancelTimers();
       
   107         if( iSession.iFTReader )
       
   108             {
       
   109             iSession.iFTReader->CancelTimers();
       
   110             }
       
   111         
       
   112         if( iSession.iTcpReader->IsActive() 
       
   113             || ( iSession.iFTReader && iSession.iFTReader->IsActive() ) 
       
   114           )
       
   115             {
       
   116             iSocket.Shutdown( iSocket.EStopOutput, iStatus );            
       
   117             iInternalState = EShuttingDownStopOutput ;               
       
   118             }
       
   119         else
       
   120             {
       
   121             iSocket.Shutdown( iSocket.ENormal, iStatus );
       
   122             iInternalState = EShuttingDownNormal;
       
   123             }
       
   124         SetActive();
       
   125         }
       
   126     }
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CUpnpSocketShutdown::MarksTimeout
       
   130 // Marks if timeout occured on session
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 void CUpnpSocketShutdown::MarksTimeout()
       
   134     {
       
   135     iSessionTimeouted = ETrue;
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CUpnpSocketShutdown::HandleError
       
   140 // Disconnect connection
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 void CUpnpSocketShutdown::HandleError( TInt /*aError*/ )
       
   144     {
       
   145     switch ( iInternalState )
       
   146         {
       
   147         case EUnknown: // error happened during reading/writing - start shutdown procedure
       
   148             ShutdownStop();
       
   149             break;
       
   150         case EShuttingDownStopOutput: // input error appeares while shutdown stop output is pending
       
   151                                       // when it returns shutdown immediate is invoked in RunL  
       
   152             iInternalState = EInputErrorReceived;
       
   153             break;
       
   154         case EWaitingForInputError: // error response from reader after shutdown stop output                  
       
   155             ShutdownImmediate();
       
   156             break;
       
   157         case EShuttingDownNormal: // do nothimg session is being closed
       
   158         case ENotConnected: // do nothing session is closed
       
   159         default:
       
   160             break;        
       
   161         }
       
   162     }
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CUpnpSocketShutdown::HandleError
       
   166 // Disconnect connection
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 void CUpnpSocketShutdown::RunL()
       
   170     {
       
   171     switch( iInternalState )
       
   172         {
       
   173         case EShuttingDownNormal: // shutdown normal response came
       
   174             ShutdownImmediate();
       
   175             break;
       
   176         case EShuttingDownStopOutput: // shutdown stop output response came
       
   177                                 // waiting for KErrEof from HandleError()                                
       
   178             if( !iSessionTimeouted )
       
   179                 {
       
   180                 iInternalState = EWaitingForInputError;                                
       
   181                 iInputTimeoutTimer->Cancel();
       
   182                 iInputTimeoutTimer->After(KSessionTimeout, EFalse);     
       
   183                 break;                
       
   184                 }
       
   185         case EInputErrorReceived: // input error came before stop output finished
       
   186             ShutdownImmediate();
       
   187             break;
       
   188         case ENotConnected:  // shutdown immediate response came          
       
   189             iSession.CloseSocketL();           
       
   190             break;
       
   191         default:
       
   192             break;    
       
   193         }
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CUpnpSocketShutdown::TimerEventL
       
   198 // Disconnect connection
       
   199 // -----------------------------------------------------------------------------
       
   200 //        
       
   201 void CUpnpSocketShutdown::TimerEventL( CUpnpNotifyTimer* /*aTimer*/ )
       
   202     {
       
   203      switch( iInternalState )
       
   204         {
       
   205         case EWaitingForInputError: // waiting for input error timeout 
       
   206             ShutdownImmediate();
       
   207             break;            
       
   208         default: 
       
   209             break;
       
   210         }
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CUpnpSocketShutdown::DoCancel
       
   215 // Disconnect connection
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CUpnpSocketShutdown::DoCancel()
       
   219     {
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CUpnpSocketShutdown::HandleError
       
   224 // Disconnect connection
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 TInt CUpnpSocketShutdown::RunError( TInt /*aError*/ )
       
   228     {
       
   229     return KErrNone;
       
   230     }
       
   231   
       
   232