realtimenetprots/sipfw/SIP/ConnectionMgr/src/CReceiverTcp.cpp
changeset 0 307788aac0a8
child 29 5f12516512fa
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2002-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          : CReceiverTcp.cpp
       
    15 // Part of       : ConnectionMgr
       
    16 // Version       : SIP/4.0 
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include "CReceiverTcp.h"
       
    23 #include "SipLogs.h"
       
    24 #include "CSipConnection.h"
       
    25 #include "MContext.h"
       
    26 #include "CErrorHandler.h"
       
    27 #include "CSIPMsgAssembler.h"
       
    28 #include "CSocketContainer.h"
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CReceiverTcp::NewL
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 CReceiverTcp* CReceiverTcp::NewL(MContext& aContext)
       
    35 	{
       
    36 	CReceiverTcp* self = NewLC(aContext);
       
    37 	CleanupStack::Pop(self);
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // CReceiverTcp::NewLC
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CReceiverTcp* CReceiverTcp::NewLC(MContext& aContext)
       
    46 	{
       
    47 	CReceiverTcp* self = new (ELeave) CReceiverTcp(aContext);
       
    48 	CleanupStack::PushL(self);
       
    49 	self->ConstructL();
       
    50 	return self;	
       
    51 	}
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CReceiverTcp::ConstructL
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 void CReceiverTcp::ConstructL()
       
    58 	{
       
    59 	iNextLength = CSIPMsgAssembler::EMsgBufferSize;
       
    60 	CActiveScheduler::Add(this);
       
    61 	iMsgAssembler = CSIPMsgAssembler::NewL(*this, *iContext.SigCompHandler());
       
    62 	}
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CReceiverTcp::RunL
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 void CReceiverTcp::RunL()
       
    69 	{
       
    70 	TInt status( iStatus.Int() );
       
    71 	
       
    72 	TBool disconnected( EFalse );
       
    73 	
       
    74 	switch ( iReceiverState )
       
    75 	    {
       
    76 	    case EConnecting:
       
    77 	        {
       
    78 	        if ( status == KErrNone )
       
    79     			{
       
    80     			iReceiverState = EConnected;
       
    81     			
       
    82     			if ( iContext.ConnectionOpenL() )
       
    83     			    {
       
    84     			    // Context allowed receiving after connection was established
       
    85     			    ReceiveDataL();
       
    86     			    }
       
    87     			}
       
    88     		else
       
    89     			{
       
    90     			disconnected = ETrue;
       
    91     			}
       
    92 	        break;
       
    93 	        }
       
    94 	    case EShuttingDown:
       
    95 	        {
       
    96 	        disconnected = ETrue;
       
    97 	        break;
       
    98 	        }
       
    99 	    default: // EIdle, EConnected
       
   100 	        {
       
   101 	        if ( status == KErrEof )
       
   102     			{
       
   103     			disconnected = ETrue;
       
   104     			}
       
   105     		if ( status == KErrNone )
       
   106     			{
       
   107     			__SIP_MESSAGE_LOG( "Connection Manager::ReceivedFromNetwork",
       
   108     							   *iReceivedMsg )
       
   109     			__SIP_DES8_LOG( "Received data via TCP", *iReceivedMsg )
       
   110     			
       
   111     			iMsgAssembler->InputL( iReceivedMsg, iNextLength );
       
   112     			iReceivedMsg = 0; // Ownership was transferred to assembler	
       
   113     			
       
   114     			ReceiveDataL(); // May lead to self deletion
       
   115     			}
       
   116 	        break;
       
   117 	        }
       
   118 	    }
       
   119 	    
       
   120 	if ( disconnected )
       
   121 	    {
       
   122 	    __SIP_INT_LOG1( "TCP connection disconnected, status:", status )
       
   123 	    
       
   124 	    iContext.DisconnectedL(); // Leads to self deletion
       
   125 	    }
       
   126 	}
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CReceiverTcp::MessageCompleteL
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 void CReceiverTcp::MessageCompleteL(CSIPMessage* aSIPMessage,
       
   133 									TInt aError,
       
   134 									TBool aCompressed)
       
   135 	{
       
   136 	TInetAddr addr;
       
   137 	iContext.SocketContainer().RemoteName(addr);
       
   138 	iContext.ReceivedDataL(aSIPMessage, addr, aError, aCompressed);
       
   139 	}
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CReceiverTcp::DoCancel
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 void CReceiverTcp::DoCancel()
       
   146 	{
       
   147 	if ( iReceiverState == EConnected )
       
   148 		{
       
   149 		iContext.SocketContainer().CancelRecv();
       
   150 		}
       
   151 	else
       
   152 		{
       
   153 		iContext.SocketContainer().Socket().CancelAll();
       
   154 		}
       
   155 	RemoveReceived();
       
   156 	}
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CReceiverTcp::RunError
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 TInt CReceiverTcp::RunError(TInt /*aError*/)
       
   163 	{
       
   164 	return iContext.Remove();
       
   165 	}
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CReceiverTcp::ReceiveDataL
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 void CReceiverTcp::ReceiveDataL()
       
   172 	{
       
   173 	if (!IsActive())
       
   174 		{
       
   175 		RemoveReceived();
       
   176 
       
   177 		// Trying to allocate KMaxTInt/2 or more bytes causes panic
       
   178 		__ASSERT_ALWAYS( iNextLength < KMaxTInt/2, User::Leave(KErrOverflow) );
       
   179 
       
   180 		iReceivedMsg = HBufC8::NewL(iNextLength);
       
   181 		iReceivedMsgPtr.Set(iReceivedMsg->Des());
       
   182 		iContext.SocketContainer().RecvOneOrMore(iReceivedMsgPtr,
       
   183                                                  0,
       
   184                                                  iStatus,
       
   185                                                  iSockLenght);
       
   186 		SetActive();
       
   187 		}
       
   188 	}
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CReceiverTcp::Connect
       
   192 // -----------------------------------------------------------------------------
       
   193 //
       
   194 void CReceiverTcp::Connect(TInetAddr& aRemoteAddr)
       
   195 	{
       
   196 	if(!IsActive())
       
   197 		{
       
   198 		iContext.SocketContainer().Connect(aRemoteAddr, iStatus);
       
   199 		iReceiverState = EConnecting;
       
   200 		SetActive();
       
   201 		}
       
   202 	}
       
   203 	
       
   204 // -----------------------------------------------------------------------------
       
   205 // CReceiverTcp::Shutdown
       
   206 // -----------------------------------------------------------------------------
       
   207 //
       
   208 void CReceiverTcp::Shutdown()
       
   209 	{
       
   210 	Cancel();
       
   211 	iContext.SocketContainer().Socket().Shutdown( RSocket::ENormal, iStatus );
       
   212 	iReceiverState = EShuttingDown;
       
   213 	SetActive();
       
   214 	}
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CReceiverTcp::~CReceiverTcp
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 CReceiverTcp::~CReceiverTcp()
       
   221 	{
       
   222 	Cancel();
       
   223 	RemoveReceived();
       
   224 	delete iMsgAssembler;
       
   225 	}
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CReceiverTcp::CReceiverTcp
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 CReceiverTcp::CReceiverTcp(MContext& aContext) :
       
   232 	CActive(CActive::EPriorityStandard),
       
   233 	iContext(aContext),
       
   234 	iReceivedMsgPtr(0,0,0),
       
   235 	iReceiverState( EIdle )
       
   236 	{
       
   237 	}
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // CReceiverTcp::RemoveReceived
       
   241 // -----------------------------------------------------------------------------
       
   242 //	
       
   243 void CReceiverTcp::RemoveReceived()
       
   244     {
       
   245     delete iReceivedMsg;
       
   246     iReceivedMsg = 0;
       
   247     }
       
   248     
       
   249 // End of file