S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CSIPExSocketEngine Class Reference

#include <SIPExSocketEngine.h>

Inheritance diagram for CSIPExSocketEngine:

MSIPExTimeOutNotify MSIPExReaderNotifier MSIPExWriterNotifier List of all members.

Public Types

enum  TSocketEngineState {
  ENotConnected, EConnecting, EConnected, EListening,
  ETimedOut
}

Public Member Functions

IMPORT_C ~CSIPExSocketEngine ()
IMPORT_C void ConnectL (const TUint32 aIapId, const TInetAddr &aIPAddress)
IMPORT_C void Write (const TDesC8 &aTxt)
IMPORT_C void Read ()
IMPORT_C void Stop ()
IMPORT_C TInetAddr & StartListeningL (const TUint32 aIapId)

Static Public Member Functions

static IMPORT_C CSIPExSocketEngine * NewL (MSIPExSocketEngineObserver &aObserver)
static IMPORT_C CSIPExSocketEngine * NewLC (MSIPExSocketEngineObserver &aObserver)

Protected Member Functions

void TimerExpired ()
void MessageReceived (TDesC8 &aBuffer)
void ErrorInReading (TInt aError)
void WriteDone ()
void ErrorInWriting (TInt aError)
void DoCancel ()
void RunL ()

Private Member Functions

void ChangeStateAndNotify (TSocketEngineState aNewState)
void StartInterfaceL (const TUint32 aIapId)
void ResolveLocalIPAddressL ()
 CSIPExSocketEngine (MSIPExSocketEngineObserver &aObserver)
void ConstructL ()

Private Attributes

MSIPExSocketEngineObserver & iObserver
RSocketServ iSocketServer
RConnection iConnection
RSocket iDataSocket
RSocket iListenSocket
CSIPExSocketReader * iReader
CSIPExSocketWriter * iWriter
CSIPExTimeOutTimer * iTimer
TSocketEngineState iState
TInetAddr iAddress

Detailed Description

Implements socket connections.

Definition at line 38 of file SIPExSocketEngine.h.


Member Enumeration Documentation

enum CSIPExSocketEngine::TSocketEngineState
 

Socket engine state enumerations.

Definition at line 100 of file SIPExSocketEngine.h.

00101             {
00102             ENotConnected,
00103             EConnecting,
00104             EConnected,
00105             EListening,
00106             ETimedOut
00107             };


Constructor & Destructor Documentation

EXPORT_C CSIPExSocketEngine::~CSIPExSocketEngine (   ) 
 

Destructor

Definition at line 72 of file SIPExSocketEngine.cpp.

References iConnection, iDataSocket, iListenSocket, iReader, iSocketServer, iTimer, and iWriter.

00073     {
00074     Cancel();
00075     
00076         if ( iTimer )
00077                 {
00078                 iTimer->Cancel();
00079                 delete iTimer;
00080                 }
00081 
00082         if ( iWriter )
00083                 {
00084                 iWriter->Cancel();
00085                 delete iWriter;
00086                 }
00087 
00088         if ( iReader )
00089                 {
00090                 iReader->Cancel();
00091                 delete iReader;
00092                 }
00093 
00094     iListenSocket.Close();
00095     iDataSocket.Close();
00096     iConnection.Close();
00097     iSocketServer.Close();
00098     }

CSIPExSocketEngine::CSIPExSocketEngine (  MSIPExSocketEngineObserver &  aObserver  )  [private]
 

Destructor

Definition at line 61 of file SIPExSocketEngine.cpp.

00062 :   CActive( EPriorityStandard ), iObserver( aObserver )
00063     {
00064     CActiveScheduler::Add( this );
00065     }


Member Function Documentation

EXPORT_C CSIPExSocketEngine * CSIPExSocketEngine::NewL (  MSIPExSocketEngineObserver &  aObserver  )  [static]
 

Static constructor

Parameters:
aObserver A reference to the socket engine observer.

Definition at line 32 of file SIPExSocketEngine.cpp.

