00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
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
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