CSHelper.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:18k
源码类别:

网格计算

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "trfAgent.h"
  3. #include "CSHelper.h"
  4. #include "register.h"
  5. #include "passwrd.h"
  6. #include "variantex.h"
  7. #include "NetAddress.h"
  8. CCSHelper::CCSHelper()
  9. {
  10. this->m_bactived= false;
  11. this->m_nAGID = 0;
  12. this->m_dwserverport = 5557;
  13. this->m_hrefthread = NULL;
  14. this->m_hconcsthread = NULL;
  15. this->m_bcanterminate = false;
  16. this->m_bcanexitconnectcs = false;
  17. this->m_chstate = CCSHelper::CHS_IDLE;
  18. }
  19. CCSHelper::~CCSHelper()
  20. {
  21. }
  22. //read connection configure.
  23. bool CCSHelper::ReadConnConfigure(void)
  24. {
  25. //open the register.
  26. CRegister regkey;
  27. if(!regkey.Open(HKEY_CURRENT_USER, REG_SSYSSETTINGKEY))
  28. {
  29. return false;
  30. }
  31. DWORD dwaddr = regkey.Read_DWord_Value(REG_STRFSERVER);
  32. DWORD dwport = regkey.Read_DWord_Value(REG_NTRFDIRSVRPORT);
  33. regkey.Close();
  34. //get the ip and port info.
  35. BYTE ipfld[4];
  36. for(int i = 4; i > 0; --i)
  37. {
  38. ipfld[i-1] = dwaddr & 0x000000FF;
  39. dwaddr >>= 8;
  40. }
  41. //create the socket object.
  42. char saddress[MAX_IP_ADDR_SIZE];
  43. sprintf(saddress, "%d.%d.%d.%d", ipfld[0], ipfld[1], ipfld[2], ipfld[3]);
  44. //get it!
  45. this->m_dwserverport = dwport;
  46. this->m_sserverip = saddress;
  47. return true;
  48. }
  49. //start the cs helper functions.
  50. bool CCSHelper::Start(void)
  51. {
  52. //read connection configure.
  53. if(!this->ReadConnConfigure()) return false;
  54. this->m_hlpsock.Set_Socket_Address(this->m_dwserverport, this->m_sserverip.c_str());
  55. if(!this->m_hlpsock.Create() || !this->m_hlpsock.Open())
  56. {
  57. MSGBOX_INFO(_LoadString(IDS_DONTCONNSVR).GetBuffer(0));
  58. }
  59. //create cycconnecting server thread.
  60. DWORD dwthreadid = 0x0000;
  61. m_hconcsthread = CreateThread(NULL, 0, CycConnectCSThread, this, 0, &dwthreadid);
  62. if(!m_hconcsthread) return false;
  63. this->FirstAuthor();
  64. //create service thread
  65. this->m_hrefthread = ::CreateThread(NULL, 0, CSRefThreadProc, this, 0, &dwthreadid);
  66. this->m_bactived = true;
  67. return true;
  68. }
  69. //stop the cs helper functions.
  70. void CCSHelper::Stop(void)
  71. {
  72. if(this->m_bactived)
  73. {
  74. this->m_hlpsock.Open(false);
  75. //terminate threads.
  76. this->m_bcanexitconnectcs = true;
  77. ::WaitForSingleObject(this->m_hconcsthread, 10000);
  78. ::CloseHandle(this->m_hconcsthread);
  79. this->m_hconcsthread = NULL;
  80. this->m_bcanterminate = true;
  81. ::WaitForSingleObject(this->m_hrefthread, 10000);
  82. ::CloseHandle(this->m_hrefthread);
  83. this->m_hrefthread = NULL;
  84. //set the cshelper state.
  85. this->m_bactived = false;
  86. this->m_chstate = CCSHelper::CHS_IDLE;
  87. }
  88. }
  89. //get local agent's identity.
  90. const int CCSHelper::GetClientID()
  91. {
  92. return this->m_nAGID;
  93. }
  94. const string &CCSHelper::GetClientName()
  95. {
  96. return this->m_sAGName;
  97. }
  98. //first authentication action
  99. bool CCSHelper::FirstAuthor(void)
  100. {
  101. //get the local agent's id info.
  102. bool bsuccessful = true;
  103. CGuiRecordSet *prs, rsu;
  104. CVariantEx varex;
  105. char sql[MAX_SQL_SIZE];
  106. try
  107. {
  108. //===get the local agent id and name===
  109. _snprintf(sql, MAX_SQL_SIZE-1, SQL::SERVER_GET_AGENTINFO, theApp.m_oAppInstance.m_sHDID);
  110. //send first state package.
  111. if(this->SendAGState())
  112. {
  113. prs = this->SendAGUsrSql(sql, bsuccessful);
  114. if(!prs)// || prs->IsEof())//prs->GetRecordCount() == 0)
  115. {
  116. _DELETE(prs);
  117. return false;
  118. }
  119. prs->GetCollect("id", varex.get_variant_t());
  120. this->m_nAGID = varex.AsInteger();
  121. prs->GetCollect("name", varex.get_variant_t());
  122. this->m_sAGName = varex.AsString();
  123. delete prs;
  124. return true;
  125. }
  126. else
  127. {
  128. if(!rsu.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql))
  129. {
  130. //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
  131. return false;
  132. }
  133. rsu.MoveFirst();
  134. if(!rsu.IsEof())
  135. {
  136. rsu.GetCollect("id", varex.get_variant_t());
  137. this->m_nAGID = varex.AsInteger();
  138. rsu.GetCollect("name", varex.get_variant_t());
  139. this->m_sAGName = varex.AsString();
  140. }
  141. rsu.Close();
  142. }
  143. }
  144. catch (...) 
  145. {
  146. //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
  147. return false;
  148. }
  149. return true;
  150. }
  151. //process agent rename action
  152. bool CCSHelper::AGRename(LPCSTR lpszNewName, const int nAGID)
  153. {
  154. bool bsuccessful = true;
  155. char sql[MAX_SQL_SIZE];
  156. _snprintf(sql, MAX_SQL_SIZE-1, SQL::SERVER_UPDATE_AGENTNAME, lpszNewName, nAGID);
  157. CGuiRecordSet *prs = this->SendAGUsrSql(sql, bsuccessful, false);
  158. _DELETE(prs);
  159. if(bsuccessful && nAGID == this->m_nAGID)
  160. {
  161. this->m_sAGName = lpszNewName;
  162. }
  163. return bsuccessful;
  164. }
  165. //verifying current administrator user twice
  166. bool CCSHelper::VerifyCurrentUser(void)
  167. {
  168. AUTHORINFO authorinfo;
  169. memcpy(&authorinfo, &(theApp.m_oAppInstance.m_authorinfo), sizeof(authorinfo));
  170. if(!theApp.m_oCSHelper.SendAGAuthor(theApp.m_oAppInstance.m_authorinfo))
  171. {
  172. theApp.m_oAppInstance.m_binadminmode = false;
  173. return false;
  174. }
  175. //用户名和用户密码加密后如果和返回的authorinfo结构中对应内容相同则表明该用户合法,否则非法.
  176. vncDecryptBytes((unsigned char*)theApp.m_oAppInstance.m_authorinfo.username, ENCRYPTKEY);
  177. //unencrypting the password
  178. CPasswrd::ToText passwrd_encry(theApp.m_oAppInstance.m_authorinfo.passwrd);
  179. memset(theApp.m_oAppInstance.m_authorinfo.passwrd, 0x0, sizeof(theApp.m_oAppInstance.m_authorinfo.passwrd));
  180. memcpy(theApp.m_oAppInstance.m_authorinfo.passwrd, (const char*)passwrd_encry, sizeof(theApp.m_oAppInstance.m_authorinfo.passwrd));
  181. if( stricmp(theApp.m_oAppInstance.m_authorinfo.username, authorinfo.username) ||
  182. stricmp(theApp.m_oAppInstance.m_authorinfo.passwrd, authorinfo.passwrd) )
  183. {
  184. theApp.m_oAppInstance.m_binadminmode = false;
  185. return false;
  186. }
  187. return true;
  188. }
  189. bool CCSHelper::RecvAnswer(KAGENTPACK &agkpack)
  190. {
  191. int size = sizeof(agkpack);
  192. //for(int i = 0; i < 3; ++i)
  193. {
  194. int recved = this->m_hlpsock.ReceiveBuf(&agkpack, size);
  195. if(-1 == recved)
  196. {
  197. this->m_hlpsock.Open(false);
  198. //MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
  199. return false;
  200. }
  201. else if(recved == size)
  202. {
  203. return true;
  204. }
  205. }
  206. return false;
  207. }
  208. bool CCSHelper::SendAGState(void)
  209. {
  210. if(!this->m_hlpsock.Socket_IsValid() || !this->m_hlpsock.Socket_IsOpened()) return false;
  211. char pszdata[256+32], pszhostname[256];
  212. DWORD dwcmpsize = sizeof(pszhostname);
  213. memset(pszdata, 0x0, sizeof(pszdata));
  214. memset(pszhostname, 0x0, sizeof(pszhostname));
  215. ::GetComputerName(pszhostname, &dwcmpsize);
  216. _snprintf(pszdata, sizeof(pszdata), "%s|%s", theApp.m_oAppInstance.m_sHDID, pszhostname);
  217. //状态包中包含了客户端旧的硬件ID和当前计算机名称的信息.
  218. RAGENTPACK agrpack;
  219. agrpack.type = RAG_STATE;
  220. agrpack.size = strlen(pszdata);/*theApp.m_oAppInstance.m_sHDID*/
  221. agrpack.data = (LPVOID)pszdata;/*theApp.m_oAppInstance.m_sHDID*/;
  222. int size = sizeof(agrpack);
  223. if(size == this->m_hlpsock.SendBuf(&agrpack, size))
  224. {
  225. //@1
  226. if(agrpack.size > 0)
  227. {
  228. int sended = this->m_hlpsock.SendBuf(agrpack.data, agrpack.size);
  229. if(-1 == sended)
  230. {
  231. this->m_hlpsock.Open(false);
  232. //MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
  233. return false;
  234. }
  235. if(sended != agrpack.size) return false;
  236. }
  237. //@2
  238. KAGENTPACK agkpack;
  239. if(this->RecvAnswer(agkpack))
  240. {
  241. bool result = (agkpack.type == KAG_STATE) && (agkpack.ret == K_OK);
  242. return result;
  243. }
  244. }
  245. return false;
  246. }
  247. bool CCSHelper::SendAGAuthor(AUTHORINFO &authorinfo)
  248. {
  249. if(!this->m_hlpsock.Socket_IsValid() || !this->m_hlpsock.Socket_IsOpened()) return false;
  250. RAGENTPACK agrpack;
  251. agrpack.size = sizeof(authorinfo);
  252. agrpack.data = &authorinfo;
  253. agrpack.type = RAG_VERIFYPWD;
  254. int size = sizeof(agrpack), sended = this->m_hlpsock.SendBuf(&agrpack, size);
  255. if(size == sended)
  256. {
  257. //encrypting the user name.
  258. vncEncryptBytes((unsigned char*)authorinfo.username, ENCRYPTKEY);
  259. //encrypting the password
  260. CPasswrd::FromText passwrd_encry(authorinfo.passwrd);
  261. memcpy(authorinfo.passwrd, (const char*)passwrd_encry, MAXPWLEN);
  262. //@1
  263. int sended = this->m_hlpsock.SendBuf(agrpack.data, agrpack.size);
  264. if(-1 == sended || sended != agrpack.size)
  265. {
  266. if(-1 == sended)
  267. {
  268. this->m_hlpsock.Open(false);
  269. MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
  270. }
  271. return false;
  272. }
  273. //@2
  274. KAGENTPACK kagpack;
  275. memset(&kagpack, 0x0, sizeof(kagpack));
  276. if(this->RecvAnswer(kagpack))
  277. {
  278. if(kagpack.ret == K_FAILED) return false;
  279. //@3
  280. int recved = this->m_hlpsock.ReceiveBuf(&authorinfo, sizeof(authorinfo));
  281. if(-1 == recved || recved != sizeof(authorinfo))
  282. {
  283. if(-1 == recved)
  284. {
  285. this->m_hlpsock.Open(false);
  286. MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
  287. }
  288. return false;
  289. }
  290. else return true;
  291. }
  292. }
  293. else if(-1 == sended)
  294. {
  295. this->m_hlpsock.Open(false);
  296. MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
  297. }
  298. return false;
  299. }
  300. CGuiRecordSet* CCSHelper::SendAGUsrSql(LPCSTR lpszUsrSql, bool &bSuccessful, bool bReturnRecordset)
  301. {
  302. if(!this->m_hlpsock.Socket_IsValid() || !this->m_hlpsock.Socket_IsOpened()) return NULL;
  303. string sUsrSql = lpszUsrSql;
  304. if(sUsrSql.empty()) return NULL;
  305. CGuiRecordSet *prs = NULL;
  306. //@1
  307. string szXMLFile("");
  308. bSuccessful = this->ProcessSQLRSXML(lpszUsrSql, szXMLFile, bReturnRecordset);
  309. if(!bSuccessful) return prs;
  310. if(bReturnRecordset && szXMLFile.empty()) bSuccessful = false;
  311. if(bReturnRecordset)
  312. {
  313. prs = new CGuiRecordSet();
  314. if(!prs->LoadFromXMLFile((LPCTSTR)szXMLFile.data()))
  315. {
  316. bSuccessful = false;
  317. _DELETE(prs);
  318. }
  319. ::DeleteFile(szXMLFile.c_str());
  320. }
  321. return prs;
  322. }
  323. bool CCSHelper::ProcessSQLRSXML(LPCSTR szSQLTxt, string &sFileName, bool bReturnRecordset)
  324. {
  325. sFileName.erase();
  326. //@1
  327. EXECSQLINFO execsqlinfo;
  328. memset(&execsqlinfo, 0x0, sizeof(execsqlinfo));
  329. _snprintf(execsqlinfo.sql, MAX_SQL_SIZE-1, "%s", szSQLTxt);
  330. execsqlinfo.needretval = bReturnRecordset ? 1:0;
  331. RAGENTPACK agrpack;
  332. agrpack.size = sizeof(execsqlinfo);
  333. agrpack.data = &execsqlinfo;
  334. agrpack.type = RAG_EXECUSRSQL;
  335. //@1
  336. int size = sizeof(agrpack);
  337. int sended = this->m_hlpsock.SendBuf(&agrpack, size);
  338. if(-1 == sended || sended != size)
  339. {
  340. this->m_hlpsock.Open(false);
  341. //MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
  342. return false;
  343. }
  344. //@2
  345. sended = this->m_hlpsock.SendBuf(agrpack.data, agrpack.size);
  346. if(-1 == sended || sended != agrpack.size)
  347. {
  348. if(-1 == sended)
  349. {
  350. this->m_hlpsock.Open(false);
  351. //MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
  352. }
  353. return false;
  354. }
  355. //@3
  356. KAGENTPACK agkpack;
  357. if(this->RecvAnswer(agkpack))
  358. {
  359. if(bReturnRecordset && !ReceiveFile(sFileName))
  360. {
  361. sFileName.erase();
  362. return false;
  363. }
  364. }
  365. if(this->RecvAnswer(agkpack))
  366. {
  367. if(agkpack.ret == K_FAILED)
  368. {
  369. sFileName.erase();
  370. return false;
  371. }
  372. }
  373. return true;
  374. }
  375. bool CCSHelper::ReceiveFile(string &sDestFile)
  376. {
  377. if(!this->m_hlpsock.Socket_IsValid() || !this->m_hlpsock.Socket_IsOpened()) return false;
  378. //@1
  379. char szTempPath[_MAX_PATH], szRSXMLFile[_MAX_PATH];
  380. memset(szTempPath, 0x0, sizeof(szTempPath));
  381. memset(szRSXMLFile, 0x0, sizeof(szRSXMLFile));
  382. GetTempPath(sizeof(szTempPath), (LPSTR)&szTempPath[0]);
  383. GetTempFileName((char*)szTempPath, "xml", 0, szRSXMLFile);
  384. sDestFile = (char*)szRSXMLFile;
  385. //@2
  386. HANDLE hFile = CreateFile((char*)szRSXMLFile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, 
  387. NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  388. if(INVALID_HANDLE_VALUE == hFile)
  389. {
  390. //TRACE0("Failed to open the specialed filen");
  391. sDestFile.erase();
  392. return false;
  393. }
  394. //@3--Recv the src file size.
  395. char  cRes = 'O';
  396. DWORD flesize = 0;
  397. int   size    = sizeof(DWORD), errtimes = 0;
  398. do
  399. {
  400. if(size != this->m_hlpsock.ReceiveBuf(&flesize, size)) ++errtimes;
  401. else{
  402. //@3--wait for client...
  403. size = sizeof(char);
  404. if(size != this->m_hlpsock.SendBuf(&cRes, size)) ++errtimes;
  405. else break;
  406. }
  407. }
  408. while(errtimes < 3);
  409. //3.1
  410. if(errtimes == 3)
  411. {
  412. //TRACE0("Failed to recv the file size infon");
  413. CloseHandle(hFile);
  414. DeleteFile(sDestFile.c_str());
  415. sDestFile.erase();
  416. return false;
  417. }
  418. //@4
  419. #define MAX_NET_PACKET 8192
  420. int   nrecved = 0, maxsize = 0;
  421. DWORD dwleft  = flesize, dwrite = 0;
  422. char  buf[MAX_NET_PACKET];
  423. while(dwleft > 0)
  424. {
  425. memset((char*)buf, 0x0, sizeof(buf));
  426. maxsize = (dwleft > MAX_NET_PACKET) ? MAX_NET_PACKET : dwleft;
  427. nrecved = this->m_hlpsock.ReceiveBuf(buf, maxsize);
  428. if(nrecved < 0)
  429. {
  430. CloseHandle(hFile);
  431. DeleteFile(sDestFile.c_str());
  432. sDestFile.erase();
  433. return false;
  434. }
  435. XorUncryptStream(buf, nrecved);
  436. WriteFile(hFile, buf, nrecved, &dwrite, NULL);
  437. //wait for client...
  438. cRes = 'O';
  439. size = sizeof(char);
  440. if(size != this->m_hlpsock.SendBuf(&cRes, size))
  441. {
  442. //TRACE0("Failed to send the file size infon");
  443. CloseHandle(hFile);
  444. DeleteFile(sDestFile.c_str());
  445. sDestFile.erase();
  446. return false;
  447. }
  448. dwleft -= nrecved;
  449. }
  450. CloseHandle(hFile);
  451. return true;
  452. }
  453. //update local "agents.db" table
  454. BOOL CCSHelper::UpdateAgentsContents(void)
  455. {
  456. bool bsuccessful = true;
  457. CGuiRecordSet *prs = this->SendAGUsrSql(SQL::SERVER_AGENT_GETALL, bsuccessful);
  458. if(NULL != prs)
  459. {
  460. CGuiRecordSet rs;
  461. try
  462. {
  463. if(rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), SQL::CLIENT_CLEAR_AGSTAB))
  464. {
  465. rs.Close();
  466. char sqltxt[MAX_SQL_SIZE];
  467. for(prs->MoveFirst(); !prs->IsEof(); prs->MoveNext())
  468. {
  469. //1---get fields value.
  470. int id, departid, state;
  471. string hostname, ipaddr, hdid, agname;
  472. CVariantEx varex;
  473. prs->GetCollect("id", varex.get_variant_t());
  474. id = varex.AsInteger();
  475. prs->GetCollect("departid", varex.get_variant_t());
  476. departid = varex.AsInteger();
  477. prs->GetCollect("state", varex.get_variant_t());
  478. state = varex.AsInteger();
  479. prs->GetCollect("hostname", varex.get_variant_t());
  480. hostname = varex.AsString();
  481. prs->GetCollect("ipaddr", varex.get_variant_t());
  482. ipaddr = varex.AsString();
  483. prs->GetCollect("hdid", varex.get_variant_t());
  484. hdid = varex.AsString();
  485. prs->GetCollect("name", varex.get_variant_t());
  486. agname = varex.AsString();;
  487. //2---make sql text.
  488. memset(sqltxt, 0x0, sizeof(sqltxt));
  489. _snprintf(sqltxt, MAX_SQL_SIZE-1, SQL::CLIENT_INSERT_AGINFO, 
  490. id, hostname.c_str(), ipaddr.c_str(), hdid.c_str(),
  491. departid, state, agname.c_str());
  492. if(!rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sqltxt))
  493. {
  494. _DELETE(prs);
  495. return FALSE;
  496. }
  497. }
  498. }
  499. _DELETE(prs);
  500. return TRUE;
  501. }
  502. catch (...)
  503. {
  504. _DELETE(prs);
  505. return FALSE;
  506. }
  507. }
  508. return FALSE;
  509. }
  510. //update local "departs.db" table
  511. BOOL CCSHelper::UpdateDepartsContents(void)
  512. {
  513. bool bsuccessful = true;
  514. CGuiRecordSet *prs = this->SendAGUsrSql(SQL::SERVER_DEPART_GETALL, bsuccessful);
  515. if(NULL != prs)
  516. {
  517. CGuiRecordSet rs;
  518. try
  519. {
  520. if(rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), SQL::CLIENT_CLEAR_DEPARTSTAB))
  521. {
  522. rs.Close();
  523. char sqltxt[MAX_SQL_SIZE];
  524. for(prs->MoveFirst(); !prs->IsEof(); prs->MoveNext())
  525. {
  526. //1---get fields value.
  527. int id = 0, parentid = 0;
  528. string name("");
  529. CVariantEx varex;
  530. prs->GetCollect("id", varex.get_variant_t());
  531. id = varex.AsInteger();
  532. prs->GetCollect("parentid", varex.get_variant_t());
  533. parentid = varex.AsInteger();
  534. prs->GetCollect("name", varex.get_variant_t());
  535. name = varex.AsString();
  536. //2---make sql text.
  537. memset(sqltxt, 0x0, sizeof(sqltxt));
  538. _snprintf( sqltxt, MAX_SQL_SIZE-1, SQL::CLIENT_INSERT_DEPARTINFO, 
  539. id, name.c_str(), parentid);
  540. if(!rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sqltxt))
  541. {
  542. _DELETE(prs);
  543. return FALSE;
  544. }
  545. }
  546. }
  547. _DELETE(prs);
  548. return TRUE;
  549. }
  550. catch (...)
  551. {
  552. _DELETE(prs);
  553. return FALSE;
  554. }
  555. }
  556. return FALSE;
  557. }
  558. //return refresh thread can be terminated flag.
  559. const bool CCSHelper::RefThreadCanTerminate(void)
  560. {
  561. return this->m_bcanterminate;
  562. }
  563. //return cycconnecting server thread can be terminated flag.
  564. const bool CCSHelper::CycConnThreadCanTerminate(void)
  565. {
  566. return this->m_bcanexitconnectcs;
  567. }
  568. //main thread entry.
  569. DWORD CALLBACK CCSHelper::CSRefThreadProc(LPVOID lpParameter)
  570. {
  571. CCSHelper *pthis = (CCSHelper*)lpParameter;
  572. if(NULL == pthis) return 0;
  573. DWORD dwcounts = 0;
  574. while(!pthis->RefThreadCanTerminate())
  575. {
  576. if(dwcounts % 5 == 0) //5 sec
  577. {
  578. if(pthis->m_chstate == CCSHelper::CHS_IDLE)
  579. pthis->m_chstate = CCSHelper::CHS_UPTAGSTATE;
  580. else
  581. pthis->m_chstate = (CCSHelper::CSHLPERSTATE)(pthis->m_chstate | CCSHelper::CHS_UPTAGSTATE);
  582. //===update the agent state===
  583. pthis->SendAGState();
  584. pthis->m_chstate = (CCSHelper::CSHLPERSTATE)(pthis->m_chstate ^ CCSHelper::CHS_UPTAGSTATE);
  585. }
  586. if(dwcounts % 120 == 0) //2 min
  587. {
  588. if(pthis->m_chstate == CCSHelper::CHS_IDLE)
  589. pthis->m_chstate = CCSHelper::CHS_UPTAGUSRLIST;
  590. else
  591. pthis->m_chstate = (CCSHelper::CSHLPERSTATE)(pthis->m_chstate | CCSHelper::CHS_UPTAGUSRLIST);
  592. //===update the local database contents===
  593. pthis->UpdateDepartsContents();
  594. pthis->UpdateAgentsContents();
  595. pthis->m_chstate = (CCSHelper::CSHLPERSTATE)(pthis->m_chstate ^ CCSHelper::CHS_UPTAGUSRLIST);
  596. if(theApp.m_pMainWnd && theApp.m_pMainWnd->GetSafeHwnd())
  597. PostMessage(theApp.m_pMainWnd->GetSafeHwnd(), UWM_REFRESHUSRLST, 0, 0);
  598. dwcounts = 0;
  599. }
  600. ++dwcounts;
  601. Sleep(1000);
  602. }
  603. return 0;
  604. }
  605. //c-s socket loop connecting thread.
  606. DWORD CALLBACK CCSHelper::CycConnectCSThread(LPVOID lpParameters)
  607. {
  608. CCSHelper* pthis = (CCSHelper*)lpParameters;
  609. if(NULL == pthis) return 0;
  610. while(!pthis->CycConnThreadCanTerminate())
  611. {
  612. if(!pthis->m_hlpsock.Socket_IsValid() || !pthis->m_hlpsock.Socket_IsOpened())
  613. {
  614. pthis->m_hlpsock.Set_Socket_Address(pthis->m_dwserverport, pthis->m_sserverip.c_str());
  615. if(!pthis->m_hlpsock.Create() || !pthis->m_hlpsock.Open())
  616. {
  617. if(pthis->m_hlpsock.Socket_IsValid())
  618. {
  619. pthis->m_hlpsock.Open(false);
  620. }
  621. }
  622. }
  623. Sleep(2000);
  624. }
  625. return 0;
  626. }