MainProcess.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #include "StdAfx.h"
  2. #include "MainProcess.h"
  3. #include "WinService.h"
  4. #include "OnlineUser.h"
  5. #include <time.h>
  6. #include "CritSection.h"
  7. #include "msg.pb.h"
  8. #include "table.pb.h"
  9. time_t g_systm;
  10. // 服务通信对象;
  11. CMainProcess* CMainProcess::m_spThis = NULL;
  12. CMainProcess::CMainProcess(void)
  13. {
  14. m_hBackupDatabase = NULL;
  15. m_hCheckServices = NULL;
  16. m_hEvent1 = NULL;
  17. m_hEvent2 = NULL;
  18. // 创建监听器对象
  19. m_pListener = ::Create_HP_TcpServerListener();
  20. // 创建 Socket 对象
  21. m_pServer = ::Create_HP_TcpPackServer(m_pListener);
  22. // 设置 Socket 监听器回调函数
  23. ::HP_Set_FN_Server_OnPrepareListen(m_pListener, OnPrepareListen);
  24. ::HP_Set_FN_Server_OnAccept(m_pListener, OnAccept);
  25. ::HP_Set_FN_Server_OnSend(m_pListener, OnSend);
  26. ::HP_Set_FN_Server_OnReceive(m_pListener, OnReceive);
  27. ::HP_Set_FN_Server_OnClose(m_pListener, OnClose);
  28. ::HP_Set_FN_Server_OnShutdown(m_pListener, OnShutdown);
  29. // 获取机器时间;
  30. time(&g_systm); // g_systm = time(NULL);
  31. }
  32. CMainProcess::~CMainProcess(void)
  33. {
  34. // 销毁 Socket 对象
  35. ::Destroy_HP_TcpPackServer(m_spThis->m_pServer);
  36. // 销毁监听器对象
  37. ::Destroy_HP_TcpServerListener(m_pListener);
  38. }
  39. En_HP_HandleResult __stdcall CMainProcess::OnPrepareListen(SOCKET soListen)
  40. {
  41. TCHAR szAddress[40];
  42. int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
  43. USHORT usPort;
  44. ::HP_Server_GetListenAddress(m_spThis->m_pServer, szAddress, &iAddressLen, &usPort);
  45. ::PostOnPrepareListen(szAddress, usPort);
  46. return HR_OK;
  47. }
  48. En_HP_HandleResult __stdcall CMainProcess::OnAccept( HP_CONNID dwConnID, SOCKET soClient)
  49. {
  50. BOOL bPass = TRUE;
  51. TCHAR szAddress[40];
  52. int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
  53. USHORT usPort;
  54. ::HP_Server_GetRemoteAddress(m_spThis->m_pServer, dwConnID, szAddress, &iAddressLen, &usPort);
  55. if (!m_spThis->m_strAddress.IsEmpty())
  56. {
  57. if (m_spThis->m_strAddress.CompareNoCase(szAddress) == 0)
  58. bPass = FALSE;
  59. }
  60. //::PostOnAccept(dwConnID, szAddress, usPort, bPass);
  61. TRACE(_T("新客户端连接:%d, %s, %d!\n"), dwConnID, szAddress, usPort);
  62. return bPass ? HR_OK : HR_ERROR;
  63. }
  64. En_HP_HandleResult __stdcall CMainProcess::OnSend( HP_CONNID dwConnID, const BYTE * pData, int iLength)
  65. {
  66. TRACE(_T("发送数据给客户端:%d, %s, %d!\n"), dwConnID, pData, iLength);
  67. //::PostOnSend(dwConnID, pData, iLength);
  68. return HR_OK;
  69. }
  70. En_HP_HandleResult __stdcall CMainProcess::OnReceive( HP_CONNID dwConnID, const BYTE * pData, int iLength)
  71. {
  72. TheProPackage thepackage;
  73. memcpy(&thepackage, pData, sizeof(TheProPackage));
  74. EnHandleResult nResult = HR_OK;
  75. BYTE *pSendData = NULL;
  76. DWORD dwSendLen = 0;
  77. switch (thepackage.nCmd)
  78. {
  79. // 客户端请求登录;
  80. case C2CCMD_REQ_LOGIN:
  81. {
  82. Req_Login(dwConnID, pData, iLength, thepackage, pSendData, dwSendLen);
  83. }
  84. break;
  85. // 客户端请求登出;
  86. case C2CCMD_REQ_LOGOUT:
  87. {
  88. Req_LogOut(dwConnID, pData, iLength, thepackage, pSendData, dwSendLen);
  89. }
  90. break;
  91. // 客户端请求添加部门;
  92. case C2CCMD_NEW_DEPARTMENT:
  93. {
  94. New_Department(dwConnID, pData, iLength, thepackage, pSendData, dwSendLen);
  95. }
  96. break;
  97. // 客户端请求删除部门;
  98. case C2CCMD_DEL_DEPARTMENT:
  99. {
  100. Del_Department(dwConnID, pData, iLength, thepackage, pSendData, dwSendLen);
  101. }
  102. break;
  103. // 客户端请求修改部门;
  104. case C2CCMD_MOD_DEPARTMENT:
  105. {
  106. // 暂未使用;
  107. Mod_Department(dwConnID, pData, iLength, thepackage, pSendData, dwSendLen);
  108. }
  109. break;
  110. // 客户端请求查询部门;
  111. case C2CCMD_QRY_DEPARTMENT:
  112. {
  113. nResult = Ask_Department(dwConnID, pData, iLength, thepackage, pSendData, dwSendLen);
  114. if (nResult != HR_IGNORE)
  115. return nResult;
  116. }
  117. break;
  118. //////////////////////////////////////////////////////////////////////////
  119. default:
  120. break;
  121. }
  122. BOOL bSendResult = ::HP_Server_Send(m_spThis->m_pServer, dwConnID, pSendData, dwSendLen);
  123. if (pSendData)
  124. delete[]pSendData;
  125. return bSendResult ? HR_OK : HR_ERROR;
  126. }
  127. En_HP_HandleResult __stdcall CMainProcess::OnClose( HP_CONNID dwConnID, En_HP_SocketOperation enOperation, int iErrorCode)
  128. {
  129. // 移除登录的用户信息;
  130. COnlineUser::GetInstance()->EraseOnlineUser(dwConnID);
  131. iErrorCode == SE_OK ? ::PostOnClose(dwConnID) : ::PostOnError(dwConnID, enOperation, iErrorCode);
  132. return HR_OK;
  133. }
  134. En_HP_HandleResult __stdcall CMainProcess::OnShutdown()
  135. {
  136. ::PostOnShutdown();
  137. return HR_OK;
  138. }
  139. void CMainProcess::StartWork()
  140. {
  141. m_hEvent1 = CreateEvent(NULL, TRUE, FALSE, NULL);
  142. if (m_hEvent1 == NULL)
  143. {
  144. //_tprintf_s(_T("创建事件失败\n"));
  145. return;
  146. }
  147. m_hEvent2 = CreateEvent(NULL, TRUE, FALSE, NULL);
  148. if (m_hEvent2 == NULL)
  149. {
  150. //_tprintf_s(_T("创建事件失败2\n"));
  151. return;
  152. }
  153. m_hCheckServices = CreateThread(NULL, 0, CheckSvcStatusThread, this, 0, NULL);
  154. if (m_hCheckServices == NULL)
  155. {
  156. SetEvent(m_hEvent1);
  157. SetEvent(m_hEvent2);
  158. CloseHandle(m_hEvent1);
  159. CloseHandle(m_hEvent2);
  160. m_hEvent1 = NULL;
  161. m_hEvent2 = NULL;
  162. //_tprintf_s(_T("创建线程失败\n"));
  163. return;
  164. }
  165. m_hBackupDatabase = CreateThread(NULL, 0, BackupDatabaseThread, this, 0, NULL);
  166. if (m_hBackupDatabase == NULL)
  167. {
  168. SetEvent(m_hEvent1);
  169. SetEvent(m_hEvent2);
  170. CloseHandle(m_hEvent1);
  171. CloseHandle(m_hEvent2);
  172. m_hEvent1 = NULL;
  173. m_hEvent2 = NULL;
  174. WaitForSingleObject(m_hCheckServices, INFINITE);
  175. if (m_hBackupDatabase)
  176. CloseHandle(m_hBackupDatabase);
  177. m_hBackupDatabase = NULL;
  178. //_tprintf_s(_T("创建线程失败2\n"));
  179. return;
  180. }
  181. Start();
  182. }
  183. void CMainProcess::EndofWork()
  184. {
  185. if ( m_hEvent1 )
  186. {
  187. SetEvent(m_hEvent1);
  188. CloseHandle(m_hEvent1);
  189. }
  190. if ( m_hEvent2 )
  191. {
  192. SetEvent(m_hEvent2);
  193. CloseHandle(m_hEvent2);
  194. }
  195. if ( m_hCheckServices )
  196. {
  197. WaitForSingleObject(m_hCheckServices,INFINITE);
  198. CloseHandle(m_hCheckServices);
  199. }
  200. if ( m_hBackupDatabase )
  201. {
  202. WaitForSingleObject(m_hBackupDatabase,INFINITE);
  203. CloseHandle(m_hBackupDatabase);
  204. }
  205. Stop();
  206. }
  207. DWORD CMainProcess::CheckSvcStatusThread(LPVOID lpVoid)
  208. {
  209. CMainProcess *pthis = (CMainProcess*)lpVoid;
  210. do
  211. {
  212. } while ( WaitForSingleObject(pthis->m_hEvent1, 200) == WAIT_TIMEOUT );
  213. return 0;
  214. }
  215. DWORD CMainProcess::BackupDatabaseThread(LPVOID lpVoid)
  216. {
  217. CMainProcess *pthis = (CMainProcess*)lpVoid;
  218. do
  219. {
  220. } while ( WaitForSingleObject(pthis->m_hEvent2, 200) == WAIT_TIMEOUT );
  221. return 0;
  222. }
  223. BOOL CMainProcess::Start()
  224. {
  225. if (m_spThis == NULL)
  226. return FALSE;
  227. ::HP_TcpPackServer_SetMaxPackSize(m_spThis->m_pServer, 0x3FFFFF);
  228. ::HP_TcpPackServer_SetPackHeaderFlag(m_spThis->m_pServer, 0x3FE);
  229. if (::HP_Server_Start(m_spThis->m_pServer, _T("0.0.0.0"), Global::g_dwSvrPort))
  230. {
  231. //::LogServerStart(ADDRESS, PORT);
  232. OutputDebugString(_T("服务器启动成功!\n"));
  233. //SetAppState(ST_STARTED);
  234. }
  235. else
  236. {
  237. //::LogServerStartFail(::HP_Server_GetLastError(m_spThis->m_pServer), ::HP_Server_GetLastErrorDesc(m_spThis->m_pServer));
  238. OutputDebugString(_T("服务器启动失败!\n"));
  239. //SetAppState(ST_STOPPED);
  240. }
  241. return TRUE;
  242. }
  243. BOOL CMainProcess::Stop()
  244. {
  245. if (::HP_Server_Stop(m_spThis->m_pServer))
  246. {
  247. OutputDebugString(_T("停止服务器成功!\n"));
  248. ::LogServerStop();
  249. //SetAppState(ST_STOPPED);
  250. }
  251. else
  252. {
  253. ASSERT(FALSE);
  254. }
  255. return TRUE;
  256. }
  257. BOOL CMainProcess::Disconnect(IN const DWORD & dwConnId)
  258. {
  259. if (::HP_Server_Disconnect(m_spThis->m_pServer, dwConnId, TRUE))
  260. {
  261. // 移除登录的用户信息;
  262. COnlineUser::GetInstance()->EraseOnlineUser(dwConnId);
  263. ::LogDisconnect(dwConnId);
  264. }
  265. else
  266. {
  267. ::LogDisconnectFail(dwConnId);
  268. }
  269. return TRUE;
  270. }
  271. //////////////////////////////////////////////////////////////////////////
  272. void CMainProcess::Req_Login(IN HP_CONNID dwConnID, IN const BYTE* pReceiveData, IN INT nReceiveLength, IN TheProPackage &thepackage, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  273. {
  274. msg_login msg;
  275. msg.ParseFromArray(thepackage.byBody, thepackage.nDataLen);
  276. if (COnlineUser::GetInstance()->IsAccountExist(msg.phone().c_str()) == -1)
  277. {// 用户没在线;
  278. // 查询数据库是否存在用户;
  279. INT nRet = USER_NULL;
  280. //CUserDetail tagUserDetail;
  281. //nRet = g_pODBCPool->QueryUserDetail(thepackage.szStudioId, thepackage.szAccount, thepackage.szPassword, &tagUserDetail);
  282. nRet = 0;
  283. if (nRet == USER_LOGIN)
  284. {
  285. CMemFile mf;
  286. CArchive ar(&mf, CArchive::store);
  287. //tagUserDetail.Serialize(ar);
  288. ar.Close();
  289. DWORD dwArchive = mf.GetLength();
  290. dwSendLen = sizeof(TheProPackage) - 2 + dwArchive;
  291. pSendData = new BYTE[dwSendLen];
  292. TheProPackage *pPackage = (TheProPackage*)pSendData;
  293. pPackage->flag = PACKAGE_ONESELF;
  294. //pPackage->nCmd = thepackage.nCmd;
  295. pPackage->nDataLen = dwArchive;
  296. pPackage->dwReserve = USER_LOGIN;
  297. BYTE *pMf = mf.Detach();
  298. memcpy(pPackage->byBody, pMf, dwArchive);
  299. if (pMf)
  300. delete pMf;
  301. // 记录在线用户;
  302. COnlineUser::GetInstance()->InsertOnlineUser(dwConnID, msg.phone().c_str());
  303. }
  304. else
  305. {
  306. dwSendLen = sizeof(TheProPackage);
  307. pSendData = new BYTE[dwSendLen];
  308. TheProPackage *pPackage = (TheProPackage*)pSendData;
  309. pPackage->flag = PACKAGE_ONESELF;
  310. pPackage->nCmd = C2CCMD_REQ_LOGIN;
  311. pPackage->nDataLen = 0;
  312. pPackage->dwReserve = nRet;
  313. OutputDebugString(_T("用户密码不对\n"));
  314. }
  315. }
  316. else
  317. {
  318. dwSendLen = sizeof(TheProPackage);
  319. pSendData = new BYTE[dwSendLen];
  320. TheProPackage *pPackage = (TheProPackage*)pSendData;
  321. pPackage->flag = PACKAGE_ONESELF;
  322. pPackage->nCmd = C2CCMD_REQ_LOGIN;
  323. pPackage->nDataLen = 0;
  324. pPackage->dwReserve = USER_HAVE_LOGIN;
  325. OutputDebugString(_T("用户已登录\n"));
  326. }
  327. }
  328. void CMainProcess::Req_LogOut(IN HP_CONNID dwConnID, IN const BYTE* pReceiveData, IN INT nReceiveLength, IN TheProPackage &thepackage, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  329. {
  330. }
  331. void CMainProcess::New_Department(IN HP_CONNID dwConnID, IN const BYTE* pReceiveData, IN INT nReceiveLength, IN TheProPackage &thepackage, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  332. {
  333. /*CMemFile mf;
  334. //mf.Attach(thepackage.szBody, thepackage.nDataLen);//错误方式;
  335. mf.Attach(const_cast<BYTE*>(pReceiveData)+sizeof(TheProPackage) - 2, thepackage.nDataLen);
  336. CArchive ar(&mf, CArchive::load);
  337. CAddDepartment tagAddDepartment;
  338. tagAddDepartment.Serialize(ar);
  339. ar.Close();
  340. mf.Detach();
  341. CString strSQL;
  342. strSQL.Format(_T("INSERT INTO [DepartmentInfo](")
  343. _T("[StudioID], [DepartmentName], [DepartmentNote]) values ('%s', '%s', '%s')"),
  344. thepackage.szStudioId, tagAddDepartment.m_strDepartmentName, tagAddDepartment.m_strDepartmentNote);
  345. BOOL bRet = g_pODBCPool->ExecuteSQL(strSQL);
  346. // 返回信息;
  347. dwSendLen = sizeof(TheProPackage);
  348. pSendData = new BYTE[dwSendLen];
  349. TheProPackage *pPackage = (TheProPackage*)pSendData;
  350. pPackage->flag = PACKAGE_ONESELF;
  351. pPackage->nCmd = thepackage.nCmd;
  352. pPackage->nDataLen = 0;
  353. pPackage->dwReserve = bRet;*/
  354. }
  355. void CMainProcess::Del_Department(IN HP_CONNID dwConnID, IN const BYTE* pReceiveData, IN INT nReceiveLength, IN TheProPackage &thepackage, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  356. {
  357. /*CMemFile mf;
  358. //mf.Attach(thepackage.szBody, thepackage.nDataLen);//错误方式;
  359. mf.Attach(const_cast<BYTE*>(pReceiveData)+sizeof(TheProPackage) - 2, thepackage.nDataLen);
  360. CArchive ar(&mf, CArchive::load);
  361. CAddDepartment tagAddDepartment;
  362. tagAddDepartment.Serialize(ar);
  363. ar.Close();
  364. mf.Detach();
  365. CString strSQL;
  366. strSQL.Format(_T("DELETE FROM [DepartmentInfo] WHERE [StudioID] = '%s' and [DepartmentName] = '%s' "), thepackage.szStudioId, tagAddDepartment.m_strDepartmentName);
  367. BOOL bRet = g_pODBCPool->ExecuteSQL(strSQL);
  368. // 返回信息;
  369. dwSendLen = sizeof(TheProPackage);
  370. pSendData = new BYTE[dwSendLen];
  371. TheProPackage *pPackage = (TheProPackage*)pSendData;
  372. pPackage->flag = PACKAGE_ONESELF;
  373. pPackage->nCmd = thepackage.nCmd;
  374. pPackage->nDataLen = 0;
  375. pPackage->dwReserve = bRet;*/
  376. }
  377. void CMainProcess::Mod_Department(IN HP_CONNID dwConnID, IN const BYTE* pReceiveData, IN INT nReceiveLength, IN TheProPackage &thepackage, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  378. {
  379. }
  380. EnHandleResult CMainProcess::Ask_Department(IN HP_CONNID dwConnID, IN const BYTE* pReceiveData, IN INT nReceiveLength, IN TheProPackage &thepackage, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  381. {
  382. CObList tagDpmList;
  383. INT nRet = 0;
  384. CMemFile mf;
  385. CArchive ar(&mf, CArchive::store);
  386. tagDpmList.Serialize(ar);
  387. ar.Close();
  388. // 删除列表对象;
  389. POSITION pos = tagDpmList.GetHeadPosition();
  390. while (pos)
  391. {
  392. LPVOID *pdpmObj = NULL;
  393. if (pdpmObj)
  394. delete pdpmObj;
  395. }
  396. tagDpmList.RemoveAll();
  397. BOOL bResult = FALSE;
  398. __int64 nArchive = mf.GetLength();
  399. if ((nArchive + sizeof(TheProPackage) - 2) > 0x3FFFFF)
  400. {
  401. __int64 nSubLen = nArchive;
  402. BYTE *pMf = mf.Detach();
  403. while (nSubLen > 0)
  404. {
  405. if (nSubLen - 0x3FFFFF + sizeof(TheProPackage) - 2 > 0)
  406. dwSendLen = 0x3FFFFF;
  407. else
  408. dwSendLen = nSubLen + sizeof(TheProPackage) - 2;
  409. pSendData = new BYTE[dwSendLen];
  410. TheProPackage *pPackage = (TheProPackage*)pSendData;
  411. pPackage->flag = PACKAGE_MULTIPLE;
  412. pPackage->nCmd = thepackage.nCmd;
  413. pPackage->nDataLen = nArchive;
  414. pPackage->nSubDataLen = (nSubLen - 0x3FFFFF + sizeof(TheProPackage) - 2) > 0 ? (0x3FFFFF - sizeof(TheProPackage) + 2) : nSubLen;
  415. pPackage->dwReserve = nRet;
  416. memcpy(pPackage->byBody, pMf + nArchive - nSubLen, pPackage->nSubDataLen);
  417. nSubLen -= (0x3FFFFF - sizeof(TheProPackage) + 2);
  418. bResult = ::HP_Server_Send(m_spThis->m_pServer, dwConnID, pSendData, dwSendLen);
  419. if (pSendData)
  420. delete[]pSendData;
  421. pSendData = NULL;
  422. if (!bResult)
  423. {
  424. if (pMf)
  425. delete pMf;
  426. return HR_ERROR;
  427. }
  428. }
  429. if (pMf)
  430. delete pMf;
  431. return HR_OK;
  432. }
  433. else
  434. {// 单包;
  435. dwSendLen = sizeof(TheProPackage) - 2 + nArchive;
  436. pSendData = new BYTE[dwSendLen];
  437. TheProPackage *pPackage = (TheProPackage*)pSendData;
  438. pPackage->flag = PACKAGE_ONESELF;
  439. pPackage->nCmd = thepackage.nCmd;
  440. pPackage->nDataLen = nArchive;
  441. pPackage->dwReserve = nRet;
  442. BYTE *pMf = mf.Detach();
  443. memcpy(pPackage->byBody, pMf, nArchive);
  444. if (pMf)
  445. delete pMf;
  446. }
  447. return HR_IGNORE;
  448. }