MainProcess.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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, msg_login(), 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 msg_login &msg, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  273. {
  274. if (COnlineUser::GetInstance()->IsAccountExist(msg.phone().c_str()) == -1)
  275. {// 用户没在线;
  276. // 查询数据库是否存在用户;
  277. INT nRet = USER_NULL;
  278. //CUserDetail tagUserDetail;
  279. //nRet = g_pODBCPool->QueryUserDetail(thepackage.szStudioId, thepackage.szAccount, thepackage.szPassword, &tagUserDetail);
  280. nRet = 0;
  281. if (nRet == USER_LOGIN)
  282. {
  283. CMemFile mf;
  284. CArchive ar(&mf, CArchive::store);
  285. //tagUserDetail.Serialize(ar);
  286. ar.Close();
  287. DWORD dwArchive = mf.GetLength();
  288. dwSendLen = sizeof(TheProPackage) - 2 + dwArchive;
  289. pSendData = new BYTE[dwSendLen];
  290. TheProPackage *pPackage = (TheProPackage*)pSendData;
  291. pPackage->flag = PACKAGE_ONESELF;
  292. //pPackage->nCmd = thepackage.nCmd;
  293. pPackage->nDataLen = dwArchive;
  294. pPackage->dwReserve = USER_LOGIN;
  295. BYTE *pMf = mf.Detach();
  296. memcpy(pPackage->byBody, pMf, dwArchive);
  297. if (pMf)
  298. delete pMf;
  299. // 记录在线用户;
  300. COnlineUser::GetInstance()->InsertOnlineUser(dwConnID, msg.phone().c_str());
  301. }
  302. else
  303. {
  304. dwSendLen = sizeof(TheProPackage);
  305. pSendData = new BYTE[dwSendLen];
  306. TheProPackage *pPackage = (TheProPackage*)pSendData;
  307. pPackage->flag = PACKAGE_ONESELF;
  308. pPackage->nCmd = C2CCMD_REQ_LOGIN;
  309. pPackage->nDataLen = 0;
  310. pPackage->dwReserve = nRet;
  311. OutputDebugString(_T("用户密码不对\n"));
  312. }
  313. }
  314. else
  315. {
  316. dwSendLen = sizeof(TheProPackage);
  317. pSendData = new BYTE[dwSendLen];
  318. TheProPackage *pPackage = (TheProPackage*)pSendData;
  319. pPackage->flag = PACKAGE_ONESELF;
  320. pPackage->nCmd = C2CCMD_REQ_LOGIN;
  321. pPackage->nDataLen = 0;
  322. pPackage->dwReserve = USER_HAVE_LOGIN;
  323. OutputDebugString(_T("用户已登录\n"));
  324. }
  325. }
  326. void CMainProcess::Req_LogOut(IN HP_CONNID dwConnID, IN const BYTE* pReceiveData, IN INT nReceiveLength, IN TheProPackage &thepackage, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  327. {
  328. }
  329. void CMainProcess::New_Department(IN HP_CONNID dwConnID, IN const BYTE* pReceiveData, IN INT nReceiveLength, IN TheProPackage &thepackage, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  330. {
  331. /*CMemFile mf;
  332. //mf.Attach(thepackage.szBody, thepackage.nDataLen);//错误方式;
  333. mf.Attach(const_cast<BYTE*>(pReceiveData)+sizeof(TheProPackage) - 2, thepackage.nDataLen);
  334. CArchive ar(&mf, CArchive::load);
  335. CAddDepartment tagAddDepartment;
  336. tagAddDepartment.Serialize(ar);
  337. ar.Close();
  338. mf.Detach();
  339. CString strSQL;
  340. strSQL.Format(_T("INSERT INTO [DepartmentInfo](")
  341. _T("[StudioID], [DepartmentName], [DepartmentNote]) values ('%s', '%s', '%s')"),
  342. thepackage.szStudioId, tagAddDepartment.m_strDepartmentName, tagAddDepartment.m_strDepartmentNote);
  343. BOOL bRet = g_pODBCPool->ExecuteSQL(strSQL);
  344. // 返回信息;
  345. dwSendLen = sizeof(TheProPackage);
  346. pSendData = new BYTE[dwSendLen];
  347. TheProPackage *pPackage = (TheProPackage*)pSendData;
  348. pPackage->flag = PACKAGE_ONESELF;
  349. pPackage->nCmd = thepackage.nCmd;
  350. pPackage->nDataLen = 0;
  351. pPackage->dwReserve = bRet;*/
  352. }
  353. void CMainProcess::Del_Department(IN HP_CONNID dwConnID, IN const BYTE* pReceiveData, IN INT nReceiveLength, IN TheProPackage &thepackage, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  354. {
  355. /*CMemFile mf;
  356. //mf.Attach(thepackage.szBody, thepackage.nDataLen);//错误方式;
  357. mf.Attach(const_cast<BYTE*>(pReceiveData)+sizeof(TheProPackage) - 2, thepackage.nDataLen);
  358. CArchive ar(&mf, CArchive::load);
  359. CAddDepartment tagAddDepartment;
  360. tagAddDepartment.Serialize(ar);
  361. ar.Close();
  362. mf.Detach();
  363. CString strSQL;
  364. strSQL.Format(_T("DELETE FROM [DepartmentInfo] WHERE [StudioID] = '%s' and [DepartmentName] = '%s' "), thepackage.szStudioId, tagAddDepartment.m_strDepartmentName);
  365. BOOL bRet = g_pODBCPool->ExecuteSQL(strSQL);
  366. // 返回信息;
  367. dwSendLen = sizeof(TheProPackage);
  368. pSendData = new BYTE[dwSendLen];
  369. TheProPackage *pPackage = (TheProPackage*)pSendData;
  370. pPackage->flag = PACKAGE_ONESELF;
  371. pPackage->nCmd = thepackage.nCmd;
  372. pPackage->nDataLen = 0;
  373. pPackage->dwReserve = bRet;*/
  374. }
  375. void CMainProcess::Mod_Department(IN HP_CONNID dwConnID, IN const BYTE* pReceiveData, IN INT nReceiveLength, IN TheProPackage &thepackage, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  376. {
  377. }
  378. EnHandleResult CMainProcess::Ask_Department(IN HP_CONNID dwConnID, IN const BYTE* pReceiveData, IN INT nReceiveLength, IN TheProPackage &thepackage, OUT BYTE*& pSendData, OUT DWORD &dwSendLen)
  379. {
  380. CObList tagDpmList;
  381. INT nRet = 0;
  382. CMemFile mf;
  383. CArchive ar(&mf, CArchive::store);
  384. tagDpmList.Serialize(ar);
  385. ar.Close();
  386. // 删除列表对象;
  387. POSITION pos = tagDpmList.GetHeadPosition();
  388. while (pos)
  389. {
  390. LPVOID *pdpmObj = NULL;
  391. if (pdpmObj)
  392. delete pdpmObj;
  393. }
  394. tagDpmList.RemoveAll();
  395. BOOL bResult = FALSE;
  396. __int64 nArchive = mf.GetLength();
  397. if ((nArchive + sizeof(TheProPackage) - 2) > 0x3FFFFF)
  398. {
  399. __int64 nSubLen = nArchive;
  400. BYTE *pMf = mf.Detach();
  401. while (nSubLen > 0)
  402. {
  403. if (nSubLen - 0x3FFFFF + sizeof(TheProPackage) - 2 > 0)
  404. dwSendLen = 0x3FFFFF;
  405. else
  406. dwSendLen = nSubLen + sizeof(TheProPackage) - 2;
  407. pSendData = new BYTE[dwSendLen];
  408. TheProPackage *pPackage = (TheProPackage*)pSendData;
  409. pPackage->flag = PACKAGE_MULTIPLE;
  410. pPackage->nCmd = thepackage.nCmd;
  411. pPackage->nDataLen = nArchive;
  412. pPackage->nSubDataLen = (nSubLen - 0x3FFFFF + sizeof(TheProPackage) - 2) > 0 ? (0x3FFFFF - sizeof(TheProPackage) + 2) : nSubLen;
  413. pPackage->dwReserve = nRet;
  414. memcpy(pPackage->byBody, pMf + nArchive - nSubLen, pPackage->nSubDataLen);
  415. nSubLen -= (0x3FFFFF - sizeof(TheProPackage) + 2);
  416. bResult = ::HP_Server_Send(m_spThis->m_pServer, dwConnID, pSendData, dwSendLen);
  417. if (pSendData)
  418. delete[]pSendData;
  419. pSendData = NULL;
  420. if (!bResult)
  421. {
  422. if (pMf)
  423. delete pMf;
  424. return HR_ERROR;
  425. }
  426. }
  427. if (pMf)
  428. delete pMf;
  429. return HR_OK;
  430. }
  431. else
  432. {// 单包;
  433. dwSendLen = sizeof(TheProPackage) - 2 + nArchive;
  434. pSendData = new BYTE[dwSendLen];
  435. TheProPackage *pPackage = (TheProPackage*)pSendData;
  436. pPackage->flag = PACKAGE_ONESELF;
  437. pPackage->nCmd = thepackage.nCmd;
  438. pPackage->nDataLen = nArchive;
  439. pPackage->dwReserve = nRet;
  440. BYTE *pMf = mf.Detach();
  441. memcpy(pPackage->byBody, pMf, nArchive);
  442. if (pMf)
  443. delete pMf;
  444. }
  445. return HR_IGNORE;
  446. }