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

网格计算

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "Server.h"
  3. #include "Service.h"
  4. #include "passwrd.h"
  5. safe_vector<PCLIENT_STATE> m_omiclientstates;
  6. bool ModifyClientState(string clienthdid, bool state)
  7. {
  8. if(!theApp.m_dbcont.IsActived()) return false;
  9. CADODataSet rs;
  10. rs.SetConnection(theApp.m_dbcont);
  11. char sql[MAX_SQL_SIZE];
  12. memset(sql, 0x0, sizeof(sql));
  13. sprintf(sql, SQL::SERVER_UPDATE_AGSTATE, (state ? 1 : 0), clienthdid.c_str());
  14. if( rs.Open(sql) )
  15. {
  16. for(int i = 0; i < m_omiclientstates.size(); ++i)
  17. {
  18. PCLIENT_STATE pitem = m_omiclientstates[i];
  19. if(NULL == pitem)
  20. {
  21. m_omiclientstates.pop_at(i--);
  22. continue;
  23. }
  24. if(!pitem->clienthdid.compare(clienthdid.c_str()))
  25. {
  26. pitem->interval = 0;
  27. break;
  28. }
  29. }
  30. return true;
  31. }
  32. return false;
  33. }
  34. //update the client state list.
  35. void UpdateClientState(void)
  36. {
  37. if(!theApp.m_dbcont.IsActived())
  38. {
  39. //TRACE0("The db connection is not active yetn");
  40. return ;
  41. }
  42. try
  43. {
  44. for(int i = 0; i < m_omiclientstates.size(); ++i)
  45. {
  46. PCLIENT_STATE pitem = m_omiclientstates[i];
  47. if(NULL == pitem)
  48. {
  49. m_omiclientstates.pop_at(i--);
  50. continue;
  51. }
  52. pitem->interval++;
  53. //update the client database state flag.
  54. if(pitem->interval > 300)
  55. {
  56. theApp.m_syslog.Print(LL_INFO, "UpdateClientState : 侦测到 [%s] 客户端连接状态改变...n", pitem->clienthdid.c_str());
  57. //restore to zero.
  58. pitem->interval = 0;
  59. ModifyClientState(pitem->clienthdid, false);
  60. }
  61. }
  62. }
  63. catch (...)
  64. {
  65. return ;
  66. }
  67. }
  68. //为以后扩充用---部门根据IP地址来划分
  69. int GetClientDepart(string &clienthdid)
  70. {
  71. return -1;
  72. }
  73. /********************************************************
  74.  *  Class Name : CService. *
  75.  * Purpose : Service objects manager class. *
  76.  *  File  Name : Service.h / Service.cpp *
  77.  *------------------------------------------------------*
  78.  * Author : Devia Lee. Date: 2004-06-01 *
  79.  ********************************************************/
  80. CService::CService()
  81. {
  82. m_bopened  = false;
  83. m_omiservice = NULL;
  84. }
  85. CService::~CService()
  86. {
  87. this->Stop();
  88. }
  89. bool CService::IsOpened(void)
  90. {
  91. return m_bopened;
  92. }
  93. bool CService::Start(const int serverport)
  94. {
  95. theApp.m_syslog.Print(LL_INFO, "CService::Start : 开始创建服务对象...n");
  96. if(NULL == this->m_omiservice)
  97. {
  98. this->m_omiservice = new CServiceThread;
  99. if(!this->m_omiservice->Create(this, false, serverport))
  100. {
  101. theApp.m_syslog.Print(LL_ERROR, "CService::Start : 创建服务对象主线程失败!!!n");
  102. this->m_bopened = false;
  103. return false;
  104. }
  105. theApp.m_syslog.Print(LL_INFO, "CService::Start : 创建服务对象主线程成功!!!n");
  106. }
  107. theApp.m_syslog.Print(LL_INFO, "CService::Start : 创建服务对象成功!!!n");
  108. this->m_bopened = true;
  109. return true;
  110. }
  111. void CService::Stop()
  112. {
  113. theApp.m_syslog.Print(LL_INFO, "CService::Stop : 开始销毁服务对象...n");
  114. if(NULL != this->m_omiservice)
  115. {
  116. this->m_omiservice->Stop();
  117. _DELETE(this->m_omiservice);
  118. theApp.m_syslog.Print(LL_INFO, "CService::Stop : 服务对象主线程已销毁!!!n");
  119. }
  120. theApp.m_syslog.Print(LL_INFO, "CService::Stop : 销毁服务对象成功!!!n");
  121. this->m_bopened = false;
  122. }
  123. /********************************************************
  124.  *  Class Name : CServiceThread. *
  125.  * Purpose : Main service thread class. *
  126.  *  File  Name : Service.h / Service.cpp *
  127.  *------------------------------------------------------*
  128.  * Author : Devia Lee. Date: 2004-06-01 *
  129.  ********************************************************/
  130. CServiceThread::CServiceThread()
  131. {
  132. this->m_service = NULL;
  133. this->m_svrsock = NULL;
  134. }
  135. CServiceThread::~CServiceThread()
  136. {
  137. this->OnStop();
  138. }
  139. //create the acceptor socket listening thread.
  140. bool CServiceThread::Create(CService *pService, bool bCreateAndSuspended, const uint unSvrPort)
  141. {
  142. assert( NULL != pService && NULL == this->m_svrsock );
  143. theApp.m_syslog.Print(LL_INFO, "CServiceThread::Create : 开始创建服务主线程...n");
  144. this->m_service = pService;
  145. //create the listening server socket object.
  146. this->m_svrsock = new CServiceSocket();
  147. this->m_svrsock->Set_Socket_Address(unSvrPort);
  148. try{
  149. if ( this->m_svrsock->Create() )
  150. {
  151. if(this->m_svrsock->Open())
  152. theApp.m_syslog.Print(LL_INFO, "CServiceThread::Create : 创建客户端Socket连接成功!!!n");
  153. else
  154. theApp.m_syslog.Print(LL_ERROR, "CServiceThread::Create : 创建客户端Socket连接失败...n");
  155. }
  156. }
  157. catch ( CNetSockException *ex ) {
  158. theApp.m_syslog.Print(LL_ERROR, "CServiceThread::Create : 创建客户端Socket连接发生异常, ErrorCode = %dn", ex->GetSockErrCode());
  159. _DELETE( this->m_svrsock );
  160. throw ex;
  161. return false;
  162. }
  163. return CThread::Create(bCreateAndSuspended, NULL);
  164. }
  165. //overrides execute function.
  166. void CServiceThread::Execute()
  167. {
  168. while(!this->CanBeTerminate())
  169. {
  170. UpdateClientState();
  171. Sleep(100);
  172. }
  173. }
  174. //start, stop, pause and resume event process functions.
  175. void CServiceThread::OnStart()
  176. {
  177. }
  178. void CServiceThread::OnStop()
  179. {
  180. //close the server socket
  181. if ( this->m_svrsock && this->m_svrsock->Open(false) )
  182. {
  183. _DELETE( this->m_svrsock );
  184. theApp.m_syslog.Print(LL_INFO, "CServiceThread::OnStop : 服务主线程退出!!!n");
  185. }
  186. }
  187. void CServiceThread::OnSuspend()
  188. {
  189. assert(NULL != this->m_svrsock);
  190. theApp.m_syslog.Print(LL_INFO, "CServiceThread::OnSuspend : 服务主线程挂起!!!n");
  191. }
  192. void CServiceThread::OnResume()
  193. {
  194. assert(NULL != this->m_svrsock);
  195. theApp.m_syslog.Print(LL_INFO, "CServiceThread::OnStop : 服务主线程恢复运行!!!n");
  196. }
  197. /********************************************************
  198.  *  Class Name : CServiceSocket. *
  199.  * Purpose : Service socket object class. *
  200.  *  File  Name : Service.h / Service.cpp *
  201.  *------------------------------------------------------*
  202.  * Author : Devia Lee. Date: 2004-06-01 *
  203.  ********************************************************/
  204. CServiceSocket::CServiceSocket()
  205. {
  206. }
  207. CServiceSocket::~CServiceSocket()
  208. {
  209. theApp.m_syslog.Print(LL_INFO, "CServiceSocket::~CServiceSocket : 开始销毁服务端Socket...n");
  210. //destroy the vector.
  211. for(int i = 0; i = (int)m_omicltthreads.size() > 0; )
  212. {
  213. CClientThread *pthread = m_omicltthreads[i-1];
  214. if(NULL == pthread)
  215. {
  216. m_omicltthreads.pop_at(i-1);
  217. continue;
  218. }
  219. if(pthread->GetSockObj()->Open(false))
  220. {
  221. theApp.m_syslog.Print(LL_INFO, "CServiceSocket::~CServiceSocket : 销毁 [%s] 客户端处理线程.n", 
  222. pthread->GetSockObj()->Get_RemoteHost_IP().c_str());
  223. pthread->Stop();
  224. _DELETE(pthread);
  225. m_omicltthreads.pop_back();
  226. }
  227. }
  228. //destroy the list.
  229. for(i = 0; i = m_omiclientstates.size() > 0; )
  230. {
  231. PCLIENT_STATE pitem = m_omiclientstates[i-1];
  232. if(NULL == pitem)
  233. {
  234. m_omiclientstates.pop_at(i-1);
  235. continue;
  236. }
  237. _DELETE(pitem);
  238. m_omiclientstates.pop_back();
  239. }
  240. theApp.m_syslog.Print(LL_INFO, "CServiceSocket::~CServiceSocket : 销毁服务端Socket结束!!!n");
  241. }
  242. //===============================================================
  243. //
  244. // Process the client's connected event and update clients list.
  245. //
  246. //===============================================================
  247. void CServiceSocket::OnClientConnect(CXWinSocket *pClientSocket)
  248. {
  249. CXWinServerSocket::OnClientConnect(pClientSocket);
  250. if(NULL == pClientSocket) return ;
  251. string sremoteip = pClientSocket->Get_RemoteHost_IP();
  252. theApp.m_syslog.Print(LL_INFO, "CServiceSocket::OnClientConnect : 侦听到远程客户端 [%s] 连接...n", sremoteip.c_str());
  253. if(!theApp.m_dbcont.IsActived())
  254. {
  255. theApp.m_syslog.Print(LL_ERROR, "CServiceSocket::OnClientConnect : 数据库连接异常,客户端 [%s] 连接被异常终止.n", sremoteip.c_str());
  256. pClientSocket->Open(false);
  257. return ;
  258. }
  259. //@1---检查该客户端是否已经存在于列表中,如果存在则直接关闭Socket返回.
  260. CClientThread *pthread = NULL;
  261. for(int i = 0; i < m_omicltthreads.size(); ++i)
  262. {
  263. pthread = m_omicltthreads[i];
  264. if(NULL == pthread)
  265. {
  266. m_omicltthreads.pop_at(i--);
  267. continue;
  268. }
  269. if( pthread->GetSockObj()->Get_RemoteHost_IP() == pClientSocket->Get_RemoteHost_IP() ||
  270. pthread->GetSockObj()->Get_RemoteHost_Name() == pClientSocket->Get_RemoteHost_Name())
  271. {
  272. theApp.m_syslog.Print(LL_WARNING, "CServiceSocket::OnClientConnect : [%s] 客户端已在服务器注册,连接终止!!!n", sremoteip.c_str());
  273. pClientSocket->Open(false);
  274. return ;
  275. }
  276. }
  277. //@3---为客户端创建处理线程
  278. theApp.m_syslog.Print(LL_INFO, "CServiceSocket::OnClientConnect : 准备为 [%s] 客户端创建处理线程...n", sremoteip.c_str());
  279. string remotename = pClientSocket->Get_RemoteHost_Name(), remoteip = pClientSocket->Get_RemoteHost_IP();
  280. pthread = new CClientThread(pClientSocket);
  281. if(pthread->Create()){
  282. m_omicltthreads.push_back(pthread);
  283. theApp.m_syslog.Print(LL_ERROR, "CServiceSocket::OnClientConnect : 为 [%s] 客户端创建处理线程成功!!!n", sremoteip.c_str());
  284. }
  285. else{
  286. //如果创建处理线程失败则恢复数据库状态标志
  287. ModifyClientState(remoteip, false);
  288. _DELETE(pthread);
  289. pClientSocket->Open(false);
  290. theApp.m_syslog.Print(LL_ERROR, "CServiceSocket::OnClientConnect : 为 [%s] 客户端创建处理线程失败!!!n", sremoteip.c_str());
  291. }
  292. }
  293. //=============================================================================
  294. //
  295. // Process the client's disconnected event and update clients list.
  296. //
  297. //=============================================================================
  298. void CServiceSocket::OnClientClose(CXWinSocket *pClientSocket)
  299. {
  300. if(NULL == pClientSocket) return ;
  301. try
  302. {
  303. string sremoteip;
  304. CClientThread *pthread = NULL;
  305. for(int i = 0; i < m_omicltthreads.size(); ++i)
  306. {
  307. pthread = m_omicltthreads[i];
  308. if(NULL == pthread)
  309. {
  310. m_omicltthreads.pop_at(i--);
  311. continue;
  312. }
  313. if(pthread->GetSockObj()->Get_Socket_Handle() == pClientSocket->Get_Socket_Handle())
  314. {
  315. //@1
  316. sremoteip = pthread->m_sremoteip;
  317. for(int j = m_omiclientstates.size(); j > 0; --j)
  318. {
  319. PCLIENT_STATE pitem = m_omiclientstates[j-1];
  320. if(NULL == pitem)
  321. {
  322. m_omiclientstates.pop_at(j-1);
  323. continue;
  324. }
  325. if(0 == pitem->clienthdid.compare(pthread->m_sclienthdid.c_str()))
  326. {
  327. if(!ModifyClientState(pitem->clienthdid, false))
  328. {
  329. theApp.m_syslog.Print(LL_ERROR, "CServiceSocket::OnClientClose : 修改 [%s] 客户端状态失败.n", pitem->clienthdid.c_str());
  330. return ;
  331. }
  332. _DELETE(pitem);
  333. m_omiclientstates.pop_at(j-1);
  334. break;
  335. }
  336. }
  337. //@2
  338. pthread->Stop();
  339. m_omicltthreads.pop_at(i);
  340. _DELETE(pthread);
  341. break;
  342. }
  343. }
  344. theApp.m_syslog.Print(LL_INFO, "CServiceSocket::OnClientClose : 客户端 [%s] 已端开.n", sremoteip.c_str());
  345. CXWinServerSocket::OnClientClose(pClientSocket);
  346. }
  347. catch (...) {
  348. CXWinServerSocket::OnClientClose(pClientSocket);
  349. }
  350. }
  351. /********************************************************
  352.  *  Class Name : CClientThread. *
  353.  * Purpose : Client processing thread class. *
  354.  *  File  Name : Service.h / Service.cpp *
  355.  *------------------------------------------------------*
  356.  * Author : Devia Lee. Date: 2004-06-01 *
  357.  ********************************************************/
  358. CClientThread::CClientThread(CXWinSocket *pClientSocket)
  359. {
  360. assert(NULL != pClientSocket && pClientSocket->Socket_IsOpened());
  361. this->m_pcltsock = pClientSocket;
  362. this->m_bfirststate = true;
  363. }
  364. CClientThread::~CClientThread()
  365. {
  366. }
  367. //send answer to peer.
  368. bool CClientThread::SendAnswer(RAGPACKTYPE akp, bool bSuccessfully)
  369. {
  370. KAGENTPACK agkpack;
  371. int size = sizeof(agkpack);
  372. memset(&agkpack, 0x0, size);
  373. agkpack.type = KAGPACKTYPE(akp);
  374. agkpack.ret  = bSuccessfully ? K_OK : K_FAILED;
  375. //for(int i = 0; i < 3; ++i)
  376. {
  377. int sended = this->m_pcltsock->SendBuf(&agkpack, size);
  378. if(-1 == sended)
  379. {
  380. theApp.m_syslog.Print(LL_ERROR, "CClientThread::SendAnswer : 发送客户端应答 [%s] 发生Socket异常,客户端处理线程终止!!!n", this->m_sremoteip.c_str());
  381. this->Stop();
  382. return false;
  383. }
  384. else if(size == sended)
  385. {
  386. return true;
  387. }
  388. }
  389. return false;
  390. }
  391. //overrides execute function.
  392. void CClientThread::Execute()
  393. {
  394. RAGENTPACK agrpack;
  395. int size = 0;
  396. this->m_sremotehost = this->m_pcltsock->Get_RemoteHost_Name();
  397. this->m_sremoteip   = this->m_pcltsock->Get_RemoteHost_IP();
  398. theApp.m_syslog.Print(LL_INFO, "CClientThread::Execute : 客户端 [%s] 线程开始运行...n", this->m_sremoteip.c_str());
  399. while(!this->CanBeTerminate() && theApp.m_dbcont.IsActived() && 
  400. m_pcltsock->Socket_IsValid() && m_pcltsock->Socket_IsOpened())
  401. {
  402. size = sizeof(agrpack);
  403. memset(&agrpack, 0x0, size);
  404. //@1---recv the package head.
  405. if(size != m_pcltsock->ReceiveBuf(&agrpack, size))
  406. {
  407. Sleep(10);
  408. continue;
  409. }
  410. else if(agrpack.type != RAG_STATE && 
  411. agrpack.type != RAG_VERIFYPWD &&
  412. agrpack.type != RAG_EXECUSRSQL)
  413. {
  414. Sleep(10);
  415. continue;
  416. }
  417. //@2---recv the package extra data.
  418. if(agrpack.size > 0)
  419. {
  420. agrpack.data = new char[agrpack.size+1];
  421. memset(agrpack.data, 0x0, agrpack.size+1);
  422. if(agrpack.size != m_pcltsock->ReceiveBuf(agrpack.data, agrpack.size))
  423. {
  424. delete []agrpack.data;
  425. Sleep(10);
  426. continue;
  427. }
  428. }
  429. //@4---process the client's requirements.
  430. switch(agrpack.type){
  431. case RAG_STATE:
  432. {
  433. theApp.m_syslog.Print(LL_INFO, "CClientThread::Execute : 接收到客户端 [%s] 状态包...n", this->m_sremoteip.c_str());
  434. this->ProcessRAGState(agrpack);
  435. }
  436. break;
  437. case RAG_VERIFYPWD:
  438. {
  439. theApp.m_syslog.Print(LL_INFO, "CClientThread::Execute : 接收到客户端 [%s] 用户身份校验包...n", this->m_sremoteip.c_str());
  440. this->ProcessRAGAuthor(agrpack);
  441. }
  442. break;
  443. case RAG_EXECUSRSQL:
  444. {
  445. theApp.m_syslog.Print(LL_INFO, "CClientThread::Execute : 接收到客户端 [%s] SQL请求包...n", this->m_sremoteip.c_str());
  446. this->ProcessRAGUsrSql(agrpack);
  447. }
  448. break;
  449. }
  450. //free the memory space
  451. if(agrpack.size > 0)
  452. {
  453. delete []agrpack.data;
  454. }
  455. Sleep(10);
  456. }
  457. }
  458. //=============================================================================
  459. //
  460. // Process the client's timed state requirement and update the database info.
  461. //
  462. //=============================================================================
  463. void CClientThread::ProcessRAGState(RAGENTPACK &agrpack)
  464. {
  465. theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGState : 开始处理客户端 [%s] 状态请求包...n", this->m_sremoteip.c_str());
  466. //////////////////////////////////////////////////////////////////////////
  467. //get hostname and hardware id.
  468. char *p = (char*)agrpack.data;
  469. char *phdid = strchr(p, '|');
  470. if(!phdid) return ;
  471. char *phostname = phdid + 1;
  472. if(!phostname) return ;
  473. *phdid = ''; phdid = p;
  474. //////////////////////////////////////////////////////////////////////////
  475. string  remotename = phostname,
  476. remoteip = this->m_sremoteip;
  477. this->m_sremotehost = (char*)phostname;
  478. bool occurerror = false, bsuccessfully = true;
  479. char sql[MAX_SQL_SIZE];
  480. char *pshdid = new char[strlen(phdid)+1];
  481. memset(pshdid, '', strlen(phdid)+1);
  482. _snprintf(pshdid, agrpack.size, "%s", phdid);
  483. m_sclienthdid = pshdid;
  484. sprintf(sql, SQL::SERVER_UPDATE_AGINFO, remotename.c_str(), remoteip.c_str(), 1, pshdid);
  485. CADODataSet rs;
  486. try
  487. {
  488. //@1---更新该客户端状态
  489. if(this->m_bfirststate)
  490. {
  491. PCLIENT_STATE pitem = new CLIENT_STATE;
  492. pitem->clienthdid = pshdid;
  493. pitem->interval = 0;
  494. m_omiclientstates.push_back(pitem);
  495. this->m_bfirststate = false;
  496. }
  497. ModifyClientState(pshdid, true);
  498. //@2---看该客户端是否已经在数据库中注册
  499. rs.SetConnection(theApp.m_dbcont);
  500. if(!rs.Open(sql))
  501. {
  502. occurerror = true; bsuccessfully = false;
  503. goto ACTION;
  504. }
  505. rs.Close();
  506. sprintf(sql, SQL::SERVER_GET_AGENTISREG, pshdid);
  507. if(!rs.Open(sql))
  508. {
  509. occurerror = true; bsuccessfully = false;
  510. goto ACTION;
  511. }
  512. //如果该客户端没有注册过则注册之...
  513. if(rs.Fields().FieldByName("CLTCOUNT").getValue().AsInteger() == 0)
  514. {
  515. rs.Close();
  516. sprintf(sql, SQL::SERVER_AGENT_REGISTER, remotename.c_str(), remoteip.c_str(), pshdid, GetClientDepart(remoteip), 1, remotename.c_str());
  517. if(!rs.Open(sql))
  518. {
  519. occurerror = true; bsuccessfully = false;
  520. goto ACTION;
  521. }
  522. }
  523. bsuccessfully = ModifyClientState(pshdid, true);
  524. ACTION:
  525. this->SendAnswer(agrpack.type, bsuccessfully);
  526. if(occurerror)
  527. {
  528. theApp.m_syslog.Print(LL_ERROR, "CClientThread::ProcessRAGState : 处理发生错误, [%s] 线程自动终止!!!n", this->m_sremoteip.c_str());
  529. this->Stop();
  530. }
  531. theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGState : 客户端 [%s] 状态请求处理完毕.n", this->m_sremoteip.c_str());
  532. if(pshdid) { delete []pshdid; pshdid = NULL; }
  533. }
  534. catch (CADOException *ex) {
  535. theApp.m_syslog.Print(LL_ERROR, "CClientThread::ProcessRAGState : 处理时发生ADO异常, [%s] 线程自动终止!!!n", this->m_sremoteip.c_str());
  536. if(pshdid) { delete []pshdid; pshdid = NULL; }
  537. this->SendAnswer(agrpack.type, false);
  538. this->Stop();
  539. delete ex;
  540. }
  541. }
  542. //============================================================
  543. //
  544. // Process the client's authentication requirement and 
  545. // return the result(OK or FAILED).
  546. //
  547. //============================================================
  548. void CClientThread::ProcessRAGAuthor(RAGENTPACK &agrpack)
  549. {
  550. string sremoteip = this->m_sremoteip;
  551. theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGAuthor : 开始处理客户端 [%s] 用户身份校验请求包...n", sremoteip.c_str());
  552. bool bsuccessful = false;
  553. if(!this->m_pcltsock || !this->m_pcltsock->Socket_IsOpened()) return ;
  554. #define JUNKUSRNAME "@#$&^!fack@you@  ^R~"
  555. #define JUNKPASSWRD "@!@#Z$&^"
  556. //TRACE0((char*)agrpack.data);
  557. AUTHORINFO authorinfo;
  558. memset(&authorinfo, 0x0, sizeof(AUTHORINFO));
  559. memcpy(&authorinfo, agrpack.data, agrpack.size);
  560. //unencrypting the user name.
  561. vncDecryptBytes((unsigned char*)authorinfo.username, ENCRYPTKEY);
  562. //unencrypting the password
  563. CPasswrd::ToText passwrd_encry(authorinfo.passwrd);
  564. char username[MAX_USR_SIZE+1], passwrd[MAXPWLEN+1], sql[MAX_SQL_SIZE];
  565. memset(username, '', sizeof(username));
  566. memcpy(username, authorinfo.username, MAX_USR_SIZE);
  567. memset(passwrd, '', sizeof(passwrd));
  568. memcpy(passwrd, (const char*)passwrd_encry, MAXPWLEN);
  569. CADODataSet rs;
  570. try
  571. {
  572. rs.SetConnection(theApp.m_dbcont);
  573. sprintf(sql, SQL::SERVER_VERIFY_SYSUSR, username, passwrd);
  574. if(!rs.Open(sql))
  575. {
  576. memcpy(authorinfo.username, JUNKUSRNAME, MAX_USR_SIZE);
  577. memcpy(authorinfo.passwrd, JUNKPASSWRD, MAXPWLEN);
  578. goto ACTION;
  579. }
  580. //if not exist then send an junk info.
  581. if(0 == rs.Fields().FieldByName("SYSUSRCNT").getValue().AsInteger())
  582. {
  583. memcpy(authorinfo.username, JUNKUSRNAME, MAX_USR_SIZE);
  584. memcpy(authorinfo.passwrd, JUNKPASSWRD, MAXPWLEN);
  585. }
  586. else
  587. {
  588. vncEncryptBytes((unsigned char*)username, ENCRYPTKEY);
  589. memset(&authorinfo, 0x0, sizeof(authorinfo));
  590. CPasswrd::FromText encryor(passwrd);
  591. memcpy(authorinfo.username, username, MAX_USR_SIZE);
  592. memcpy(authorinfo.passwrd, (const char*)encryor, MAXPWLEN);
  593. bsuccessful = true;
  594. }
  595. rs.Close();
  596. theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGAuthor : 客户端 [%s] 用户身份校验请求处理完毕.n", sremoteip.c_str());
  597. }
  598. catch (CADOException *ex) {
  599. theApp.m_syslog.Print(LL_ERROR, "CClientThread::ProcessRAGAuthor :处理时发生ADO异常!!!n");
  600. delete ex;
  601. }
  602. ACTION:
  603. if(this->SendAnswer(RAG_VERIFYPWD, bsuccessful) && bsuccessful)
  604. {
  605. if(-1 == this->m_pcltsock->SendBuf(&authorinfo, sizeof(AUTHORINFO)))
  606. {
  607. theApp.m_syslog.Print(LL_ERROR, "CClientThread::ProcessRAGAuthor : 处理时发生Socket异常, [%s] 自动退出!!!n", sremoteip.c_str());
  608. this->Stop();
  609. }
  610. }
  611. }
  612. //==========================================================================
  613. //
  614. // Process the client's SQL requirement and return the recordset file(XML).
  615. //
  616. //==========================================================================
  617. void CClientThread::ProcessRAGUsrSql(RAGENTPACK &agrpack)
  618. {
  619. if(!this->m_pcltsock || !this->m_pcltsock->Socket_IsOpened()) return ;
  620. string sremoteip = this->m_sremoteip;
  621. theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGUsrSql : 开始处理客户端 [%s] SQL处理请求包...n", sremoteip.c_str());
  622. //@1
  623. if(!this->SendAnswer(agrpack.type)) return;
  624. EXECSQLINFO execsqlinfo;
  625. memcpy(&execsqlinfo, agrpack.data, sizeof(EXECSQLINFO));
  626. //@2---query the dataset and transfer to the client.
  627. CADODataSet rs;
  628. try
  629. {
  630. rs.SetConnection(theApp.m_dbcont);
  631. if(rs.Open((const char*)execsqlinfo.sql))
  632. {
  633. //if the recordset has exists some records then 
  634. //return the recordset xml file to client peer.
  635. if(execsqlinfo.needretval)
  636. {
  637. //@1---save record set into xml file.
  638. char szRSXMLFile[_MAX_PATH];
  639. memset(szRSXMLFile, 0x0, sizeof(szRSXMLFile));
  640. theApp._GetTempFileName("xml", szRSXMLFile);
  641. rs.SaveToXMLFile((char*)szRSXMLFile);
  642. rs.Close();
  643. //@2---send the xml file and delete the temporary xml file.
  644. if(!SendFile((char*)&szRSXMLFile[0]))
  645. {
  646. theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGUsrSql : 客户端 [%s] SQL处理中文件发送失败!!!n", sremoteip.c_str());
  647. }
  648. else
  649. {
  650. theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGUsrSql : 客户端 [%s] SQL处理中文件发送成功!!!n", sremoteip.c_str());
  651. }
  652. DeleteFile((LPCSTR)szRSXMLFile);
  653. }
  654. this->SendAnswer(RAG_EXECUSRSQL, true);
  655. theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGUsrSql : 客户端 [%s] SQL处理请求处理完毕.n", sremoteip.c_str());
  656. }
  657. }
  658. catch (CADOException *ex) {
  659. theApp.m_syslog.Print(LL_ERROR, "CClientThread::ProcessRAGUsrSql : 处理时发生ADO异常!!!n");
  660. this->SendAnswer(RAG_EXECUSRSQL, false);
  661. delete ex;
  662. }
  663. }
  664. //==========================================================================
  665. //
  666. // Process the client's SQL requirement and return the recordset file(XML).
  667. // (sub procedure for sending the recordset file(XML).)
  668. //
  669. //==========================================================================
  670. bool CClientThread::SendFile(LPCSTR lpszFile)
  671. {
  672. if(!this->m_pcltsock || !this->m_pcltsock->Socket_IsOpened()) return false;
  673. //@1---open the file.
  674. HANDLE hFile = CreateFile(lpszFile, GENERIC_READ, FILE_SHARE_READ, 
  675. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  676. if(INVALID_HANDLE_VALUE == hFile)
  677. {
  678. //TRACE0("Failed to open the specialed filen");
  679. return false;
  680. }
  681. //@2--send the file size.
  682. DWORD sizehigh = 0, sizelower = 0;
  683. sizelower = GetFileSize(hFile, &sizehigh);
  684. char cRes = 'O';
  685. int size = 0, errtimes = 0;
  686. do{
  687. size = sizeof(DWORD);
  688. if(size != this->m_pcltsock->SendBuf(&sizelower, size)) ++errtimes;
  689. else{
  690. //@3--wait for client...
  691. size = sizeof(char);
  692. if(size != this->m_pcltsock->ReceiveBuf(&cRes, size)) ++errtimes;
  693. else break;
  694. }
  695. }
  696. while(errtimes < 3);
  697. //3.1
  698. if(errtimes == 3)
  699. {
  700. //TRACE0("Failed to send the file size infon");
  701. CloseHandle(hFile);
  702. return false;
  703. }
  704. //@4
  705. int   nsended = 0, maxsize = 0;
  706. DWORD dwleft  = sizelower, dwread = 0;
  707. char  buf[MAX_NET_PACKET];
  708. //need encrypting process???
  709. while(dwleft > 0)
  710. {
  711. memset((char*)buf, 0x0, sizeof(buf));
  712. maxsize = (dwleft > MAX_NET_PACKET) ? MAX_NET_PACKET : dwleft;
  713. ReadFile(hFile, buf, maxsize, &dwread, NULL);
  714. XorEncryptStream(buf, dwread);
  715. nsended = this->m_pcltsock->SendBuf(buf, dwread);
  716. if(nsended < 0)
  717. {
  718. CloseHandle(hFile);
  719. return false;
  720. }
  721. //wait for client...
  722. size = sizeof(char);
  723. if(size != this->m_pcltsock->ReceiveBuf(&cRes, size))
  724. {
  725. //TRACE0("Failed to send the file size infon");
  726. CloseHandle(hFile);
  727. return false;
  728. }
  729. if('O' == cRes){
  730. dwleft -= nsended;
  731. }
  732. else{
  733. CloseHandle(hFile);
  734. return false;
  735. }
  736. }
  737. CloseHandle(hFile);
  738. return true;
  739. }