SDK.cpp 17 KB

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