hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/Csocket.cpp
branchRCL_3
changeset 59 8ad140f3dd41
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  General purpose socket implementetion
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "Csocket.h"
       
    22 #include <es_sock.h>
       
    23 #include <in_sock.h>
       
    24 #include "CSocketWriter.h"
       
    25 #include "Csocketreader.h"
       
    26 #include "MSocketObserver.h"
       
    27 
       
    28 #define DEBUG_FILENAME "IPProxyEngine.log"
       
    29 #include "DebugPrint.h"
       
    30 
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CSocket::CSocket
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CSocket::CSocket( RSocket* aSocket, TInt aUDPRemotePort /*= -1*/ ) :
       
    39     iSocket( aSocket ),
       
    40     iHasOwnership( EFalse ),
       
    41     iUDPRemotePort( aUDPRemotePort )
       
    42     {
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CSocket::ConstructL
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 void CSocket::ConstructL()
       
    50     {
       
    51     iSocketWriter = CSocketWriter::NewL( *iSocket, iUDPRemotePort );
       
    52     iSocketWriter->SetObserver( this );
       
    53     iSocketReader = CSocketReader::NewL( *iSocket, iUDPRemotePort );
       
    54     iSocketReader->SetObserver( this );
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CSocket::NewL
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 CSocket* CSocket::NewL( RSocket* aSocket, TInt aUDPRemotePort /*= -1*/ )
       
    62     {
       
    63     CSocket* self = CSocket::NewLC( aSocket, aUDPRemotePort );
       
    64     CleanupStack::Pop();
       
    65 
       
    66     return self;
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CSocket::NewLC
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CSocket* CSocket::NewLC( RSocket* aSocket, TInt aUDPRemotePort /*= -1*/ )
       
    74     {
       
    75     CSocket* self = new( ELeave ) CSocket( aSocket, aUDPRemotePort );
       
    76     CleanupStack::PushL( self );
       
    77 
       
    78     self->ConstructL();
       
    79     return self;
       
    80     }
       
    81 
       
    82 
       
    83 // Destructor
       
    84 CSocket::~CSocket()
       
    85     {
       
    86     delete iSocketWriter;
       
    87     delete iSocketReader;
       
    88     if ( iHasOwnership && iSocket )
       
    89         {
       
    90         TRequestStatus status;
       
    91         iSocket->Shutdown( RSocket::EImmediate, status );
       
    92         User::WaitForRequest( status );
       
    93         iSocket->Close();
       
    94         delete iSocket;
       
    95         }
       
    96     }
       
    97 
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CSocket::WriteL
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CSocket::WriteL( const TDesC8& aData )
       
   104     {
       
   105     iSocketWriter->IssueWriteL( aData );
       
   106     }
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CSocket::GetRSocket
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 RSocket* CSocket::GetRSocket()
       
   113     {
       
   114     return iSocket;
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CSocket::RemotePort
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 TUint CSocket::RemotePort() const
       
   122     {
       
   123     if ( IsUDP() )
       
   124         {
       
   125         return iUDPRemotePort;
       
   126         }
       
   127     else
       
   128         {
       
   129         TSockAddr addr;
       
   130         iSocket->RemoteName( addr );
       
   131         return addr.Port();
       
   132         }
       
   133 
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CSocket::LocalPort
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 TUint CSocket::LocalPort() const
       
   141     {
       
   142     return iSocket->LocalPort();
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CSocket::SetObserver
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 void CSocket::SetObserver( MSocketObserver* aObserver )
       
   150     {
       
   151     iObserver = aObserver;
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CSocket::SetSocketOwnershipMode
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void CSocket::SetSocketOwnershipMode( TBool aHasOwnership )
       
   159     {
       
   160     iHasOwnership = aHasOwnership;
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CSocket::IssueRead
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 void CSocket::IssueRead()
       
   168     {
       
   169     iSocketReader->Start();
       
   170     }
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CSocket::Cancel
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 void CSocket::Cancel()
       
   177     {
       
   178     iSocketReader->Cancel();
       
   179     iSocketWriter->Cancel();
       
   180     }
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CSocket::DataReceivedL
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 void CSocket::SocketInfo( TProtocolDesc& aDesc) const
       
   187     {
       
   188     iSocket->Info( aDesc );
       
   189     }
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // CSocket::IsUDP
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 TBool CSocket::IsUDP() const
       
   196     {
       
   197     TProtocolDesc desc;
       
   198     iSocket->Info( desc );
       
   199     if ( desc.iProtocol == KProtocolInetUdp && iUDPRemotePort > -1 )
       
   200         {
       
   201         return ETrue;
       
   202         }
       
   203     else
       
   204         {
       
   205         return EFalse;
       
   206         }
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CSocket::DataReceivedL
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void CSocket::DataReceivedL( const TDesC8& aData )
       
   214     {
       
   215     iObserver->DataReceivedL( this, aData );
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CSocket::BufferUnderrunL
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 void CSocket::BufferUnderrunL()
       
   223     {
       
   224     //No implementation
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CSocket::WriterErrorL
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 void CSocket::WriterErrorL( TInt aErrorCode )
       
   232     {
       
   233     DEBUG_PRINT( DEBUG_STRING(
       
   234         "CSocket::WriterErrorL( %d )" ), aErrorCode );
       
   235 
       
   236     iObserver->ErrorL( this, aErrorCode );
       
   237     }
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // CSocket::ReaderErrorL
       
   241 // -----------------------------------------------------------------------------
       
   242 //
       
   243 void CSocket::ReaderErrorL( TInt aErrorCode )
       
   244     {
       
   245     DEBUG_PRINT( DEBUG_STRING(
       
   246         "CSocket::ReaderErrorL( %d ), UDP port=%d" ), aErrorCode, iUDPRemotePort );
       
   247 
       
   248     if ( aErrorCode == KErrEof || aErrorCode == KErrCancel ||
       
   249         aErrorCode == KErrDisconnected || aErrorCode == KErrNotReady )
       
   250         {
       
   251         iObserver->DisconnectedL( this );
       
   252         }
       
   253     else
       
   254         {
       
   255         iObserver->ErrorL( this, aErrorCode );
       
   256         }
       
   257     }
       
   258 
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CSocket::ObserverLeaved
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 void CSocket::ObserverLeaved( TInt aLeaveCode )
       
   265     {
       
   266     iObserver->ObserverLeaved( this, aLeaveCode );
       
   267     }
       
   268 
       
   269 
       
   270 //  End of File