ServerPtr.cpp 11 KB

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