applayerpluginsandutils/httptransportplugins/httptransporthandler/ctcptransportlayer.cpp
branchRCL_3
changeset 13 26ce6fb6aee2
parent 0 b16258d2340f
child 18 5f1cd966e0d9
equal deleted inserted replaced
12:f21293830889 13:26ce6fb6aee2
     1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2001-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    23 #include "csocketreader.h"
    23 #include "csocketreader.h"
    24 #include "csocketwriter.h"
    24 #include "csocketwriter.h"
    25 
    25 
    26 _LIT(KTcpProtName, "tcp");
    26 _LIT(KTcpProtName, "tcp");
    27 
    27 
       
    28 const TInt KMaxHostResolverCacheCount = 8; // 8 Should be sufficient here as we can have maximum of 8 connections
       
    29                                            // at anytime to the host. And it is not neccassarly mean that 8 host resolvers
       
    30                                            // will be operating simulataneously. 
    28 CTcpTransportLayer* CTcpTransportLayer::NewL(TAny* aTransportConstructionParams)
    31 CTcpTransportLayer* CTcpTransportLayer::NewL(TAny* aTransportConstructionParams)
    29 /**	
    32 /**	
    30 	The factory constructor.
    33 	The factory constructor.
    31 	@param		aTransportConstructionParams	A pointer to the instantiaton 
    34 	@param		aTransportConstructionParams	A pointer to the instantiaton 
    32 												parameters
    35 												parameters
    52 	// Delete the socket connectors
    55 	// Delete the socket connectors
    53 	iConnectorStore.ResetAndDestroy();
    56 	iConnectorStore.ResetAndDestroy();
    54 
    57 
    55 	// Delete the socket controllers
    58 	// Delete the socket controllers
    56 	iControllerStore.ResetAndDestroy();
    59 	iControllerStore.ResetAndDestroy();
    57 
    60 	
       
    61 	
       
    62 	// Empty and close the host resolver cache.
       
    63 	EmptyHostResolverCache();
       
    64 	iHostResolverCache.Close();
       
    65 	
    58 	// Close the socket server session if owned
    66 	// Close the socket server session if owned
    59 	if( iOwnsConnection )
    67 	if( iOwnsConnection )
    60 		{
    68 		{
    61 		if( iConnection )
    69 		if( iConnection )
    62 			{
    70 			{
    71 
    79 
    72 	__FLOG_CLOSE;
    80 	__FLOG_CLOSE;
    73 	}
    81 	}
    74 
    82 
    75 CTcpTransportLayer::CTcpTransportLayer(MConnectionPrefsProvider& aTransLayerObserver)
    83 CTcpTransportLayer::CTcpTransportLayer(MConnectionPrefsProvider& aTransLayerObserver)
    76 : CHttpTransportLayer(), iConnectionPrefsProvider(aTransLayerObserver)
    84 : CHttpTransportLayer(), iConnectionPrefsProvider(aTransLayerObserver), iHostResolverCache(KMaxHostResolverCacheCount)
    77 /**	
    85 /**	
    78 	Constructor.
    86 	Constructor.
    79 */
    87 */
    80 	{
    88 	{
    81 	__FLOG_OPEN("http", "httptransporthandler.txt");
    89 	__FLOG_OPEN("http", "httptransporthandler.txt");
   189                     {
   197                     {
   190                     iControllerStore[i]->InputStream().Shutdown();
   198                     iControllerStore[i]->InputStream().Shutdown();
   191                     // Socket and controller will be deleted by itself
   199                     // Socket and controller will be deleted by itself
   192                     }
   200                     }
   193                 iConnectorStore.ResetAndDestroy();
   201                 iConnectorStore.ResetAndDestroy();
       
   202                 EmptyHostResolverCacheIfNeeded();
   194                 }
   203                 }
   195             }	        
   204             }	        
   196         }		
   205         }		
   197 
   206 
   198    // Create the socket connector
   207    // Create the socket connector
   330 	__ASSERT_DEBUG( found, User::Invariant() );
   339 	__ASSERT_DEBUG( found, User::Invariant() );
   331 	
   340 	
   332 	// Remove the socket connector from the store and compress the store.
   341 	// Remove the socket connector from the store and compress the store.
   333 	iConnectorStore.Remove(index);
   342 	iConnectorStore.Remove(index);
   334 	iConnectorStore.Compress();
   343 	iConnectorStore.Compress();
       
   344 	
       
   345 	// Empty the host resolver cache if needed
       
   346 	EmptyHostResolverCacheIfNeeded();
   335 	}
   347 	}
   336 
   348 
   337 /*
   349 /*
   338  *	Methods from MSocketControllerStore
   350  *	Methods from MSocketControllerStore
   339  */
   351  */
   353 	__ASSERT_DEBUG( found, User::Invariant() );
   365 	__ASSERT_DEBUG( found, User::Invariant() );
   354 	
   366 	
   355 	// Remove the socket controller from the store and compress the store.
   367 	// Remove the socket controller from the store and compress the store.
   356 	iControllerStore.Remove(index);
   368 	iControllerStore.Remove(index);
   357 	iControllerStore.Compress();
   369 	iControllerStore.Compress();
       
   370 	
       
   371    // Empty the host resolver cache if needed
       
   372     EmptyHostResolverCacheIfNeeded();
   358 	}
   373 	}
   359 
   374 
   360 /*
   375 /*
   361  *	Methods from MCommsInfoProvider
   376  *	Methods from MCommsInfoProvider
   362  */
   377  */
   472  			}
   487  			}
   473          }
   488          }
   474 	return ( iConnection != NULL );
   489 	return ( iConnection != NULL );
   475 	}
   490 	}
   476 
   491 
   477 
   492 void CTcpTransportLayer::HostResolverFromCache(RHostResolver& aResolver)
       
   493     {
       
   494     TInt count = iHostResolverCache.Count();
       
   495     if(count > 0)
       
   496         {
       
   497         RDebug::Printf("Returning the host resolver from cache...");
       
   498         // Returns the last host resolver that is added
       
   499         aResolver = iHostResolverCache[count - 1];
       
   500         iHostResolverCache.Remove(count - 1); // Remove from the cache.        
       
   501         }
       
   502     }
       
   503 
       
   504 void CTcpTransportLayer::AddToHostResolverCache(RHostResolver& aResolver)
       
   505     {
       
   506     if(iHostResolverCache.Append(aResolver) != KErrNone)
       
   507         {
       
   508         aResolver.Close();
       
   509         }
       
   510     }
       
   511 
       
   512 void CTcpTransportLayer::EmptyHostResolverCacheIfNeeded()
       
   513     {
       
   514     // Remove the host resolver if
       
   515     // 1/ if the Connector store is empty and
       
   516     // 2/ if the socket controller is empty
       
   517     // This is important to get the mobility and one click connectivity cases working
       
   518     // Otherwise the inconsistent behaviour will result as the RConnection & RSocketServ can be handled
       
   519     // entirely outside of the HTTP stack, ie; from the application
       
   520     if(iConnectorStore.Count() == 0 && iControllerStore.Count() == 0)
       
   521         {
       
   522         EmptyHostResolverCache();
       
   523         }
       
   524     }
       
   525 
       
   526 void CTcpTransportLayer::EmptyHostResolverCache()
       
   527     {
       
   528     TInt count = iHostResolverCache.Count();
       
   529     while(count > 0)
       
   530         {
       
   531         // Close the RHostResolver and remove from the array
       
   532         iHostResolverCache[count - 1].Close();
       
   533         iHostResolverCache.Remove(--count);
       
   534         }
       
   535     iHostResolverCache.Compress();
       
   536     }