📄 taskmanagerengine.cpp
字号:
{
return iIAPs;
}
// ----------------------------------------------------
// CTaskManagerEngine::ConnectL() (overloaded)
// First phase of initiating a connection between sockets.
// Handles DNS-lookup etc.
// ----------------------------------------------------
void CTaskManagerEngine::ConnectL()
{
if( iState == ENotConnected )
{
TInetAddr addr;
// determine if the server address is a valid ip address
if( addr.Input( iServer ) == KErrNone )
{
ConnectL( addr.Address() );
}
else // if not, we must try DNS-lookup
{
// open a dns-resolver session
User::LeaveIfError( iResolver.Open( iSocketServer, KAfInet, KProtocolInetUdp) );
// DNS-lookup request
iResolver.GetByName( iServer, iNameEntry, iStatus );
iState = ELookingUp;
iTimer->After( KTimeOut );
SetActive();
}
}
}
// ----------------------------------------------------
// CTaskManagerEngine::ConnectL() (overloaded)
// True connection function. Takes a valid IP-address
// as parameter (valid as in xxx.xxx.xxx.xxx form)
// ----------------------------------------------------
void CTaskManagerEngine::ConnectL( TUint32 aAddr )
{
if( iState == ENotConnected )
{
// open a new socket
User::LeaveIfError( iSocket.Open( iSocketServer,
KAfInet,
KSockStream,
KProtocolInetTcp,
iConnection ) );
// set correct port and address
iAddress.SetPort( iPort );
iAddress.SetAddress( aAddr );
// try to connect the socket
iSocket.Connect( iAddress, iStatus );
iState = EConnecting;
iTimer->After( KTimeOut );
SetActive();
}
}
// ----------------------------------------------------
// CTaskManagerEngine::DoCancel()
// Called when a cancel is done during activity.
// ----------------------------------------------------
//
void CTaskManagerEngine::DoCancel()
{
iTimer->Cancel();
switch( iState )
{
case EStartingGPRS:
{
iConnection.Stop();
break;
}
case ELookingUp:
{
iResolver.Cancel();
iResolver.Close();
iState = ENotConnected;
break;
}
case EConnecting:
{
iSocket.CancelConnect();
iSocket.Close();
iState = ENotConnected;
break;
}
case ESSLConnecting:
{
iSecureSocket->CancelHandshake();
EndConnection();
break;
}
case EWriting:
{
iSecureSocket->CancelSend();
iReader->Cancel();
EndConnection();
break;
}
default:
{
Panic( ETaskManagerInvalidState );
break;
}
};
}
// ----------------------------------------------------
// CTaskManagerEngine::RunL()
// Handles the states of the object and ensures everything
// is done in right order.
// ----------------------------------------------------
//
void CTaskManagerEngine::RunL()
{
iTimer->Cancel();
switch( iState )
{
// After GPRS service has been started (or failed to start)
case EStartingGPRS:
{
iState = ENotConnected;
if( iStatus == KErrNone )
{
iOpeningConnection = EFalse;
iTransactionObserver.ConnectingToServerL( (iOperation == TRequest::EFetchTasks ) );
ConnectL();
}
else
{
iTransactionObserver.ErrorL( KGPRSStartError );
}
break;
}
// After DNS resolver has finished
case ELookingUp:
{
iResolver.Close();
iState = ENotConnected;
if( iStatus == KErrNone ) // DNS-lookup was successful
{
iNameRecord = iNameEntry();
TBuf<15> ipAddr;
TInetAddr::Cast( iNameRecord.iAddr ).Output( ipAddr );
ConnectL( TInetAddr::Cast( iNameRecord.iAddr ).Address() );
}
else // DNS-lookup failed
{
iTransactionObserver.ErrorL( KLookupError );
}
break;
}
// After socket has finished connecting
case EConnecting:
{
// everything went as planned
if( iStatus == KErrNone )
{
iState = ESSLConnecting;
iSecureSocket = CSecureSocket::NewL( iSocket, KSSL3 );
iSecureSocket->StartClientHandshake( iStatus );
iTimer->After( KTimeOut );
SetActive();
}
else // we couldn't open a connection
{
iSocket.Close();
iTransactionObserver.ErrorL( KConnectionError );
iState = ENotConnected;
}
break;
}
// After the SSL-handshake has been completed
case ESSLConnecting:
{
if( iStatus == KErrNone )
{
iState = EConnected;
iReader->SetSecureSocket( iSecureSocket );
// These parameters are formatted into a string that is saved to iWriteBuffer
TRequest::GetMessage( iUsername, iPassword, iOperation, iMarkId, iWriteBuffer );
// We begin reading just before writing just so we won't miss anything
iReader->Start();
// We send the command data to the server
iSecureSocket->Send( iWriteBuffer, iStatus );
iState = EWriting;
// Start a timer
iTimer->After( KTimeOut );
SetActive();
}
else
{
// CSecureSocket can be instantiated only after a socket has been connected so
// it must be deleted after every connection
iSecureSocket->Close(); // this closes the regular socket as well
delete iSecureSocket;
iSecureSocket = NULL;
iState = ENotConnected;
iTransactionObserver.ErrorL( KSSLConnError );
}
break;
}
case EWriting:
{
if( iStatus == KErrNone )
{
iTimer->After( KTimeOut ); // So we won't wait forever for a response
iState = EReading;
}
else
{ // writing failed
iState = EConnected;
iTransactionObserver.ErrorL( KWriteError );
}
break;
}
default:
{
User::Leave( ETaskManagerInvalidState );
}
};
}
TInt CTaskManagerEngine::RunError( TInt /*aError*/ )
{
return KErrNone;
}
// ----------------------------------------------------
// CTaskManagerEngine::GPRSConnectL()
// Will open up a (GPRS) connection. We open it using the
// IAP the user chose at the beginning. (View)
// ----------------------------------------------------
//
void CTaskManagerEngine::GPRSConnectL()
{
TBool connected = EFalse;
// Lets first check are we already connected.
TUint connectionCount;
User::LeaveIfError(iConnection.EnumerateConnections(connectionCount));
TPckgBuf<TConnectionInfoV2> connectionInfo;
for (TUint i = 1; i <= connectionCount; i++)
{
User::LeaveIfError(iConnection.GetConnectionInfo(i, connectionInfo));
if (connectionInfo().iIapId == iIap)
{
connected = ETrue;
break;
}
}
// Not yet connected, start connection
if (!connected)
{
//Define preferences for connection
TCommDbConnPref prefs;
prefs.SetIapId(iIap);
prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
//Start Connection
iTransactionObserver.OpeningConnectionL();
//Asynchronic call
iOpeningConnection = ETrue;
iConnection.Start(prefs, iStatus);
iState = EStartingGPRS;
iTimer->After( KTimeOut );
SetActive();
return;
}
ConnectL();
}
// ----------------------------------------------------
// CTaskManagerEngine::PackageReceivedL()
// The socket reader calls this function to pass the received
// data to the engine. The function passes it on to CResponse
// which does the actual parsing.
// ----------------------------------------------------
//
TBool CTaskManagerEngine::PackageReceivedL( const TDesC8& aData )
{
// Timer is stopped
iTimer->Cancel();
if( !iResponse )
{
iResponse = CResponse::NewL();
}
iResponse->InputDataL( aData );
switch( iResponse->GetState() )
{
case CResponse::ENotComplete:
{
iTimer->After( KTimeOut );
return ETrue;
}
case CResponse::EError:
{
iTransactionObserver.ErrorL( KErroneousPackage );
}
case CResponse::EComplete:
{
iTransactionObserver.SuccessL( *iResponse );
CheckRefreshL();
}
}
EndConnection();
return EFalse;
}
// ----------------------------------------------------
// CTaskManagerEngine::EndConnection()
// Ends the connection to the server in a controlled manner.
// ----------------------------------------------------
//
void CTaskManagerEngine::EndConnection()
{
delete iResponse;
iResponse = NULL;
if( iSecureSocket )
{
iSecureSocket->Close(); // also closes the normal socket
}
else
{
iSocket.Close();
}
delete iSecureSocket;
iSecureSocket = NULL;
iRunning = EFalse;
iState = ENotConnected;
}
// ----------------------------------------------------
// CTaskManagerEngine::TimerExpired()
// Timer calls this function to notify a timeout with
// connection, write or read.
// ----------------------------------------------------
//
void CTaskManagerEngine::TimerExpired()
{
TRAPD( err, iTransactionObserver.FailedL( 1 ) );
if( err != KErrNone )
{
Panic( err );
}
Cancel();
}
// End of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -