pkthndlr.cpp
Upload User: zhongxx05
Upload Date: 2007-06-06
Package Size: 33641k
Code Size: 6k
Category:

Symbian

Development Platform:

C/C++

  1. /* ***** BEGIN LICENSE BLOCK ***** 
  2.  * Version: RCSL 1.0/RPSL 1.0 
  3.  *  
  4.  * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 
  5.  *      
  6.  * The contents of this file, and the files included with this file, are 
  7.  * subject to the current version of the RealNetworks Public Source License 
  8.  * Version 1.0 (the "RPSL") available at 
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed 
  10.  * the file under the RealNetworks Community Source License Version 1.0 
  11.  * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 
  12.  * in which case the RCSL will apply. You may also obtain the license terms 
  13.  * directly from RealNetworks.  You may not use this file except in 
  14.  * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 
  15.  * applicable to this file, the RCSL.  Please see the applicable RPSL or 
  16.  * RCSL for the rights, obligations and limitations governing use of the 
  17.  * contents of the file.  
  18.  *  
  19.  * This file is part of the Helix DNA Technology. RealNetworks is the 
  20.  * developer of the Original Code and owns the copyrights in the portions 
  21.  * it created. 
  22.  *  
  23.  * This file, and the files included with this file, is distributed and made 
  24.  * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  25.  * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  26.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
  27.  * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  28.  * 
  29.  * Technology Compatibility Kit Test Suite(s) Location: 
  30.  *    http://www.helixcommunity.org/content/tck 
  31.  * 
  32.  * Contributor(s): 
  33.  *  
  34.  * ***** END LICENSE BLOCK ***** */ 
  35. #include "hxtypes.h"
  36. #include "hxresult.h"
  37. #include "hxcom.h"  
  38. #include "hlxclib/string.h"
  39. #include "ihxpckts.h"
  40. #include "hxassert.h"
  41. #include "rtpwrap.h"
  42. #include "pkthndlr.h"
  43. RTCPPacker::RTCPPacker()
  44.     : m_pReport(NULL)
  45.     , m_pSDES(NULL)
  46.     , m_pBYE(NULL)
  47.     , m_pAPP(NULL)
  48. {
  49. }
  50. HX_RESULT
  51. RTCPPacker::Set(RTCPPacket* pPkt)
  52. {
  53.     HX_ASSERT(pPkt);
  54.     switch(pPkt->packet_type)
  55.     {
  56.     case RTCP_SR:
  57. HX_ASSERT(!m_pReport);
  58. m_pReport = pPkt;
  59. break;
  60.     case RTCP_RR:
  61. HX_ASSERT(!m_pReport);
  62.      m_pReport = pPkt;
  63. break;
  64.     case RTCP_SDES:
  65. m_pSDES = pPkt;
  66. break;
  67.     case RTCP_BYE:
  68. m_pBYE = pPkt;
  69. break;
  70.     case RTCP_APP:
  71. m_pAPP = pPkt;
  72. break;
  73.     default:
  74. HX_ASSERT(!"RTCPPacker::Set():  Don't know this packet type");
  75. return HXR_FAIL;
  76.     }
  77.     return HXR_OK;
  78. }
  79. void
  80. RTCPPacker::PackOne(RTCPPacket* pPkt, REF(UCHAR*) pc, REF(UINT32) ulPackedLen)
  81. {
  82.     HX_ASSERT(pPkt && pc);
  83.     pc = pPkt->pack(pc, ulPackedLen);    
  84. }
  85. /*
  86. *   SR or RR must have been set
  87. *   SDES must have been set
  88. */   
  89. HX_RESULT
  90. RTCPPacker::Pack(REF(IHXBuffer*) pBuf)
  91. {    
  92.     HX_ASSERT(m_pReport);
  93.     HX_ASSERT(m_pSDES);
  94.     HX_ASSERT(pBuf); // needs to be an object
  95.     UINT32 ulBufSize = (m_pReport->length + 1) * 4;
  96.     ulBufSize += (m_pSDES->length + 1) * 4;
  97.     if (m_pBYE)
  98.     {
  99. ulBufSize += (m_pBYE->length + 1) * 4;
  100.     }
  101.     if (m_pAPP)
  102.     {
  103. ulBufSize += (m_pAPP->length + 1) * 4;
  104.     }
  105.     // now pack!
  106.     pBuf->SetSize(ulBufSize);
  107.     UCHAR* pc = pBuf->GetBuffer();
  108.     UINT32 ulPackedLen = 0;
  109.     PackOne(m_pReport, pc, ulPackedLen);
  110.     HX_ASSERT((((UINT32)m_pReport->length + 1) * 4) == ulPackedLen);
  111.     PackOne(m_pSDES, pc, ulPackedLen);
  112.     HX_ASSERT((((UINT32)m_pSDES->length + 1) * 4) == ulPackedLen);
  113.     if (m_pAPP)
  114.     {
  115. PackOne(m_pAPP, pc, ulPackedLen);
  116. HX_ASSERT((((UINT32)m_pAPP->length + 1) * 4) == ulPackedLen);
  117.     }
  118.     /* BYE pkt needs to be the last pkt! */
  119.     if (m_pBYE)
  120.     {    
  121. PackOne(m_pBYE, pc, ulPackedLen);
  122. HX_ASSERT((((UINT32)m_pBYE->length + 1) * 4) == ulPackedLen);
  123.     }
  124.     return HXR_OK;
  125. }
  126. RTCPUnPacker::RTCPUnPacker()
  127. {
  128.     /* nothing to do */
  129. }
  130. /*
  131. *   if there are left over pkts in the list, delete them.
  132. */
  133. RTCPUnPacker::~RTCPUnPacker()
  134. {
  135.     if (!m_PktList.IsEmpty())
  136.     {
  137. CHXSimpleList::Iterator i;
  138. for (i = m_PktList.Begin(); i != m_PktList.End(); ++i)
  139. {
  140.     delete (RTCPPacket*)(*i);
  141. }
  142.     }
  143.     HX_ASSERT(m_PktList.IsEmpty());
  144. }
  145. /*
  146. *   Get a pkt in the head of the list.  Caller is responsible of deleting it
  147. */
  148. HX_RESULT
  149. RTCPUnPacker::Get(REF(RTCPPacket*) pPkt)
  150. {
  151.     if (m_PktList.IsEmpty())
  152.     {
  153. return HXR_FAIL;
  154.     }
  155.     pPkt = (RTCPPacket*)m_PktList.RemoveHead();    
  156.     HX_ASSERT(pPkt);
  157.     return HXR_OK;
  158. }
  159. /*
  160. *   RTCPPacket will be created for each pkt in this compound pkt.
  161. *   Caller is responsible of freeing them
  162. */
  163. HX_RESULT
  164. RTCPUnPacker::UnPack(IHXBuffer* pCompound)
  165. {
  166.     if (HXR_OK != Validate(pCompound))
  167.     {
  168. return HXR_FAIL;
  169.     }
  170.     
  171.     BYTE* pFirst = pCompound->GetBuffer();
  172.     BYTE* pNext = pFirst;
  173.     RTCPPacket* pPkt = NULL;
  174.     
  175.     while (pNext && pNext < (pFirst + pCompound->GetSize()))
  176.     {
  177. pPkt = new RTCPPacket();
  178. pNext = pPkt->unpack(pNext, (pFirst + pCompound->GetSize()) - pNext);
  179. m_PktList.AddTail(pPkt);
  180.     }
  181.     return HXR_OK;
  182. }
  183. /*
  184. *   Copied from the RFC with a feeling...
  185. */
  186. HX_RESULT
  187. RTCPUnPacker::Validate(IHXBuffer* pCompound)
  188. {
  189.     HX_ASSERT(pCompound);
  190.     
  191.     UINT32 ulLen = pCompound->GetSize();
  192.     BYTE* pc = pCompound->GetBuffer();
  193.     BYTE* end = pc + ulLen;
  194.     BYTE* pcTemp;    
  195.     UINT16 unPktLen = 0;
  196.     UINT8  uchVer = 0;
  197.     do
  198.     {
  199. pcTemp = pc;
  200. uchVer = (*pcTemp++ & 0xc0) >> 6;
  201. *pcTemp++;  // this is type
  202. unPktLen = *pcTemp++ << 8;
  203. unPktLen |= *pcTemp++;
  204. pc = (pc + ((unPktLen + 1) * 4));
  205.     } while (pc < end && 2 == uchVer);
  206.     
  207.     if (pc != end)
  208.     {
  209. /* something is wrong */
  210. HX_ASSERT(!"RTCPUnPacker::Validate() failed");
  211. return HXR_FAIL;
  212.     }
  213.     return HXR_OK;
  214. }