rtp/rtpstack/src/rtpcommsend.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 /*
       
     2 * Copyright (c) 2002-2003 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:   
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "rtpcommsend.h"
       
    22 
       
    23 // CONSTANTS
       
    24 
       
    25 /////////////////////Class CRtpCommSend///////////////////////////////////////
       
    26 // ================= MEMBER FUNCTIONS =======================
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 // Set to address
       
    30 // ---------------------------------------------------------------------------
       
    31 // 
       
    32 void CRtpCommSend::SetToAddress(TInetAddr& aAddr)
       
    33     {
       
    34     iToAddr.SetAddress( aAddr.Address() ); 
       
    35     iToAddr.SetPort( aAddr.Port() );     
       
    36     if (aAddr.Family() == KAfInet6 )
       
    37     	{
       
    38     	iToAddr.SetAddress( aAddr.Ip6Address() ); 
       
    39     	}  
       
    40     }
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // C++ default constructor can NOT contain any code, that
       
    44 // might leave.
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CRtpCommSend::CRtpCommSend( RSocket& aSocket, MRtpErrNotify& aErrNotify, TInetAddr& aAddr ) 
       
    48     : CActive( EPriorityStandard ), 
       
    49       iSocket( aSocket ),
       
    50       iToAddr(aAddr),      
       
    51       iWriteStatus( EWaiting ),
       
    52       iBufPtr( NULL, 0 ),
       
    53       iRtpErrNotify( aErrNotify ),
       
    54       iSendQue(CRtpSendItem::iOffset ),
       
    55       iQueueIter(iSendQue)
       
    56     {
       
    57     CActiveScheduler::Add( this );
       
    58     }
       
    59   
       
    60 // ---------------------------------------------------------------------------
       
    61 // Symbian 2nd phase constructor can leave.
       
    62 // ---------------------------------------------------------------------------
       
    63 // 
       
    64 void CRtpCommSend::ConstructL( /*TInt aBufSize */)
       
    65     {
       
    66 
       
    67    	}
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // Two-phased constructor.
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CRtpCommSend* CRtpCommSend::NewL( RSocket& aSocket, MRtpErrNotify& aErrNotify,
       
    74                                   /*TInt aBufSize,*/ TInetAddr& aAddr )
       
    75     {
       
    76     CRtpCommSend* self = new ( ELeave ) CRtpCommSend( aSocket, aErrNotify, aAddr );
       
    77     CleanupStack::PushL( self );
       
    78     self->ConstructL( /*aBufSize*/ );
       
    79     CleanupStack::Pop(); //self
       
    80     return self;
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // Destructor
       
    85 // 
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 CRtpCommSend::~CRtpCommSend()
       
    89     {
       
    90     Cancel();
       
    91     CompleteAndRemoveRequests( KErrAbort );
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // CRtpCommSend::RunL()
       
    96 // Called when request has completed.
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 void CRtpCommSend::RunL()
       
   100     {
       
   101     // Active object request complete handler
       
   102     if ( iStatus == KErrNone )
       
   103         {
       
   104         RemoveFromQueue();
       
   105 		
       
   106         switch( iWriteStatus )
       
   107             {
       
   108             case ESending:
       
   109                 SendNextPacket();
       
   110                 break;
       
   111             default:
       
   112                 RTP_DEBUG_DETAIL( "Tx state error" );
       
   113                 User::Leave( KErrInUse );
       
   114                 break;
       
   115             }
       
   116         }
       
   117     else 
       
   118         {
       
   119         RTP_DEBUG_DETAIL_DVALUE( "CRtpCommSend::RunL, ERR: ", iStatus.Int() );
       
   120         
       
   121         // Client originated cancelling is handled in CancelAsyncSend() function.
       
   122         if ( KErrCancel != iStatus.Int() )
       
   123             {
       
   124             CompleteAndRemoveRequests( iStatus.Int() );
       
   125             }
       
   126         
       
   127         iRtpErrNotify.ErrorNotify( iStatus.Int() );
       
   128         }
       
   129     }
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // CRtpCommSend::RemoveFromQueue()
       
   133 // Remove Item from Queue
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 void CRtpCommSend::RemoveFromQueue()
       
   137 	{
       
   138 	if ( !( iSendQue.IsEmpty() ) )
       
   139 		{
       
   140 		CRtpSendItem* item = iSendQue.First();
       
   141 		CompleteClientRequest( *item, KErrNone );
       
   142 		iSendQue.Remove(*item);
       
   143 		delete item;
       
   144 		}
       
   145 	}
       
   146 // ---------------------------------------------------------------------------
       
   147 // CRtpCommSend::DoCancel()
       
   148 // Cancel ongoing requests.
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 void CRtpCommSend::DoCancel()
       
   152     {	
       
   153     // Cancel asychronous send request
       
   154     CancelAsyncSend();
       
   155     } 
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // CRtpCommSend::CancelAsyncSend()
       
   159 // 
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 void CRtpCommSend::CancelAsyncSend()
       
   163 	{
       
   164     RTP_DEBUG_DETAIL( "CRtpCommSend::CancelAsyncSend" );
       
   165 
       
   166     iSocket.CancelSend();
       
   167     CompleteAndRemoveRequests( KErrCancel );
       
   168 	}
       
   169 
       
   170 // ---------------------------------------------------------------------------
       
   171 // CRtpCommSend::Send()
       
   172 // 
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 void CRtpCommSend::Send( const TDesC8& aPacket, TRequestStatus& aStatus )
       
   176     {
       
   177     RTP_DEBUG_DETAIL( "CRtpCommSend::Send -- ASynchronous" );
       
   178     
       
   179     aStatus = KRequestPending;
       
   180     
       
   181     TInt err( KErrNone );
       
   182     TRAP( err, 
       
   183         iSendQue.AddLast( *CRtpSendItem::NewL( aPacket, iStatus, aStatus ) ) );
       
   184     
       
   185     if ( KErrNone == err )
       
   186         {
       
   187         if ( !IsActive() )
       
   188             {
       
   189             SendNextPacket();
       
   190             }
       
   191         }
       
   192     else
       
   193         {
       
   194         // Probably out of memory, so don't send any more packets
       
   195         TRequestStatus* status = &aStatus;
       
   196         User::RequestComplete( status, err );
       
   197         CompleteAndRemoveRequests( err );
       
   198         RTP_DEBUG_DETAIL( "Error Occured while put Packet to Send Item Que" );
       
   199         iRtpErrNotify.ErrorNotify( err );
       
   200         }
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // CRtpCommSend::Send()
       
   205 // 
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 TInt CRtpCommSend::Send( const TDesC8& aPacket )
       
   209     {
       
   210     RTP_DEBUG_DETAIL( "CRtpCommSend::Send -- Synchronous Entry" );
       
   211     
       
   212     TInt err( KErrNone );
       
   213     if ( ( iWriteStatus != EWaiting ) && ( iWriteStatus != ESending ) )
       
   214         {
       
   215         RTP_DEBUG_DETAIL( "Write status is not correct" );
       
   216         return KErrDisconnected;
       
   217         }
       
   218     
       
   219     // Put in in our buffer
       
   220     TRAP( err, iSendQue.AddLast(*CRtpSendItem::NewL(aPacket, iStatus)) );
       
   221 	RTP_DEBUG_DETAIL( "Put Packet to Send Item Que" );
       
   222     
       
   223     if ( err != KErrNone )
       
   224         {
       
   225         // Probably out of memory, so don't send any more packets
       
   226         CompleteAndRemoveRequests( err );
       
   227         
       
   228 		RTP_DEBUG_DETAIL( "Error Occured while put Packet to Send Item Que" );
       
   229         return err;
       
   230         }
       
   231     
       
   232     if ( !IsActive() )
       
   233         {
       
   234         SendNextPacket();
       
   235 		}
       
   236 	
       
   237 	RTP_DEBUG_DETAIL( "CRtpCommSend::Send -- Synchronous Exit" );
       
   238     return err;
       
   239     }
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // CRtpCommSend::SendNextPacket()
       
   243 // Write data from buffer to socket.
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 void CRtpCommSend::SendNextPacket()
       
   247     {
       
   248     RTP_DEBUG_DETAIL( "CRtpCommSend::SendNextPacket Entry" );
       
   249     
       
   250     iWriteStatus = EWaiting;
       
   251     iQueueIter.SetToFirst();
       
   252 	CRtpSendItem* item;
       
   253     if ( ( item=iQueueIter++ ) != NULL )
       
   254         {
       
   255         iSocket.SendTo( item->GetData(), iToAddr, 0, item->iStatus );
       
   256         SetActive();
       
   257         iWriteStatus = ESending;
       
   258         }
       
   259 
       
   260     RTP_DEBUG_DETAIL( "CRtpCommSend::SendNextPacket Exit" );
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // CRtpCommSend::CompleteAndRemoveRequests()
       
   265 // Removes all requests in the queue.
       
   266 // ---------------------------------------------------------------------------
       
   267 //
       
   268 void CRtpCommSend::CompleteAndRemoveRequests( TInt aError )
       
   269     {
       
   270     RTP_DEBUG_DETAIL( "CRtpCommSend::CompleteAndRemoveRequests Entry" );
       
   271     
       
   272     CRtpSendItem* item;
       
   273     iQueueIter.SetToFirst();
       
   274     item = iQueueIter++;
       
   275     while ( item )
       
   276         {
       
   277         CompleteClientRequest( *item, aError );
       
   278         iSendQue.Remove( *item );
       
   279         delete item;
       
   280         item = iQueueIter++;
       
   281         }
       
   282     
       
   283     RTP_DEBUG_DETAIL( "CRtpCommSend::CompleteAndRemoveRequests Exit" );
       
   284     }
       
   285 
       
   286 // ---------------------------------------------------------------------------
       
   287 // CRtpCommSend::CompleteClientRequest()
       
   288 // Completes the request.
       
   289 // ---------------------------------------------------------------------------
       
   290 //
       
   291 void CRtpCommSend::CompleteClientRequest( CRtpSendItem& aItem, 
       
   292     TInt aReason ) const
       
   293     {
       
   294     RTP_DEBUG_DETAIL( "CRtpCommSend::CompleteClientRequest Entry" );
       
   295 
       
   296     TRequestStatus* status = aItem.ClientStatus();
       
   297     if ( NULL != status )
       
   298         {
       
   299         User::RequestComplete( status, aReason );
       
   300         }
       
   301     }
       
   302 
       
   303 // End of file
       
   304 
       
   305