realtimenetprots/sipfw/SIP/ConnectionMgr/src/CReceiver.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Name          : CReceiver.cpp
       
    15 // Part of       : ConnectionMgr
       
    16 // Version       : SIP/5.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include "CReceiver.h"
       
    22 #include "SipLogs.h"
       
    23 #include "CSipConnection.h"
       
    24 #include "CommonConsts.h"
       
    25 #include "MContext.h"
       
    26 #include "MSigCompController.h"
       
    27 #include "CSocketContainer.h"
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CReceiver::NewL
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CReceiver* CReceiver::NewL( MContext& aContext, TUint aIPAddrFamily )
       
    34 	{
       
    35 	CReceiver* self = NewLC( aContext, aIPAddrFamily );
       
    36 	CleanupStack::Pop( self );
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CReceiver::NewLC
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 CReceiver* CReceiver::NewLC( MContext& aContext, TUint aIPAddrFamily )
       
    45 	{
       
    46 	CReceiver* self = new ( ELeave ) CReceiver( aContext, aIPAddrFamily );
       
    47 	CleanupStack::PushL( self );
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CReceiver::CReceiver
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 CReceiver::CReceiver( MContext& aContext, TUint aIPAddrFamily ) : 
       
    56     CActive( EPriorityStandard ), 
       
    57     iContext( aContext ),
       
    58     iIPAddrFamily( aIPAddrFamily ),
       
    59     iState( EIdle ),
       
    60     iReceivedMsgPtr( 0, 0, 0 )
       
    61 	{
       
    62 	CActiveScheduler::Add( this );
       
    63 	}
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CReceiver::~CReceiver
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CReceiver::~CReceiver()
       
    70 	{
       
    71 	Cancel();
       
    72 	delete iReceivedMsg;
       
    73 	}
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CReceiver::DoCancel
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 void CReceiver::DoCancel()
       
    80 	{
       
    81 	CSocketContainer* socketContainer = 
       
    82 	    iContext.SocketContainer( iIPAddrFamily );
       
    83 	if ( iState == EListening )
       
    84 	    {
       
    85 	    socketContainer->Socket().CancelIoctl();
       
    86 	    }
       
    87 	else
       
    88         {
       
    89         socketContainer->Socket().CancelRecv();
       
    90         }
       
    91 	}
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CReceiver::Listen
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 void CReceiver::Listen()
       
    98 	{
       
    99 	CSocketContainer* socketContainer = 
       
   100 	    iContext.SocketContainer( iIPAddrFamily );	
       
   101 	if ( socketContainer && !IsActive() )
       
   102 		{	
       
   103 		iState = EListening;
       
   104 		
       
   105 		iFlags() = KSockSelectRead | KSockSelectExcept;
       
   106 	    socketContainer->Socket().Ioctl( 
       
   107 	        KIOctlSelect, 
       
   108 	        iStatus, 
       
   109 	        &iFlags, 
       
   110             KSOLSocket );	
       
   111 		SetActive();
       
   112 		}
       
   113 	}
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CReceiver::RunL
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 void CReceiver::RunL()
       
   120 	{
       
   121 	switch ( iStatus.Int() )
       
   122 		{
       
   123 		case KErrNone:
       
   124 			{
       
   125 			if ( iState == EListening )
       
   126 			    {
       
   127 			    ReceivePendingL();
       
   128 			    }
       
   129 			else if ( iState == EReceiving )
       
   130 			    {
       
   131     			__SIP_MESSAGE_LOG( "Connection Manager::ReceivedFromNetwork",
       
   132     							   *iReceivedMsg )
       
   133     			__SIP_DES8_LOG( "Received data via UDP", *iReceivedMsg )	
       
   134     			// Give away ownership of the received message
       
   135     			HBufC8* tmpReceivedMsg = iReceivedMsg;
       
   136     			iReceivedMsg = 0; 
       
   137     			
       
   138     			Listen();
       
   139     			ReceivedDataL( tmpReceivedMsg );
       
   140 			    }
       
   141 			else
       
   142 			    {
       
   143 			    // Required by PC-Lint
       
   144 			    }
       
   145 			break;
       
   146 			}
       
   147 		case KErrCancel:
       
   148 			break;
       
   149 		default:
       
   150 			{
       
   151 			__SIP_INT_LOG1( "CReceiver::RunL() status", iStatus.Int() )
       
   152 			iContext.Remove();
       
   153 			break;
       
   154 			}
       
   155 		}
       
   156 	}
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CReceiver::ReceivedDataL
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 void CReceiver::ReceivedDataL( HBufC8* aData )
       
   163 	{
       
   164 	if ( iContext.SigCompHandler()->IsSupported() &&		
       
   165 		 iContext.SigCompHandler()->IsSigCompMsg( *aData ) )
       
   166 		{
       
   167 		__SIP_LOG( "IsSigComp complete == ETrue" )
       
   168 
       
   169 		CleanupStack::PushL( aData );
       
   170 		TUint bytesconsumed = 0;
       
   171 		CBufBase* decompressedmessage =				  
       
   172 			iContext.SigCompHandler()->DecompressL( *aData,
       
   173 													bytesconsumed,
       
   174 													EFalse );
       
   175 		CleanupStack::PopAndDestroy( aData );
       
   176 		CleanupStack::PushL( decompressedmessage );
       
   177 
       
   178 		HBufC8* receiveddata = HBufC8::NewL( decompressedmessage->Size() );
       
   179 		receiveddata->Des().Copy( decompressedmessage->Ptr( 0 ) );
       
   180 		CleanupStack::PopAndDestroy( decompressedmessage );
       
   181 
       
   182 		iContext.ReceivedDataL( receiveddata, iAddr, ETrue );
       
   183 		}
       
   184 	else
       
   185 		{
       
   186 		iContext.ReceivedDataL( aData, iAddr, EFalse );
       
   187 		}
       
   188 	}
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CReceiver::ReceivePendingL
       
   192 // -----------------------------------------------------------------------------
       
   193 //	
       
   194 void CReceiver::ReceivePendingL()
       
   195     {
       
   196     __ASSERT_ALWAYS( !IsActive(), User::Leave( KErrInUse ) );
       
   197     
       
   198     CSocketContainer* socketContainer = 
       
   199 	    iContext.SocketContainer( iIPAddrFamily );
       
   200     __ASSERT_ALWAYS( socketContainer, User::Leave( KErrNotFound ) );
       
   201     
       
   202     iState = EReceiving;
       
   203 			    
       
   204 	TInt bytesPending( 0 );
       
   205 	User::LeaveIfError( 
       
   206 	    socketContainer->Socket().GetOpt( 
       
   207 	        KSOReadBytesPending, 
       
   208             KSOLSocket, 
       
   209             bytesPending ) );
       
   210 
       
   211     __SIP_INT_LOG1( "CReceiver::ReceivePendingL() bytes", bytesPending )
       
   212 
       
   213 	// Don't allow excessive buffer
       
   214 	if ( bytesPending > KMaxMessageLength )
       
   215 	    {
       
   216 	    bytesPending = KMaxMessageLength;
       
   217 	    }
       
   218 	    
       
   219     delete iReceivedMsg;
       
   220 	iReceivedMsg = 0;
       
   221 	iReceivedMsg = HBufC8::NewL( bytesPending );
       
   222 	iReceivedMsgPtr.Set( iReceivedMsg->Des() );
       
   223 	socketContainer->Socket().RecvFrom( iReceivedMsgPtr, iAddr, 0, iStatus );     	
       
   224 	SetActive();
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CReceiver::RunError
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 TInt CReceiver::RunError( TInt aError )
       
   232 	{
       
   233 	Listen();
       
   234 	if ( aError != KErrNoMemory )
       
   235 		{
       
   236 		return KErrNone;
       
   237 		}
       
   238 	return aError;
       
   239 	}