mod_download.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #ifndef RVC_MOD_DOWNLOAD_H_
  2. #define RVC_MOD_DOWNLOAD_H_
  3. #include "stdafx.h"
  4. #include "SpBase.h"
  5. #include "SpIni.h"
  6. #include "SpTest.h"
  7. #include "modVer.h"
  8. #include "strutil.h"
  9. #include <list>
  10. #include "DownloadFSM.h"
  11. #include "Download_server_g.h"
  12. #include "libtoolkit/path.h"
  13. using namespace Download;
  14. class CDownloadEntity;
  15. class DownloadSession : public DownloadService_ServerSessionBase
  16. {
  17. public:
  18. DownloadSession(CDownloadEntity* pEntity) : m_pEntity(pEntity) {}
  19. virtual ~DownloadSession() {}
  20. virtual void Handle_DownloadFile(SpReqAnsContext<DownloadService_DownloadFile_Req, DownloadService_DownloadFile_Ans>::Pointer ctx);
  21. virtual void Handle_IsDownloading(SpReqAnsContext<DownloadService_IsDownloading_Req, DownloadService_IsDownloading_Ans>::Pointer ctx);
  22. virtual void Handle_CancelDownloadFile(SpReqAnsContext<DownloadService_CancelDownloadFile_Req, DownloadService_CancelDownloadFile_Ans>::Pointer ctx);
  23. virtual void Handle_QueryDownloadState(SpReqAnsContext<DownloadService_QueryDownloadState_Req, DownloadService_QueryDownloadState_Ans>::Pointer ctx);
  24. private:
  25. CDownloadEntity* m_pEntity;
  26. };
  27. class CDownloadEntity : public CEntityBase, public IFSMStateHooker
  28. {
  29. public:
  30. CDownloadEntity() :m_bSynchronized(false) {}
  31. virtual ~CDownloadEntity() {}
  32. virtual const char* GetEntityName() const { return "Download"; } // 0x105
  33. virtual bool IsService()const { return true; }
  34. const char* GetEntityVersion() const override
  35. {
  36. return MODULE_VERSION_FULL;
  37. }
  38. ON_ENTITYT_TEST();
  39. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
  40. {
  41. ErrorCodeEnum Error = Error_Succeed;
  42. //初始化:加载状态hooker,初始化状态机
  43. m_fsm.AddStateHooker(this);
  44. Error = m_fsm.Init(this);
  45. pTransactionContext->SendAnswer(Error);
  46. }
  47. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer<ITransactionContext> pTransactionContext)
  48. {
  49. pTransactionContext->SendAnswer(Error_Succeed);
  50. }
  51. virtual CServerSessionBase* OnNewSession(const char* /*pszRemoteEntityName*/, const char* /*pszClass*/)
  52. {
  53. LOG_FUNCTION();
  54. return new DownloadSession(this);
  55. }
  56. virtual void OnSelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext)
  57. {
  58. if (Test_ShakeHand == eTestType)
  59. {
  60. pTransactionContext->SendAnswer(Error_Succeed);
  61. }
  62. }
  63. virtual void OnStateTrans(int iSrcState, int iDstState)
  64. {
  65. if (iDstState == DownloadFSM::SYNC_STATE) { // Enter
  66. m_bSynchronized = true;
  67. }
  68. else if (iSrcState == DownloadFSM::SYNC_STATE) { // Leave
  69. m_bSynchronized = false;
  70. }
  71. }
  72. bool IsSynchronized()
  73. {
  74. return m_bSynchronized;
  75. }
  76. //接收新下载任务
  77. ErrorCodeEnum DownloadFile(SpReqAnsContext<DownloadService_DownloadFile_Req, DownloadService_DownloadFile_Ans>::Pointer ctx)
  78. {
  79. Dbg("DownloadFile req file name is : [%s] , expireTime is [%d]", (const char*)ctx->Req.strFileName, ctx->Req.dwExpireTime);
  80. ErrorCodeEnum Error = m_fsm.addDownLoadFileWork(ctx->Req.strFileName, ctx->Req.dwExpireTime);
  81. if (Error != Error_Succeed) {
  82. Dbg("DownloadFile req fail, file name is : [%s] ", (const char*)ctx->Req.strFileName);
  83. }
  84. return Error;
  85. }
  86. //整个流程是否正在下载任务
  87. ErrorCodeEnum IsDownloading(bool& bDownloading)
  88. {
  89. bDownloading = !m_bSynchronized;
  90. return Error_Succeed;
  91. }
  92. //取消下载任务
  93. ErrorCodeEnum CancelDownloadFile(SpReqAnsContext<DownloadService_CancelDownloadFile_Req, DownloadService_CancelDownloadFile_Ans>::Pointer ctx)
  94. {
  95. Dbg("CancelDownloadFile req file name is : [%s]", (const char*)ctx->Req.strFileName);
  96. //ErrorCodeEnum Error = m_fsm.deleteCurrentDownloadFile(ctx->Req.strFileName);
  97. ErrorCodeEnum Error = m_fsm.deleteDownLoadFileWork(ctx->Req.strFileName);
  98. if (Error != Error_Succeed) {
  99. Dbg("CancelDownloadFile req fail, file name is : [%s] ", (const char*)ctx->Req.strFileName);
  100. }
  101. m_fsm.PostEventFIFO(new FSMEvent(USER_EVT_JMP_DISABLE));//因下载任务发生变化,结束当前下载,重新开始新的一轮下载。
  102. return Error;
  103. }
  104. //查询下载任务状态
  105. ErrorCodeEnum QueryDownloadState(SpReqAnsContext<DownloadService_QueryDownloadState_Req, DownloadService_QueryDownloadState_Ans>::Pointer ctx)
  106. {
  107. //检查文件是否下载完毕,并检查本地文件是否存在
  108. const char* filename = (const char*)ctx->Req.strFileName;
  109. Dbg("QueryDownloadState req file name is : [%s]", filename);
  110. //当前正在下载,排队,不存在后则看是否下载完成,不存在
  111. //1:正在下载 2:排队中 3:下载完成 4:无下载任务并且没有文件
  112. if (strcmp(filename, m_fsm.m_currentFileName.GetData()) == 0) {
  113. ctx->Ans.iDownloadState = 1;
  114. return Error_Succeed;
  115. }
  116. if (m_fsm.isDownloadTask(filename)) {
  117. ctx->Ans.iDownloadState = 2;
  118. return Error_Succeed;
  119. }
  120. return Error_Succeed;
  121. CSimpleStringA str;
  122. GetFunction()->GetPath("Download", str);
  123. char tmp[MAX_PATH];
  124. strcpy(tmp, str.GetData());
  125. if (str_has_suffix(str.GetData(), SPLIT_SLASH_STR)) {
  126. strcat(tmp, filename);
  127. }
  128. else {
  129. strcat(tmp, SPLIT_SLASH_STR);
  130. strcat(tmp, filename);
  131. }
  132. //WIN32_FIND_DATAA fd;
  133. //HANDLE hFind = FindFirstFileA(tmp, &fd);
  134. //if (hFind != INVALID_HANDLE_VALUE) {
  135. // FindClose(hFind);
  136. // ctx->Ans.iDownloadState = 3;
  137. // return Error_Succeed;
  138. //}
  139. //else {
  140. // FindClose(hFind);
  141. // ctx->Ans.iDownloadState = 4;
  142. // return Error_Succeed;
  143. //}
  144. FILE* fp = fopen(tmp, "rb");
  145. if(fp!=NULL)
  146. {
  147. fclose(fp);
  148. ctx->Ans.iDownloadState = 3;
  149. return Error_Succeed;
  150. }
  151. else
  152. {
  153. ctx->Ans.iDownloadState = 4;
  154. return Error_Succeed;
  155. }
  156. }
  157. private:
  158. /*ErrorCodeEnum MakeFileList(std::list<CSimpleStringA>& fileList)
  159. {
  160. CSimpleStringA str;
  161. GetFunction()->GetPath("Download", str);
  162. return __MakeFileList(str, fileList);
  163. }*/
  164. /*ErrorCodeEnum __MakeFileList(const char* base_dir, std::list<CSimpleStringA>& fileList)
  165. {
  166. char tmp[MAX_PATH];
  167. WIN32_FIND_DATAA fd;
  168. strcpy(tmp, base_dir);
  169. if (str_has_suffix(base_dir, "\\")) {
  170. strcat(tmp, "*");
  171. }
  172. else {
  173. strcat(tmp, "\\*.*");
  174. }
  175. HANDLE hFind = FindFirstFileA(tmp, &fd);
  176. if (hFind != INVALID_HANDLE_VALUE) {
  177. do {
  178. if ((fd.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) && !(fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) {
  179. fileList.push_back(CSimpleStringA(fd.cFileName));
  180. }
  181. } while (FindNextFileA(hFind, &fd));
  182. FindClose(hFind);
  183. }
  184. return Error_Succeed;
  185. }*/
  186. private:
  187. bool m_bSynchronized;
  188. DownloadFSM m_fsm;
  189. };
  190. #endif