Msrp/MsrpServer/src/CMSRPConnection.cpp
branchMSRP_FrameWork
changeset 25 505ad3f0ce5c
child 58 cdb720e67852
equal deleted inserted replaced
22:f1578314b8da 25:505ad3f0ce5c
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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 * Initial Contributors:
       
     9 * Nokia Corporation - initial contribution.
       
    10 * Contributors:
       
    11 *
       
    12 * Description:
       
    13 * MSRP Implementation
       
    14 *
       
    15 */
       
    16 
       
    17 // CLASS HEADER
       
    18 #include "CMSRPConnection.h"
       
    19 #include "MSRPCommon.h"
       
    20 // INTERNAL INCLUDES
       
    21 #include "CMSRPConnector.h"
       
    22 #include "CMSRPReader.h"
       
    23 #include "CMSRPParser.h"
       
    24 #include "CMSRPWriter.h"
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // Constructor
       
    28 // -----------------------------------------------------------------------------
       
    29 //
       
    30 CMSRPConnection::CMSRPConnection( TInetAddr& aRemoteAddr, MMSRPConnectionManager& aConnMngr ): iHostAddress(aRemoteAddr), iConnMngr(aConnMngr)
       
    31     {
       
    32     MSRPLOG( "CMSRPConnection::CMSRPConnection enter" )
       
    33     MSRPLOG( "CMSRPConnection::CMSRPConnection exit" )
       
    34     }
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CMSRPConnection::~CMSRPConnection
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 CMSRPConnection::~CMSRPConnection()
       
    41 	{
       
    42 	MSRPLOG( "CMSRPConnection::~CMSRPConnection enter" )
       
    43 	iSubsessions.Reset();
       
    44 	iSubsessions.Close();
       
    45 	delete iListenTimer;
       
    46 	delete iConnector;
       
    47 	delete iWriter;
       
    48 	delete iParser;
       
    49 	delete iReader;  
       
    50 	//delete iBufPool;
       
    51 	if(iSocket)
       
    52 	    {
       
    53 	    iSocket->Close();
       
    54 	    delete iSocket;
       
    55 	    }
       
    56 	
       
    57 	MSRPLOG( "CMSRPConnection::~CMSRPConnection exit" )
       
    58 	}
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CMSRPConnection* CMSRPConnection::NewL
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 /*static*/ MMSRPConnection* CMSRPConnection::NewL( TInetAddr& aRemoteAddr, MMSRPConnectionManager& aConnMngr )
       
    65     {
       
    66     MSRPLOG( "CMSRPConnection::NewL enter" )
       
    67 
       
    68 	// Perform construction.
       
    69     CMSRPConnection* self = new ( ELeave ) CMSRPConnection(aRemoteAddr, aConnMngr);
       
    70     CleanupStack::PushL( self );
       
    71     self->ConstructL( );
       
    72     CleanupStack::Pop( self );
       
    73 
       
    74     MSRPLOG( "CMSRPConnection::NewL exit" )
       
    75     return self;
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CMSRPConnection::ConstructL
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 void CMSRPConnection::ConstructL( )
       
    83 	{
       
    84 	MSRPLOG( "CMSRPConnection::ConstructL enter" )
       
    85 	iConnectionState = ENotConnected;
       
    86 	iListenTimer = CMSRPTimeOutTimer::NewL( *this );
       
    87 	MSRPLOG( "CMSRPConnection::ConstructL exit" )	
       
    88 	}
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CMSRPConnection::CheckIfConnected
       
    92 // listening state check for validating listener connections 
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 TBool CMSRPConnection::CheckConnection(TInetAddr& aRemoteAddr, TBool aOnlyListen)
       
    96 	{
       
    97 	MSRPLOG( "CMSRPConnection::CheckConnection enter" )
       
    98 	TUint port_stored = iHostAddress.Port();
       
    99 	TUint port_connect = aRemoteAddr.Port();
       
   100 	
       
   101 	//if (iHostAddress.CmpAddr(aRemoteAddr))//v4, v6 issue	
       
   102 	if (iHostAddress.Match(aRemoteAddr) && port_stored == port_connect)
       
   103 	    {
       
   104 	    if (aOnlyListen && iConnectionState == EListening) //check if listening
       
   105 	        return ETrue;	    
       
   106 	    else if (!aOnlyListen /*&& iConnectionState <= EConnected */) //not connected, connecting, listening, connected
       
   107 	        return ETrue;	   
       
   108 	    /*else
       
   109 	        return EFalse;*/ //timed out, disconnecting, disconnected, create a new conn object
       
   110 	    }
       
   111 	MSRPLOG( "CMSRPConnection::CheckConnection exit" )	
       
   112 	return EFalse;
       
   113 	}
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CMSRPConnection::ConnectL
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 TInt CMSRPConnection::ConnectL(MMSRPConnectionObserver& aSubsession)
       
   120 	{
       
   121 	MSRPLOG("CMSRPConnection::ConnectL() enter")
       
   122 	//subsession to ensure it does not reissue
       
   123 	//connection does not check for pre-existent observers
       
   124 	
       
   125 	iSubsessions.AppendL(&aSubsession);	
       
   126 	
       
   127 	switch(iConnectionState)
       
   128 	    {
       
   129 	    case ENotConnected:
       
   130 	        //create connector and issue connect
       
   131 	        //delete iConnector;
       
   132 	        iConnector = CMSRPConnector::NewL( iConnMngr, *this );
       
   133 	        iConnector->Connect(iHostAddress);
       
   134 	        iConnectionState = EConnecting;
       
   135 	        
       
   136 	        break;
       
   137 	        
       
   138 	    case EConnecting:        
       
   139 	        break;
       
   140 	        
       
   141 	    case EListening: //as of now, dont try to issue connect,complete when other party connects
       
   142 	        //TBD
       
   143 	        //later change state to connecting and issue connect
       
   144 	        //if connect completes before listen, then change state to connected
       
   145 	        // if at all listen completes on same conn object, then check if state == connected and drop listen conn
       
   146 	        // do vice versa if listen completes before connect	        	        
       
   147 	        break;
       
   148 	        
       
   149 	    case EConnected:
       
   150             break;
       
   151             
       
   152 	    case EListenTimedOut:
       
   153 	    case EConnectTimedOut:
       
   154 	    case EError:
       
   155 	        //shudn't reach here, if connect issued immediately after/within getConn
       
   156 	        //User::Leave(MMSRPConnectionObserver::EConnectionStateUnexpected);
       
   157 	        break;                	        
       
   158 	    
       
   159 	    }
       
   160 	MSRPLOG("CMSRPConnection::ConnectL() exit")
       
   161 	return iConnectionState;
       
   162 	}
       
   163 		
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CMSRPConnection::ListenL
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 TInt CMSRPConnection::ListenL(MMSRPConnectionObserver& aSubsession)
       
   170     {
       
   171     MSRPLOG("CMSRPConnection::ListenL() enter")
       
   172         
       
   173     iSubsessions.AppendL(&aSubsession);
       
   174         
       
   175     
       
   176     switch(iConnectionState)
       
   177         {
       
   178         case ENotConnected:
       
   179             iConnectionState = EListening;
       
   180             iConnMngr.ListenL(this);
       
   181             //iListenTimer->After( KListenTimeoutInSeconds * KSecondinMicroseconds );
       
   182             
       
   183             break;
       
   184             
       
   185         case EConnecting: //dont issue listen, drop conn if other party connects, change later          
       
   186             
       
   187             //added check if listening in check connection to prevent accepting conn, 
       
   188             //simply allowing will corrupt listen count, TBD: introduce another EListeningandConnecting state                        
       
   189             break;
       
   190             
       
   191         case EListening:                      
       
   192             break;
       
   193             
       
   194         case EConnected:
       
   195             break;
       
   196                 
       
   197         case EListenTimedOut:
       
   198         case EConnectTimedOut:
       
   199         case EError:
       
   200             //shudn't reach here, if connect issued immediately after/within getConn
       
   201             //User::Leave(MMSRPConnectionObserver::EConnectionStateUnexpected);
       
   202             break;                         
       
   203         
       
   204         }
       
   205     MSRPLOG("CMSRPConnection::ListenL() exit")
       
   206     return iConnectionState;
       
   207     
       
   208     }
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // CMSRPConnection::ReleaseConnection
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 void CMSRPConnection::ReleaseConnection(MMSRPConnectionObserver& aSubsession)
       
   215     {
       
   216     MSRPLOG("CMSRPConnection::ReleaseConnection() enter")
       
   217     
       
   218     Remove(&aSubsession);
       
   219         
       
   220     if(iSubsessions.Count())
       
   221         {
       
   222         MSRPLOG("CMSRPConnection::ReleaseConnection() exit")
       
   223         return;
       
   224         }
       
   225     
       
   226     //if no subsessions (TBD : start timeout timer and disconnect on timeout)
       
   227     /*if(iConnectionState == EConnecting)
       
   228         {
       
   229             //iConnector->CancelConnect();
       
   230             delete iConnector;
       
   231             iConnector = NULL;
       
   232         }
       
   233     else if(iConnectionState == EConnected)*/
       
   234                 
       
   235     CloseConnection();
       
   236         
       
   237     
       
   238     //iConnectionState = ENotConnected;
       
   239     MSRPLOG("CMSRPConnection::ReleaseConnection() exit")
       
   240     return;
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CMSRPConnection::ConnectionEstablishedL
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 void CMSRPConnection::ConnectionEstablishedL( TInt aNewState, RSocket* aSocket, TInt aStatus )
       
   248     {
       
   249     MSRPLOG("CMSRPConnection::ConnectionEstablishedL() enter")
       
   250     iListenTimer->Cancel();
       
   251     //if not error
       
   252     if(aNewState == EConnected)
       
   253         {
       
   254         iConnectionState = aNewState;
       
   255         iSocket = aSocket;
       
   256         if(iConnector)
       
   257            {
       
   258            iConnector->ConnectComplete();
       
   259            /*dont delete now,as in case of leave, connector needs 2 b available*/
       
   260            //delete iConnector;
       
   261            //iConnector = NULL;
       
   262            }
       
   263         iWriter = CMSRPWriter::NewL(*iSocket , *this);               
       
   264         iParser = CMSRPParser::NewL(*this);
       
   265         iReader = CMSRPReader::NewL(*iSocket , *this);           
       
   266              
       
   267         iReader->StartReceivingL(iBufPool.ProvideBufferL());
       
   268         }
       
   269 
       
   270     /*not deleting connector as yet, notify all could leave in subsession  and also delete conn on conn error*/
       
   271     //intimate all subsessions
       
   272     NotifyAllL(aNewState, aStatus );
       
   273     
       
   274     //on error  or connect timeout close connection, i.e. intimate all subsessions and delete connection obj
       
   275     if(aStatus != KErrNone)
       
   276         {
       
   277         //listenaccepted does not comeback with error intimations, hence safe to decrement listen count
       
   278         //currently no risk of double decrementing listener count
       
   279         //CloseConnection();
       
   280         }
       
   281     
       
   282 
       
   283       
       
   284     MSRPLOG("CMSRPConnection::ConnectionEstablishedL() exit")   
       
   285     }
       
   286 
       
   287 TInt CMSRPConnection::getConnectionState()
       
   288     {
       
   289     return iConnectionState;
       
   290     // iConnection state should not be Tint, it should be MMSRPConnection::TMSRPConnectionState.
       
   291     }
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CMSRPConnection::ReadStatus
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 void CMSRPConnection::ReadStatusL(RMsrpBuf& aBuf, TInt aStatus)
       
   298     {
       
   299     MSRPLOG("CMSRPConnection::ReadStatusL() enter")
       
   300     if(aStatus == KErrNone)
       
   301         {
       
   302         //post to parser and return immediate
       
   303         iParser->ParseL(aBuf);
       
   304         
       
   305         RMsrpBuf copy(aBuf);
       
   306         if(copy.MsrpRightExtract() > KThreshold)
       
   307             {
       
   308             iReader->StartReceivingL(copy);
       
   309             }   
       
   310         else
       
   311             {
       
   312             iReader->StartReceivingL(iBufPool.ProvideBufferL());
       
   313             }
       
   314         return;
       
   315         }
       
   316     //read error or connection error
       
   317     //intimate all subsessions
       
   318     NotifyAllL(EError, aStatus );
       
   319     //CloseConnection();
       
   320     
       
   321     MSRPLOG("CMSRPConnection::ReadStatusL() exit")     
       
   322     }
       
   323 
       
   324 void CMSRPConnection::ParseStatusL (CMSRPMessageHandler* aMsg, TInt aStatus)
       
   325     {
       
   326     MSRPLOG("CMSRPConnection::ParseStatus enter")
       
   327     if (aStatus != KErrNone)
       
   328         {
       
   329         NotifyAllL(EError, aStatus );
       
   330         //CloseConnection();
       
   331         return;
       
   332         }
       
   333     
       
   334     /*if error, count is zero*/
       
   335     //while msg not consumed try to handover    
       
   336     TBool consumed = FALSE;
       
   337     for ( TInt i = 0; (!consumed) && i < iSubsessions.Count(); i++ )
       
   338          {
       
   339          consumed = iSubsessions[ i ]->MessageReceivedL( aMsg );
       
   340          }
       
   341     
       
   342     //if unclaimed by any subsession, callback to first subsession
       
   343     if(!consumed && iSubsessions.Count())
       
   344         {
       
   345         iSubsessions[0]->UnclaimedMessageL( aMsg );
       
   346         }
       
   347           
       
   348     MSRPLOG("CMSRPConnection::ParseStatus exit")
       
   349     }
       
   350 
       
   351 // -----------------------------------------------------------------------------
       
   352 // CMSRPConnection::WriteSocketError
       
   353 // -----------------------------------------------------------------------------
       
   354 //
       
   355 void CMSRPConnection::WriteSocketError(TInt aStatus)
       
   356     {
       
   357     MSRPLOG("CMSRPConnection::WriteSocketError() enter")
       
   358     TInt err =0;
       
   359     TRAP(err, NotifyAllL(EError, aStatus));
       
   360     //CloseConnection();
       
   361     MSRPLOG("CMSRPConnection::WriteSocketError() exit")
       
   362     }
       
   363 
       
   364 // -----------------------------------------------------------------------------
       
   365 // CMSRPConnection::Send
       
   366 // -----------------------------------------------------------------------------
       
   367 //
       
   368 void CMSRPConnection::SendL(MMSRPWriterObserver& aMsg)
       
   369     {
       
   370     MSRPLOG("CMSRPConnection::SendL() enter")
       
   371     if(iConnectionState == EConnected)
       
   372         iWriter->RequestSendL(aMsg);
       
   373     MSRPLOG("CMSRPConnection::SendL() exit") 
       
   374     }
       
   375 
       
   376 // -----------------------------------------------------------------------------
       
   377 // CMSRPConnection::TimerExpired
       
   378 // -----------------------------------------------------------------------------
       
   379 //
       
   380 void CMSRPConnection::TimerExpiredL()   
       
   381     {
       
   382     MSRPLOG("CMSRPConnection::TimerExpired() enter")
       
   383     if(iConnectionState == EListening)
       
   384         NotifyAllL(EListenTimedOut, KErrTimedOut);
       
   385     else 
       
   386         NotifyAllL(EConnectTimedOut, KErrTimedOut);
       
   387     //iConnMngr.ListenCancel(this);
       
   388     MSRPLOG("CMSRPConnection::TimerExpired() exit")
       
   389     }
       
   390 
       
   391 
       
   392 // -----------------------------------------------------------------------------
       
   393 // CMSRPConnection::Remove
       
   394 // -----------------------------------------------------------------------------
       
   395 //
       
   396 void CMSRPConnection::Remove(MMSRPConnectionObserver* aSubsession)
       
   397     {
       
   398     MSRPLOG("CMSRPConnection::Remove() enter")
       
   399     TInt index = iSubsessions.Find(aSubsession);    
       
   400     //iSubsessions[index].DisconnectIssued;
       
   401     if(index != KErrNotFound)
       
   402         iSubsessions.Remove(index);
       
   403     MSRPLOG("CMSRPConnection::Remove() exit")
       
   404     }
       
   405 
       
   406 // -----------------------------------------------------------------------------
       
   407 // CMSRPConnection::CloseConnection
       
   408 // -----------------------------------------------------------------------------
       
   409 //
       
   410 void CMSRPConnection::CloseConnection()
       
   411     {
       
   412     MSRPLOG("CMSRPConnection::CloseConnection() enter")
       
   413     //for connected no listen cancel needed
       
   414     //for listener error listener deleted
       
   415     if(/*iConnectionState == EListenConnected ||*/ iConnectionState == EListening || iConnectionState == EListenTimedOut)
       
   416         iConnMngr.ListenCancel(this);
       
   417     else
       
   418         //deleting connection, takes care of issuing cancel connect on connector also (if connecting)
       
   419         iConnMngr.Remove(this);
       
   420     MSRPLOG("CMSRPConnection::CloseConnection() exit")
       
   421     }
       
   422 
       
   423 
       
   424 
       
   425 // -----------------------------------------------------------------------------
       
   426 // CMSRPConnection::NotifyAll
       
   427 // -----------------------------------------------------------------------------
       
   428 //
       
   429 void CMSRPConnection::NotifyAllL(TInt aNewState, TInt aStatus ) 
       
   430     {
       
   431     MSRPLOG("CMSRPConnection::NotifyAll() enter")
       
   432     iConnectionState = aNewState;
       
   433     for ( TInt i = 0; i < iSubsessions.Count(); i++ )
       
   434          {
       
   435          //make sure connectionstate does not release in notify
       
   436          //else returns a value to reflect the same
       
   437          iSubsessions[ i ]->ConnectionStateL( aNewState, aStatus );
       
   438          }
       
   439     MSRPLOG("CMSRPConnection::NotifyAll() exit")
       
   440     }
       
   441 
       
   442 
       
   443 // End of File