Week 32 contribution of PDK documentation content. See release notes for details. Fixes bug Bug 3582
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License
"Eclipse Public License v1.0" which accompanies this distribution,
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
Nokia Corporation - initial contribution.
Contributors:
-->
<!DOCTYPE concept
PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept xml:lang="en" id="GUID-05E3ED3B-41F8-5FC2-87A2-627BD5E6BB04"><title> Attaching to an existing RConnection connection: Tutorial</title><prolog><metadata><keywords/></metadata></prolog><conbody><p>The following example shows how an application can attach to an existing connection in order to gather information on the connection. </p> <p>Note that error checking is included in this example. </p> <codeblock id="GUID-32123F0F-45CC-5966-AD0B-E0895D316D3F" xml:space="preserve">
//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);
}</codeblock> <p>At this point <codeph>myConnection</codeph> 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. </p> <p>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. </p> <codeblock id="GUID-F9F6D0DB-BA01-5610-BFDD-B15A61DF0757" xml:space="preserve">
//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;
}</codeblock> <p>Once the connection has been attached it is possible to start querying individual subconnections. </p> <p>From this point the client has access to <codeph>subConnectionInfo().iSubConnectionUniqueId</codeph>, 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 <codeph>RConnection</codeph>, for example querying the amount of data currently transferred by the specified subconnection. </p> <codeblock id="GUID-436EFBF5-3338-55C1-B864-1A32B115EAD0" xml:space="preserve">
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);
</codeblock> </conbody></concept>