References NewLC().

00034     {
00035     CSIPExSocketEngine* self = CSIPExSocketEngine::NewLC( aObserver );
00036     CleanupStack::Pop(self);
00037     return self;
00038     }

EXPORT_C CSIPExSocketEngine * CSIPExSocketEngine::NewLC (  MSIPExSocketEngineObserver &  aObserver  )  [static]
 

Static constructor. On return the instance is left to the CleanupStack

Parameters:
aObserver A reference to the socket engine observer.

Definition at line 46 of file SIPExSocketEngine.cpp.

References CSIPExSocketEngine().

00048     {
00049     CSIPExSocketEngine* self = new (ELeave) CSIPExSocketEngine( aObserver );
00050     CleanupStack::PushL(self);
00051     self->ConstructL();
00052     return self;
00053     }

EXPORT_C void CSIPExSocketEngine::ConnectL (  const TUint32  aIapId,
const TInetAddr &  aIPAddress
) 
 

Connecting the socket to the aIPAddress.

Parameters:
aIapId An iap id used for connection
aIPAddress An ip address where to connect.

Definition at line 205 of file SIPExSocketEngine.cpp.

00208     {
00209     if( iState != ENotConnected )
00210         {
00211         return;
00212         }
00213 
00214     // Make first sure that underlying interface is active
00215     StartInterfaceL( aIapId );
00216     
00217         // Open a TCP socket
00218         User::LeaveIfError( iDataSocket.Open( 
00219             iSocketServer, KAfInet, KSockStream, KProtocolInetTcp, iConnection ) );
00220     
00221     iAddress = aIPAddress;
00222         iAddress.SetPort( KGamePort );
00223 
00224     // The Connect() allocate local address automatically if 
00225     // not bind yet.
00226     iDataSocket.Connect( iAddress, iStatus );
00227          
00228     iState = EConnecting;
00229     SetActive();
00230     iTimer->After( KTimeOut );
00231     }

EXPORT_C void CSIPExSocketEngine::Write (  const TDesC8 &  aTxt  ) 
 

Writes to the socket.

Parameters:
aTxt A text to write to the socket.

Definition at line 238 of file SIPExSocketEngine.cpp.

References iState, iWriter, and CSIPExSocketWriter::Write().

00239     {
00240     if( iState == EConnected && !iWriter->IsActive() )
00241         {
00242         iWriter->Write( aTxt );
00243         }
00244     }

EXPORT_C void CSIPExSocketEngine::Read (   ) 
 

Reads from the socket.

Definition at line 251 of file SIPExSocketEngine.cpp.

References iReader, iState, and CSIPExSocketReader::Read().

00252     {
00253     if( iState == EConnected && !iReader->IsActive() )
00254         {
00255         iReader->Read();
00256         }
00257     }

EXPORT_C void CSIPExSocketEngine::Stop (   ) 
 

Closing the socket.

Definition at line 264 of file SIPExSocketEngine.cpp.

References ChangeStateAndNotify(), iConnection, iDataSocket, iReader, iState, and iWriter.

00265     {
00266     switch( iState )
00267         {
00268         case EConnected:
00269                         {
00270             iWriter->Cancel();
00271             iReader->Cancel();
00272             TRequestStatus status;
00273             iDataSocket.Shutdown( RSocket::EImmediate, status );
00274             User::WaitForRequest( status);
00275             iConnection.Close();
00276             ChangeStateAndNotify( ENotConnected );
00277                         }
00278             break;
00279         case EListening:
00280         case EConnecting:
00281             Cancel();
00282             break;
00283         default:
00284             break;
00285         }
00286     }

EXPORT_C TInetAddr & CSIPExSocketEngine::StartListeningL (  const TUint32  aIapId  ) 
 

Starts listening local game port

Parameters:
aIapId The IAP id of the active accesspoint.
Returns:
A local address

Definition at line 293 of file SIPExSocketEngine.cpp.

