📄 ftpproc.cpp
字号:
// FtpProc.cpp : implementation file
//
#include "stdafx.h"
#include "recsys.h"
#include "FtpProc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFtpProc
IMPLEMENT_DYNCREATE(CFtpProc, CWinThread)
CFtpProc::CFtpProc()
{
}
CFtpProc::~CFtpProc()
{
}
BOOL CFtpProc::InitInstance()
{
// TODO: perform and per-thread initialization here
m_pInetSession=NULL;
m_pFtpConnection=NULL;
m_pRemoteFinder=NULL;
local_front=0;
local_rear=0;
tl=0;
mainwnd=NULL;
mainwnd=mainproc;
if(mainwnd==NULL)
return false;
SetTimer(NULL,100,5*1000,NULL);
if(!ConnectFtp())
::PostMessage(mainwnd,WM_PROCMSG,0,FTPSERVER_FAILD);
return TRUE;
}
int CFtpProc::ExitInstance()
{
// TODO: perform any per-thread cleanup here
tl=local_rear;
tl=(tl-1)%FTPQUEUE_MAX;
try
{
if(m_pFtpConnection)
{
m_pFtpConnection->Close();
}
if(m_pInetSession)
{
delete m_pInetSession;
}
}
catch(CInternetException *pEx)
{
//获取错误
if(pEx->GetErrorMessage(ftpinfo[tl].statemsg,1023))
{
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
}
else
{
sprintf(ftpinfo[tl].statemsg,"关闭FTP服务器出现程序异常!");
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
}
pEx->Delete();
m_pFtpConnection=NULL;
}
catch(...)
{
if(m_pInetSession)
{delete m_pInetSession;m_pInetSession=NULL;}
sprintf(ftpinfo[tl].statemsg,"关闭FTP服务器出现未知异常!");
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
}
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(CFtpProc, CWinThread)
//{{AFX_MSG_MAP(CFtpProc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_THREAD_MESSAGE( FTP_WANTE,OnFtpFile )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFtpProc message handlers
BOOL CFtpProc::ConnectFtp()
{
BOOL proc_ret=false;
//
ftp_ok=false;
tl=local_rear;
tl=(tl-1)%FTPQUEUE_MAX;
try
{
if(m_pFtpConnection)
{
m_pFtpConnection->Close();
m_pFtpConnection=NULL;
}
if(m_pInetSession)
{
m_pInetSession->Close();
delete m_pInetSession;
}
if(m_pRemoteFinder)
{
m_pRemoteFinder->Close();
delete m_pRemoteFinder;
m_pRemoteFinder=NULL;
}
m_pInetSession=new CInternetSession(AfxGetAppName(),1,LOCAL_INTERNET_ACCESS);
//新建连接对象
// m_pInetSession->EnableStatusCallback(true);
m_pFtpConnection=m_pInetSession->GetFtpConnection(FTP_SERVER,FTP_USERNAME,
FTP_PASSWORD,21);
m_pRemoteFinder = new CFtpFileFind(m_pFtpConnection);
}
catch(CInternetException *pEx)
{
//获取错误
if(pEx->GetErrorMessage(ftpinfo[tl].statemsg,1023))
{
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
}
else
{
sprintf(ftpinfo[tl].statemsg,"连接FTP服务器出现程序异常!");
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
::PostMessage(mainwnd,WM_PROCMSG,0,FTPSERVER_FAILD);
}
pEx->Delete();
m_pFtpConnection=NULL;
}
catch(...)
{
if(m_pInetSession)
{delete m_pInetSession;m_pInetSession=NULL;}
sprintf(ftpinfo[tl].statemsg,"连接FTP服务器出现未知异常!");
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
::PostMessage(mainwnd,WM_PROCMSG,0,FTPSERVER_FAILD);
}
if(m_pFtpConnection)
{
sprintf(ftpinfo[tl].statemsg,"连接FTP服务器成功");
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
::PostMessage(mainwnd,WM_PROCMSG,tl,FTPSERVER_RECORVER);
proc_ret=true;
ftp_ok=true;
}
else
{
sprintf(ftpinfo[tl].statemsg,"连接FTP服务器失败");
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
}
//
return proc_ret;
}
LRESULT CFtpProc::OnFtpFile(WPARAM wParam,LPARAM lParam)
{
CString strRemoteFile;
strRemoteFile.Format("%s",ftpinfo[wParam].filename);
int i=strRemoteFile.Find("e:\\rec\\");
int trytimes=0;
BOOL ftpret=false;
if(i>=0)
strRemoteFile.Delete(i,7);
// strRemoteFile.Replace("e:\\rec\\","\\");
local_rear=wParam;
while(1)
{
try
{
tl=wParam;
ftpret=m_pFtpConnection->PutFile(ftpinfo[wParam].filename,strRemoteFile,FTP_TRANSFER_TYPE_BINARY|INTERNET_FLAG_DONT_CACHE ,0);
//for check the memery leak;
/* _CrtMemState oldstate,newstate,statediff;
_CrtMemCheckpoint(&oldstate);
////for check the memery leak-end
_CrtMemCheckpoint(&newstate);
if(_CrtMemDifference(&statediff,&oldstate,&newstate))
{
_CrtMemDumpStatistics(&statediff);
_CrtMemDumpAllObjectsSince(&oldstate);
}*/
if(ftpret)
{
sprintf(ftpinfo[wParam].statemsg,"文件%s上传成功!",strRemoteFile);
::PostMessage(mainwnd,WM_PROCMSG,wParam,SHOW_FTP_MSG);
//删除已上传的文件
try
{
if(!DeleteFile(ftpinfo[wParam].filename))
{
sprintf(ftpinfo[wParam].statemsg,"文件%s删除失败!",ftpinfo[wParam].filename);
::PostMessage(mainwnd,WM_PROCMSG,wParam,SHOW_FTP_MSG);
}
}
catch (...) {
sprintf(ftpinfo[wParam].statemsg,"文件%s删除出现异常!",ftpinfo[wParam].filename);
::PostMessage(mainwnd,WM_PROCMSG,local_rear,SHOW_FTP_MSG);
}
break;
}
else
{
long errcode=GetLastError();
tl=(wParam-1)%FTPQUEUE_MAX;
if(trytimes>=3)
{
ftpinfo[tl].groupid=ftpinfo[(tl+1)%FTPQUEUE_MAX].groupid;
sprintf(ftpinfo[tl].statemsg,"文件%s3次上传失败!",ftpinfo[wParam].filename);
sprintf(ftpinfo[tl].filename,"%s",strRemoteFile);
::PostMessage(mainwnd,WM_PROCMSG,tl,FTP_FAILD);
tl=(wParam+1)%FTPQUEUE_MAX;
trytimes=0;
if(!ConnectFtp())
{
// ::PostMessage(mainwnd,WM_PROCMSG,tl,FTPSERVER_FAILD);
}
break;
}
else
{
trytimes++;
sprintf(ftpinfo[tl].statemsg,"文件%s上传失败,重新上传!",strRemoteFile);
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
if(!ConnectFtp())
{
// ::PostMessage(mainwnd,WM_PROCMSG,tl,FTPSERVER_FAILD);
}
}
}
}
catch(CInternetException *pEx)
{
//获取错误
// ::InterlockedExchange(&tl,ftp_rear);
tl=(tl-1)%FTPQUEUE_MAX;
if(pEx->GetErrorMessage(ftpinfo[tl].statemsg,1023))
{
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
}
else
{
sprintf(ftpinfo[tl].statemsg,"ftp 异常!");
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
}
pEx->Delete();
if(!ConnectFtp())
{
::PostMessage(mainwnd,WM_PROCMSG,tl,FTPSERVER_FAILD);
}
}
catch(...)
{
tl=(tl-1)%FTPQUEUE_MAX;
if(trytimes>=3)
{
ftpinfo[tl].groupid=ftpinfo[(tl+1)%FTPQUEUE_MAX].groupid;
sprintf(ftpinfo[tl].statemsg,"文件%s3次上传失败!(unkown except)",ftpinfo[local_rear].filename);
sprintf(ftpinfo[tl].filename,"%s",strRemoteFile);
::PostMessage(mainwnd,WM_PROCMSG,tl,FTP_FAILD);
local_rear=(local_rear+1)%FTPQUEUE_MAX;
tl=local_rear;
trytimes=0;
if(!ConnectFtp())
{
::PostMessage(mainwnd,WM_PROCMSG,tl,FTPSERVER_FAILD);
}
break;
}
else
{
trytimes++;
sprintf(ftpinfo[tl].statemsg,"文件%s上传失败,重新上传!(unkown except)",ftpinfo[local_rear].filename);
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
if(!ConnectFtp())
{
::PostMessage(mainwnd,WM_PROCMSG,tl,FTPSERVER_FAILD);
}
}
}
}
return 0;
}
void CFtpProc::OnTimer() //测试FTP连接
{
BOOL res=false;
tl=local_rear-1;
if(ftp_ok)
{
try
{
res= m_pRemoteFinder->FindFile("*.*");
if(res)
{
::PostMessage(mainwnd,WM_PROCMSG,tl,FTP_WORKING);
return;
}
}
catch(CInternetException *pEx)
{
//获取错误
if(pEx->GetErrorMessage(ftpinfo[tl].statemsg,1023))
{
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
}
else
{
sprintf(ftpinfo[tl].statemsg,"连接FTP服务器出现程序异常!");
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
}
pEx->Delete();
}
catch(...)
{
if(m_pInetSession)
{delete m_pInetSession;m_pInetSession=NULL;}
sprintf(ftpinfo[tl].statemsg,"连接FTP服务器出现未知异常!");
::PostMessage(mainwnd,WM_PROCMSG,tl,SHOW_FTP_MSG);
ftp_ok=false;
}
::PostMessage(mainwnd,WM_PROCMSG,tl,FTPSERVER_FAILD);
ftp_ok=false;
}
else
{
ConnectFtp();
}
}
BOOL CFtpProc::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_TIMER)
{
OnTimer();
}
return CWinThread::PreTranslateMessage(pMsg);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -