FtpTransmitFile.cpp
资源名称:FTP总集.rar [点击查看]
上传用户:tjfeida
上传日期:2013-03-10
资源大小:1917k
文件大小:10k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // FtpTransmitFile.cpp: implementation of the CFtpTransmitFile class.
- //
- //////////////////////////////////////////////////////////////////////
- /*********************************************
- **该文件是属于WolfFTP工程中的。如果有什么问题
- **请联系
- ** tablejiang@21cn.com
- **或者访问
- ** http://wolfftp.51.net
- **以得到最新的支持。
- *********************************************/
- #include "stdafx.h"
- #include "quickftp.h"
- #include "FtpTransmitFile.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CFtpTransmitFile::CFtpTransmitFile()
- {
- m_pFileQueue = NULL ;
- }
- CFtpTransmitFile::~CFtpTransmitFile()
- {
- }
- /****************************************************
- ** @Description
- ** thread function .
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** @e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- UINT StartTransmitThread( LPVOID pParam )
- {
- CFtpTransmitFile* pFtp = ( CFtpTransmitFile * )pParam ;
- pFtp->StartTransmitFunction( ) ;
- return 1 ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** @e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFtpTransmitFile::StartTransmit( HWND hWnd )
- {
- CWinThread* pThread = AfxBeginThread( StartTransmitThread , this ) ;
- m_TransmitThread.thread = pThread->m_hThread ;
- m_TransmitThread.threadid = pThread->m_nThreadID ;
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** @e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFtpTransmitFile::StartTransmitFunction()
- {
- if( m_pFileQueue == NULL )
- return false ;
- m_bRun = true ;
- while( !m_pFileQueue->IsEmpty() )
- {
- m_pCurFile = m_pFileQueue->GetNextItem ( ) ;
- if( m_pCurFile == NULL )
- return true ;
- SetFileState( m_pCurFile , FILE_STATE_RUNNING ) ;
- if( !SetTransmitFileSiteInfo ( ) )
- {
- SetFileState( m_pCurFile , FILE_STATE_ERROR ) ;
- continue ;
- }
- if( !LoginAndCheckServer( ) )
- {
- SetFileState( m_pCurFile , FILE_STATE_ERROR ) ;
- continue ;
- }
- TransmitFileFunction( m_pCurFile ) ;
- SendFtpCommand( "QUITrn" , m_hControlSocket ) ;
- closesocket( m_hControlSocket ) ;
- }
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** @e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFtpTransmitFile::LoginAndCheckServer()
- {
- if( !InitSocket ( ) )
- {
- return false ;
- }
- //connect with server.
- m_hControlSocket = ConnectServer( m_SiteInfo.host ) ;
- if( m_hControlSocket == INVALID_SOCKET )
- {
- //MessageBox( NULL , "Can't connect server !" , "error" , MB_OK ) ;
- return false ;
- }
- //login server .
- if( m_SiteInfo.logintype == 0 )
- {
- if( !UserLogin( m_hControlSocket , m_SiteInfo.user , m_SiteInfo.pass ) )
- {
- //MessageBox( NULL , "Login failed ! " , "error" , MB_OK ) ;
- return false ;
- }
- }
- else
- {
- if( !AnonymousLogin( m_hControlSocket ) )
- {
- //MessageBox( NULL , "Login failed ! " , "error" , MB_OK ) ;
- return false ;
- }
- }
- //check the broken download.
- if( !CheckBrokenDownload( ) )
- return false ;
- if( !CheckServerType() )
- return false ;
- if( !EnterFtpDirectory( m_pCurFile->remotepath ) )
- return false ;
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** @e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFtpTransmitFile::SetTransmitFileSiteInfo()
- {
- if( m_pCurFile == NULL )
- return false ;
- memcpy( &m_SiteInfo , &m_pCurFile->site , sizeof( SITEINFO ) ) ;
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** @e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- void CFtpTransmitFile::SetFileQueue(CFileQueue *pFileQueue)
- {
- m_pFileQueue = pFileQueue ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** @e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- void CFtpTransmitFile::SetFileState(FTPFILEINFO *pInfo, int iState)
- {
- m_pFileQueue->SetItemState( iState , pInfo ) ;
- // m_pFileQueue->LockTheFileList( ) ;
- // m_pCurFile->state = iState ;
- // m_pFileQueue->UnLockTheFileList() ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** @e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFtpTransmitFile::TransmitFileFunction( FTPFILEINFO* pFile )
- {
- int iDownUpFile = 0 ;
- BOOL bRet = false ;
- FTPFILEINFO FtpFile ;
- GetCurrentFtpDir( ) ;
- pFile->hSocket = m_hControlSocket ;
- FTPITEM* pItemCur ;
- if( !m_pFileQueue->IsEmpty() )
- {
- ListDirectory( false ) ;
- pItemCur = m_ListItem ;
- }
- else
- {
- pItemCur = NULL ;
- }
- SetFileState( pFile , FILE_STATE_RUNNING ) ;
- if( pFile->bfileput )
- {
- if( pFile->bIsDirectory )
- {
- char szPathBak[MAX_PATH] ;
- strcpy( szPathBak , m_CurrentFtpDirectory ) ;
- if( !UploadDirectory( pFile ) )
- {
- SetFileState( pFile , FILE_STATE_ERROR ) ;
- }
- EnterFtpDirectory( szPathBak ) ;
- if( !GetCurrentFtpDir() )
- return false ;
- }
- else
- {
- pFile->startpos = FindFileInFileList( pFile->remotefilename , pItemCur ) ;
- if( !BrokenDownload( pFile ) )
- m_FileQueue.SetItemState( FILE_STATE_ERROR , pFile ) ;
- }
- }
- else
- {
- if( pFile->bIsDirectory )
- {
- char szPathBak[MAX_PATH] ;
- strcpy( szPathBak , m_CurrentFtpDirectory ) ;
- if( !DownLoadDirectory( pFile ) )
- SetFileState( pFile , FILE_STATE_ERROR ) ;
- EnterFtpDirectory( szPathBak ) ;
- if( !GetCurrentFtpDir() )
- return false ;
- }
- else
- {
- char szFilePathName[MAX_PATH] ;
- sprintf( szFilePathName , "%s%s" , pFile->localpath ,
- pFile->localfilename ) ;
- pFile->startpos = FindFileInLocal( szFilePathName ) ;
- if( !BrokenDownload( pFile ) )
- m_FileQueue.SetItemState( FILE_STATE_ERROR , pFile ) ;
- }
- }
- // ::PostMessage( m_hWndInfo.hWndJobWnd , REFRESH_WND_MSG , 0 , 0 ) ;
- // ::PostMessage( m_hRefWnd , REFRESH_WND_MSG , 0 , 0 ) ;
- iDownUpFile ++ ;
- memcpy( &FtpFile , pFile , sizeof( FTPFILEINFO ) ) ;
- m_pFileQueue->DeleteItem( pFile ) ;
- //when transmit current file ,
- //transmit queue file .
- while( !m_pFileQueue->IsEmpty() )
- {
- FTPFILEINFO* pItem ;
- pItem = m_pFileQueue->GetSameSiteFile( &FtpFile ) ;
- if( pItem == NULL )
- break ;
- pItem->hSocket = m_hControlSocket ;
- SetFileState( pItem , FILE_STATE_RUNNING ) ;
- if( !EnterFtpDirectory( pItem->remotepath ) )
- SetFileState( pItem , FILE_STATE_ERROR ) ;
- GetCurrentFtpDir( ) ;
- if( pItem->bfileput )
- {
- if( pItem->bIsDirectory )
- {
- char szPathBak[MAX_PATH] ;
- strcpy( szPathBak , m_CurrentFtpDirectory ) ;
- if( !UploadDirectory( pItem ) )
- SetFileState( pItem , FILE_STATE_ERROR ) ;
- EnterFtpDirectory( szPathBak ) ;
- if( !GetCurrentFtpDir() )
- return false ;
- }
- else
- {
- pItem->startpos = FindFileInFileList( pItem->remotefilename , pItemCur ) ;
- if( !BrokenDownload( pItem ) )
- m_FileQueue.SetItemState( FILE_STATE_ERROR , pItem ) ;
- }
- }
- else
- {
- if( pItem->bIsDirectory )
- {
- char szPathBak[MAX_PATH] ;
- strcpy( szPathBak , m_CurrentFtpDirectory ) ;
- if( !DownLoadDirectory( pItem ) )
- SetFileState( pItem , FILE_STATE_ERROR ) ;
- EnterFtpDirectory( szPathBak ) ;
- if( !GetCurrentFtpDir() )
- return false ;
- }
- else
- {
- char szFilePathName[MAX_PATH] ;
- sprintf( szFilePathName , "%s%s" , pItem->localpath ,
- pItem->localfilename ) ;
- pItem->startpos = FindFileInLocal( szFilePathName ) ;
- if( !BrokenDownload( pItem ) )
- m_FileQueue.SetItemState( FILE_STATE_ERROR , pItem ) ;
- }
- }
- // ::PostMessage( m_hRefWnd , REFRESH_WND_MSG , 0 , 0 ) ;
- iDownUpFile ++ ;
- if( m_bRun == 1 )
- m_pFileQueue->DeleteItem( pItem ) ;
- m_bRun = 1 ;
- }
- FreeItemList( pItemCur ) ;
- if( iDownUpFile )
- ListDirectory( true ) ;
- return true ;
- }
- void CFtpTransmitFile::SetParentWnd(HWND hWnd)
- {
- m_hRefWnd = hWnd ;
- }
- UINT AbordThread2( LPVOID pParam )
- {
- CFtpTransmitFile* pFtp = (CFtpTransmitFile*)pParam ;
- pFtp->AbordThreadFunction ( ) ;
- return 1;
- }
- void CFtpTransmitFile::StopNowCommand()
- {
- if( !IsConnect( ) )
- return ;
- CWinThread* pThread = AfxBeginThread( AbordThread2 , this ) ;
- m_AbordThread.thread = pThread->m_hThread ;
- m_AbordThread.threadid = pThread->m_nThreadID ;
- //WaitForSingleObject( m_AbordThread.thread , 1000 ) ;
- if( m_hDataSocket )
- {
- closesocket( m_hDataSocket ) ;
- m_hDataSocket = NULL ;
- }
- }