SDK.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. #include "StdAfx.h"
  2. #include "SDK.h"
  3. #include "CritSection.h"
  4. #include "Global.h"
  5. #include "DataImpl.h"
  6. #include <sys/stat.h>
  7. #include "CharEncoding.h"
  8. ThreadSection g_csTask;
  9. std::list<STMid> CSDK::m_vtMidTask;
  10. std::string CSDK::_host;
  11. CSDK::CSDK(void):
  12. m_hDownloadEvent(NULL),
  13. m_hThreadDownload(NULL),
  14. m_hReportEvent(NULL),
  15. m_hThreadReport(NULL)
  16. {
  17. Global::Init();
  18. Global::GetMacAddress();
  19. }
  20. CSDK::~CSDK(void)
  21. {
  22. EndDowloadThread();
  23. EndReportThread();
  24. }
  25. int CSDK::QueryMidInfo(std::string order)
  26. {
  27. if ( order.size() == 0 )
  28. return -1;
  29. CDataImpl db;
  30. if ( !db.Open() )
  31. return -2;
  32. STMid mid;
  33. int nRow = db.QueryMidInfo(order.c_str(), mid);
  34. if ( nRow == -1)
  35. return -3;
  36. if ( nRow == 0 )
  37. return 0;
  38. if ( nRow == 1)
  39. {
  40. if ( _tcsicmp(mid.status.c_str(), "0") == 0 )
  41. return 1;
  42. else if ( _tcsicmp(mid.status.c_str(), "1") == 0 )
  43. return 2;
  44. else if ( _tcsicmp(mid.status.c_str(), "1") == -1 )
  45. return 3;
  46. }
  47. return -4;
  48. }
  49. int CSDK::DownloadMidData(std::string order)
  50. {
  51. if ( m_hThreadDownload == NULL )
  52. {
  53. m_hDownloadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  54. if ( m_hDownloadEvent == NULL )
  55. {
  56. return -1;
  57. }
  58. m_hThreadDownload = CreateThread(NULL, 0, ThreadDownload, this, 0, NULL);
  59. if ( m_hThreadDownload == NULL )
  60. {
  61. CloseHandle(m_hDownloadEvent);
  62. m_hDownloadEvent = NULL;
  63. return -1;
  64. }
  65. }
  66. int nRet = 0;
  67. STMid host_mid;
  68. STMid local_mid;
  69. // https请求;
  70. nRet = GetMidInfo(order, host_mid);
  71. if ( nRet == -1)
  72. return -5;
  73. else if ( nRet != 0 )
  74. return 0;
  75. // 判断该订单是否存在;
  76. CDataImpl db;
  77. if ( !db.Open() )
  78. return -2;
  79. // 查询mid;
  80. nRet = db.QueryMidInfo(order, local_mid);
  81. if ( nRet == -1)
  82. return -3;
  83. if ( nRet == 0 )
  84. {
  85. // 新增mid;
  86. db.InsertMidInfo(host_mid);
  87. }
  88. else if ( nRet == 1 )
  89. {
  90. // 是否已下载过,已下载过的不允许重新下载;
  91. if ( local_mid.status == "1" && local_mid.finish_date.size() )
  92. return 2;
  93. // 更新Mid;
  94. db.UpdateMidInfo(host_mid);
  95. }
  96. else if ( nRet > 1 )
  97. return -4;
  98. // 加入任务中;
  99. CSDK::AddDownloadTask(host_mid);
  100. return 1;
  101. }
  102. int CSDK::QueryKeyInfo(std::string sn, STKeys &keys)
  103. {
  104. if ( sn.size() == 0 )
  105. return -1;
  106. CDataImpl db;
  107. if ( !db.Open() )
  108. return -2;
  109. int nRow = db.QueryKeyInfo(sn.c_str(), keys);
  110. if ( nRow == -1)
  111. return -3;
  112. if ( nRow == 0 )
  113. return 0;
  114. if ( nRow == 1 )
  115. return 1;
  116. return -4;
  117. }
  118. int CSDK::UpdateKeyCopyStatus(std::string sn )
  119. {
  120. if ( sn.size() == 0 )
  121. return -1;
  122. CDataImpl db;
  123. if ( !db.Open() )
  124. return -2;
  125. BOOL bRet = FALSE;
  126. // 只更新抄写成功的状态;
  127. bRet = db.UpdateKeyCopyStatus(sn.c_str());
  128. return bRet == TRUE ? 1 : 0;
  129. }
  130. int CSDK::UpdateKeyReportStatus(std::string sn)
  131. {
  132. if ( sn.size() == 0 )
  133. return -1;
  134. CDataImpl db;
  135. if ( !db.Open() )
  136. return -2;
  137. BOOL bRet = FALSE;
  138. // 更新抄写成功状态:1,失败-1;
  139. bRet = db.UpdateKeyReportStatus(sn.c_str());
  140. return bRet == TRUE ? 1 : 0;
  141. }
  142. int CSDK::ReportKeyCopyResults(std::string sn)
  143. {
  144. if ( sn.size() == 0 )
  145. return -1;
  146. CDataImpl db;
  147. if ( !db.Open() )
  148. return -2;
  149. STKeys keys;
  150. int nRet = db.QueryKeyInfo(sn.c_str(), keys);
  151. if ( nRet == -1)
  152. return -3;
  153. if ( nRet == 0 )
  154. return 0;
  155. if ( nRet == 1 )
  156. {
  157. // 上报;
  158. std::string result;
  159. std::map<std::string, std::string> form;
  160. form.insert(std::pair<std::string,std::string>("sn", sn));
  161. form.insert(std::pair<std::string,std::string>("date", keys.copy_date));
  162. CCurlClient curl;
  163. curl.Initialize();
  164. if ( curl.FormPost("http://test.admin.ota.qhmoka.com/IDManage/reportlist.do?", form, result,3) == CURLE_OK)
  165. {
  166. cJSON *pJson = cJSON_Parse(result.c_str());
  167. if ( pJson == NULL )
  168. {
  169. return -6;
  170. }
  171. std::string code, des;
  172. code = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valuestring: "";
  173. des = cJSON_GetObjectItem(pJson, "des") ? cJSON_GetObjectItem(pJson, "des")->valuestring: "";
  174. if ( _tcsicmp(code.c_str(), "1000") )
  175. {
  176. return -7;
  177. }
  178. // 更新上报状态;
  179. nRet = db.UpdateKeyReportStatus(sn);
  180. if ( nRet == 1 )
  181. return 1;
  182. else
  183. return -8; // 上报成功,更新失败;
  184. }
  185. return -5;
  186. }
  187. return -4;
  188. }
  189. int CSDK::BatchReportKeyCopyResults()
  190. {
  191. CDataImpl db;
  192. if ( !db.Open() )
  193. return -2;
  194. std::vector<STKeys> vtkeys;
  195. int nRet = db.QueryUnReportKeyInfo(vtkeys);
  196. if ( nRet == -1)
  197. return -3;
  198. if ( nRet == 0 )
  199. return 0;
  200. // 上报;
  201. std::string result;
  202. std::map<std::string, std::string> form;
  203. std::vector<STKeys>::iterator it = vtkeys.begin();
  204. for (; it != vtkeys.end(); it++)
  205. {
  206. form.insert(std::pair<std::string,std::string>("sn", it->sn));
  207. form.insert(std::pair<std::string,std::string>("date", it->copy_date));
  208. }
  209. CCurlClient curl;
  210. curl.Initialize();
  211. if ( curl.FormPost("http://test.admin.ota.qhmoka.com/IDManage/reportlist.do?", form, result,3) == CURLE_OK)
  212. {
  213. cJSON *pJson = cJSON_Parse(result.c_str());
  214. if ( pJson == NULL )
  215. {
  216. return -6;
  217. }
  218. std::string code, des;
  219. code = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valuestring: "";
  220. des = cJSON_GetObjectItem(pJson, "des") ? cJSON_GetObjectItem(pJson, "des")->valuestring: "";
  221. if ( _tcsicmp(code.c_str(), "1000") )
  222. {
  223. return -7;
  224. }
  225. // 更新上报状态;
  226. nRet = db.BatchUpdateKeyReportStatus(vtkeys);
  227. if ( nRet == 1 )
  228. return 1;
  229. else
  230. return -8; // 上报成功,更新失败;
  231. }
  232. return -5;
  233. }
  234. void CSDK::AddDownloadTask(STMid &mid)
  235. {
  236. AutoThreadSection ats(&g_csTask);
  237. bool has = false;
  238. std::list<STMid>::iterator it = m_vtMidTask.begin();
  239. for (; it != m_vtMidTask.end(); it++ )
  240. {
  241. if ( _tcsicmp(it->order.c_str(), mid.order.c_str()) == 0 )
  242. {
  243. has = true;
  244. *it = mid; // 更新;
  245. break;
  246. }
  247. }
  248. if ( !has )
  249. {
  250. m_vtMidTask.push_back(mid);
  251. }
  252. }
  253. BOOL CSDK::PopDownloadTask(STMid &mid)
  254. {
  255. AutoThreadSection ats(&g_csTask);
  256. if ( m_vtMidTask.size() )
  257. {
  258. mid = m_vtMidTask.front();
  259. m_vtMidTask.pop_front();
  260. return TRUE;
  261. }
  262. return FALSE;
  263. }
  264. bool CSDK::CheckDownload(std::string file, STMid& mid)
  265. {
  266. // 校验文件md5值;
  267. std::string fileMd5 = GetFileMD5(file.c_str());
  268. if ( _tcsicmp(fileMd5.c_str(), mid.pmd5.c_str()) == 0 )
  269. {
  270. // 文件下载成功,批量插入数据库;
  271. std::vector<STKeys> vtKeys;
  272. ParserKey(file, vtKeys, mid);
  273. if ( vtKeys.size() )
  274. {
  275. CDataImpl db;
  276. if ( db.Open() )
  277. {
  278. if ( db.BatchInsertKeyInfo(vtKeys) == 0 )
  279. {
  280. // 更新任务状态;
  281. if ( ReportDownloadStatus(mid.order) == 0 )
  282. {
  283. db.UpdateDownloadStatus(mid.order, 1);
  284. }
  285. return true;
  286. }
  287. }
  288. }
  289. }
  290. return false;
  291. }
  292. int CSDK::GetMidInfo(std::string order, STMid &mid)
  293. {
  294. // http获取mid;
  295. std::string result;
  296. std::string url = "http://test.admin.ota.qhmoka.com/IDManage/getofflinelist.do?";
  297. std::string content = "orderNumber=";
  298. content.append(order);
  299. //content.append("&mac=");
  300. //content.append(Global::g_strMacs);
  301. CCurlClient curl;
  302. curl.Initialize();
  303. if ( curl.Post(url, content, result) == CURLE_OK)
  304. {
  305. // 解析json字符串;
  306. cJSON *pJson = cJSON_Parse(result.c_str());
  307. if ( !pJson )
  308. {
  309. return -2;
  310. }
  311. std::string code, des;
  312. code = cJSON_GetObjectItem(pJson, _T("code")) ? cJSON_GetObjectItem(pJson, _T("code"))->valuestring : "";
  313. des = cJSON_GetObjectItem(pJson, _T("des")) ? cJSON_GetObjectItem(pJson, _T("des"))->valuestring : "";
  314. cJSON *pData = cJSON_GetObjectItem(pJson, _T("data"));
  315. if ( _tcsicmp(code.c_str(), "1000") )
  316. {
  317. cJSON_Delete(pJson);
  318. return -3;
  319. }
  320. if ( !pData )
  321. {
  322. cJSON_Delete(pJson);
  323. return -4;
  324. }
  325. TCHAR szData[10] = {0};
  326. mid.order = cJSON_GetObjectItem(pData, _T("order_number")) ? cJSON_GetObjectItem(pData, _T("order_number"))->valuestring : "";
  327. mid.version = cJSON_GetObjectItem(pData, _T("soft_version")) ? cJSON_GetObjectItem(pData, _T("soft_version"))->valuestring : "";
  328. //mid.number = cJSON_GetObjectItem(pData, _T("order_quantity")) ? cJSON_GetObjectItem(pData, _T("order_quantity"))->valuestring : "";
  329. _itoa_s(cJSON_GetObjectItem(pData, _T("order_quantity")) ? cJSON_GetObjectItem(pData, _T("order_quantity"))->valueint : 0, szData, 10);
  330. mid.number = szData;
  331. mid.pmd5 = cJSON_GetObjectItem(pData, _T("packet_md5")) ? cJSON_GetObjectItem(pData, _T("packet_md5"))->valuestring : "";
  332. mid.ctype = cJSON_GetObjectItem(pData, _T("client_type")) ? cJSON_GetObjectItem(pData, _T("client_type"))->valuestring : "";
  333. mid.pid = cJSON_GetObjectItem(pData, _T("project_id")) ? cJSON_GetObjectItem(pData, _T("project_id"))->valuestring : "";
  334. mid.purl = cJSON_GetObjectItem(pData, _T("packet_url")) ? cJSON_GetObjectItem(pData, _T("packet_url"))->valuestring : "";
  335. //mid.psize = cJSON_GetObjectItem(pData, _T("packet_size")) ? cJSON_GetObjectItem(pData, _T("packet_size"))->valuestring : "";
  336. memset(szData, 0, 10);
  337. _itoa_s(cJSON_GetObjectItem(pData, _T("packet_size")) ? cJSON_GetObjectItem(pData, _T("packet_size"))->valueint : 0, szData, 10);
  338. mid.psize = szData;
  339. cJSON_Delete(pJson);
  340. pJson = NULL;
  341. return 0;
  342. }
  343. // http请求异常;
  344. return -1;
  345. }
  346. BOOL CSDK::HttpPost(std::string host, std::string context, std::string &result, DATATYPE dt)
  347. {
  348. CCurlClient curl;
  349. curl.Initialize();
  350. CharEncoding::ASCII2UTF8(context.c_str(), context);
  351. curl.SetHeaders("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*");
  352. curl.SetHeaders("Accept-Language: zh-cn");
  353. if (dt == DATA_JSON)
  354. curl.SetHeaders("Content-Type: application/json;charset=utf-8");
  355. int nRecCode = CURLE_OK;
  356. if (_tcsstr(host.c_str(), "https://"))
  357. nRecCode = curl.Posts(host, context, result);
  358. else
  359. nRecCode = curl.Posts(host, context, result);
  360. result = CharEncoding::DeCode_URLUTF8(result.c_str());
  361. return CURLE_OK == nRecCode;
  362. }
  363. BOOL CSDK::GetBidInfo(std::string order, std::string macs)
  364. {
  365. // Json数据;
  366. cJSON *pJson = cJSON_CreateObject();
  367. cJSON_AddStringToObject(pJson, "clientType", "");
  368. cJSON_AddStringToObject(pJson, "version", "");
  369. cJSON_AddStringToObject(pJson, "mac", macs.c_str());
  370. cJSON_AddStringToObject(pJson, "bid", order.c_str());
  371. char *pJsonText = cJSON_Print(pJson);
  372. // post请求;
  373. std::string name, type;
  374. std::string host, result, context;
  375. context = pJsonText;
  376. if (pJsonText)
  377. delete pJsonText;
  378. pJsonText = NULL;
  379. cJSON_Delete(pJson);
  380. pJson = NULL;
  381. std::string code, message;
  382. host = Global::g_bTestHost ? _T("http://test.admin.uc.qhmoka.com/scbc-server/clientType/getMessage.do") : _T("https://cn.uc.qhmoka.com/scbc-server/clientType/getMessage.do");
  383. BOOL bRet = HttpPost(host, context, result, DATA_JSON);
  384. if (!bRet)
  385. {
  386. goto end;
  387. }
  388. // 解析返回值;
  389. pJson = cJSON_Parse(result.c_str());
  390. if (pJson == NULL)
  391. {
  392. bRet = FALSE;
  393. goto end;
  394. }
  395. message = cJSON_GetObjectItem(pJson, "message") ? cJSON_GetObjectItem(pJson, "message")->valuestring : "";
  396. code = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valuestring : "";
  397. _host = cJSON_GetObjectItem(pJson, "host") ? (cJSON_GetObjectItem(pJson, "host")->valuestring != NULL ? cJSON_GetObjectItem(pJson, "host")->valuestring : "") : "";
  398. if (_tcsicmp(code.c_str(), "1000") != 0)
  399. {
  400. bRet = FALSE;
  401. goto end;
  402. }
  403. if (_host.size() == 0)
  404. {
  405. bRet = FALSE;
  406. goto end;
  407. }
  408. end:
  409. // 释放Json;
  410. if (pJsonText)
  411. delete pJsonText;
  412. pJsonText = NULL;
  413. cJSON_Delete(pJson);
  414. return bRet;
  415. }
  416. int CSDK::ReportDownloadStatus(std::string order)
  417. {
  418. // http获取mid;
  419. std::string result;
  420. std::string url = "http://test.admin.ota.qhmoka.com/IDManage/getreportofflinesn.do?";
  421. std::string content = "orderNumber=";
  422. content.append(order);
  423. CCurlClient curl;
  424. curl.Initialize();
  425. if ( curl.Post(url, content, result) == CURLE_OK)
  426. {
  427. // 解析json字符串;
  428. cJSON *pJson = cJSON_Parse(result.c_str());
  429. if ( !pJson )
  430. {
  431. return -2;
  432. }
  433. std::string code, des;
  434. code = cJSON_GetObjectItem(pJson, _T("code")) ? cJSON_GetObjectItem(pJson, _T("code"))->valuestring : "";
  435. des = cJSON_GetObjectItem(pJson, _T("des")) ? cJSON_GetObjectItem(pJson, _T("des"))->valuestring : "";
  436. if ( _tcsicmp(code.c_str(), "1000") && _tcsicmp(code.c_str(), "1006") && _tcsicmp(code.c_str(), "1007") )
  437. {
  438. cJSON_Delete(pJson);
  439. return -3;
  440. }
  441. cJSON_Delete(pJson);
  442. pJson = NULL;
  443. return 0;
  444. }
  445. // http请求异常;
  446. return -1;
  447. }
  448. void CSDK::ParserKey(std::string file, std::vector<STKeys> &vtKyes, STMid &mid)
  449. {
  450. // 读取文件;
  451. FILE* pf = NULL;
  452. _tfopen_s(&pf, file.c_str(), _T("rb"));
  453. if (!pf)
  454. return;
  455. // 获取文件长度;
  456. fseek(pf, 0, SEEK_END);
  457. size_t file_size = ftell(pf);
  458. fseek(pf, 0, SEEK_SET);
  459. // 读取文件内容;
  460. byte* pdata = (byte*)malloc(file_size);
  461. fread(pdata, file_size, 1, pf);
  462. fclose(pf);
  463. // Json数据;
  464. cJSON* pJson = cJSON_Parse((const char*)pdata);
  465. if (pJson == NULL)
  466. {
  467. goto end;
  468. }
  469. //////////////////////////////////////////////////////////////////////////
  470. // 解析Json;
  471. cJSON *pItem = NULL;
  472. int nCount = cJSON_GetArraySize(pJson);
  473. for ( int i = 0; i < nCount; i++ )
  474. {
  475. pItem = cJSON_GetArrayItem(pJson, i);
  476. if ( pItem )
  477. {
  478. STKeys key;
  479. key.bid = mid.order;
  480. key.pid = mid.pid;
  481. // 序列号;
  482. key.sn = cJSON_GetObjectItem(pItem, _T("sn")) ? cJSON_GetObjectItem(pItem, _T("sn"))->valuestring : "";
  483. cJSON *pJsonKeys = cJSON_GetObjectItem(pItem, _T("key"));
  484. if ( pJsonKeys )
  485. {
  486. char *lpszKeys = cJSON_Print(pJsonKeys);
  487. if ( lpszKeys)
  488. {
  489. key.keys = lpszKeys;
  490. free(lpszKeys);
  491. }
  492. }
  493. vtKyes.push_back(key);
  494. }
  495. }
  496. end:
  497. if (pdata)
  498. free(pdata);
  499. if (pJson)
  500. cJSON_Delete(pJson);
  501. }
  502. bool CSDK::ParserSNKey(std::string json, SNKeys &snKey)
  503. {
  504. bool bRet = false;
  505. // Json数据;
  506. cJSON* pJson = cJSON_Parse(json.c_str());
  507. if ( pJson )
  508. {
  509. // 序列号;
  510. snKey.did = cJSON_GetObjectItem(pJson, _T("sn")) ? cJSON_GetObjectItem(pJson, _T("sn"))->valuestring : "";
  511. snKey.mac = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
  512. snKey.hdcp = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
  513. snKey.hdcp22 = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
  514. snKey.widi = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
  515. snKey.widevine = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
  516. snKey.esn = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
  517. snKey.cikey = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
  518. bRet = true;
  519. }
  520. if (pJson)
  521. cJSON_Delete(pJson);
  522. return bRet;
  523. }
  524. DWORD CSDK::ThreadDownload(LPVOID lpParam)
  525. {
  526. CSDK *that = (CSDK*)lpParam;
  527. // key包文件路径;
  528. TCHAR szKeyPacket[MAX_PATH] = {0};
  529. do
  530. {
  531. if ( m_vtMidTask.size() )
  532. {
  533. // 取出任务;
  534. STMid mid = m_vtMidTask.front();
  535. // 判断文件是否存在,并检验md5值;
  536. _stprintf_s(szKeyPacket, _T("%scache\\%s.json"), Global::g_szCurModuleDir, mid.order.c_str());
  537. if ( _taccess(szKeyPacket, 0) != -1 )
  538. {
  539. if ( CheckDownload(szKeyPacket, mid) )
  540. {
  541. // 弹出任务;
  542. AutoThreadSection ats(&g_csTask);
  543. if ( m_vtMidTask.size() )
  544. {
  545. m_vtMidTask.pop_front();
  546. }
  547. continue;
  548. }
  549. // 文件大小相同,删除文件;
  550. struct stat fst;
  551. if ( stat(szKeyPacket, &fst) == 0 )
  552. {
  553. if ( atoi(mid.psize.c_str()) == fst.st_size )
  554. DeleteFile(szKeyPacket);
  555. }
  556. }
  557. // 文件不存在,开始下载;
  558. CCurlClient curl;
  559. curl.Initialize();
  560. curl.DownloadEx(mid.purl, szKeyPacket);
  561. }
  562. } while (WaitForSingleObject(that->m_hDownloadEvent, 5000) == WAIT_TIMEOUT );
  563. return 0L;
  564. }
  565. void CSDK::EndDowloadThread()
  566. {
  567. if ( m_hDownloadEvent )
  568. {
  569. SetEvent(m_hDownloadEvent);
  570. }
  571. if ( m_hThreadDownload)
  572. {
  573. WaitForSingleObject(m_hThreadDownload, INFINITE);
  574. CloseHandle(m_hThreadDownload);
  575. m_hThreadDownload = NULL;
  576. if ( m_hDownloadEvent )
  577. CloseHandle(m_hDownloadEvent);
  578. m_hDownloadEvent = NULL;
  579. }
  580. }
  581. DWORD CSDK::ThreadReport(LPVOID lpParam)
  582. {
  583. CSDK *that = (CSDK*)lpParam;
  584. do
  585. {
  586. // 自动上报;
  587. } while (WaitForSingleObject(that->m_hReportEvent, 3000) == WAIT_TIMEOUT );
  588. return 0L;
  589. }
  590. void CSDK::EndReportThread()
  591. {
  592. if ( m_hReportEvent )
  593. {
  594. SetEvent(m_hReportEvent);
  595. }
  596. if ( m_hThreadReport)
  597. {
  598. WaitForSingleObject(m_hThreadReport, INFINITE);
  599. CloseHandle(m_hThreadReport);
  600. m_hThreadReport = NULL;
  601. if ( m_hReportEvent )
  602. CloseHandle(m_hReportEvent);
  603. m_hReportEvent = NULL;
  604. }
  605. }