FtpObject.cpp
资源名称:FTP总集.rar [点击查看]
上传用户:tjfeida
上传日期:2013-03-10
资源大小:1917k
文件大小:26k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // FtpObject.cpp: implementation of the CFtpObject class.
- //
- //////////////////////////////////////////////////////////////////////
- /*********************************************
- **该文件是属于WolfFTP工程中的。如果有什么问题
- **请联系
- ** tablejiang@21cn.com
- **或者访问
- ** http://wolfftp.51.net
- **以得到最新的支持。
- *********************************************/
- #include "stdafx.h"
- #include "FtpObject.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CFtpObject::CFtpObject()
- {
- m_bIsConnected = FALSE ;
- m_hEvent = NULL ;
- m_CurrentFtpDirectory[0] = 0 ;
- memset( &m_SiteInfo , 0 , sizeof( m_SiteInfo ) ) ;
- m_ListItem = NULL ;
- m_hDataSocket = NULL ;
- }
- CFtpObject::~CFtpObject()
- {
- }
- /***********************************************************
- ** @Description:
- ** This is thread function . Start the thread message queue .
- **
- ** @Parameter:
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- UINT FtpThreadFunction( LPVOID pParam )
- {
- CFtpObject* pFtp = ( CFtpObject *)pParam ;
- pFtp->StartCommunication( ) ;
- return 1 ;
- }
- /***********************************************************
- ** @Description:
- ** Start the ftp communication .
- **
- ** @Parameter:
- ** void
- **
- ** @Return: Start success return true , else return false .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpObject::StartFtpCommunication()
- {
- m_hEvent = CreateEvent( NULL , FALSE , FALSE , "CreateThreadEvent" ) ;
- CWinThread* pThread = AfxBeginThread( FtpThreadFunction , this ) ;
- m_ControlThread.thread = pThread->m_hThread ;
- m_ControlThread.threadid = pThread->m_nThreadID ;
- WaitForSingleObject( m_hEvent , INFINITE ) ;
- CloseHandle( m_hEvent ) ;
- m_hEvent = NULL ;
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** Start the connect ftp server and check the ftp server
- ** status .
- **
- ** @Parameter:
- ** void
- **
- ** @Return: if start success return true , else return false .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpObject::Start()
- {
- SetEvent( m_hEvent ) ;
- //disable ftp dirlist window.
- PostMessage( m_hWndInfo.hFtpDirListWnd , ENABLE_FTP_LIST_MSG , 0/*disable*/ , 0 ) ;
- if( !InitSocket ( ) )
- {
- return false ;
- }
- //send status msg .
- sprintf( m_szFtpBuffer , _T("欢迎使用 WolfFTP 传输工具!rn") ) ;
- SendStatusStr( m_szFtpBuffer ) ;
- sprintf( m_szFtpBuffer , _T("有问题请联系tablejiang@21cn.comrn") ) ;
- SendStatusStr( m_szFtpBuffer ) ;
- sprintf( m_szFtpBuffer , _T("开始连接 %s 站点........") , m_SiteInfo.host ) ;
- SendStatusStr( m_szFtpBuffer ) ;
- //connect with server.
- m_hControlSocket = ConnectServer( m_SiteInfo.host ) ;
- if( m_hControlSocket == INVALID_SOCKET )
- {
- //MessageBox( NULL , "Can't connect server !" , "error" , MB_OK ) ;
- //send status msg .
- sprintf( m_szFtpBuffer , _T("连接错误!") , m_SiteInfo.host ) ;
- SendStatusStr( m_szFtpBuffer ) ;
- return false ;
- }
- //login server .
- if( m_SiteInfo.logintype == 0 )
- {
- if( !UserLogin( m_hControlSocket , m_SiteInfo.user , m_SiteInfo.pass ) )
- {
- sprintf( m_szFtpBuffer , _T("登录错误!") , m_SiteInfo.host ) ;
- SendStatusStr( m_szFtpBuffer ) ;
- return false ;
- }
- }
- else
- {
- if( !AnonymousLogin( m_hControlSocket ) )
- {
- //MessageBox( NULL , "Login failed ! " , "error" , MB_OK ) ;
- sprintf( m_szFtpBuffer , _T("登录错误!") , m_SiteInfo.host ) ;
- SendStatusStr( m_szFtpBuffer ) ;
- return false ;
- }
- }
- //send login success msg .
- sprintf( m_szFtpBuffer , _T("成功登录Ftp服务器") ) ;
- SendStatusStr( m_szFtpBuffer ) ;
- //test : 2001 4 24
- //test pwd command .
- char pwdcom[MAX_PATH] ;
- sprintf( pwdcom , "PWDrn" ) ;
- //SendFtpCommand( pwdcom , m_hControlSocket ) ;
- //check the broken download.
- if( !CheckBrokenDownload( ) )
- return false ;
- //what's operate system in server ?
- if( !CheckServerType() )
- return false ;
- //set connected tag .
- m_bIsConnected = TRUE ;
- //enable ftp dir list window .
- PostMessage( m_hWndInfo.hFtpDirListWnd , ENABLE_FTP_LIST_MSG , 1 /*enable*/ , 0 ) ;
- //enter the init ftp directory
- EnterInitFtpDirectory( ) ;
- //display the current directory.
- GetCurrentFtpDir( ) ;
- //Init complete.Notify user.
- sprintf( m_szFtpBuffer , _T("初始化完毕,等待命令") ) ;
- SendStatusStr( m_szFtpBuffer ) ;
- //bulid the thread message queue .
- StartMessageCycle( );
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** Enter the ftp init directory .
- **
- ** @Parameter:
- **
- ** @Return: if success return true ,else return false .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpObject::EnterInitFtpDirectory()
- {
- UINT nReplyCode = 0 ;
- char szCommand[MAX_PATH] ;
- memset( szCommand , 0 , MAX_PATH ) ;
- if( strlen( m_SiteInfo.remotepath ) != 0 )
- {
- sprintf( szCommand , "CWD %srn" , m_SiteInfo.remotepath ) ;
- nReplyCode = SendFtpCommand( szCommand , m_hControlSocket ) ;
- if( nReplyCode >= 400 )
- return false ;
- }
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** The message cycle queue .
- **
- ** @Parameter:
- **
- ** @Return: if success return true ,else return false .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpObject::StartMessageCycle()
- {
- MSG msg ;
- char* pCommand ;
- while( true )
- {
- //we must sleep , i don't know why , but
- //if not , the thread will be Killed by system .
- Sleep( 10 ) ;
- if( PeekMessage( &msg , NULL , 0 , 0 , PM_REMOVE ) )
- {
- if( msg.message == FTP_SERVER_COMMAND_MSG )
- {
- //disable ftp dirlist window.
- PostMessage( m_hWndInfo.hFtpDirListWnd , ENABLE_FTP_LIST_MSG , 0/*disable*/ , 0 ) ;
- switch( msg.wParam )
- {
- case FTP_COMM_EXIT:
- //case 0: want quit the ftp communication .
- PostMessage( m_hWndInfo.hFtpDirListWnd , ENABLE_FTP_LIST_MSG , 1 /*enable*/ , 0 ) ;
- return true ;
- break ;
- case FTP_COMM_NORMAL_COMMAND:
- //case 1: send the normal command .
- pCommand = ( char * )msg.lParam ;
- SendFtpCommand( pCommand , m_hControlSocket ) ;
- try
- {
- delete pCommand ;
- }
- catch( ... )
- {
- TRACE( "can't delete point! %s " , pCommand ) ;
- }
- pCommand = NULL ;
- if( m_hEvent != NULL )
- SetEvent( m_hEvent ) ;
- break ;
- case FTP_COMM_LIST_DIR:
- //case 2: want list current ftp directory .
- ListDirectory( true ) ;
- break ;
- case FTP_COMM_GET_CUR_DIR:
- //case 3: want get current ftp directory .
- GetCurrentFtpDir() ;
- break;
- case FTP_COMM_DOWN_UP_FILE:
- //case 4: want download or upload file .
- {
- FTPFILEINFO* pFile = ( FTPFILEINFO* )msg.lParam ;
- DownUpFtpItem( pFile ) ;
- if( pFile != NULL )
- delete pFile ;
- break ;
- }
- break ;
- case FTP_COMM_DEL_FILE :
- {
- FTPFILEINFO* pFile = ( FTPFILEINFO* )msg.lParam ;
- DeleteItem( pFile ) ;
- delete pFile ;
- }
- break ;
- default:
- break ;
- }
- //enable ftp dir list window.
- PostMessage( m_hWndInfo.hFtpDirListWnd , ENABLE_FTP_LIST_MSG , 1 /*enable*/ , 0 ) ;
- m_bRun = true ;
- }
- }
- }
- TRACE( "rn********Thread Exit******** rn" ) ;
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** this function is send the message to thread cycle
- ** let the thread send command to ftp server .
- **
- ** @Parameter:
- ** LPSTR szCommand : the command string .
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- void CFtpObject::SendCommand(LPSTR szCommand)
- {
- if( !IsConnect () )
- return ;
- if( m_ControlThread.thread == NULL )
- {
- MessageBox( m_hWndInfo.hInfoWnd , "Can't send command, not connected." , "error" , MB_OK ) ;
- return ;
- }
- //make command string .
- char *pCommand ;
- pCommand = new char[strlen( szCommand ) + 1 ] ;
- strcpy( pCommand , szCommand ) ;
- pCommand[strlen( szCommand )] = ' ' ;
- //post command message to thread queue .
- BOOL bRet = PostThreadMessage( m_ControlThread.threadid , FTP_SERVER_COMMAND_MSG ,
- FTP_COMM_NORMAL_COMMAND , (LPARAM)pCommand ) ;
- }
- /***********************************************************
- ** @Description:
- ** this public function is set the site info .and windows
- ** info handle .
- **
- ** @Parameter:
- ** SITEINFO* pSite:the site pointer.
- ** HWNDINFO* pWndInfo: The windows handle info pointer .
- **
- ** @Return: if success return true ,else return false .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpObject::SetSiteInfo(SITEINFO *pSite , HWNDINFO* pWndInfo )
- {
- memset( &m_SiteInfo , 0 , sizeof( m_SiteInfo ) ) ;
- strcpy( m_SiteInfo.sitename , pSite->sitename ) ;
- strcpy( m_SiteInfo.host , pSite->host ) ;
- m_SiteInfo.hosttype = pSite->hosttype ;
- strcpy( m_SiteInfo.user , pSite->user ) ;
- strcpy( m_SiteInfo.pass , pSite->pass ) ;
- strcpy( m_SiteInfo.remotepath , pSite->remotepath ) ;
- strcpy( m_SiteInfo.localpath , pSite->localpath ) ;
- m_SiteInfo.logintype = pSite->logintype ;
- m_SiteInfo.transfertype = pSite->transfertype ;
- /*
- ZeroMemory( &m_hWndInfo , sizeof( HWNDINFO ) ) ;
- m_hWndInfo.hInfoWnd = pWndInfo->hInfoWnd ;
- m_hWndInfo.hFtpDirListWnd = pWndInfo->hFtpDirListWnd ;
- m_hWndInfo.hWndJobWnd = pWndInfo->hWndJobWnd ;
- m_hWndInfo.hMultiDownUpWnd = pWndInfo->hMultiDownUpWnd ;
- */
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** this public function is used for logout ftp server .
- ** and abort the message thread cycle .
- **
- ** @Parameter:
- ** void
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- void CFtpObject::Logout()
- {
- if( !IsConnect( ) )
- return ;
- //StopNowCommand( ) ;
- //m_hEvent = CreateEvent( NULL , FALSE , FALSE , "LogoutEvent" ) ;
- ::PostThreadMessage( m_ControlThread.threadid , FTP_SERVER_COMMAND_MSG , FTP_COMM_EXIT , 0 ) ;
- DWORD dwRet = WaitForSingleObject( m_ControlThread.thread , 2000 ) ;
- if( dwRet == WAIT_TIMEOUT )
- {
- TerminateThread( m_ControlThread.thread , 0 ) ;
- CloseHandle( m_ControlThread.thread ) ;
- }
- closesocket (m_hControlSocket ) ;
- //CloseHandle( m_hEvent ) ;
- //m_hEvent = NULL ;
- }
- /***********************************************************
- ** @Description:
- ** this public function is used for list current directory.
- **
- ** @Parameter:
- ** void
- **
- ** @Return: void
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- void CFtpObject::ListCurrentDirectory()
- {
- if( m_ControlThread.thread != NULL )
- PostThreadMessage( m_ControlThread.threadid , FTP_SERVER_COMMAND_MSG , FTP_COMM_LIST_DIR , 0 ) ;
- }
- /***********************************************************
- ** @Description:
- **
- ** @Parameter:
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- /*
- UINT PutGetFileThread( LPVOID pParam )
- {
- FTPFILEINFO* pFtpFileInfo = ( FTPFILEINFO *)pParam ;
- CFtpLib ftp ;
- UINT nReplyCode ;
- SOCKET hDataSocket ;
- SOCKET hListenSocket ;
- char szCommand[MAX_PATH] ;
- ftp.m_hWndInfo.hInfoWnd = pFtpFileInfo->hWnd ;
- //entry the directory .
- //sprintf( szCommand , "CWD %srn" , pFtpFileInfo->remotepath ) ;
- //nReplyCode = ftp.SendFtpCommand( szCommand , pFtpFileInfo->hSocket ) ;
- //if ( nReplyCode >= 400 )
- // return false ;
- //make the command , is put or get file .
- //if put file to server , send command STOR.
- //if get file from server , send command RETR.
- memset( szCommand , 0 , MAX_PATH );
- if( pFtpFileInfo->bfileput )
- sprintf( szCommand , "STOR %srn" , pFtpFileInfo->remotefilename ) ;
- else
- sprintf( szCommand , "RETR %srn" , pFtpFileInfo->remotefilename ) ;
- //set data type to I.
- nReplyCode = ftp.SendFtpCommand( "TYPE Irn" , pFtpFileInfo->hSocket ) ;
- if( nReplyCode >= 400 )
- return false ;
- //make the date link
- hListenSocket = ftp.CreateListenCannel( pFtpFileInfo->hSocket ) ;
- if( hListenSocket == INVALID_SOCKET )
- return false ;
- nReplyCode = ftp.SendFtpCommand( szCommand , pFtpFileInfo->hSocket ) ;
- if( nReplyCode >= 400 )
- return false ;
- hDataSocket = ftp.AcceptDataConnect( hListenSocket ) ;
- if( hDataSocket == INVALID_SOCKET )
- return false ;
- if( pFtpFileInfo->bfileput )
- nReplyCode = ftp.WriteDataCannelFromFile( pFtpFileInfo->hSocket , hDataSocket , pFtpFileInfo->localpath ) ;
- else
- nReplyCode = ftp.ReadDataCannelToFile( pFtpFileInfo->hSocket , hDataSocket , pFtpFileInfo->localpath ) ;
- if( nReplyCode >= 400 || nReplyCode == 0 )
- return false ;
- delete pFtpFileInfo ;
- return 1 ;
- }
- */
- /***********************************************************
- ** @Description:
- ** this public function is used for put or get file with
- ** ftp server .
- **
- ** @Parameter:
- ** FTPFILEINFO* pFtpFile :the file info .
- **
- ** @Return: void
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- void CFtpObject::PutGetFileToServer( FTPFILEINFO* pFtpFile )
- {
- FTPFILEINFO* pFtpFileInfo ;
- pFtpFileInfo = new FTPFILEINFO ;
- memset( pFtpFileInfo , 0 , sizeof( FTPFILEINFO ) ) ;
- memcpy( pFtpFileInfo , pFtpFile , sizeof( FTPFILEINFO ) ) ;
- pFtpFileInfo->hSocket = m_hControlSocket ;
- pFtpFile->hSocket = m_hControlSocket ;
- //add it to file queue .
- //we must add it before post message to thread .
- m_FileQueue.AddItem( pFtpFile ) ;
- //CWinThread* pThread = AfxBeginThread( PutGetFileThread , pFtpFileInfo ) ;
- ::PostThreadMessage( m_ControlThread.threadid , FTP_SERVER_COMMAND_MSG , FTP_COMM_DOWN_UP_FILE , (LPARAM)pFtpFileInfo ) ;
- }
- /***********************************************************
- ** @Description:
- ** enter the specify directory
- **
- ** @Parameter:
- ** LPSTR RemotePath : Remote path which want enter.
- **
- ** @Return: void
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- void CFtpObject::EnterDirectory(LPSTR RemotePath)
- {
- char szCommand[MAX_PATH] ;
- memset( szCommand , 0 , MAX_PATH ) ;
- sprintf( szCommand , "CWD %srn" , RemotePath ) ;
- SendCommand( szCommand ) ;
- GetCurrentDir( ) ;
- }
- /***********************************************************
- ** @Description:
- ** enter the father directory .
- **
- ** @Parameter:
- ** void
- **
- ** @Return: void
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- void CFtpObject::EnterParentDir()
- {
- char szCommand[MAX_PATH] ;
- memset( szCommand , 0 , MAX_PATH ) ;
- sprintf( szCommand , "CDUPrn" ) ;
- SendCommand( szCommand ) ;
- GetCurrentDir ( ) ;
- }
- /***********************************************************
- ** @Description:
- ** abort current command .
- **
- ** @Parameter:
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- UINT AbordThread( LPVOID pParam )
- {
- CFtpObject* pFtp = (CFtpObject*)pParam ;
- pFtp->AbordThreadFunction ( ) ;
- return 1;
- }
- /***********************************************************
- ** @Description:
- ** This is use for abort current communication command .
- **
- ** @Parameter:
- ** void
- **
- ** @Return: if success return true , else return false .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpObject::StopNowCommand()
- {
- if( !IsConnect( ) )
- return false ;
- CWinThread* pThread = AfxBeginThread( AbordThread , 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 ;
- }
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** Init the ftp class data .
- **
- ** @Parameter:
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpObject::InitDate()
- {
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** This function is start the ftp communication connect ,
- ** and build the message queue .
- **
- ** @Parameter:
- ** void
- **
- ** @Return: the function will return after user quit ftp communication .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpObject::StartCommunication()
- {
- // InitSocket( ) ;
- int iRetryTime = DEFAULT_RETRY_TIME ;
- while( !Start( ) && iRetryTime )
- {
- iRetryTime -- ;
- if( !iRetryTime )
- {
- sprintf( m_szFtpBuffer , _T("与服务器通讯失败!rn") ) ;
- SendStatusStr( m_szFtpBuffer ) ;
- sprintf( m_szFtpBuffer , _T("请检查服务器信息是否正确!或者是服务器连接已满!rn") ) ;
- SendStatusStr( m_szFtpBuffer ) ;
- sprintf( m_szFtpBuffer , _T("请稍候再尝试!rn") ) ;
- SendStatusStr( m_szFtpBuffer ) ;
- }
- }
- if( IsConnect( ) )
- {
- //exit the current ftp communication .
- char szCommand[MAX_PATH] ;
- sprintf( szCommand , "QUITrn" ) ;
- SendFtpCommand( szCommand , m_hControlSocket ) ;
- }
- CloseFtpConnect( m_hControlSocket ) ;
- m_hControlSocket = INVALID_SOCKET ;
- m_bIsConnected = FALSE ;
- if( m_hEvent != NULL )
- SetEvent( m_hEvent ) ;
- TRACE( "rn********Thread Exit******** rn" ) ;
- //enable ftp dir list window.
- PostMessage( m_hWndInfo.hFtpDirListWnd , ENABLE_FTP_LIST_MSG , 1 /*enable*/ , 0 ) ;
- UnInitSocket() ;
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** down or up load a item .
- **
- ** @Parameter:
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpObject::DownUpFtpItem(FTPFILEINFO *pFile)
- {
- int iDownUpFile = 0 ;
- BOOL bRet = false ;
- FTPITEM* pItemCur ;
- pItemCur = NULL ;
- if( !m_FileQueue.IsEmpty() )
- {
- ListDirectory( false ) ;
- pItemCur = m_ListItem ;
- }
- if( pFile != NULL )
- {
- if( m_FileQueue.IsInFileQueue( pFile ) && pFile->state == FILE_STATE_READY )
- {
- DownUpSingleItem( pFile , pItemCur ) ;
- //::PostMessage( m_hWndInfo.hWndJobWnd , REFRESH_WND_MSG , 0 , 0 ) ;
- iDownUpFile ++ ;
- }
- }
- //when transmit current file ,
- //transmit queue file .
- while( !m_FileQueue.IsEmpty() )
- {
- FTPFILEINFO* pItem ;
- pItem = m_FileQueue.GetNextItem( );
- if( pItem == NULL )
- break ;
- DownUpSingleItem( pItem , pItemCur ) ;
- iDownUpFile ++ ;
- }
- FreeItemList( pItemCur ) ;
- m_bAskUser = true ;
- if( iDownUpFile )
- ListDirectory( true ) ;
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** send a delete file command to server.
- **
- ** @Parameter:
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- void CFtpObject::DeleteFile(FTPFILEINFO *pFtpFile)
- {
- FTPFILEINFO* pFtpFileInfo ;
- pFtpFileInfo = new FTPFILEINFO ;
- memset( pFtpFileInfo , 0 , sizeof( FTPFILEINFO ) ) ;
- memcpy( pFtpFileInfo , pFtpFile , sizeof( FTPFILEINFO ) ) ;
- pFtpFileInfo->hSocket = m_hControlSocket ;
- pFtpFile->hSocket = m_hControlSocket ;
- // add it to file queue .
- //we must add it before post message to thread .
- m_DeleteFileQueue.AddItem( pFtpFile ) ;
- //CWinThread* pThread = AfxBeginThread( PutGetFileThread , pFtpFileInfo ) ;
- ::PostThreadMessage( m_ControlThread.threadid , FTP_SERVER_COMMAND_MSG , FTP_COMM_DEL_FILE , (LPARAM)pFtpFileInfo ) ;
- }
- /***********************************************************
- ** @Description:
- ** Delete a item ( file or directory ) .
- **
- ** @Parameter:
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpObject::DeleteItem(FTPFILEINFO *pFile)
- {
- int iDelItem = 0 ;
- if( m_DeleteFileQueue.IsInFileQueue( pFile ) )
- {
- DeleteSingleItem( pFile ) ;
- iDelItem ++ ;
- }
- //when transmit current file complete ,
- //transmit queue file .
- while( !m_DeleteFileQueue.IsEmpty() )
- {
- FTPFILEINFO* pItem ;
- //get next item .
- pItem = m_DeleteFileQueue.GetNextItem( );
- DeleteSingleItem( pItem ) ;
- iDelItem ++ ;
- }
- //refresh the file list .
- if( iDelItem != 0 )
- ListDirectory( true ) ;
- return true ;
- }
- /****************************************************
- ** @Description
- ** send rename file command to server.
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- ****************************************************/
- void CFtpObject::RenameFile(LPSTR szSrc, LPSTR szDes)
- {
- char szCommand[MAX_PATH] ;
- sprintf( szCommand , "RNFR %srn" , szSrc ) ;
- SendCommand( szCommand ) ;
- sprintf( szCommand , "RNTO %srn" , szDes ) ;
- SendCommand( szCommand ) ;
- }
- /****************************************************
- ** @Description
- ** run the list task.
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- ****************************************************/
- BOOL CFtpObject::RunJobList()
- {
- ::PostThreadMessage( m_ControlThread.threadid , FTP_SERVER_COMMAND_MSG , FTP_COMM_DOWN_UP_FILE , NULL ) ;
- return true ;
- }
- /****************************************************
- ** @Description
- ** send a make dir command to ftp server.
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- ****************************************************/
- void CFtpObject::CreateDirectory(LPCTSTR szPath)
- {
- char szCommand[MAX_PATH] ;
- sprintf( szCommand , "MKD %srn" , szPath ) ;
- SendCommand( szCommand ) ;
- }
- /****************************************************
- ** @Description
- ** download a ftp item .
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- ****************************************************/
- BOOL CFtpObject::DownUpSingleItem(FTPFILEINFO *pFile , FTPITEM* pItemCur )
- {
- //first ,we transmit the current file , after complete this ,
- //we will transmit the task queue.
- pFile->hSocket = m_hControlSocket ;
- m_FileQueue.SetItemState( FILE_STATE_RUNNING , pFile ) ;
- //is upload
- if( pFile->bfileput )
- {
- //is directory.
- if( pFile->bIsDirectory )
- {
- //backup current ftp path .
- //because transmit directory will change the dir.
- char szPathBak[MAX_PATH] ;
- strcpy( szPathBak , m_CurrentFtpDirectory ) ;
- //upload.
- if( !UploadDirectory( pFile ) )
- //if error , set status.
- m_FileQueue.SetItemState( FILE_STATE_ERROR , pFile ) ;
- //restore the directory.
- EnterFtpDirectory( szPathBak ) ;
- if( !GetCurrentFtpDir() )
- return false ;
- }
- else
- {
- //is file already exist in ftp server .
- pFile->startpos = FindFileInFileList( pFile->remotefilename , pItemCur ) ;
- if( !BrokenDownload( pFile ) )
- m_FileQueue.SetItemState( FILE_STATE_ERROR , pFile ) ;
- }
- }
- //download.
- else
- {
- if( pFile->bIsDirectory )
- {
- //backup directory.
- char szPathBak[MAX_PATH] ;
- strcpy( szPathBak , m_CurrentFtpDirectory ) ;
- if( !DownLoadDirectory( pFile ) )
- m_FileQueue.SetItemState( FILE_STATE_ERROR , pFile ) ;
- 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 ) ;
- }
- }
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- void CFtpObject::GetCurrentDir()
- {
- if( !IsConnect() )
- return ;
- PostThreadMessage( m_ControlThread.threadid , FTP_SERVER_COMMAND_MSG , FTP_COMM_GET_CUR_DIR , 0 ) ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFtpObject::DeleteSingleItem(FTPFILEINFO *pItem)
- {
- if( pItem == NULL )
- return false ;
- if( pItem->bIsDirectory )
- {
- //backup current directory .
- char szPathBak[MAX_PATH] ;
- strcpy( szPathBak , m_CurrentFtpDirectory ) ;
- strcpy( szPathBak , m_CurrentFtpDirectory ) ;
- //first we must clear the dir.
- DeleteFtpDirectory( pItem ) ;
- EnterFtpDirectory( szPathBak ) ;
- if( !GetCurrentFtpDir() )
- return false ;
- //after clear the directory , remove it.
- RemoveFtpDirectory( pItem->remotefilename ) ;
- }
- else
- DeleteFtpFile( pItem->remotefilename ) ;
- //remove the queue item .
- m_DeleteFileQueue.DeleteItem( pItem );
- return true ;
- }