📄 hostbrowser.cpp
字号:
//
// HostBrowser.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2004.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "StdAfx.h"
#include "Shareaza.h"
#include "Settings.h"
#include "Network.h"
#include "Buffer.h"
#include "G1Packet.h"
#include "G2Packet.h"
#include "GProfile.h"
#include "Neighbours.h"
#include "HostBrowser.h"
#include "Transfers.h"
#include "QueryHit.h"
#include "Downloads.h"
#include "VendorCache.h"
#include "WndBrowseHost.h"
#include "XML.h"
#include <zlib.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// CHostBrowser construction
CHostBrowser::CHostBrowser(CBrowseHostWnd* pNotify, IN_ADDR* pAddress, WORD nPort, BOOL bMustPush, GGUID* pClientID)
{
m_nState = hbsNull;
m_pNotify = pNotify;
m_pProfile = NULL;
m_bNewBrowse = FALSE;
m_pAddress = *pAddress;
m_nPort = nPort;
m_bMustPush = bMustPush;
m_bCanPush = FALSE;
if ( pClientID != NULL )
{
m_bCanPush = TRUE;
m_pClientID = *pClientID;
}
m_tPushed = 0;
m_bConnect = FALSE;
m_nHits = 0;
m_pVendor = NULL;
m_bCanChat = FALSE;
m_pBuffer = NULL;
m_pInflate = NULL;
}
CHostBrowser::~CHostBrowser()
{
Stop();
if ( m_pProfile ) delete m_pProfile;
}
//////////////////////////////////////////////////////////////////////
// CHostBrowser browse control
BOOL CHostBrowser::Browse()
{
CSingleLock pLock( &Transfers.m_pSection, TRUE );
if ( m_hSocket != INVALID_SOCKET ) return FALSE;
m_sAddress = inet_ntoa( m_pAddress );
if ( m_bMustPush )
{
if ( SendPush( FALSE ) )
{
theApp.Message( MSG_SYSTEM, IDS_BROWSE_PUSHED_TO, (LPCTSTR)m_sAddress );
}
else
{
theApp.Message( MSG_ERROR, IDS_BROWSE_CANT_PUSH_TO, (LPCTSTR)m_sAddress );
return FALSE;
}
}
else
{
if ( ConnectTo( &m_pAddress, m_nPort ) )
{
theApp.Message( MSG_SYSTEM, IDS_BROWSE_CONNECTING_TO, (LPCTSTR)m_sAddress );
}
else
{
theApp.Message( MSG_ERROR, IDS_BROWSE_CANT_CONNECT_TO, (LPCTSTR)m_sAddress );
return FALSE;
}
}
m_nState = hbsConnecting;
m_nHits = 0;
if ( m_pProfile != NULL ) delete m_pProfile;
m_pProfile = NULL;
return TRUE;
}
void CHostBrowser::Stop(BOOL bCompleted)
{
CSingleLock pLock( &Transfers.m_pSection, TRUE );
if ( m_hSocket != INVALID_SOCKET )
{
theApp.Message( MSG_DEFAULT, IDS_BROWSE_CLOSED, (LPCTSTR)m_sAddress );
}
CTransfer::Close();
m_nState = hbsNull;
m_tPushed = 0;
if ( m_pBuffer != NULL )
{
delete m_pBuffer;
m_pBuffer = NULL;
}
if ( m_pInflate != NULL )
{
z_streamp pStream = (z_streamp)m_pInflate;
inflateEnd( pStream );
delete pStream;
m_pInflate = NULL;
}
}
BOOL CHostBrowser::IsBrowsing() const
{
return m_nState != hbsNull;
}
float CHostBrowser::GetProgress() const
{
if ( m_nState != hbsContent || m_nLength == 0 || m_nLength == 0xFFFFFFFF ) return 0;
return (float)m_nReceived / (float)m_nLength;
}
//////////////////////////////////////////////////////////////////////
// CHostBrowser event handling
BOOL CHostBrowser::OnConnected()
{
CTransfer::OnConnected();
SendRequest();
return TRUE;
}
BOOL CHostBrowser::OnRead()
{
if ( m_pInput == NULL || m_pOutput == NULL ) return TRUE;
CTransfer::OnRead();
switch ( m_nState )
{
case hbsRequesting:
if ( ! ReadResponseLine() ) return FALSE;
if ( m_nState != hbsHeaders ) break;
case hbsHeaders:
if ( ! ReadHeaders() ) return FALSE;
if ( m_nState != hbsContent ) break;
case hbsContent:
return ReadContent();
}
return TRUE;
}
void CHostBrowser::OnDropped(BOOL bError)
{
if ( m_hSocket == INVALID_SOCKET ) return;
if ( m_nState == hbsConnecting )
{
theApp.Message( MSG_ERROR, IDS_BROWSE_CANT_CONNECT_TO, (LPCTSTR)m_sAddress );
if ( ! m_tPushed && SendPush( TRUE ) ) return;
}
else
{
if ( m_nLength == 0xFFFFFFFF )
{
m_nLength = m_pInput->m_nLength;
ReadContent();
return;
}
theApp.Message( MSG_ERROR, IDS_BROWSE_DROPPED, (LPCTSTR)m_sAddress );
}
Stop();
}
BOOL CHostBrowser::OnRun()
{
CTransfer::OnRun();
DWORD nNow = GetTickCount();
switch ( m_nState )
{
case hbsConnecting:
if ( nNow - m_tConnected > Settings.Connection.TimeoutConnect * 2 )
{
OnDropped( TRUE );
return FALSE;
}
break;
case hbsRequesting:
case hbsHeaders:
if ( nNow - m_tConnected > Settings.Connection.TimeoutHandshake * 3 )
{
theApp.Message( MSG_ERROR, IDS_BROWSE_TIMEOUT, (LPCTSTR)m_sAddress );
Stop();
return FALSE;
}
break;
case hbsContent:
if ( nNow - m_mInput.tLast > Settings.Connection.TimeoutTraffic )
{
theApp.Message( MSG_ERROR, IDS_BROWSE_TIMEOUT, (LPCTSTR)m_sAddress );
Stop();
return FALSE;
}
}
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// CHostBrowser push handling
BOOL CHostBrowser::SendPush(BOOL bMessage)
{
if ( ! m_bCanPush ) return FALSE;
if ( Network.SendPush( &m_pClientID, 0 ) )
{
CTransfer::Close();
m_tPushed = GetTickCount();
if ( bMessage )
theApp.Message( MSG_DEFAULT, IDS_BROWSE_PUSHED_TO, (LPCTSTR)m_sAddress );
return TRUE;
}
else
{
return FALSE;
}
}
BOOL CHostBrowser::OnPush(GGUID* pClientID, CConnection* pConnection)
{
if ( m_tPushed == 0 ) return FALSE;
if ( m_hSocket != INVALID_SOCKET ) return FALSE;
if ( m_pClientID != *pClientID ) return FALSE;
AttachTo( pConnection );
m_pAddress = m_pHost.sin_addr;
m_nPort = htons( m_pHost.sin_port );
SendRequest();
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// CHostBrowser send request
void CHostBrowser::SendRequest()
{
if ( m_hSocket == INVALID_SOCKET ) return;
if ( m_bNewBrowse )
{
m_pOutput->Print( "GET /gnutella/browse/v1 HTTP/1.1\r\n" );
}
else
{
m_pOutput->Print( Settings.Downloads.RequestHTTP11 ? _T("GET / HTTP/1.1\r\n") : _T("GET / HTTP/1.0\r\n") );
}
CString strHeader = Settings.SmartAgent( Settings.General.UserAgent );
if ( strHeader.GetLength() )
{
m_pOutput->Print( _T("User-Agent: ") + strHeader + _T("\r\n") );
}
m_pOutput->Print( "Accept: text/html, application/x-gnutella-packets, application/x-gnutella2\r\n" );
m_pOutput->Print( "Accept-Encoding: deflate\r\n" );
m_pOutput->Print( "Connection: close\r\n" );
strHeader.Format( _T("Host: %s:%lu\r\n\r\n"),
(LPCTSTR)m_sAddress, htons( m_pHost.sin_port ) );
m_pOutput->Print( strHeader );
CTransfer::OnWrite();
m_nState = hbsRequesting;
m_nProtocol = -1;
m_bDeflate = FALSE;
m_nLength = 0xFFFFFFFF;
m_bConnect = TRUE;
m_mInput.pLimit = m_mOutput.pLimit = &Settings.Bandwidth.Downloads;
theApp.Message( MSG_DEFAULT, IDS_BROWSE_SENT_REQUEST, (LPCTSTR)m_sAddress );
}
//////////////////////////////////////////////////////////////////////
// CHostBrowser read response line
BOOL CHostBrowser::ReadResponseLine()
{
CString strLine, strCode, strMessage;
if ( ! m_pInput->ReadLine( strLine ) ) return TRUE;
if ( strLine.IsEmpty() ) return TRUE;
if ( strLine.GetLength() > 512 ) strLine = _T("#LINE_TOO_LONG#");
if ( strLine.GetLength() >= 12 && strLine.Left( 9 ) == _T("HTTP/1.1 ") )
{
strCode = strLine.Mid( 9, 3 );
strMessage = strLine.Mid( 12 );
}
else if ( strLine.GetLength() >= 12 && strLine.Left( 9 ) == _T("HTTP/1.0 ") )
{
strCode = strLine.Mid( 9, 3 );
strMessage = strLine.Mid( 12 );
}
else if ( strLine.GetLength() >= 8 && strLine.Left( 4 ) == _T("HTTP") )
{
strCode = strLine.Mid( 5, 3 );
strMessage = strLine.Mid( 8 );
}
else
{
theApp.Message( MSG_DEBUG, _T("UNKNOWN BROWSE RESPONSE: %s: %s"), (LPCTSTR)m_sAddress, (LPCTSTR)strLine );
theApp.Message( MSG_ERROR, IDS_BROWSE_NOT_HTTP, (LPCTSTR)m_sAddress );
Stop();
return FALSE;
}
if ( strCode == _T("200") || strCode == _T("206") )
{
m_nState = hbsHeaders;
}
else
{
strMessage.TrimLeft();
if ( strMessage.GetLength() > 256 ) strMessage = _T("No Message");
theApp.Message( MSG_ERROR, IDS_BROWSE_HTTPCODE, (LPCTSTR)m_sAddress,
(LPCTSTR)strCode, (LPCTSTR)strMessage );
Stop();
return FALSE;
}
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// CHostBrowser read headers
BOOL CHostBrowser::OnHeaderLine(CString& strHeader, CString& strValue)
{
theApp.Message( MSG_DEBUG, _T("%s: BROWSE HEADER: %s: %s"), (LPCTSTR)m_sAddress, (LPCTSTR)strHeader, (LPCTSTR)strValue );
if ( strHeader.CompareNoCase( _T("Server") ) == 0 )
{
m_sServer = strValue;
if ( m_sServer.GetLength() > 64 ) m_sServer = m_sServer.Left( 64 );
m_pVendor = VendorCache.LookupByName( m_sServer );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -