applayerpluginsandutils/httptransportplugins/httptransporthandler/csocketconnector.cpp
changeset 0 b16258d2340f
child 22 26ce6fb6aee2
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "csocketconnector.h"
       
    17 
       
    18 #include "csocket.h"
       
    19 #include "msocketconnectobserver.h"
       
    20 #include "msocketconnectorstore.h"
       
    21 #include "msocketcontrollerfactory.h"
       
    22 #include "mcommsinfoprovider.h"
       
    23 #include "csocketcontroller.h"
       
    24 #include "httptransporthandlercommon.h"
       
    25 #include "thttptrlayerpanic.h"
       
    26 
       
    27 #if defined (_DEBUG) && defined (_LOGGING)
       
    28 const TInt KHostNameSize	= 126;
       
    29 #endif
       
    30 
       
    31 CSocketConnector* CSocketConnector::NewL(
       
    32 										MSocketConnectorStore&		aStore,
       
    33 										MSocketControllerFactory&	aSocketControllerFactory,
       
    34 										MCommsInfoProvider&			aCommsInfoProvider, TBool aPriority
       
    35 										)
       
    36 /**	
       
    37 	The factory constructor.
       
    38 	@param		aStore						The socket connector store.
       
    39 	@param		aSocketControllerFactory	The socket controller factory.
       
    40 	@param		aCommsInfoProvider			The comms info provider.
       
    41 	@return		A pointer to a fully constructed object.
       
    42 */
       
    43 	{
       
    44 	return new (ELeave) CSocketConnector(aStore, aSocketControllerFactory, aCommsInfoProvider,aPriority);
       
    45 	}
       
    46 
       
    47 CSocketConnector::~CSocketConnector()
       
    48 /**	
       
    49 	Destructor.
       
    50 */
       
    51 	{
       
    52 	// Cancel any outstanding requests
       
    53 	Cancel();
       
    54 
       
    55 	// Cleanup...
       
    56 	delete iHost;
       
    57 	delete iConnectingSocket;
       
    58 	iHostResolver.Close();
       
    59 
       
    60 //	__FLOG_CLOSE;
       
    61 	}
       
    62 
       
    63 CSocketConnector::CSocketConnector(
       
    64 								  MSocketConnectorStore&	aStore,
       
    65 								  MSocketControllerFactory&	aSocketControllerFactory,
       
    66 								  MCommsInfoProvider&		aCommsInfoProvider,
       
    67 								  TBool aPriority
       
    68 								  )
       
    69 : CActive(aPriority ? CActive::EPriorityHigh : CActive::EPriorityStandard), iStore(aStore),
       
    70 									   iSocketControllerFactory(aSocketControllerFactory),
       
    71 									   iCommsInfoProvider(aCommsInfoProvider)
       
    72 /**	
       
    73 	Constructor.
       
    74 	@param		aStore						The socket connector store.
       
    75 	@param		aSocketControllerFactory	The socket controller factory.
       
    76 	@param		aCommsInfoProvider			The comms info provider.
       
    77 */	{
       
    78 	if(aPriority)
       
    79 		{
       
    80 		CActive::SetPriority(CActive::EPriorityHigh);	
       
    81 		}
       
    82 	
       
    83 	CActiveScheduler::Add(this);
       
    84 
       
    85 //	__FLOG_OPEN("http", "httptransporthandler.txt");
       
    86 	}
       
    87 
       
    88 void CSocketConnector::ConnectL(MSocketConnectObserver& aObserver, const TDesC8& aRemoteHost, TUint16 aRemotePort, TInetAddr* aRemoteAddress)
       
    89 /**	
       
    90 	Start connection to specified remote host. The socket connector starts 
       
    91 	connecting to the specified remote host on the specified port.
       
    92 	@param		aRemoteHost		The host name/IP address of the remote host
       
    93 	@param		aRemotePort		The port number of the remote host
       
    94 	@pre		The socket connector is in the Idle state.
       
    95 	@post		The socket connector is not in the Idle state.
       
    96 */
       
    97 	{
       
    98 	__ASSERT_DEBUG( iState == EIdle, THttpTrLayerPanic::Panic(THttpTrLayerPanic::EBadSocketConnectorState) );
       
    99 
       
   100 	// Set the observer
       
   101 	iObserver = &aObserver;
       
   102 
       
   103 	// Copy the remote host IP address and port
       
   104 	iHost = HBufC::NewL(aRemoteHost.Length());
       
   105 	iHost->Des().Copy(aRemoteHost);
       
   106 	iPort = aRemotePort;
       
   107 
       
   108     // Move to the PendingDNSLookup state and self complete.
       
   109     if(aRemoteAddress == NULL)
       
   110         {
       
   111         // Address is unknown / DNS lookup is needed
       
   112         iState = EPendingDNSLookup;
       
   113         }
       
   114     else
       
   115         {
       
   116         // Address is know. No lookup is needed. Just go and connect.
       
   117         iHostDnsEntry().iAddr = *aRemoteAddress;
       
   118         iState = EConnecting;
       
   119         }
       
   120 	CompleteSelf();
       
   121 	}
       
   122 
       
   123 void CSocketConnector::CompleteSelf()
       
   124 /**	
       
   125 	Requests that the socket connector complete itself. This will caused the 
       
   126 	RunL() to be called by the scheduler at the next opportunity.
       
   127 	@pre		The socket connector object is not active.
       
   128 	@post		The socket connector object is active and the request has been
       
   129 				completed.
       
   130 */
       
   131 	{
       
   132 	TRequestStatus* pStat = &iStatus;
       
   133 	User::RequestComplete(pStat, KErrNone);
       
   134 	SetActive();
       
   135 	}
       
   136 
       
   137 void CSocketConnector::Suicide()
       
   138 /**	
       
   139 	The socket connector is finished, it needs to remove itself from the store
       
   140 	and self-destruct.
       
   141 */
       
   142 	{
       
   143 	// Inform store that connection is complete and then suicide.
       
   144 	iStore.ConnectionCompleted(*this);
       
   145 	delete this;
       
   146 	}
       
   147 
       
   148 /*
       
   149  *	Methods from MSocketConnector
       
   150  */
       
   151 
       
   152 void CSocketConnector::StopConnect()
       
   153 /**
       
   154 	@see		MSocketConnector
       
   155 */
       
   156 	{
       
   157 	// Cancel any outstanding requests.
       
   158 	Cancel();
       
   159 
       
   160 #if defined (_DEBUG) && defined (_LOGGING)
       
   161 	TBuf8<KHostNameSize> host;
       
   162 	host.Copy((*iHost).Left(KHostNameSize)); //just get the KHostNameSize characters
       
   163 
       
   164 	__FLOG_0(_T8("!! Stopping connection"));
       
   165 	__FLOG_1(_T8("-> connect to host %S stopped"), &host);
       
   166 #endif
       
   167 
       
   168 	Suicide();
       
   169 	}
       
   170 
       
   171 void CSocketConnector::MSocketConnector_Reserved()
       
   172 /**	
       
   173 	@see		MSocketConnector
       
   174 */
       
   175 	{
       
   176 	User::Invariant();
       
   177 	}
       
   178 
       
   179 /*
       
   180  *	Methods from CActive
       
   181  */
       
   182 
       
   183 void CSocketConnector::RunL()
       
   184 /**	
       
   185 	The request servicing function. Behaviour depends on the state of the socket
       
   186 	connector.
       
   187 */
       
   188 	{
       
   189 	// Leave if there has been an error
       
   190 	User::LeaveIfError(iStatus.Int());
       
   191 
       
   192 	switch( iState )
       
   193 		{
       
   194 	case EPendingDNSLookup:
       
   195 		{
       
   196 #if defined (_DEBUG) && defined (_LOGGING)
       
   197 		TBuf8<KHostNameSize> host;
       
   198 		host.Copy((*iHost).Left(KHostNameSize)); //just get the KHostNameSize characters
       
   199 
       
   200 		__FLOG_1(_T8("Doing DNS lookup -> searching for host %S"), &host);
       
   201 #endif
       
   202 
       
   203 		__OOM_LEAVE_TEST
       
   204 
       
   205 		if ( iCommsInfoProvider.HasConnection() )
       
   206 			{
       
   207 			// Open the host resolver session with the preffered connection
       
   208 			User::LeaveIfError(iHostResolver.Open(
       
   209 												 iCommsInfoProvider.SocketServer(),
       
   210 												 iCommsInfoProvider.ProtocolDescription().iAddrFamily, 
       
   211 												 KProtocolInetUdp,
       
   212 												 iCommsInfoProvider.Connection()
       
   213 												 ));				
       
   214 			}
       
   215 		else
       
   216 			{							
       
   217 			// Open the host resolver session with no connection
       
   218 			User::LeaveIfError(iHostResolver.Open(
       
   219 												 iCommsInfoProvider.SocketServer(),
       
   220 												 iCommsInfoProvider.ProtocolDescription().iAddrFamily, 
       
   221 												 KProtocolInetUdp
       
   222 												 ));				
       
   223 			}
       
   224 
       
   225 		// Start the DNS lookup for the remote host name.
       
   226 		iHostResolver.GetByName(*iHost, iHostDnsEntry, iStatus);
       
   227 
       
   228 		// Move to the Connecting state and go active
       
   229 		iState = EConnecting;
       
   230 		SetActive();
       
   231 		} break;
       
   232 	case EConnecting:
       
   233 		{
       
   234 		__OOM_LEAVE_TEST
       
   235 
       
   236 		// DNS lookup successful - form the internet address object
       
   237 		iAddress = TInetAddr(iHostDnsEntry().iAddr);
       
   238 		iAddress.SetPort(iPort);
       
   239 
       
   240 #if defined (_DEBUG) && defined (_LOGGING)
       
   241 		TBuf8<KHostNameSize> host;
       
   242 		host.Copy((*iHost).Left(KHostNameSize)); //just get the KHostNameSize characters
       
   243 
       
   244 		TBuf<KIpv6MaxAddrSize> ip16bit;
       
   245 		iAddress.Output(ip16bit);
       
   246 
       
   247 		TBuf8<KIpv6MaxAddrSize> ip;
       
   248 		ip.Copy(ip16bit);
       
   249 		
       
   250 		__FLOG_2(_T8("DNS lookup complete -> host %S has IP address %S"), &host, &ip);
       
   251 #endif
       
   252 		
       
   253 		// Start a default RConnection, if one is not started and not local loopback address		
       
   254 		if ( !iCommsInfoProvider.HasConnection() && !iAddress.IsLoopback() )
       
   255 			{
       
   256 			iCommsInfoProvider.StartDefaultCommsConnectionL ();				
       
   257 			}
       
   258 
       
   259 		// Create the connecting socket
       
   260 		iConnectingSocket = CSocket::NewL(iCommsInfoProvider, CSocket::EProtocolSocket);
       
   261 
       
   262 		// Start connecting to the remote client
       
   263 		iConnectingSocket->Connect(iAddress, iStatus);
       
   264 
       
   265 		__FLOG_2(_T8("Connecting -> to host %S on IP address %S"), &host, &ip);
       
   266 
       
   267 		// Move to the Connected state and go active
       
   268 		iState = EConnected;
       
   269 		SetActive();
       
   270 		} break;
       
   271 	case EConnected:
       
   272 		{
       
   273 		__OOM_LEAVE_TEST
       
   274 
       
   275 #if defined (_DEBUG) && defined (_LOGGING)
       
   276 		TBuf8<KHostNameSize> host;
       
   277 		host.Copy((*iHost).Left(KHostNameSize)); //just get the KHostNameSize characters
       
   278 
       
   279 		TInetAddr addr;
       
   280 		iConnectingSocket->RemoteName(addr);
       
   281 
       
   282 		TBuf<KIpv6MaxAddrSize> ip16bit;
       
   283 		addr.Output(ip16bit);
       
   284 
       
   285 		TBuf8<KIpv6MaxAddrSize> ip;
       
   286 		ip.Copy(ip16bit);
       
   287 
       
   288 		TInetAddr local;
       
   289 		iConnectingSocket->LocalName(local);
       
   290 
       
   291 		__FLOG_1(_T8("!! Connection with host %S established"), &host);
       
   292 		__FLOG_3(_T8("-> on local port %d with %S, remote port %d"), local.Port(), &ip, addr.Port());
       
   293 #endif
       
   294 
       
   295 		// A connection has been made with the remote client - lose ownership of
       
   296 		// the connecting socket.
       
   297 		CSocket* connectedSocket = iConnectingSocket;
       
   298 		iConnectingSocket = NULL;
       
   299 
       
   300 		// Create a socket controller object to own the connected socket.
       
   301 		CSocketController* socketController = iSocketControllerFactory.CreateSocketControllerLC(connectedSocket, *iHost, iPort, iAddress);
       
   302 
       
   303 		// Inform the socket connect observer that a TCP connection is established - 
       
   304 		// pass back the input and output stream objects.
       
   305 		iObserver->ConnectionMadeL(socketController->InputStream(), socketController->OutputStream());
       
   306 
       
   307 		// Remove socket controller from cleanup stack - transferring ownership
       
   308 		// to the store.
       
   309 		CleanupStack::Pop(socketController);
       
   310 
       
   311 		// Add the socket controller in the store - ownership is transferred to
       
   312 		// the store.
       
   313 		iSocketControllerFactory.AddToStoreL(socketController);
       
   314 
       
   315 		// Socket connector is finished - suicide.
       
   316 		Suicide();
       
   317 		} break;
       
   318 	case EIdle:
       
   319 	default:
       
   320 		THttpTrLayerPanic::Panic(THttpTrLayerPanic::EBadSocketConnectorState);
       
   321 		break;
       
   322 		}
       
   323 	}
       
   324 
       
   325 void CSocketConnector::DoCancel()
       
   326 /**	
       
   327 	The asynchronous request cancel.
       
   328 */
       
   329 	{
       
   330 	// Check state
       
   331 	switch( iState )
       
   332 		{
       
   333 	case EConnecting:
       
   334 		{
       
   335 		// DNS lookup is pending - cancel
       
   336 		iHostResolver.Cancel();
       
   337 		} break;
       
   338 	case EConnected:
       
   339 		{
       
   340 		if( iConnectingSocket )
       
   341 			{
       
   342 			// Connection is pending - cancel and delete the socket
       
   343 			iConnectingSocket->CancelConnect();
       
   344 			delete iConnectingSocket;
       
   345 			iConnectingSocket = NULL;
       
   346 			}
       
   347 		} break;
       
   348 	case EIdle:
       
   349 	case EPendingDNSLookup:
       
   350 	default:
       
   351 		// Do nothing...
       
   352 		break;
       
   353 		}
       
   354 	}
       
   355 
       
   356 TInt CSocketConnector::RunError(TInt aError)
       
   357 /**	
       
   358 	The error handler for when RunL() leaves. This function does any necessary
       
   359 	cleanup. The socket connector is then set to suicide.
       
   360 	@param		aError	The leave code.
       
   361 	@return		A value of KErrNone.if the error has been handled, any other 
       
   362 				value if it has not been handled.
       
   363 	@post		The socket connector is in the Suicide state.
       
   364 */
       
   365 	{
       
   366 #if defined (_DEBUG) && defined (_LOGGING)
       
   367 	TBuf8<KHostNameSize> host;
       
   368 	host.Copy((*iHost).Left(KHostNameSize)); //just get the KHostNameSize characters
       
   369 
       
   370 	__FLOG_1(_T8("!! Error : %d"), aError);
       
   371 	__FLOG_1(_T8("-> connect to host %S failed"), &host);
       
   372 #endif
       
   373 
       
   374 	TInt errorToPropagate = aError;
       
   375 	TInt error = KErrNone;
       
   376 
       
   377 	switch( iState )
       
   378 		{
       
   379 	case EPendingDNSLookup:
       
   380 	case EConnecting:
       
   381 		{
       
   382 		// In EReadyForDNS or EReadyToConnect states, KErrNotReady may be 
       
   383 		// returned by the comms call that require a connection. The RConnection
       
   384 		// that is passed in MUST be started or this error will occur. If we own
       
   385 		// the RConnection we should start the RConnection again, else if our
       
   386 		// client own the RConnection, we just propagate the Comms error.
       
   387 		if( iCommsInfoProvider.HasConnection() && iCommsInfoProvider.OwnsConnection() && aError == KErrNotReady )
       
   388 			{
       
   389 			__FLOG_0(_T8("-> re-starting comms interface..."));
       
   390 
       
   391 			// We own the RConnection and the error is KErrNotReady
       
   392 			errorToPropagate = iCommsInfoProvider.Connection().Start();
       
   393 
       
   394 			if( errorToPropagate == KErrNone )
       
   395 				{
       
   396 				__FLOG_0(_T8("!! Re-started comms interface"));
       
   397 				__FLOG_1(_T8("-> try connecting to host %S again"), &host);
       
   398 
       
   399 				// RConnection started successfully, try to connect again
       
   400 				CompleteSelf();
       
   401 				return KErrNone;
       
   402 				}
       
   403 #if defined (_DEBUG) && defined (_LOGGING)
       
   404 			else
       
   405 				{
       
   406 				__FLOG_1(_T8("!! Error : %d"), errorToPropagate);
       
   407 				__FLOG_0(_T8("-> failed to re-start comms interface"));
       
   408 				__FLOG_1(_T8("-> connect to host %S failed"), &host);
       
   409 				}			
       
   410 #endif
       
   411 			}
       
   412 		// Opening the connecting socket has failed or the DNS lookup completed
       
   413 		// with an error code - suicide after notifying the observer - do nothing 
       
   414 		// except drop through to the Connected state case...
       
   415 		}
       
   416 	case EConnected:
       
   417 		{
       
   418 		// Either the socket controller factory left in AddToStoreL() or in
       
   419 		// CreateSocketControllerLC(), or the observer left in ConnectionMadeL()
       
   420 		// or the connect request completed with an error code. Inform the 
       
   421 		// observer of the error.
       
   422 		error = iObserver->HandleConnectError(errorToPropagate);
       
   423 		} break;
       
   424 	case EIdle:
       
   425 	default:
       
   426 		THttpTrLayerPanic::Panic(THttpTrLayerPanic::EBadSocketConnectorState);
       
   427 		break;
       
   428 		}
       
   429 
       
   430 	Suicide();
       
   431 
       
   432 	return error;
       
   433 	}