examples/ForumNokia/Symbian_OS_End-to-End_Sockets_API_Example/SocketTaskManager_S60/engine/TaskManagerEngine.cpp

00001 /*
00002 * ============================================================================
00003 *  Name     : CTaskManagerEngine from TaskManagerEngine.cpp
00004 *  Part of  : TaskManager
00005 *  Created  : 15/03/2006 by Forum Nokia
00006 *  Version  : 1.2
00007 *  Copyright: Nokia Corporation
00008 * ============================================================================
00009 */
00010 
00011 // INCLUDE FILES
00012 #include "TimeOutTimer.h"
00013 #include "Response.h"
00014 #include "Request.h"
00015 #include "TaskManagerEngine.h"
00016 #include "TaskManagerEngineReader.h"
00017 #include "TaskManager.pan"
00018 
00019 #include <msvids.h>                             // KMsvGlobalInBoxIndexEntryId
00020 #include <txtrich.h>                    // CRichText
00021 #include <mtclreg.h>                    // CClientMtmRegistry
00022 #include <mtclbase.h>                   // CBaseMtm
00023 #include <commdb.h>                             // CCommsDatabase
00024 #include <commdbconnpref.h>             // TCommDbConnPref
00025 #include <es_enum.h>                    // TConnectionInfoV2Buf
00026 #include <in_sock.h>                    // TInetAddr
00027 #include <eikenv.h>
00028 #include <s32mem.h>                             // TDesBuf
00029 #include <e32std.h>                             // TLex
00030 #include <securesocket.h>       // CSecureSocket
00031 #include <SocketTaskManager.rsg>
00032 
00033 // CONSTANTS
00034 
00035 _LIT( KSSL3, "SSL3.0" );
00036 
00037 _LIT( KGPRSStartError, "Error starting GPRS service" );
00038 _LIT( KLookupError, "Error looking up server IP");
00039 _LIT( KConnectionError, "Error in forming the connection" );
00040 _LIT( KSSLConnError, "Error forming SSL link between client and server" );
00041 _LIT( KWriteError, "Error writing data to server" );
00042 _LIT( KErroneousPackage, "Package received was erroneous" );
00043 
00044 _LIT(KUserNotSet, "Set username and/or password");
00045 _LIT(KIapNotSet, "IAP not selected");
00046 _LIT(KServerNotSet, "Server name not set");
00047 _LIT(KSmsUpdateMessage, "TaskManagerUpdate");
00048 
00049 const TInt CTaskManagerEngine::KTimeOut = 30000000; // 30 seconds time-out
00050 
00051 // ================= MEMBER FUNCTIONS =======================
00052 
00053 // constructor
00054 CTaskManagerEngine::CTaskManagerEngine(MTransactionObserver& aObserver)
00055         : CActive( EPriorityStandard ), iState( ENotConnected ),
00056         iOperation( TRequest::EFetchTasks ), iMarkId( 0 ),
00057         iTransactionObserver( aObserver )
00058         
00059         {
00060         }
00061         
00062 // destructor
00063 CTaskManagerEngine::~CTaskManagerEngine()
00064         {
00065         Cancel(); // Closes the socket and resolver if engine is active
00066 
00067     if( iReader )
00068         {
00069         iReader->Cancel();
00070         }
00071         delete iReader;
00072         
00073         delete iTimer;
00074         delete iSecureSocket;
00075         
00076         iConnection.Close();
00077         iSocketServer.Close();
00078         
00079         delete iMsvEntry;
00080         delete iMsvSession;
00081         iIAPs.Close();
00082         }
00083 
00084 // ----------------------------------------------------
00085 // CTaskManagerEngine::NewL()
00086 // Two-phased constructor.
00087 // ----------------------------------------------------
00088 //      
00089 CTaskManagerEngine* CTaskManagerEngine::NewL(MTransactionObserver& aObserver)
00090         {
00091         CTaskManagerEngine* self = new (ELeave) CTaskManagerEngine(aObserver);
00092         CleanupStack::PushL(self);
00093         self->ConstructL();
00094         CleanupStack::Pop(self);
00095         return self;
00096         }
00097 
00098 // ----------------------------------------------------
00099 // CTaskManagerEngine::ConstructL()
00100 // Symbian OS default constructor can leave.
00101 // ----------------------------------------------------
00102 //      
00103 void CTaskManagerEngine::ConstructL()
00104         {
00105         User::LeaveIfError(iSocketServer.Connect());
00106         User::LeaveIfError(iConnection.Open(iSocketServer));
00107         
00108         // instantiate a timer for connection, socketread etc. timeouts 
00109         iTimer = CTimeOutTimer::NewL( EPriorityHigh, *this );
00110         
00111     iReader = CTaskManagerEngineReader::NewL( *this );
00112     
00113     CActiveScheduler::Add( this );
00114         
00115         iMsvSession = CMsvSession::OpenAsyncL(*this);
00116         
00117         LoadIapsL();
00118         
00119         // no IAPs defined in the device.
00120         if (iIAPs.Count() == 0)
00121                 {
00122                 CEikonEnv::Static()->LeaveWithInfoMsg(R_TASKMANAGER_NO_IAPS_DEFINED);
00123                 }
00124         
00125         }
00126 
00127 // ----------------------------------------------------
00128 // CTaskManagerEngine::SetConnectionSettings()
00129 // Sets all connections settings. 
00130 // ----------------------------------------------------
00131 //              
00132 void CTaskManagerEngine::SetConnectionSettings(const TDesC& aServerName, 
00133                                                                                                 const TInt& aPort,
00134                                                                                                 const TDesC& aUsername, 
00135                                                                                                 const TDesC& aPassword)
00136         {
00137         iUsername.Copy(aUsername);
00138         iPassword.Copy(aPassword);
00139         iServer.Copy(aServerName);
00140         iPort = aPort;
00141         }
00142 
00143 // ----------------------------------------------------
00144 // CTaskManagerEngine::SetIap()
00145 // Sets the IAP to be used in the connections.
00146 // ----------------------------------------------------
00147 //      
00148 void CTaskManagerEngine::SetIap(const TUint32& aId)
00149         {
00150         iIap = aId;
00151         }
00152         
00153 // ----------------------------------------------------
00154 // CTaskManagerEngine::IapSet()
00155 // Returns ETrue if the IAP is set, EFalse if not.
00156 // ----------------------------------------------------
00157 //      
00158 TBool CTaskManagerEngine::IapSet() const
00159         {
00160         if (iIap == 0)
00161                 {
00162                 return EFalse;
00163                 }
00164         else
00165                 {
00166                 return ETrue;
00167                 }
00168         }
00169 
00170 // ----------------------------------------------------
00171 // CTaskManagerEngine::CheckAndReportErrorsL()
00172 // Checks that all necessary connections settings are 
00173 // set and reports from missing data.
00174 // ----------------------------------------------------
00175 //      
00176 TBool CTaskManagerEngine::CheckAndReportErrorsL()
00177         {
00178         if (!IapSet())
00179                 {
00180                 iTransactionObserver.ErrorL(KIapNotSet);
00181                 return ETrue;
00182                 }
00183                 
00184         if (iServer.Length() == 0)
00185                 {
00186                 iTransactionObserver.ErrorL(KServerNotSet);
00187                 return ETrue;
00188                 }
00189 
00190         return EFalse;
00191         }
00192 
00193 // ----------------------------------------------------
00194 // CTaskManagerEngine::FetchTasksL()
00195 // Starts fetching tasks from the server.
00196 // Called from UI.
00197 // ----------------------------------------------------
00198 //              
00199 void CTaskManagerEngine::FetchTasksL()
00200         {
00201         iOperation = TRequest::EFetchTasks;
00202         
00203     iTransactionObserver.ConnectingToServerL( ETrue );
00204         
00205         iRunning = ETrue;
00206         GPRSConnectL();
00207         }
00208         
00209 // ----------------------------------------------------
00210 // CTaskManagerEngine::MarkTaskDoneL()
00211 // Informs the server that a task has been completed.
00212 // Server updates task info in the database.
00213 // ----------------------------------------------------
00214 //      
00215 void CTaskManagerEngine::MarkTaskDoneL(const TInt& aTaskId)
00216         {
00217         iOperation = TRequest::ETaskDone;
00218         iMarkId.Num( aTaskId );
00219         
00220         iRunning = ETrue;
00221         GPRSConnectL();
00222         }
00223 
00224 // ----------------------------------------------------
00225 // CTaskManagerEngine::CancelTransaction()
00226 // Can be used for cancelling a transaction at 
00227 // any time.
00228 // ----------------------------------------------------
00229 //              
00230 void CTaskManagerEngine::CancelTransaction()
00231         {
00232         Cancel();
00233         
00234         iRunning = EFalse;
00235         
00236         TRAPD( error, iTransactionObserver.CancelledL() );
00237         if( error != KErrNone )
00238             {
00239             Panic( error );
00240             }
00241         
00242         }
00243 
00244 // ----------------------------------------------------
00245 // CTaskManagerEngine::CheckRefreshL()
00246 // An SMS update message may have been received while 
00247 // a transaction was running. If so, tasks will 
00248 // be fetched.
00249 // ----------------------------------------------------
00250 //
00251 void CTaskManagerEngine::CheckRefreshL()
00252         {
00253         if (iRunning)
00254             {
00255             return;
00256             }
00257 
00258         if (iDoRefresh)
00259                 {
00260                 iDoRefresh = EFalse;
00261                 FetchTasksL();
00262                 }
00263         }
00264 
00265 // ----------------------------------------------------
00266 // CTaskManagerEngine::SetAutomaticUpdateL()
00267 // Defines whether tasks are downloaded automatically 
00268 // when an update SMS message arrives or not.
00269 // ----------------------------------------------------
00270 //
00271 void CTaskManagerEngine::SetAutomaticUpdateL(const TBool& aOn)
00272         {
00273         iAutomaticUpdate = aOn;
00274         if (iAutomaticUpdate)
00275                 {
00276                 CheckRefreshL();
00277                 }
00278         }
00279 
00280 // ----------------------------------------------------
00281 // CTaskManagerEngine::HandleSessionEventL()
00282 // Indicates an event in the message server has occurred. 
00283 // Used here for listening incoming SMS messages.
00284 // ----------------------------------------------------
00285 //      
00286 void CTaskManagerEngine::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/)
00287         {
00288         switch (aEvent)
00289                 {
00290                 case EMsvServerReady:
00291                         if (!iMsvEntry)
00292                                 {
00293                                 iMsvEntry = CMsvEntry::NewL(*iMsvSession, KMsvGlobalInBoxIndexEntryId, TMsvSelectionOrdering());
00294                                 }
00295                         break;
00296                         
00297                 case EMsvEntriesCreated:
00298                         if (*(static_cast<TMsvId*>(aArg2)) == KMsvGlobalInBoxIndexEntryId)
00299                                 {
00300                                 CMsvEntrySelection* entries = static_cast<CMsvEntrySelection*>(aArg1);
00301 
00302                                 iMsvEntry->SetEntryL(entries->At(0));
00303                                 TMsvEntry msvEntry(iMsvEntry->Entry());
00304                                 msvEntry.SetVisible(EFalse);
00305 
00306                                 CClientMtmRegistry* mtmReg = CClientMtmRegistry::NewL(*iMsvSession);
00307                                 CleanupStack::PushL(mtmReg);
00308                                 CBaseMtm* smsMtm = mtmReg->NewMtmL(msvEntry.iMtm);
00309                                 smsMtm->SwitchCurrentEntryL(entries->At(0));
00310                                 smsMtm->LoadMessageL();
00311                                 TBool CorrectSms = EFalse;
00312                                 
00313                                 // received SMS message is a 'Task manager update' SMS.
00314                                 if (smsMtm->Body().Read(0,KSmsUpdateMessage().Length()).Compare(KSmsUpdateMessage)==0)
00315                                         {
00316                                         msvEntry.SetUnread(EFalse);
00317                                         CorrectSms = ETrue;
00318                                         }
00319                                 // not 'Task manager update' SMS, show it to user.
00320                                 else
00321                                         {
00322                                         msvEntry.SetVisible(ETrue);
00323                                         }
00324                                         
00325                                 iMsvEntry->ChangeL(msvEntry);
00326 
00327                                 CleanupStack::PopAndDestroy(smsMtm);
00328 
00329                                 // if received SMS message was a 'Task Manager update' SMS.
00330                                 if (CorrectSms)
00331                                         {
00332                                         // delete the received SMS message for it is not intended for the user to read.
00333                                         iMsvEntry->DeleteL(entries->At(0));
00334                                         
00335                                         // if a transaction is running or program is in the background (paused) 
00336                                         // don't fetch tasks yet.
00337                                         if (iRunning || iAutomaticUpdate)
00338                                                 {
00339                                                 iDoRefresh = ETrue;
00340                                                 }
00341                                         // Transaction is not running, fetch tasks.
00342                                         else
00343                                                 {
00344                                                 FetchTasksL();
00345                                                 }
00346                                         }
00347                                 }
00348                         break;
00349                         
00350                         default:
00351                         break;
00352                 }
00353         }
00354 
00355 // ----------------------------------------------------
00356 // CTaskManagerEngine::LoadIapsL()
00357 // Loads all IAPs of the device.
00358 // ----------------------------------------------------
00359 //      
00360 void CTaskManagerEngine::LoadIapsL()
00361         {
00362         // open commdb
00363         CCommsDatabase* commDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
00364         CleanupStack::PushL(commDb);
00365 
00366         // open IAP table
00367         CCommsDbTableView* commView = commDb->OpenIAPTableViewMatchingBearerSetLC(ECommDbBearerCSD|ECommDbBearerGPRS,ECommDbConnectionDirectionOutgoing);
00368         
00369         // search all IAPs
00370         if (commView->GotoFirstRecord() == KErrNone)
00371                 {
00372                 do
00373                         {
00374                         TIap iap;
00375                         commView->ReadTextL(TPtrC(COMMDB_NAME), iap.iName);
00376                         commView->ReadUintL(TPtrC(COMMDB_ID), iap.iId);
00377                         User::LeaveIfError(iIAPs.Append(iap));
00378                         }
00379                 while (commView->GotoNextRecord() == KErrNone);
00380                 }
00381         CleanupStack::PopAndDestroy(commView);
00382         CleanupStack::PopAndDestroy(commDb);
00383         }
00384 
00385 // ----------------------------------------------------
00386 // CTaskManagerEngine::Iaps()
00387 // Returns all IAPs of the device.
00388 // ----------------------------------------------------
00389 //              
00390 RArray<TIap>& CTaskManagerEngine::Iaps() 
00391         {
00392         return iIAPs;
00393         }
00394 
00395         
00396 // ----------------------------------------------------
00397 // CTaskManagerEngine::ConnectL() (overloaded)
00398 // First phase of initiating a connection between sockets.
00399 // Handles DNS-lookup etc.
00400 // ----------------------------------------------------
00401 void CTaskManagerEngine::ConnectL()
00402         {
00403         if( iState == ENotConnected )
00404         {
00405                 TInetAddr addr;
00406                 
00407                 // determine if the server address is a valid ip address
00408                 if( addr.Input( iServer ) == KErrNone )
00409                 {
00410                         ConnectL( addr.Address() );
00411                 }
00412                 else // if not, we must try DNS-lookup
00413                 {
00414                         // open a dns-resolver session
00415                         User::LeaveIfError( iResolver.Open( iSocketServer, KAfInet, KProtocolInetUdp) );
00416                         
00417                         // DNS-lookup request
00418                         iResolver.GetByName( iServer, iNameEntry, iStatus );
00419                         
00420                         iState = ELookingUp;
00421                         iTimer->After( KTimeOut );
00422                         SetActive();
00423                 }
00424         }
00425 }
00426 
00427 
00428 // ----------------------------------------------------
00429 // CTaskManagerEngine::ConnectL() (overloaded)
00430 // True connection function. Takes a valid IP-address
00431 // as parameter (valid as in xxx.xxx.xxx.xxx form)
00432 // ----------------------------------------------------
00433 void CTaskManagerEngine::ConnectL( TUint32 aAddr )
00434         {
00435         if( iState == ENotConnected )
00436                 {
00437                 // open a new socket
00438 #ifdef __WINS__
00439                 // Emulator can't handle RConnections
00440                 User::LeaveIfError( iSocket.Open( iSocketServer,
00441                                                                                   KAfInet,
00442                                                                                   KSockStream,
00443                                                                                   KProtocolInetTcp ) );
00444 #else
00445                 User::LeaveIfError( iSocket.Open( iSocketServer,
00446                                                                                   KAfInet,
00447                                                                                   KSockStream,
00448                                                                                   KProtocolInetTcp,
00449                                                                                   iConnection ) );
00450 #endif
00451                 // set correct port and address
00452                 iAddress.SetPort( iPort );
00453                 iAddress.SetAddress( aAddr );
00454                 
00455                 // try to connect the socket
00456                 iSocket.Connect( iAddress, iStatus );
00457                 iState = EConnecting;
00458                 
00459                 iTimer->After( KTimeOut );
00460                 
00461                 SetActive();
00462                 }
00463         }
00464         
00465 // ----------------------------------------------------
00466 // CTaskManagerEngine::DoCancel()
00467 // Called when a cancel is done during activity.
00468 // ----------------------------------------------------
00469 //
00470 void CTaskManagerEngine::DoCancel()
00471         {
00472         iTimer->Cancel();
00473         
00474         switch( iState )
00475                 {
00476                 case EStartingGPRS:
00477                     {
00478                     iConnection.Stop();
00479                     break;
00480                     }
00481                 case ELookingUp:
00482                         {
00483                         iResolver.Cancel();
00484                         iResolver.Close();
00485                     iState = ENotConnected;
00486                         break;
00487                         }
00488                 case EConnecting:
00489                         {
00490                 iSocket.CancelConnect();
00491                         iSocket.Close();
00492                         iState = ENotConnected;
00493                         break;
00494                         }
00495                 case ESSLConnecting:
00496                     {
00497             iSecureSocket->CancelHandshake();
00498             
00499             EndConnection();
00500                     break;
00501                     }
00502                 case EWriting:
00503                     {
00504                     iSecureSocket->CancelSend();
00505                     iReader->Cancel();
00506                     
00507             EndConnection();
00508                     break;
00509                     }
00510                 default:
00511                     {
00512                     Panic( ETaskManagerInvalidState );
00513                     break;
00514                     }
00515                 };
00516         }
00517 
00518 // ----------------------------------------------------
00519 // CTaskManagerEngine::RunL()
00520 // Handles the states of the object and ensures everything
00521 // is done in right order.
00522 // ----------------------------------------------------
00523 //
00524 void CTaskManagerEngine::RunL()
00525         {
00526     iTimer->Cancel();
00527         
00528         switch( iState )
00529                 {
00530                 // After GPRS service has been started (or failed to start)
00531                 case EStartingGPRS:
00532                     {
00533                     iState = ENotConnected;
00534                     
00535                     if( iStatus == KErrNone )
00536                         {
00537                         iOpeningConnection = EFalse;
00538                         iTransactionObserver.ConnectingToServerL( (iOperation == TRequest::EFetchTasks ) );
00539                         ConnectL();
00540                         }
00541                     else
00542                         {
00543                         iTransactionObserver.ErrorL( KGPRSStartError );
00544                         }
00545                     break;
00546                     }
00547                 // After DNS resolver has finished
00548                 case ELookingUp:
00549                     {
00550                         iResolver.Close();
00551                         iState = ENotConnected;
00552                         
00553                         if( iStatus == KErrNone ) // DNS-lookup was successful
00554                                 {
00555                                 iNameRecord = iNameEntry();
00556                                 
00557                                 TBuf<15> ipAddr;
00558                                 TInetAddr::Cast( iNameRecord.iAddr ).Output( ipAddr );
00559                                 
00560                                 ConnectL( TInetAddr::Cast( iNameRecord.iAddr ).Address() );
00561                                 }
00562                         else // DNS-lookup failed
00563                                 {
00564                                 iTransactionObserver.ErrorL( KLookupError );
00565                                 }
00566 
00567                     break;
00568                     }
00569         // After socket has finished connecting
00570                 case EConnecting:
00571                     {
00572                         // everything went as planned
00573                         if( iStatus == KErrNone )
00574                                 {
00575                                 iState = ESSLConnecting;
00576                 iSecureSocket = CSecureSocket::NewL( iSocket, KSSL3 );
00577 
00578                 #ifdef __SERIES60_30__
00579                 // The CSecureSocket implementation has changed slightly in S60 3rd Edition,
00580                 // this additional option with the server host name needs to be set (in a 8-bit
00581                 // descriptor).
00582                 TBuf8<KMaxServerNameLength> tempName;
00583                 tempName.Copy(iServer);
00584                 iSecureSocket->SetOpt(KSoSSLDomainName, KSolInetSSL, tempName);
00585                 #endif
00586 
00587                                 iSecureSocket->StartClientHandshake( iStatus );
00588                 iTimer->After( KTimeOut );
00589                                 SetActive();
00590                                 }
00591                         else // we couldn't open a connection
00592                                 {
00593                 iSocket.Close();
00594                                 iTransactionObserver.ErrorL( KConnectionError );
00595                                 iState = ENotConnected;
00596                                 }
00597 
00598                     break;
00599                     }
00600                 // After the SSL-handshake has been completed
00601                 case ESSLConnecting:
00602                     {
00603                     if( iStatus == KErrNone )
00604                         {
00605                         iState = EConnected;
00606                         iReader->SetSecureSocket( iSecureSocket );
00607 
00608                 // These parameters are formatted into a string that is saved to iWriteBuffer
00609                 TRequest::GetMessage( iUsername, iPassword, iOperation, iMarkId, iWriteBuffer );
00610                 
00611                 // We begin reading just before writing just so we won't miss anything
00612                 iReader->Start();
00613                 
00614                 // We send the command data to the server
00615                 iSecureSocket->Send( iWriteBuffer, iStatus );
00616                 iState = EWriting;
00617 
00618                 // Start a timer
00619                 iTimer->After( KTimeOut );
00620                 SetActive();
00621                         }
00622                 else
00623                         {
00624                         // CSecureSocket can be instantiated only after a socket has been connected so
00625                         // it must be deleted after every connection
00626                         iSecureSocket->Close(); // this closes the regular socket as well
00627                         delete iSecureSocket;
00628                         iSecureSocket = NULL;
00629 
00630                         iState = ENotConnected;
00631                         iTransactionObserver.ErrorL( KSSLConnError );
00632                         }
00633                     break;
00634                     }
00635                 case EWriting:
00636                     {
00637                         if( iStatus == KErrNone )
00638                             {
00639                         iTimer->After( KTimeOut ); // So we won't wait forever for a response
00640                         iState = EReading;
00641                             }
00642                     else
00643                                 { // writing failed
00644                                 iState = EConnected;
00645                                 
00646                                 iTransactionObserver.ErrorL( KWriteError );
00647                                 }
00648                     break;
00649                     }
00650                 default:
00651                     {
00652                     User::Leave( ETaskManagerInvalidState );
00653                     }
00654                 };
00655         }
00656 
00657 TInt CTaskManagerEngine::RunError( TInt /*aError*/ )
00658     {
00659     return KErrNone;
00660     }
00661 
00662 // ----------------------------------------------------
00663 // CTaskManagerEngine::GPRSConnectL()
00664 // Will open up a (GPRS) connection. We open it using the
00665 // IAP the user chose at the beginning. (View)
00666 // ----------------------------------------------------
00667 //              
00668 void CTaskManagerEngine::GPRSConnectL()
00669         {
00670 // this functionality is not applicable for the emulator
00671 #ifndef __WINS__ 
00672         TBool connected = EFalse;
00673         
00674         // Lets first check are we already connected.
00675         TUint connectionCount;
00676         User::LeaveIfError(iConnection.EnumerateConnections(connectionCount));
00677         TPckgBuf<TConnectionInfoV2> connectionInfo; 
00678         for (TUint i = 1; i <= connectionCount; i++)
00679                 {
00680                 User::LeaveIfError(iConnection.GetConnectionInfo(i, connectionInfo));
00681                 if (connectionInfo().iIapId == iIap)
00682                         {
00683                         connected = ETrue;
00684                         break;
00685                         }
00686                 }
00687         
00688         // Not yet connected, start connection
00689         if (!connected)
00690                 {
00691                 //Define preferences for connection
00692                 TCommDbConnPref prefs;
00693                 prefs.SetIapId(iIap);
00694                 prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
00695 
00696                 //Start Connection
00697                 iTransactionObserver.OpeningConnectionL();
00698                 
00699         //Asnychronic call
00700         iOpeningConnection = ETrue;
00701                 iConnection.Start(prefs, iStatus);
00702 
00703                 iState = EStartingGPRS;
00704                 iTimer->After( KTimeOut );
00705                 SetActive();
00706                 
00707                 return;
00708                 }
00709 #endif // __WINS__
00710 
00711     ConnectL();
00712         }
00713 
00714 // ----------------------------------------------------
00715 // CTaskManagerEngine::PackageReceivedL()
00716 // The socket reader calls this function to pass the received
00717 // data to the engine. The function passes it on to CResponse
00718 // which does the actual parsing.
00719 // ----------------------------------------------------
00720 //
00721 TBool CTaskManagerEngine::PackageReceivedL( const TDesC8& aData )
00722     {
00723     // Timer is stopped
00724     iTimer->Cancel();
00725     
00726     if( !iResponse )
00727         {
00728         iResponse = CResponse::NewL();
00729         }
00730     
00731     iResponse->InputDataL( aData );
00732     
00733     switch( iResponse->GetState() )
00734         {
00735         case CResponse::ENotComplete:
00736             {
00737             iTimer->After( KTimeOut );
00738             return ETrue;
00739             }
00740         case CResponse::EError:
00741             {
00742             iTransactionObserver.ErrorL( KErroneousPackage );
00743             }
00744         case CResponse::EComplete:
00745             {
00746             iTransactionObserver.SuccessL( *iResponse );
00747             CheckRefreshL();
00748             }
00749         }
00750 
00751     EndConnection();
00752     return EFalse;
00753     }
00754 
00755 // ----------------------------------------------------
00756 // CTaskManagerEngine::EndConnection()
00757 // Ends the connection to the server in a controlled manner.
00758 // ----------------------------------------------------
00759 //
00760 void CTaskManagerEngine::EndConnection()
00761     {
00762     delete iResponse;
00763     iResponse = NULL;
00764         
00765         if( iSecureSocket )
00766             {
00767             iSecureSocket->Close(); // also closes the normal socket    
00768             }
00769     else
00770         {
00771         iSocket.Close();
00772         }
00773     
00774     delete iSecureSocket;
00775     iSecureSocket = NULL;
00776 
00777         iRunning = EFalse;
00778         iState = ENotConnected;
00779     }
00780 
00781 // ----------------------------------------------------
00782 // CTaskManagerEngine::TimerExpired()
00783 // Timer calls this function to notify a timeout with
00784 // connection, write or read.
00785 // ----------------------------------------------------
00786 //
00787 void CTaskManagerEngine::TimerExpired()
00788     {
00789     TRAPD( err, iTransactionObserver.FailedL( 1 ) );
00790     
00791     if( err != KErrNone )
00792         {
00793         Panic( err );
00794         }
00795     Cancel();
00796     }
00797 
00798 // End of file

Generated by  doxygen 1.6.2