ServerCommSockMsg.cpp
上传用户:szopptop
上传日期:2013-04-23
资源大小:1047k
文件大小:7k
源码类别:

模拟服务器

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. extern HWND g_hStatusBar;
  3. extern HANDLE g_hIOCP;
  4. extern SOCKET g_ssock;
  5. extern BOOL g_fTerminated;
  6. CWHList<CServerInfo*> g_xServerList;
  7. CWHQueue g_DBMsgQ;
  8. CWHQueue g_ServerMsgQ;
  9. void UpdateStatusBarSession(BOOL fGrow)
  10. {
  11. static long nNumOfCurrSession = 0;
  12. TCHAR szText[20];
  13. (fGrow ? InterlockedIncrement(&nNumOfCurrSession) : InterlockedDecrement(&nNumOfCurrSession));
  14. wsprintf(szText, _TEXT("%d Sessions"), nNumOfCurrSession);
  15. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(3, 0), (LPARAM)szText);
  16. }
  17. DWORD WINAPI AcceptThread(LPVOID lpParameter)
  18. {
  19. int nLen = sizeof(SOCKADDR_IN);
  20. SOCKET Accept;
  21. SOCKADDR_IN Address;
  22. DWORD dwRecvBytes;
  23. DWORD dwFlags;
  24. TCHAR szGateIP[16];
  25. while (TRUE)
  26. {
  27. Accept = accept(g_ssock, (struct sockaddr FAR *)&Address, &nLen);
  28. if (g_fTerminated) return 0L;
  29. CServerInfo* pServerInfo = (CServerInfo*)GlobalAlloc(GPTR, sizeof(CServerInfo));
  30. if (pServerInfo)
  31. {
  32. pServerInfo->m_sock = Accept;
  33. CreateIoCompletionPort((HANDLE)pServerInfo->m_sock, g_hIOCP, (DWORD)pServerInfo, 0);
  34. if (g_xServerList.AddNewNode(pServerInfo))
  35. {
  36. int zero = 0;
  37. setsockopt(pServerInfo->m_sock, SOL_SOCKET, SO_SNDBUF, (char *)&zero, sizeof(zero));
  38. ZeroMemory(&(pServerInfo->Overlapped), sizeof(OVERLAPPED));
  39. pServerInfo->DataBuf.len = DATA_BUFSIZE;
  40. pServerInfo->DataBuf.buf = pServerInfo->Buffer;
  41. pServerInfo->nOvFlag = OVERLAPPED_RECV;
  42. dwFlags = 0;
  43. WSARecv(pServerInfo->m_sock, &(pServerInfo->DataBuf), 1, &dwRecvBytes, &dwFlags, &(pServerInfo->Overlapped), NULL);
  44. UpdateStatusBarSession(TRUE);
  45. _stprintf(szGateIP, _T("%d.%d.%d.%d"), Address.sin_addr.s_net, Address.sin_addr.s_host, 
  46. Address.sin_addr.s_lh, Address.sin_addr.s_impno);
  47. InsertLogMsgParam(IDS_ACCEPT_GATESERVER, szGateIP, LOGPARAM_STR);
  48. }
  49. }
  50. }
  51. return 0;
  52. }
  53. DWORD WINAPI ServerWorkerThread(LPVOID CompletionPortID)
  54. {
  55. DWORD dwBytesTransferred;
  56. CServerInfo* pServerInfo;
  57. LPOVERLAPPED lpOverlapped;
  58. DWORD dwFlags;
  59. DWORD dwRecvBytes;
  60. char szBuff[DATA_BUFSIZE];
  61. int nBuffLen;
  62. char *pszFirst, *pszEnd, *pszDivide;
  63. LONG lValid;
  64. WORD w1, w2;
  65. char szCC[32];
  66. int nPos;
  67. while (TRUE)
  68. {
  69. GetQueuedCompletionStatus((HANDLE)CompletionPortID, &dwBytesTransferred, (LPDWORD)&pServerInfo, (LPOVERLAPPED *)&lpOverlapped, INFINITE);
  70. if (g_fTerminated) return 0L;
  71. if (dwBytesTransferred == 0)
  72. {
  73. closesocket(pServerInfo->m_sock);
  74. if (pServerInfo) GlobalFree(pServerInfo);
  75. UpdateStatusBarSession(FALSE);
  76. continue;
  77. }
  78. if (pServerInfo->nOvFlag == OVERLAPPED_RECV)
  79. {
  80. if (pServerInfo->nRemainBuffLen)
  81. {
  82. memmove(szBuff, pServerInfo->RemainBuff, pServerInfo->nRemainBuffLen); // Copy previous remain data in szBuff
  83. memmove(&szBuff[pServerInfo->nRemainBuffLen], pServerInfo->Buffer, dwBytesTransferred + 1); // Append new received data in szBuff
  84. nBuffLen = pServerInfo->nRemainBuffLen + dwBytesTransferred;
  85. pszEnd = &szBuff[0];
  86. }
  87. else
  88. {
  89. pszEnd = &pServerInfo->Buffer[0];
  90. nBuffLen = dwBytesTransferred;
  91. }
  92. while (nBuffLen)
  93. {
  94. if ((pszFirst = (char *)memchr(pszEnd, '#', nBuffLen)) && (pszEnd = (char *)memchr(pszFirst, '!', nBuffLen)))
  95. {
  96. *pszEnd++ = '';
  97. if (pszDivide = (char *)memchr(pszFirst, '/', pszEnd - pszFirst))
  98. {
  99. *pszDivide++ = '';
  100. _LPTSENDBUFF lpSendUserData = new _TSENDBUFF;
  101. if (lpSendUserData)
  102. {
  103. lpSendUserData->nCertification = AnsiStrToVal(pszFirst + 1);
  104. w1 = lpSendUserData->nCertification ^ 0xAA;
  105. w2 = memlen(pszDivide) - 1;
  106. lValid = MAKELONG(w1, w2);
  107. nPos = fnEncode6BitBufA((unsigned char *)&lValid, szCC, sizeof(LONG), sizeof(szCC));
  108. szCC[nPos] = '';
  109. if (memcmp((pszEnd - nPos - 1), szCC, nPos) == 0)
  110. {
  111. fnDecodeMessageA(&lpSendUserData->DefMsg, pszDivide);
  112. *(pszEnd - nPos - 1) = '';
  113. if (lpSendUserData->DefMsg.wIdent >= 100 && lpSendUserData->DefMsg.wIdent <= 200)
  114. {
  115. lpSendUserData->pServerInfo = pServerInfo;
  116. switch (lpSendUserData->DefMsg.wIdent)
  117. {
  118. case DB_MAKEITEMRCD2:
  119. {
  120. lpSendUserData->lpbtAddData = new BYTE[sizeof(_TMAKEITEMRCD)];
  121. fnDecode6BitBufA((pszDivide + DEFBLOCKSIZE), (char *)lpSendUserData->lpbtAddData, sizeof(_TMAKEITEMRCD));
  122. lpSendUserData->lpbtAddData2 = NULL;
  123. break;
  124. }
  125. default:
  126. {
  127. nPos = fnDecode6BitBufA((pszDivide + DEFBLOCKSIZE), (char *)&lpSendUserData->HumanLoad, sizeof(lpSendUserData->HumanLoad));
  128. switch (lpSendUserData->DefMsg.wIdent)
  129. {
  130. case DB_MAKEITEMRCD:
  131. {
  132. lpSendUserData->lpbtAddData = new BYTE[sizeof(_TMAKEITEMRCD)];
  133. fnDecode6BitBufA((pszDivide + DEFBLOCKSIZE + 75), (char *)lpSendUserData->lpbtAddData, sizeof(_TMAKEITEMRCD));
  134. lpSendUserData->lpbtAddData2 = NULL;
  135. break;
  136. }
  137. case DB_SAVEHUMANRCD:
  138. {
  139. lpSendUserData->lpbtAddData = new BYTE[sizeof(_THUMANRCD)];
  140. fnDecode6BitBufA((pszDivide + DEFBLOCKSIZE + 75), (char *)lpSendUserData->lpbtAddData, sizeof(_THUMANRCD));
  141. int nRemainLen = (int)(pszEnd - pszFirst) - (DEFBLOCKSIZE + 75 + HUMANRCDBLOCKSIZE);
  142. lpSendUserData->lpbtAddData2 = new BYTE[nRemainLen + 1];
  143. memcpy(lpSendUserData->lpbtAddData2, (pszDivide + DEFBLOCKSIZE + 75 + HUMANRCDBLOCKSIZE), nRemainLen);
  144. lpSendUserData->lpbtAddData2[nRemainLen] = '';
  145. break;
  146. }
  147. default:
  148. lpSendUserData->lpbtAddData = NULL;
  149. lpSendUserData->lpbtAddData2 = NULL;
  150. }
  151. break;
  152. }
  153. }
  154. g_DBMsgQ.PushQ((BYTE *)lpSendUserData);
  155. }
  156. else
  157. g_ServerMsgQ.PushQ((BYTE *)lpSendUserData);
  158. }
  159. else
  160. delete lpSendUserData;
  161. }
  162. }
  163. nBuffLen -= (pszEnd - pszFirst);
  164. }
  165. else
  166. break;
  167. } // while
  168. if (pszFirst && nBuffLen)
  169. {
  170. memmove(pServerInfo->RemainBuff, pszFirst, nBuffLen);
  171. pServerInfo->nRemainBuffLen = nBuffLen;
  172. #ifdef _DEBUG
  173. _RPT2(_CRT_WARN, "Remain Packet : %d, %sn", nBuffLen, pszFirst);
  174. #endif
  175. }
  176. else
  177. pServerInfo->nRemainBuffLen = 0;
  178. // Set next overlapped Process
  179. dwFlags = 0;
  180. ZeroMemory(&(pServerInfo->Overlapped), sizeof(OVERLAPPED));
  181. pServerInfo->DataBuf.len = DATA_BUFSIZE;
  182. pServerInfo->DataBuf.buf = pServerInfo->Buffer;
  183. pServerInfo->nOvFlag = OVERLAPPED_RECV;
  184. if (WSARecv(pServerInfo->m_sock, &(pServerInfo->DataBuf), 1, &dwRecvBytes, &dwFlags, &(pServerInfo->Overlapped), NULL) == SOCKET_ERROR)
  185. {
  186. if (WSAGetLastError() != ERROR_IO_PENDING)
  187. {
  188. InsertLogMsg(_T("WSARecv() failed"));
  189. // CloseSession(pServerInfo);
  190. continue;
  191. }
  192. }
  193. } // if (OVERLAPPED_RECV)
  194. }
  195. return 0;
  196. }