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

Datagram.cpp

Go to the documentation of this file.
00001 //
00002 // Datagram.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 "Datagram.h"
00025 #include "Datagrams.h"
00026 #include "Buffer.h"
00027 #include "G2Packet.h"
00028 
00029 #ifdef _DEBUG
00030 #undef THIS_FILE
00031 static char THIS_FILE[]=__FILE__;
00032 #define new DEBUG_NEW
00033 #endif
00034 
00035 
00037 // CDatagramIn construction
00038 
00039 CDatagramIn::CDatagramIn()
00040 {
00041         m_pBuffer = NULL;
00042         m_pLocked = NULL;
00043         m_nBuffer = 0;
00044 }
00045 
00046 CDatagramIn::~CDatagramIn()
00047 {
00048         if ( m_pLocked ) delete [] m_pLocked;
00049         if ( m_pBuffer ) delete [] m_pBuffer;
00050 }
00051 
00053 // CDatagramIn prepare to handle a datagram
00054 
00055 void CDatagramIn::Create(SOCKADDR_IN* pHost, BYTE nFlags, WORD nSequence, BYTE nCount)
00056 {
00057         CopyMemory( &m_pHost, pHost, sizeof(SOCKADDR_IN) );
00058 
00059         m_bCompressed   = ( nFlags & SGP_DEFLATE ) ? TRUE : FALSE;
00060         m_nSequence             = nSequence;
00061         m_nCount                = nCount;
00062         m_nLeft                 = nCount;
00063 
00064         m_tStarted      = GetTickCount();
00065 
00066         if ( m_nBuffer < m_nCount )
00067         {
00068                 if ( m_pLocked ) delete [] m_pLocked;
00069                 if ( m_pBuffer ) delete [] m_pBuffer;
00070 
00071                 m_nBuffer       = m_nCount;
00072                 m_pBuffer       = new CBuffer*[ m_nBuffer ];
00073                 m_pLocked       = new BOOL[ m_nBuffer ];
00074         }
00075 
00076         ZeroMemory( m_pBuffer, sizeof(CBuffer*) * m_nBuffer );
00077         ZeroMemory( m_pLocked, sizeof(BOOL) * m_nBuffer );
00078 }
00079 
00081 // CDatagramIn add a datagram part
00082 
00083 BOOL CDatagramIn::Add(BYTE nPart, LPCVOID pData, DWORD nLength)
00084 {
00085         if ( nPart < 1 || nPart > m_nCount ) return FALSE;
00086         if ( m_nLeft == 0 ) return FALSE;
00087 
00088         if ( m_pLocked[ nPart - 1 ] == FALSE )
00089         {
00090                 m_pBuffer[ nPart - 1 ]->Add( pData, nLength );
00091                 m_pLocked[ nPart - 1 ] = TRUE;
00092 
00093                 if ( --m_nLeft == 0 ) return TRUE;
00094         }
00095 
00096         return FALSE;
00097 }
00098 
00100 // CDatagramIn convert to a packet
00101 
00102 CG2Packet* CDatagramIn::ToG2Packet()
00103 {
00104         if ( m_nCount != 1 )
00105         {
00106                 for ( int nPart = 1 ; nPart < m_nCount ; nPart++ )
00107                 {
00108                         m_pBuffer[0]->AddBuffer( m_pBuffer[ nPart ] );
00109                 }
00110         }
00111 
00112         if ( m_bCompressed && ! m_pBuffer[0]->Inflate() ) return NULL;
00113 
00114         return CG2Packet::ReadBuffer( m_pBuffer[0] );
00115 }
00116 

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