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

模拟服务器

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. extern HWND g_hStatusBar;
  3. extern SOCKET g_gcSock;
  4. UINT WINAPI ThreadFuncForMsg(LPVOID lpParameter);
  5. extern HANDLE g_hIOCP;
  6. extern BOOL g_fTerminated;
  7. CWHList<CGateInfo*> g_xGateList;
  8. CWHList<GAMESERVERINFO*> g_xGameServerList;
  9. CWHList<GATESERVERINFO*> g_xGateServerList;
  10. char g_szServerList[1024];
  11. unsigned long g_hThreadForMsg = 0;
  12. void UpdateStatusBar(BOOL fGrow)
  13. {
  14. static long nNumOfCurrSession = 0;
  15. TCHAR szText[20];
  16. (fGrow ? InterlockedIncrement(&nNumOfCurrSession) : InterlockedDecrement(&nNumOfCurrSession));
  17. wsprintf(szText, _TEXT("%d Sessions"), nNumOfCurrSession);
  18. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(3, 0), (LPARAM)szText);
  19. }
  20. BOOL InitServerThreadForMsg()
  21. {
  22. UINT dwThreadIDForMsg = 0;
  23. if (!g_hThreadForMsg)
  24. {
  25. g_hThreadForMsg = _beginthreadex(NULL, 0, ThreadFuncForMsg, NULL, 0, &dwThreadIDForMsg);
  26. if (g_hThreadForMsg)
  27. return TRUE;
  28. }
  29. return FALSE;
  30. }
  31. DWORD WINAPI AcceptThread(LPVOID lpParameter)
  32. {
  33. int nLen = sizeof(SOCKADDR_IN);
  34. SOCKET Accept;
  35. SOCKADDR_IN Address;
  36. while (TRUE)
  37. {
  38. Accept = accept(g_gcSock, (struct sockaddr FAR *)&Address, &nLen);
  39. if (g_fTerminated)
  40. return 0;
  41. CGateInfo* pGateInfo = new CGateInfo;
  42. if (pGateInfo)
  43. {
  44. pGateInfo->sock = Accept;
  45. CreateIoCompletionPort((HANDLE)pGateInfo->sock, g_hIOCP, (DWORD)pGateInfo, 0);
  46. if (g_xGateList.AddNewNode(pGateInfo))
  47. {
  48. int zero = 0;
  49. setsockopt(pGateInfo->sock, SOL_SOCKET, SO_SNDBUF, (char *)&zero, sizeof(zero) );
  50. pGateInfo->Recv();
  51. UpdateStatusBar(TRUE);
  52. #ifdef _DEBUG
  53. TCHAR szGateIP[256];
  54. wsprintf(szGateIP, _T("%d.%d.%d.%d"), Address.sin_addr.s_net, Address.sin_addr.s_host, 
  55. Address.sin_addr.s_lh, Address.sin_addr.s_impno);
  56. InsertLogMsgParam(IDS_ACCEPT_GATESERVER, szGateIP);
  57. #endif
  58. }
  59. }
  60. }
  61. return 0;
  62. }
  63. void CloseGate(CGateInfo* pGateInfo)
  64. {
  65. }
  66. DWORD WINAPI ServerWorkerThread(LPVOID CompletionPortID)
  67. {
  68. DWORD dwBytesTransferred = 0;
  69. CGateInfo* pGateInfo = NULL;
  70. LPOVERLAPPED lpOverlapped = NULL;
  71. char szTmp[DATA_BUFSIZE];
  72. while (TRUE)
  73. {
  74. if ( GetQueuedCompletionStatus(
  75. (HANDLE)CompletionPortID, 
  76. &dwBytesTransferred, 
  77. (LPDWORD)&pGateInfo, 
  78. (LPOVERLAPPED *)&lpOverlapped, 
  79. INFINITE) == 0 )
  80. {
  81. return 0;
  82. }
  83. if (g_fTerminated)
  84. {
  85. PLISTNODE pListNode;
  86. if (g_xGateList.GetCount())
  87. {
  88. pListNode = g_xGateList.GetHead();
  89. while (pListNode)
  90. {
  91. pGateInfo = g_xGateList.GetData(pListNode);
  92. if (pGateInfo)
  93. pGateInfo->Close();
  94. delete pGateInfo;
  95. pGateInfo = NULL;
  96. pListNode = g_xGateList.RemoveNode(pListNode);
  97. }
  98. }
  99. return 0;
  100. }
  101. if ( dwBytesTransferred == 0 )
  102. {
  103. pGateInfo->Close();
  104. continue;
  105. }
  106. pGateInfo->bufLen += dwBytesTransferred;
  107. while ( pGateInfo->HasCompletionPacket() )
  108. {
  109. *(pGateInfo->ExtractPacket( szTmp ) - 1) = '';
  110. switch ( szTmp[1] )
  111. {
  112. case '-':
  113. pGateInfo->SendKeepAlivePacket();
  114. break;
  115. case 'A':
  116. pGateInfo->ReceiveSendUser(&szTmp[2]);
  117. break;
  118. case 'O':
  119. pGateInfo->ReceiveOpenUser(&szTmp[2]);
  120. break;
  121. case 'X':
  122. pGateInfo->ReceiveCloseUser(&szTmp[2]);
  123. break;
  124. case 'S':
  125. pGateInfo->ReceiveServerMsg(&szTmp[2]);
  126. break;
  127. case 'M':
  128. pGateInfo->MakeNewUser(&szTmp[2]);
  129. break;
  130. }
  131. }
  132. if ( pGateInfo->Recv() == SOCKET_ERROR && WSAGetLastError() != ERROR_IO_PENDING )
  133. {
  134. InsertLogMsg(_TEXT("WSARecv() failed"));
  135. continue;
  136. }
  137. }
  138. return 0;
  139. }