|
@@ -0,0 +1,208 @@
|
|
|
+#ifndef RVC_MOD_DOWNLOAD_H_
|
|
|
+#define RVC_MOD_DOWNLOAD_H_
|
|
|
+#include "stdafx.h"
|
|
|
+#include "SpBase.h"
|
|
|
+#include "SpIni.h"
|
|
|
+
|
|
|
+#include "SpTest.h"
|
|
|
+#include "modVer.h"
|
|
|
+
|
|
|
+#include "strutil.h"
|
|
|
+
|
|
|
+#include <list>
|
|
|
+
|
|
|
+#include "DownloadFSM.h"
|
|
|
+
|
|
|
+#include "Download_server_g.h"
|
|
|
+
|
|
|
+using namespace Download;
|
|
|
+
|
|
|
+class CDownloadEntity;
|
|
|
+
|
|
|
+class DownloadSession : public DownloadService_ServerSessionBase
|
|
|
+{
|
|
|
+public:
|
|
|
+ DownloadSession(CDownloadEntity* pEntity) : m_pEntity(pEntity) {}
|
|
|
+ virtual ~DownloadSession() {}
|
|
|
+
|
|
|
+ virtual void Handle_DownloadFile(SpReqAnsContext<DownloadService_DownloadFile_Req, DownloadService_DownloadFile_Ans>::Pointer ctx);
|
|
|
+ virtual void Handle_IsDownloading(SpReqAnsContext<DownloadService_IsDownloading_Req, DownloadService_IsDownloading_Ans>::Pointer ctx);
|
|
|
+ virtual void Handle_CancelDownloadFile(SpReqAnsContext<DownloadService_CancelDownloadFile_Req, DownloadService_CancelDownloadFile_Ans>::Pointer ctx);
|
|
|
+ virtual void Handle_QueryDownloadState(SpReqAnsContext<DownloadService_QueryDownloadState_Req, DownloadService_QueryDownloadState_Ans>::Pointer ctx);
|
|
|
+
|
|
|
+
|
|
|
+private:
|
|
|
+ CDownloadEntity* m_pEntity;
|
|
|
+};
|
|
|
+
|
|
|
+class CDownloadEntity : public CEntityBase, public IFSMStateHooker
|
|
|
+{
|
|
|
+public:
|
|
|
+ CDownloadEntity() :m_bSynchronized(false) {}
|
|
|
+ virtual ~CDownloadEntity() {}
|
|
|
+ virtual const char* GetEntityName() const { return "Download"; } // 0x105
|
|
|
+ virtual bool IsService()const { return true; }
|
|
|
+
|
|
|
+ const char* GetEntityVersion() const override
|
|
|
+ {
|
|
|
+ return MODULE_VERSION_FULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ ON_ENTITYT_TEST();
|
|
|
+
|
|
|
+ virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
|
|
|
+ {
|
|
|
+ ErrorCodeEnum Error = Error_Succeed;
|
|
|
+ //初始化:加载状态hooker,初始化状态机
|
|
|
+ m_fsm.AddStateHooker(this);
|
|
|
+ Error = m_fsm.Init(this);
|
|
|
+
|
|
|
+ pTransactionContext->SendAnswer(Error);
|
|
|
+ }
|
|
|
+
|
|
|
+ virtual void OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer<ITransactionContext> pTransactionContext)
|
|
|
+ {
|
|
|
+ pTransactionContext->SendAnswer(Error_Succeed);
|
|
|
+ }
|
|
|
+
|
|
|
+ virtual CServerSessionBase* OnNewSession(const char* /*pszRemoteEntityName*/, const char* /*pszClass*/)
|
|
|
+ {
|
|
|
+ LOG_FUNCTION();
|
|
|
+ return new DownloadSession(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ virtual void OnSelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext)
|
|
|
+ {
|
|
|
+ if (Test_ShakeHand == eTestType)
|
|
|
+ {
|
|
|
+ pTransactionContext->SendAnswer(Error_Succeed);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ virtual void OnStateTrans(int iSrcState, int iDstState)
|
|
|
+ {
|
|
|
+ if (iDstState == DownloadFSM::SYNC_STATE) { // Enter
|
|
|
+ m_bSynchronized = true;
|
|
|
+ }
|
|
|
+ else if (iSrcState == DownloadFSM::SYNC_STATE) { // Leave
|
|
|
+ m_bSynchronized = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ bool IsSynchronized()
|
|
|
+ {
|
|
|
+ return m_bSynchronized;
|
|
|
+ }
|
|
|
+
|
|
|
+ //接收新下载任务
|
|
|
+ ErrorCodeEnum DownloadFile(SpReqAnsContext<DownloadService_DownloadFile_Req, DownloadService_DownloadFile_Ans>::Pointer ctx)
|
|
|
+ {
|
|
|
+ Dbg("DownloadFile req file name is : [%s] , expireTime is [%d]", (const char*)ctx->Req.strFileName, ctx->Req.dwExpireTime);
|
|
|
+ ErrorCodeEnum Error = m_fsm.addDownLoadFileWork(ctx->Req.strFileName, ctx->Req.dwExpireTime);
|
|
|
+ if (Error != Error_Succeed) {
|
|
|
+ Dbg("DownloadFile req fail, file name is : [%s] ", (const char*)ctx->Req.strFileName);
|
|
|
+ }
|
|
|
+ return Error;
|
|
|
+ }
|
|
|
+ //整个流程是否正在下载任务
|
|
|
+ ErrorCodeEnum IsDownloading(bool& bDownloading)
|
|
|
+ {
|
|
|
+ bDownloading = !m_bSynchronized;
|
|
|
+ return Error_Succeed;
|
|
|
+ }
|
|
|
+ //取消下载任务
|
|
|
+ ErrorCodeEnum CancelDownloadFile(SpReqAnsContext<DownloadService_CancelDownloadFile_Req, DownloadService_CancelDownloadFile_Ans>::Pointer ctx)
|
|
|
+ {
|
|
|
+ Dbg("CancelDownloadFile req file name is : [%s]", (const char*)ctx->Req.strFileName);
|
|
|
+
|
|
|
+ //ErrorCodeEnum Error = m_fsm.deleteCurrentDownloadFile(ctx->Req.strFileName);
|
|
|
+
|
|
|
+ ErrorCodeEnum Error = m_fsm.deleteDownLoadFileWork(ctx->Req.strFileName);
|
|
|
+ if (Error != Error_Succeed) {
|
|
|
+ Dbg("CancelDownloadFile req fail, file name is : [%s] ", (const char*)ctx->Req.strFileName);
|
|
|
+ }
|
|
|
+ m_fsm.PostEventFIFO(new FSMEvent(USER_EVT_JMP_DISABLE));//因下载任务发生变化,结束当前下载,重新开始新的一轮下载。
|
|
|
+ return Error;
|
|
|
+ }
|
|
|
+ //查询下载任务状态
|
|
|
+ ErrorCodeEnum QueryDownloadState(SpReqAnsContext<DownloadService_QueryDownloadState_Req, DownloadService_QueryDownloadState_Ans>::Pointer ctx)
|
|
|
+ {
|
|
|
+ //检查文件是否下载完毕,并检查本地文件是否存在
|
|
|
+
|
|
|
+
|
|
|
+ const char* filename = (const char*)ctx->Req.strFileName;
|
|
|
+ Dbg("QueryDownloadState req file name is : [%s]", filename);
|
|
|
+ //当前正在下载,排队,不存在后则看是否下载完成,不存在
|
|
|
+ //1:正在下载 2:排队中 3:下载完成 4:无下载任务并且没有文件
|
|
|
+ if (strcmp(filename, m_fsm.m_currentFileName.GetData()) == 0) {
|
|
|
+ ctx->Ans.iDownloadState = 1;
|
|
|
+ return Error_Succeed;
|
|
|
+ }
|
|
|
+ if (m_fsm.isDownloadTask(filename)) {
|
|
|
+ ctx->Ans.iDownloadState = 2;
|
|
|
+ return Error_Succeed;
|
|
|
+ }
|
|
|
+
|
|
|
+ CSimpleStringA str;
|
|
|
+ GetFunction()->GetPath("Download", str);
|
|
|
+ char tmp[MAX_PATH];
|
|
|
+ WIN32_FIND_DATAA fd;
|
|
|
+ strcpy(tmp, str.GetData());
|
|
|
+ if (str_has_suffix(str.GetData(), "\\")) {
|
|
|
+ strcat(tmp, filename);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ strcat(tmp, "\\");
|
|
|
+ strcat(tmp, filename);
|
|
|
+ }
|
|
|
+
|
|
|
+ HANDLE hFind = FindFirstFileA(tmp, &fd);
|
|
|
+
|
|
|
+ if (hFind != INVALID_HANDLE_VALUE) {
|
|
|
+ FindClose(hFind);
|
|
|
+ ctx->Ans.iDownloadState = 3;
|
|
|
+ return Error_Succeed;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ FindClose(hFind);
|
|
|
+ ctx->Ans.iDownloadState = 4;
|
|
|
+ return Error_Succeed;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+private:
|
|
|
+ ErrorCodeEnum MakeFileList(std::list<CSimpleStringA>& fileList)
|
|
|
+ {
|
|
|
+ CSimpleStringA str;
|
|
|
+ GetFunction()->GetPath("Download", str);
|
|
|
+ return __MakeFileList(str, fileList);
|
|
|
+ }
|
|
|
+ ErrorCodeEnum __MakeFileList(const char* base_dir, std::list<CSimpleStringA>& fileList)
|
|
|
+ {
|
|
|
+ char tmp[MAX_PATH];
|
|
|
+ WIN32_FIND_DATAA fd;
|
|
|
+ strcpy(tmp, base_dir);
|
|
|
+ if (str_has_suffix(base_dir, "\\")) {
|
|
|
+ strcat(tmp, "*");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ strcat(tmp, "\\*.*");
|
|
|
+ }
|
|
|
+ HANDLE hFind = FindFirstFileA(tmp, &fd);
|
|
|
+ if (hFind != INVALID_HANDLE_VALUE) {
|
|
|
+ do {
|
|
|
+ if ((fd.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) && !(fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) {
|
|
|
+ fileList.push_back(CSimpleStringA(fd.cFileName));
|
|
|
+ }
|
|
|
+ } while (FindNextFileA(hFind, &fd));
|
|
|
+ FindClose(hFind);
|
|
|
+ }
|
|
|
+ return Error_Succeed;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+private:
|
|
|
+ bool m_bSynchronized;
|
|
|
+ DownloadFSM m_fsm;
|
|
|
+};
|
|
|
+#endif
|