?? downloadftp.cpp
字號(hào):
// DownloadFtp.cpp: implementation of the CDownloadFtp class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "NetDownMTR.h"
#include "DownloadFtp.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
void DownloadNotify ( int nIndex, UINT nNotityType, LPVOID lpNotifyData, LPVOID pDownloadMTR );
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDownloadFtp::CDownloadFtp()
{
}
CDownloadFtp::~CDownloadFtp()
{
}
//
// 創(chuàng)建一個(gè) FTP 的數(shù)據(jù)通道連接
//
BOOL CDownloadFtp::CreateFTPDataConnect(CSocketClient &SocketClient)
{
ASSERT ( SocketClient.m_hSocket == INVALID_SOCKET );
// 設(shè)被動(dòng)模式
if ( !m_SocketClient.SendString ( "PASV\r\n" ) ) return FALSE;
CString csResponseStr;
if ( !m_SocketClient.GetResponse ( 227, &csResponseStr ) )
return FALSE;
CString csPasvIP;
USHORT uPasvPort = 0;
if ( !m_SocketClient.GetIPAndPortByPasvString ( csResponseStr, csPasvIP, uPasvPort ) )
return FALSE;
// 創(chuàng)建數(shù)據(jù)通道連接
if ( !SocketClient.Connect ( csPasvIP, uPasvPort ) )
return FALSE;
return TRUE;
}
//
// 連接需要下載的服務(wù)器
//
BOOL CDownloadFtp::Connect ()
{
if ( !CDownloadPub::Connect () )
return FALSE;
int nResponseCode = m_SocketClient.GetResponse ();
if ( nResponseCode != 220 )
{
Log ( L_WARNING, "Connect to [%s:%u] failed", m_csServer, m_nPort );
}
// 登錄
if ( m_csUsername.IsEmpty () )
m_csUsername = "anonymous";
if ( !m_SocketClient.SendString ( "USER %s\r\n", m_csUsername ) ) return FALSE;
nResponseCode = m_SocketClient.GetResponse ();
// 需要密碼
if ( nResponseCode == 331 )
{
if ( !m_SocketClient.SendString ( "PASS %s\r\n", m_csPassword ) ) return FALSE;
nResponseCode = m_SocketClient.GetResponse ();
}
// 登錄失敗
if ( nResponseCode != 230 )
{
Log ( L_ERROR, "Login failed" );
return FALSE;
}
return TRUE;
}
BOOL CDownloadFtp::DownloadOnce()
{
// 不需要下載了
int nWillDownloadSize = Get_WillDownloadSize(); // 本次應(yīng)該下載的字節(jié)數(shù)
int nDownloadedSize = Get_DownloadedSize (); // 已下載字節(jié)數(shù)
if ( nWillDownloadSize > 0 && nDownloadedSize >= nWillDownloadSize )
return DownloadEnd(TRUE);
if ( !CDownloadPub::DownloadOnce () )
return DownloadEnd(FALSE);
// 設(shè)置接收數(shù)據(jù)類(lèi)型為 二進(jìn)制模式
if ( !m_SocketClient.SendString ( "TYPE I\r\n" ) )
return DownloadEnd(FALSE);
if ( !m_SocketClient.GetResponse ( 200 ) )
return DownloadEnd(FALSE);
SLEEP_RETURN_Down ( 0 );
// 創(chuàng)建數(shù)據(jù)通道連接
CSocketClient SocketClient;
if ( !CreateFTPDataConnect ( SocketClient ) )
return DownloadEnd(FALSE);
SLEEP_RETURN_Down ( 0 );
// 設(shè)定下載文件的起始位置
int nWillDownloadStartPos = Get_WillDownloadStartPos (); // 開(kāi)始位置
if ( !m_SocketClient.SendString ( "REST %d\r\n", nWillDownloadStartPos+nDownloadedSize ) )
return DownloadEnd(FALSE);
if ( !m_SocketClient.GetResponse ( 350 ) )
return DownloadEnd(FALSE);
// 提交下載文件的請(qǐng)求
if ( !m_SocketClient.SendString ( "RETR %s\r\n", m_csObject ) )
return DownloadEnd(FALSE);
if ( !m_SocketClient.GetResponse ( 150 ) )
return DownloadEnd(FALSE);
SLEEP_RETURN_Down ( 0 );
// 從數(shù)據(jù)通道讀取數(shù)據(jù),并保存到文件中
BOOL bRet = RecvDataAndSaveToFile(SocketClient);
SocketClient.Disconnect ();
return DownloadEnd ( bRet );
}
//
// 獲取遠(yuǎn)程站點(diǎn)信息,如:是否支持?jǐn)帱c(diǎn)續(xù)傳、要下載的文件大小和創(chuàng)建時(shí)間等
//
BOOL CDownloadFtp::GetRemoteSiteInfo()
{
BOOL bRet = FALSE;
CSocketClient SocketClient;
CString csReadData;
char szRecvBuf[NET_BUFFER_SIZE] = {0};
int nReadSize = 0;
CString csOneLine;
int i;
if ( !CDownloadPub::GetRemoteSiteInfo() )
goto Finished;
if ( !m_SocketClient.Is_Connected () && !Connect () )
goto Finished;
// 設(shè)置接收數(shù)據(jù)類(lèi)型為 ASCII
if ( !m_SocketClient.SendString ( "TYPE A\r\n" ) ) goto Finished;
if ( !m_SocketClient.GetResponse ( 200 ) )
goto Finished;
SLEEP_RETURN ( 0 );
// 判斷是否支持?jǐn)帱c(diǎn)續(xù)傳
if ( !m_SocketClient.SendString ( "REST 1\r\n" ) ) goto Finished;
m_bSupportResume = ( m_SocketClient.GetResponse () == 350 );
TRACE ( "是否支持?jǐn)帱c(diǎn)續(xù)傳:%s\n", m_bSupportResume ? "支持":"不支持" );
if ( m_bSupportResume )
{
if ( !m_SocketClient.SendString ( "REST 0\r\n" ) ) goto Finished;
VERIFY ( m_SocketClient.GetResponse ( 350 ) );
}
SLEEP_RETURN ( 0 );
// 創(chuàng)建數(shù)據(jù)通道連接
if ( !CreateFTPDataConnect ( SocketClient ) )
goto Finished;
// 發(fā)送列舉文件命令
if ( !m_SocketClient.SendString ( "LIST %s\r\n", m_csObject ) )
goto Finished;
if ( !m_SocketClient.GetResponse ( 150 ) )
goto Finished;
SLEEP_RETURN ( 0 );
// 從數(shù)據(jù)通道讀取文件列表信息,直到命令通道返回 "226" 回應(yīng)碼,注意:這里接收數(shù)據(jù)用非阻塞型的。
for ( i=0; ;i++ )
{
memset ( szRecvBuf, 0, sizeof(szRecvBuf) );
nReadSize = SocketClient.Receive ( szRecvBuf, sizeof(szRecvBuf), FALSE );
if ( nReadSize < 0 )
{
Log ( L_WARNING, "Receive file list info failed" );
break;
}
csReadData += szRecvBuf;
int nResponseCode = m_SocketClient.GetResponse ( (CString*)NULL, FALSE );
if ( nResponseCode == -1 ) goto Finished;
else if ( nResponseCode == 0 )
{
SLEEP_RETURN ( 100 );
}
else if ( nResponseCode == 226 )
{
break;
}
else goto Finished;
SLEEP_RETURN ( 0 );
}
for ( i=0; !csReadData.IsEmpty() ; i++ )
{
csOneLine = GetOneLine ( csReadData );
if ( !csOneLine.IsEmpty() )
{
ParseFileInfoStr ( csOneLine );
}
}
bRet = TRUE;
Finished:
m_SocketClient.Disconnect ();
return bRet;
}
//
// 從 "-rw-rw-rw- 1 user group 37979686 Mar 9 13:39 FTP-Callgle 1.4.0_20060309.cab" 字符串中分析
// 出文件信息
//
void CDownloadFtp::ParseFileInfoStr(CString &csFileInfoStr)
{
csFileInfoStr.TrimLeft (); csFileInfoStr.TrimRight ();
if ( csFileInfoStr.IsEmpty() ) return;
BOOL bLastCharIsSpace = ( csFileInfoStr[0]==' ' );
int nSpaceCount = 0;
CString csFileTime1, csFileTime2, csFileTime3;
CString csNodeStr;
CString csFileName;
for ( int i=0; i<csFileInfoStr.GetLength(); i++ )
{
if ( csFileInfoStr[i]==' ' )
{
if ( !bLastCharIsSpace )
{
csNodeStr = csFileInfoStr.Mid ( i );
csNodeStr.TrimLeft (); csNodeStr.TrimRight ();
nSpaceCount ++;
int nFindPos = csNodeStr.Find ( " ", 0 );
if ( nFindPos < 0 ) nFindPos = csNodeStr.GetLength() - 1;
csNodeStr = csNodeStr.Left ( nFindPos );
// 文件大小
if ( nSpaceCount == 4 )
{
if ( m_nIndex == -1 ) // 主線程才需要獲取文件大小的信息
{
m_nFileTotalSize = (int)atoi(csNodeStr);
DownloadNotify ( m_nIndex, NOTIFY_TYPE_GOT_REMOTE_FILESIZE, (LPVOID)m_nFileTotalSize, m_pDownloadMTR );
int nWillDownloadStartPos = Get_WillDownloadStartPos (); // 開(kāi)始位置
int nWillDownloadSize = Get_WillDownloadSize(); // 本次應(yīng)該下載的字節(jié)數(shù)
int nDownloadedSize = Get_DownloadedSize (); // 已下載字節(jié)數(shù)
if ( m_nFileTotalSize > 0 && nWillDownloadSize-nDownloadedSize > m_nFileTotalSize )
Set_WillDownloadSize ( m_nFileTotalSize-nDownloadedSize );
}
}
// 文件時(shí)間第一節(jié)
else if ( nSpaceCount == 5 )
{
csFileTime1 = csNodeStr;
}
// 文件時(shí)間第二節(jié)
else if ( nSpaceCount == 6 )
{
csFileTime2 = csNodeStr;
}
// 文件時(shí)間第三節(jié)
else if ( nSpaceCount == 7 )
{
csFileTime3 = csNodeStr;
}
else if ( nSpaceCount > 7 )
{
csFileName = csFileInfoStr.Mid ( i );
csFileName.TrimLeft(); csFileName.TrimRight();
break;
}
}
bLastCharIsSpace = TRUE;
}
else
{
bLastCharIsSpace = FALSE;
}
}
GetFileTimeInfo ( csFileTime1, csFileTime2, csFileTime3 );
}
void CDownloadFtp::GetFileTimeInfo(CString csFileTime1, CString csFileTime2, CString csFileTime3)
{
if ( csFileTime3.IsEmpty() ) return;
CString csYear, csMonth, csDay, csHour, csMinute, csSecond;
int nMonth = GetMouthByShortStr ( csFileTime1 );
ASSERT ( nMonth >= 1 && nMonth <= 12 );
csMonth.Format ( "%02d", nMonth );
COleDateTime tOleNow = COleDateTime::GetCurrentTime ();
csDay.Format ( "%02d", atoi(csFileTime2) );
csSecond = "00";
// 帶年的日期,如:Aug 21 2006
if ( csFileTime3.Find ( ":", 0 ) < 0 )
{
csYear = csFileTime3;
csHour = "00";
csMinute = "00";
}
// 不帶年的日期,如:Feb 21 01:11
else
{
csYear.Format ( "%04d", tOleNow.GetYear() );
int nPos = csFileTime3.Find ( ":", 0 );
if ( nPos < 0 ) nPos = csFileTime3.GetLength() - 1;
csHour = csFileTime3.Left ( nPos );
csMinute = csFileTime3.Mid ( nPos+1 );
}
CString csFileTimeInfo;
csFileTimeInfo.Format ( "%s-%s-%s %s:%s:%s", csYear, csMonth, csDay, csHour, csMinute, csSecond );
ConvertStrToCTime ( csFileTimeInfo.GetBuffer(0), m_TimeLastModified );
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -