Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



CEchoEngine class

CEchoEngine serves three purposes:

The public exported functions can be divided to follow these groups, plus construction and destruction:


Construction and destruction functions

CEchoEngine has the usual two-phase construction functions.

IMPORT_C CEchoEngine();

IMPORT_C static CEchoEngine* NewL(MUINotify* aConsole);

IMPORT_C static CEchoEngine* NewLC(MUINotify* aConsole);

IMPORT_C void ConstructL(MUINotify* aConsole);

~CEchoEngine();

Note that the creation functions take a MUINotify-type argument. This is passed to the CEchoRead and CEchoWrite objects when they are created, and so allows all the engine classes to make up-calls to a user interface object.

The ConstructL() function initialises the objects required to serve the three purposes noted.

EXPORT_C void CEchoEngine::ConstructL(MUINotify* aConsole)
// Construct object, and open a socket
    {
    iConsole = aConsole;
    iEngineStatus = EComplete;

    iTimeOut = KTimeOut;
    iTimer = CTimeOutTimer::NewL(EPriorityHigh, *this);

    CActiveScheduler::Add(this);

    // Open channel to Socket Server
    User::LeaveIfError(iSocketServ.Connect());
    // Open a TCP socket
    User::LeaveIfError(iEchoSocket.Open(iSocketServ, KAfInet,
        KSockStream, KProtocolInetTcp));
         iEchoRead = CEchoRead::NewL(&iEchoSocket, aConsole);
    iEchoWrite = CEchoWrite::NewL(&iEchoSocket, aConsole);
    }

It takes the following steps:

[Top]


Connection and shutdown functions

The following functions provide the three type of connections that we want (using an IP address, using a hostname, and using an IP address, obtaining the symbolic name for this IP address first, respectively), and a function to shutdown the current connection.

IMPORT_C void ConnectL(TUint32 aAddr);

IMPORT_C void ConnectL(const TDesC& aServerName);

IMPORT_C void TestGetByAddr(const TInetAddr& aInetAddr);

IMPORT_C void Stop();

[Top]


Reading and writing functions

The following functions allow users of the engine to read and write data respectively.

IMPORT_C void Read();

IMPORT_C void Write(TChar aChar);

As noted, these functions in turn call the CEchoRead and CEchoWrite functions that make the actual read and write calls to RSocket.

[Top]


Connection code

We can now look in more detail at a connection method. Below is the code for the connect by IP address request function.

EXPORT_C void CEchoEngine::ConnectL(TUint32 aAddr)
// Connect to an Echo Socket by IP address
    {
    iAddress.SetPort(7);
    iAddress.SetAddress(aAddr);
    iEchoSocket.Connect(iAddress, iStatus);
    iEngineStatus = EConnecting;
    SetActive();
    iTimer->After(iTimeOut);
    };

It takes the following steps:

As an active object, CEchoEngine defines a RunL() function to be called by the active scheduler when a request completes. The section of this function that handles the connection request just discussed is as follows:

iTimer->Cancel();
switch(iEngineStatus)
    {
case EConnecting:
    if (iStatus == KErrNone)
        {
        iConsole->PrintNotify(KConnecting);
        iEngineStatus = EConnected;
        Read(); //Start CEchoRead Active object
        }
    else
        iConsole->ErrorNotify(KConnectionFailed, iStatus.Int());
    break;

The steps are as follows: