hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/CProtocolUDP.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:  UDP protocol implementation for IP proxy
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CProtocolUDP.h"
       
    22 #include "CExprUDPMsg.h"
       
    23 #include "MSocketWriter.h"
       
    24 #include "MProtocolObserverUDP.h"
       
    25 #include "CommRouterDefinitions.h"
       
    26 
       
    27 #define DEBUG_FILENAME "IPProxyEngine.log"
       
    28 #include "DebugPrint.h"
       
    29 
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CProtocolUDP::CProtocolUDP
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 CProtocolUDP::CProtocolUDP( MProtocolObserverUDP* aObserver )
       
    38     : iObserver( aObserver )
       
    39     {
       
    40     __ASSERT_DEBUG( iObserver, User::Invariant() );
       
    41     }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CProtocolUDP::ConstructL
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 void CProtocolUDP::ConstructL()
       
    48     {
       
    49     // Add all the expressions for UDP
       
    50     iExpressionArray = new (ELeave) CArrayPtrFlat<MBPExpression> ( 3 );
       
    51 
       
    52     CExprUDPMsg* UDPMsg = CExprUDPMsg::NewL( this );
       
    53     iExpressionArray->AppendL( UDPMsg );
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CProtocolUDP::NewL
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CProtocolUDP* CProtocolUDP::NewL( MProtocolObserverUDP* aObserver )
       
    61     {
       
    62     CProtocolUDP* self = CProtocolUDP::NewLC( aObserver );
       
    63     CleanupStack::Pop();
       
    64 
       
    65     return self;
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CProtocolUDP::NewLC
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 CProtocolUDP* CProtocolUDP::NewLC( MProtocolObserverUDP* aObserver )
       
    73     {
       
    74     CProtocolUDP* self = new( ELeave ) CProtocolUDP( aObserver );
       
    75     CleanupStack::PushL( self );
       
    76 
       
    77     self->ConstructL();
       
    78     return self;
       
    79     }
       
    80 
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CProtocolUDP::~CProtocolUDP
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 CProtocolUDP::~CProtocolUDP()
       
    87     {
       
    88     if ( iExpressionArray )
       
    89         {
       
    90         iExpressionArray->ResetAndDestroy();
       
    91         delete iExpressionArray;
       
    92         }
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CProtocolUDP::FrameStarted
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 void CProtocolUDP::FrameStarted()
       
   100     {
       
   101     iObserver->FrameStarted();
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CProtocolUDP::FrameParsedL
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 void CProtocolUDP::FrameParsedL( TUint aPort, const TDesC8& aData )
       
   109     {
       
   110     iObserver->UDPFrameParsedL( aPort, aData );
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CProtocolUDP::ProtocolErrorL
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 void CProtocolUDP::ProtocolErrorL(
       
   118     TInt aErrorCode, const TDesC8& aReceivedData )
       
   119     {
       
   120     iObserver->ProtocolErrorL( aErrorCode, aReceivedData );
       
   121     }
       
   122 
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CProtocolUDP::WriteFrameL
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CProtocolUDP::WriteFrameL( MSocketWriter& aSocketWriter,
       
   129             TUint aPeerPort, TUint aOriginalPort, const TDesC8& aData ) const
       
   130     {
       
   131     DEBUG_PRINT( DEBUG_STRING(
       
   132         "CProtocolUDP::WriteFrameL(), peer port=%d, original port=%d" ),
       
   133         aPeerPort, aOriginalPort );
       
   134 
       
   135     TInt bufSize = 0;
       
   136     bufSize += KUDPPrefix().Length();
       
   137     bufSize += KHexDecimalLength;
       
   138     bufSize += KPortSuffix().Length();
       
   139     bufSize += KHexDecimalLength;
       
   140     bufSize += KLengthSuffix().Length();
       
   141     bufSize += aData.Length();
       
   142     bufSize += KDataSuffix().Length();
       
   143     bufSize += KMessageSuffix().Length();
       
   144     
       
   145     TBuf8<KHexDecimalLength> hexbuf;
       
   146     HBufC8* buf = HBufC8::NewLC( bufSize );
       
   147     TPtr8 ptr( buf->Des() );
       
   148     
       
   149     // Append UDP prefix
       
   150     ptr.Append( KUDPPrefix );
       
   151 
       
   152     // Append peer port
       
   153     hexbuf.Format( KHexFormat, aPeerPort );
       
   154     ptr.Append( hexbuf );
       
   155     ptr.Append( KPortSuffix );
       
   156 
       
   157     // Append data length
       
   158     hexbuf.Format( KHexFormat, aData.Length() );
       
   159     ptr.Append( hexbuf );
       
   160     ptr.Append( KLengthSuffix );
       
   161 
       
   162     // Append data
       
   163     ptr.Append( aData );
       
   164     ptr.Append( KDataSuffix );
       
   165     ptr.Append( KMessageSuffix );
       
   166     
       
   167     // Write to socket
       
   168     aSocketWriter.WriteL( *buf );
       
   169 
       
   170     CleanupStack::PopAndDestroy( buf );
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CProtocolUDP::HandleReceivedDataL
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 TBool CProtocolUDP::HandleReceivedDataL( TDes8& aData, TInt& aStartPos,
       
   178                                          TInt& aLength )
       
   179     {
       
   180     TBool msgHandled = EFalse;
       
   181     TInt protCount = iExpressionArray->Count();
       
   182 
       
   183     for ( TInt i = 0; i < protCount; i++ )
       
   184         {
       
   185         msgHandled = iExpressionArray->At( i )->HandleRecievedMsgL( aData,
       
   186                                                                     aStartPos,
       
   187                                                                     aLength);
       
   188         if ( msgHandled )
       
   189             {
       
   190             break;
       
   191             }
       
   192         }
       
   193 
       
   194     return msgHandled;
       
   195     }
       
   196 //  End of File