References iAddress, iDataSocket, iListenSocket, iSocketServer, iState, ResolveLocalIPAddressL(), and StartInterfaceL().

00294     {
00295     StartInterfaceL( aIapId );
00296     ResolveLocalIPAddressL();
00297     iAddress.SetPort( KGamePort );
00298     User::LeaveIfError( iListenSocket.Open( 
00299             iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );
00300     User::LeaveIfError( iDataSocket.Open( iSocketServer ) );
00301     User::LeaveIfError( iListenSocket.Bind( iAddress ) );
00302     
00303     User::LeaveIfError( iListenSocket.Listen( 1 ) );
00304 
00305     iListenSocket.Accept( iDataSocket, iStatus );    
00306     iState = EListening;
00307     SetActive();
00308 
00309     return iAddress;
00310     }

void CSIPExSocketEngine::TimerExpired (   )  [protected, virtual]
 

From MSIPExTimeOutNotify Destructor

Implements MSIPExTimeOutNotify.

Definition at line 361 of file SIPExSocketEngine.cpp.

References iState.

00362     {
00363     Cancel();
00364         iState = ETimedOut;
00365         
00366     TRequestStatus* status = &iStatus;          
00367     SetActive();
00368         User::RequestComplete( status, KErrNone );
00369     }

void CSIPExSocketEngine::MessageReceived (  TDesC8 &  aBuffer  )  [protected, virtual]
 

From MSIPExReaderNotifier

Implements MSIPExReaderNotifier.

Definition at line 376 of file SIPExSocketEngine.cpp.

References iObserver, and MSIPExSocketEngineObserver::SocketData().

00377     {
00378     iObserver.SocketData( aBuffer );
00379     }

void CSIPExSocketEngine::ErrorInReading (  TInt  aError  )  [protected, virtual]
 

From MSIPExReaderNotifier

Implements MSIPExReaderNotifier.

Definition at line 386 of file SIPExSocketEngine.cpp.

00387     {
00388     TRequestStatus* status = &iStatus;          
00389     SetActive();
00390         User::RequestComplete( status, aError );
00391     }

void CSIPExSocketEngine::WriteDone (   )  [protected, virtual]
 

From MSIPExWriterNotifier

Implements MSIPExWriterNotifier.

Definition at line 399 of file SIPExSocketEngine.cpp.

00400     {
00401     // Nothing to do
00402     }

void CSIPExSocketEngine::ErrorInWriting (  TInt  aError  )  [protected, virtual]
 

From MSIPExWriterNotifier

Implements MSIPExWriterNotifier.

Definition at line 409 of file SIPExSocketEngine.cpp.

00410     {
00411     TRequestStatus* status = &iStatus;          
00412     SetActive();
00413         User::RequestComplete( status, aError );
00414     }

void CSIPExSocketEngine::DoCancel (   )  [protected]
 

From CActive Cancels any outstanding request.

Definition at line 123 of file SIPExSocketEngine.cpp.

References iConnection, iDataSocket, iListenSocket, iState, and iTimer.

00124     {    
00125     iTimer->Cancel();
00126 
00127     // Cancel appropriate request to socket
00128         switch ( iState )
00129                 {
00130             case EConnecting:
00131                     iDataSocket.CancelConnect();
00132                     break;
00133 
00134         case EListening:
00135             iListenSocket.CancelAccept();
00136             break;
00137         
00138         default:
00139             break;
00140         }    
00141         
00142     iConnection.Close();
00143     }

void CSIPExSocketEngine::RunL (   )  [protected]
 

From CActive Handles the completion of the active request.

Definition at line 150 of file SIPExSocketEngine.cpp.

References ChangeStateAndNotify(), iState, iTimer, and Read().

00151     {
00152     iTimer->Cancel();
00153 
00154     switch( iState )
00155         {
00156         case EConnecting:
00157             if( iStatus.Int() == KErrNone )
00158                 {
00159                 ChangeStateAndNotify( EConnected );
00160                 Read();
00161                 }
00162             else
00163                 {
00164                 ChangeStateAndNotify( ENotConnected );
00165                 }
00166             break;
00167 
00168         case EListening:
00169                         {
00170             iListenSocket.Close();
00171             if( iStatus.Int() == KErrNone )
00172                 {
00173                 ChangeStateAndNotify( EConnected );
00174                 Read();
00175                 }
00176             else
00177                 {
00178                 ChangeStateAndNotify( ENotConnected );
00179                 }
00180                         }
00181             break;
00182 
00183         case ETimedOut:
00184             if( iStatus.Int() == KErrNone )
00185                 {
00186                 iObserver.SocketState( ETimedOut );
00187                 iState = ENotConnected;
00188                 }
00189             else
00190                 {
00191                 ChangeStateAndNotify( ENotConnected );
00192                 }
00193             break;
00194 
00195         default:
00196             iObserver.SocketState( iStatus.Int() );
00197         }
00198     }

void CSIPExSocketEngine::ChangeStateAndNotify (  TSocketEngineState  aNewState  )  [private]
 

Changes the new state to iState and notifies observer.

Parameters:
aNewState A new state of socket engine

Definition at line 421 of file SIPExSocketEngine.cpp.

References iObserver, iState, and MSIPExSocketEngineObserver::SocketState().

00422     {
00423     iState = aNewState;
00424     iObserver.SocketState( iState );
00425     }

void CSIPExSocketEngine::StartInterfaceL (  const TUint32  aIapId  )  [private]
 

Activates PDP ctx of passed iap

Parameters:
aIapId The IAP id of the activated accesspoint.

Definition at line 317 of file SIPExSocketEngine.cpp.

References iConnection, and iSocketServer.

00318     {
00319     if( !iConnection.SubSessionHandle() )
00320         { 
00321         User::LeaveIfError( iConnection.Open( iSocketServer ) );
00322         }
00323 
00324     TCommDbConnPref prefs;
00325     prefs.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
00326     prefs.SetDirection( ECommDbConnectionDirectionOutgoing );
00327     prefs.SetIapId( aIapId );
00328 
00329     User::LeaveIfError( iConnection.Start( prefs ) );
00330     }

void CSIPExSocketEngine::ResolveLocalIPAddressL (   )  [private]
 

Gets the local IP address with host resolver. The address is set to the iAddress variable.

Definition at line 337 of file SIPExSocketEngine.cpp.

References iAddress, and iConnection.

00338     {
00339     RHostResolver hostResolver;
00340     TNameEntry entry;
00341 
00342     User::LeaveIfError( hostResolver.Open( iSocketServer, KAfInet, 
00343                                           KProtocolInetTcp, iConnection ));
00344     CleanupClosePushL( hostResolver );
00345  
00346     User::LeaveIfError( hostResolver.GetByName( TPtrC(), entry ) );
00347     if ( !TInetAddr::Cast( entry().iAddr ).IsWildAddr() )
00348          {
00349          iAddress = TInetAddr::Cast( entry().iAddr );
00350          }
00351          
00352     CleanupStack::PopAndDestroy(); // hostResolver
00353     }

void CSIPExSocketEngine::ConstructL (   )  [private]
 

Destructor

Definition at line 105 of file SIPExSocketEngine.cpp.

References iDataSocket, iReader, iSocketServer, iState, iTimer, iWriter, CSIPExSocketWriter::NewL(), CSIPExSocketReader::NewL(), and CSIPExTimeOutTimer::NewL().

00106     {
00107     iState = ENotConnected;
00108 
00109     iTimer = CSIPExTimeOutTimer::NewL( *this );
00110 
00111     // Open channel to Socket Server
00112         User::LeaveIfError( iSocketServer.Connect() );
00113 
00114     iReader = CSIPExSocketReader::NewL( iDataSocket, *this );
00115     iWriter = CSIPExSocketWriter::NewL( iDataSocket, *this );
00116     }


© Nokia 2006

Back to top