Attaching to an existing RConnection connection: Tutorial

The following example shows how an application can attach to an existing connection in order to gather information on the connection.

Note that error checking is included in this example.

      
       
      
      //Open a session with the socket server
RSocketServ socketServer;
TInt err(KErrNone);
err = socketServer.Connect();
if(err != KErrNone) return err;

//Open a subsession with the socket server for the RConnection
RConnection myConnection;
err = myConnection.Open(socketServer);
if(err != KErrNone) return err;

//Start or attach the RConnection to an interface on the server
TUint connectionCount;
//Enumerate currently active connections across all socket servers
err = myConnection.EnumerateConnections(connectionCount);
if(err != KErrNone) return err;
if(connectionCount == 0) // if no connections are started
{
            err = myConnection.Start();
}
else
{
            TPckgBuf<TConnectionInfoV2> connectionInfo;
            err = myConnection.GetConnectionInfo(1, connectionInfo); // 1 = first active connection
            if(err!=KErrNone) return err;
            err = myConnection.Attach(connectionInfo, RConnection::EAttachTypeMonitor);
}
     

At this point myConnection points to a specific connection. Obviously the code snippet above could be expanded in several ways such as getting connection information for all connections and selecting a connection based on the information rather than simply selecting the first connection, as in this case.

Note for this and the next section that the number of connections and subconnections are not constant and can be expected to change during execution.

      
       
      
      //Store subconnection information

TUint subConnectionCount(0);
err = myConnection.EnumerateSubConnections(subConnectionCount);
if(err != KErrNone) return err;

//TSubConnectionInfoGprsUmts is a superclass of TSubConnectionInfo, so we will use it
//to hold either GPRS/UMTS or CSD connection information as required. (CSD connection 
//information is held in a plain TSubConnectionInfo object).
TPckgBuf<TSubConnectionInfoGprsUmts> subConnectionInfo[subConnectionCount];
//note that for a CSD connection the subConnectionCount will be 1

for (TUint i=0; i<subConnectionCount; i++)
{
            err = myConnection.GetSubConnectionInfo(i, subConnectionInfo[i]);
            if(err != KErrNone) return err;
}
     

Once the connection has been attached it is possible to start querying individual subconnections.

From this point the client has access to subConnectionInfo().iSubConnectionUniqueId , which is the unique identifier for the subconnection that has been queried. Now that this value is available the client can make any further calls to subconnection specific functionality within the RConnection , for example querying the amount of data currently transferred by the specified subconnection.

      
       
      
      TUint connectionuplinkVolume(0);
TUint connectiondownlinkVolume(0);
TUint subconnectionuplinkVolume(0);
TUint subconnectiondownlinkVolume(0);
TPckg<TUint> connectionUplinkVolume(connectionuplinkVolume);
TPckg<TUint> connectionDownlinkVolume(connectiondownlinkVolume);
TPckg<TUint> subConnectionUplinkVolume(subconnectionuplinkVolume);
TPckg<TUint> subConnectionDownlinkVolume(subconnectiondownlinkVolume);
TRequestStatus status;

//Query data transfer over the connection
myConnection.DataTransferredRequest(connectionUplinkVolume, connectionDownlinkVolume, status);

//Query data transfer over a specified subconnection
myConnection.DataTransferredRequest(subConnectionInfo[0]().iSubConnectionUniqueId, connectionUplinkVolume, connectionDownlinkVolume, status);