FtpWorker.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:9k
- #include "stdAfx.h"
- #include "FtpWorker.h"
- CFtpWorker::CFtpWorker()
- {
- m_hMsgWnd = NULL;
- m_bIsConnected = FALSE;
-
- m_pFtpConnection = NULL;
- m_nConnectionTimeout = 10000;
- m_hEventKill = CreateEvent(NULL, FALSE, FALSE, NULL);
-
- m_poInternetSession = new CInternetSession("trfFtpWorker/1.0");
- m_poInternetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, m_nConnectionTimeout);
- m_poInternetSession->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, m_nConnectionTimeout);
- m_poInternetSession->SetOption(INTERNET_OPTION_SEND_TIMEOUT, m_nConnectionTimeout);
- }
- CFtpWorker::CFtpWorker(CFtpSite &oFtpSite)
- {
- m_hMsgWnd = NULL;
- m_oFptSite = oFtpSite;
- m_bIsConnected = FALSE;
-
- m_pFtpConnection = NULL;
- m_nConnectionTimeout = 10000;
-
- m_hEventKill = CreateEvent(NULL, FALSE, FALSE, NULL);
- m_poInternetSession = new CInternetSession("trfFtpWorker/1.0");
- m_poInternetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, m_nConnectionTimeout);
- m_poInternetSession->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, m_nConnectionTimeout);
- m_poInternetSession->SetOption(INTERNET_OPTION_SEND_TIMEOUT, m_nConnectionTimeout);
- }
- CFtpWorker::~CFtpWorker()
- {
- CloseHandle(this->m_hEventKill);
- if(NULL != m_poInternetSession)
- {
- delete m_poInternetSession;
- }
- this->Disconnect();
- }
- BOOL CFtpWorker::IsConnected(void)
- {
- return m_bIsConnected;
- }
- CFtpSite &CFtpWorker::GetFtpSiteInfo(void) const
- {
- return (CFtpSite&)m_oFptSite;
- }
- void CFtpWorker::SetFtpSiteInfo(CFtpSite &oFtpSite)
- {
- this->m_oFptSite = oFtpSite;
- }
- BOOL CFtpWorker::Connect(void)
- {
- try
- {
- m_pFtpConnection = m_poInternetSession->GetFtpConnection(m_oFptSite.m_sHost,
- m_oFptSite.m_sUser, m_oFptSite.m_sPassword,
- m_oFptSite.m_nPort, m_oFptSite.m_bPASVMode);
- return (m_bIsConnected = (NULL != m_pFtpConnection));
- }
- catch (CInternetException *ex)
- {
- ex->Delete();
- m_pFtpConnection = NULL;
- m_bIsConnected = FALSE;
- return FALSE;
- }
- return FALSE;
- }
- void CFtpWorker::Disconnect(void)
- {
- if(NULL != m_pFtpConnection)
- {
- m_pFtpConnection->Close();
- delete m_pFtpConnection;
- m_pFtpConnection = NULL;
- m_bIsConnected = FALSE;
- }
- }
- BOOL CFtpWorker::GetCurrentDir(CString strDirName)
- {
- try
- {
- strDirName.Empty();
- return m_pFtpConnection->GetCurrentDirectory(strDirName);
- }
- catch (CInternetException *ex)
- {
- ex->Delete();
- return FALSE;
- }
- return FALSE;
- }
- BOOL CFtpWorker::ChangeCurrentDir(LPCSTR lpszToDir)
- {
- try
- {
- if(NULL == lpszToDir) return FALSE;
- return m_pFtpConnection->SetCurrentDirectory(lpszToDir);
- }
- catch (CInternetException *ex)
- {
- ex->Delete();
- return FALSE;
- }
- return FALSE;
- }
- BOOL CFtpWorker::GetCurSubDirectorys(CStringList &oSubDirNames, LPCSTR lpszSubDirMask)
- {
- CFtpFileFind oFtpFileFinder(this->m_pFtpConnection);
- try
- {
- CString strFindMask = (NULL == lpszSubDirMask) ? "*" : lpszSubDirMask;
-
- BOOL bContinue = oFtpFileFinder.FindFile(strFindMask.GetBuffer(0));
-
- for(oSubDirNames.RemoveAll(); bContinue; )
- {
- bContinue = oFtpFileFinder.FindNextFile();
-
- if(oFtpFileFinder.IsDots()) continue;
-
- if(!oFtpFileFinder.IsDirectory()) continue;
- oSubDirNames.AddTail(oFtpFileFinder.GetFileName());
- }
- oFtpFileFinder.Close();
- return TRUE;
- }
- catch(CInternetException *ex)
- {
- ex->Delete();
- oFtpFileFinder.Close();
- return FALSE;
- }
- return FALSE;
- }
- BOOL CFtpWorker::GetFileNamesOfCurDir(CList<LPREMOTEFILEINFO,LPREMOTEFILEINFO&> &oFileInfos, LPCSTR lpszFileNameMask)
- {
- CFtpFileFind oFtpFileFinder(this->m_pFtpConnection);
- try
- {
- CString strFindMask = (NULL == lpszFileNameMask) ? "*" : lpszFileNameMask;
-
- BOOL bContinue = oFtpFileFinder.FindFile(strFindMask.GetBuffer(0));
-
- for(; oFileInfos.GetCount() > 0; )
- {
- REMOTEFILEINFO *pitem = (REMOTEFILEINFO*)oFileInfos.GetHead();
- if(NULL != pitem) delete pitem;
- oFileInfos.RemoveHead();
- }
- for(; bContinue; )
- {
- bContinue = oFtpFileFinder.FindNextFile();
-
- if(oFtpFileFinder.IsDots()) continue;
-
- if(oFtpFileFinder.IsDirectory()) continue;
- REMOTEFILEINFO *lpRemoteFile = new REMOTEFILEINFO;
- memset(lpRemoteFile, 0x0, sizeof(REMOTEFILEINFO));
- lpRemoteFile->dwFilesize = oFtpFileFinder.GetLength();
- _snprintf(lpRemoteFile->szRemoteFile, MAX_PATH, "%s", oFtpFileFinder.GetFileName());
- oFileInfos.AddTail(lpRemoteFile);
- }
- oFtpFileFinder.Close();
- return TRUE;
- }
- catch(CInternetException *ex)
- {
- ex->Delete();
- oFtpFileFinder.Close();
- return FALSE;
- }
- return FALSE;
- }
- void CFtpWorker::SetMsgWnd(HWND hwnd)
- {
- this->m_hMsgWnd = hwnd;
- }
- BOOL CFtpWorker::DownloadFile(LPREMOTEFILEINFO lpRemoteFile, LPCSTR lpszNewFilename)
- {
- DWORD dwExitCode = 0x0000;
- LPDOWNLOADARGS lpParameters = new DOWNLOADARGS;
- lpParameters->lpAddition = this;
- memcpy(&(lpParameters->rfRemoteFile), lpRemoteFile, sizeof(REMOTEFILEINFO));
- _snprintf(lpParameters->szLocalFilename, MAX_PATH, "%s", lpszNewFilename);
-
- // create dummy window as parent for the progress dialog
- if (!m_wndDummy.CreateEx(0, AfxRegisterWndClass(0), "trfAgent Download Dummy Window",
- WS_OVERLAPPED, 0, 0, 0, 0, NULL, NULL))
- {
- delete lpParameters;
- return FALSE;
- }
- // Create the progress dialog box.
- if (!m_ProgressDlg.Create(&m_wndDummy, m_hEventKill))
- {
- delete lpParameters;
- return FALSE;
- }
- WaitForProgressDialog();
- this->SetMsgWnd(m_ProgressDlg.GetSafeHwnd());
- m_ProgressDlg.m_oFromToHint.Format("%s -> %s", lpRemoteFile->szRemoteFile, lpszNewFilename);
- m_ProgressDlg.UpdateData(FALSE);
- DWORD dwthreadid = 0x0000;
- HANDLE hDownThread = CreateThread(NULL, 0, Routine_Download, lpParameters, 0, &dwthreadid);
- DWORD dwWaitRet = WaitForSingleObject(hDownThread, 0);
- for(; TRUE;)
- {
- dwWaitRet = WaitForSingleObject(hDownThread, 0);
- if(dwWaitRet == WAIT_OBJECT_0) break;
- DoEvents();
- Sleep(10);
- }
- GetExitCodeThread(hDownThread, &dwExitCode);
- CloseHandle(hDownThread);
- delete lpParameters;
- m_ProgressDlg.DestroyWindow();
- m_wndDummy.DestroyWindow();
- return (dwExitCode == 0);
- }
- DWORD CFtpWorker::Routine_Download(LPVOID lpParameters)
- {
- char lpBuffer[MAX_BUFSIZE];
- DWORD dwLeftSize = 0x0000, dwReaded = 0x0000, dwWriten = 0x0000;
-
- HANDLE hLocalFile = INVALID_HANDLE_VALUE;
- CInternetFile *pfile = NULL;
- //get parameters.
- LPDOWNLOADARGS lpDownParams = (LPDOWNLOADARGS)lpParameters;
- if(NULL == lpDownParams) return 1;
- CFtpWorker *pthis = (CFtpWorker*)lpDownParams->lpAddition;
- if(NULL == pthis) return 1;
- try
- {
- //open remote file.
- for(; (WaitForSingleObject(pthis->m_hEventKill, 0) == WAIT_TIMEOUT); )
- {
- dwLeftSize = lpDownParams->rfRemoteFile.dwFilesize;
-
- pfile = pthis->m_pFtpConnection->OpenFile(
- lpDownParams->rfRemoteFile.szRemoteFile);
- if(NULL == pfile) return 1;
-
- //create local file.
-
- hLocalFile = CreateFile(lpDownParams->szLocalFilename, GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if(INVALID_HANDLE_VALUE == hLocalFile)
- {
- //close remote file.
- pfile->Close();
- return 1;
- }
- //start read and write actions.
-
- if(pthis->m_hMsgWnd)
- {
- SendMessage(pthis->m_hMsgWnd, UWM_SETPPROGRESS, (WPARAM)0x0000, dwLeftSize);
- }
- BOOL bReadFinished = FALSE;
- for(; (WaitForSingleObject(pthis->m_hEventKill, 0) == WAIT_TIMEOUT) && dwLeftSize > 0; )
- {
- //read contents from remote file.
- memset(lpBuffer, 0x0, sizeof(lpBuffer));
- dwReaded = pfile->Read(lpBuffer, MAX_BUFSIZE);
-
- //write remote file's contents into local file.
- WriteFile(hLocalFile, lpBuffer, dwReaded, &dwWriten, NULL);
- if(pthis->m_hMsgWnd)
- {
- PostMessage(pthis->m_hMsgWnd, UWM_SETPPROGRESS, (WPARAM)0x0001, dwReaded);
- }
- dwLeftSize -= dwReaded;
-
- //if arrived end of file...
- if(dwReaded < MAX_BUFSIZE)
- {
- bReadFinished = TRUE;
- break;
- }
- }
- CloseHandle(hLocalFile);
- pfile->Close();
- if(pthis->m_hMsgWnd)
- {
- SendMessage(pthis->m_hMsgWnd, UWM_SETPPROGRESS, (WPARAM)0x0002, 0);
- }
- return bReadFinished ? 0 : 1;
- }
- }
- catch (CInternetException *ex)
- {
- ex->Delete();
- if(NULL != pfile) pfile->Close();
- return 1;
- }
- return 0L;
- }
- /********************************************************************/
- /* */
- /* Function name : WaitForProgressDialog */
- /* Description : If the thread has just started, it may not have */
- /* had time yet to initialize the dialog window. */
- /* */
- /********************************************************************/
- void CFtpWorker::WaitForProgressDialog(void)
- {
- if(m_ProgressDlg.m_hWnd == NULL)
- {
- while(m_ProgressDlg.m_hWnd == NULL)
- {
- DoEvents();
- }
- }
- if(!::IsWindow(m_ProgressDlg.m_hWnd))
- {
- while(!::IsWindow(m_ProgressDlg.m_hWnd))
- {
- DoEvents();
- }
- }
- }