Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

BTPacket.cpp

Go to the documentation of this file.
00001 //
00002 // BTPacket.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "BTPacket.h"
00025 #include "Buffer.h"
00026 
00027 #ifdef _DEBUG
00028 #undef THIS_FILE
00029 static char THIS_FILE[]=__FILE__;
00030 #define new DEBUG_NEW
00031 #endif
00032 
00033 CBTPacket::CBTPacketPool CBTPacket::POOL;
00034 
00035 
00037 // CBTPacket construction
00038 
00039 CBTPacket::CBTPacket() : CPacket( PROTOCOL_BT )
00040 {
00041         m_nType = 0;
00042 }
00043 
00044 CBTPacket::~CBTPacket()
00045 {
00046 }
00047 
00049 // CBTPacket serialize
00050 
00051 void CBTPacket::ToBuffer(CBuffer* pBuffer) const
00052 {
00053         ASSERT( pBuffer != NULL );
00054 
00055         if ( m_nType == BT_PACKET_KEEPALIVE )
00056         {
00057                 DWORD nZero = 0;
00058                 pBuffer->Add( &nZero, 4 );
00059         }
00060         else
00061         {
00062                 DWORD nLength = SWAP_LONG( m_nLength + 1 );
00063                 pBuffer->Add( &nLength, 4 );
00064                 pBuffer->Add( &m_nType, 1 );
00065                 pBuffer->Add( m_pBuffer, m_nLength );
00066         }
00067 }
00068 
00070 // CBTPacket unserialize
00071 
00072 CBTPacket* CBTPacket::ReadBuffer(CBuffer* pBuffer)
00073 {
00074         ASSERT( pBuffer != NULL );
00075         if ( pBuffer->m_nLength < 4 ) return NULL;
00076 
00077         DWORD nLength = *(DWORD*)pBuffer->m_pBuffer;
00078         nLength = SWAP_LONG( nLength );
00079         if ( pBuffer->m_nLength < 4 + nLength ) return NULL;
00080 
00081         if ( nLength == 0 )
00082         {
00083                 pBuffer->Remove( 4 );
00084                 return CBTPacket::New( BT_PACKET_KEEPALIVE );
00085         }
00086 
00087         CBTPacket* pPacket = CBTPacket::New( pBuffer->m_pBuffer[4] );
00088         pPacket->Write( pBuffer->m_pBuffer + 5, nLength - 1 );
00089 
00090         pBuffer->Remove( 4 + nLength );
00091         return pPacket;
00092 }
00093 
00095 // CBTPacket debugging
00096 
00097 LPCTSTR CBTPacket::GetType() const
00098 {
00099         static TCHAR szTypeBuffer[16];
00100         _stprintf( szTypeBuffer, _T("%i"), int( m_nType ) );
00101         return szTypeBuffer;
00102 }

Generated on Thu Dec 15 10:39:33 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2