SockMsg_GateComm.cpp
上传用户:tt_chan
上传日期:2009-12-03
资源大小:4523k
文件大小:6k
源码类别:

模拟服务器

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. void UpdateStatusBarSession(BOOL fGrow)
  3. {
  4. static long nNumOfCurrSession = 0;
  5. TCHAR szText[20];
  6. (fGrow ? InterlockedIncrement(&nNumOfCurrSession) : InterlockedDecrement(&nNumOfCurrSession));
  7. wsprintf(szText, _TEXT("%d Sessions"), nNumOfCurrSession);
  8. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(1, 0), (LPARAM)szText);
  9. }
  10. void UpdateStatusBarUsers(BOOL fGrow)
  11. {
  12. static long nNumOfUsers = 0;
  13. TCHAR szText[20];
  14. (fGrow ? InterlockedIncrement(&nNumOfUsers) : InterlockedDecrement(&nNumOfUsers));
  15. wsprintf(szText, _TEXT("%d Users"), nNumOfUsers);
  16. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(2, 0), (LPARAM)szText);
  17. }
  18. DWORD WINAPI AcceptThread(LPVOID lpParameter)
  19. {
  20. int nLen = sizeof(SOCKADDR_IN);
  21. SOCKET Accept;
  22. SOCKADDR_IN Address;
  23. TCHAR szGateIP[16];
  24. while (TRUE)
  25. {
  26. nLen = sizeof( Address );
  27. Accept = WSAAccept( g_ssock, (SOCKADDR *) &Address, &nLen, NULL, 0 );
  28. if (g_fTerminated) return 0L;
  29. CGateInfo* pGateInfo = new CGateInfo;
  30. if (pGateInfo)
  31. {
  32. pGateInfo->m_sock = Accept;
  33. CreateIoCompletionPort((HANDLE)pGateInfo->m_sock, g_hIOCP, (DWORD)pGateInfo, 0);
  34. if (g_xGateList.AddNewNode(pGateInfo))
  35. {
  36. int zero = 0;
  37. setsockopt(pGateInfo->m_sock, SOL_SOCKET, SO_SNDBUF, (char *)&zero, sizeof(zero) );
  38. ZeroMemory(&(pGateInfo->OverlappedEx), sizeof(OVERLAPPED) * 2);
  39. pGateInfo->OverlappedEx[1].nOvFlag = OVERLAPPED_SEND;
  40. pGateInfo->Recv();
  41. UpdateStatusBarSession(TRUE);
  42. _stprintf(szGateIP, _T("%d.%d.%d.%d"), Address.sin_addr.s_net, Address.sin_addr.s_host, 
  43. Address.sin_addr.s_lh, Address.sin_addr.s_impno);
  44. InsertLogMsgParam(IDS_ACCEPT_GATESERVER, szGateIP, LOGPARAM_STR);
  45. }
  46. }
  47. }
  48. return 0;
  49. }
  50. DWORD WINAPI ServerWorkerThread(LPVOID CompletionPortID)
  51. {
  52. DWORD dwBytesTransferred;
  53. CGateInfo* pGateInfo;
  54. LPOVERLAPPEDEX lpOverlapped;
  55. _LPTMSGHEADER pMsgHeader;
  56. char completionPacket[DATA_BUFSIZE];
  57. while (TRUE)
  58. {
  59. GetQueuedCompletionStatus((HANDLE)CompletionPortID, &dwBytesTransferred, (LPDWORD)&pGateInfo, (LPOVERLAPPED *)&lpOverlapped, INFINITE);
  60. if (g_fTerminated) return 0L;
  61. if (dwBytesTransferred == 0)
  62. {
  63. if (g_xUserInfoList.GetCount())
  64. {
  65. PLISTNODE pListNode = g_xUserInfoList.GetHead();
  66. while (pListNode)
  67. {
  68. CUserInfo *pUserInfo = g_xUserInfoList.GetData(pListNode);
  69. if (pUserInfo->m_pGateInfo == pGateInfo)
  70. {
  71. pUserInfo->Lock();
  72. pUserInfo->m_bEmpty = true;
  73. pUserInfo->Unlock();
  74. // 绊媚具 凳
  75. if (pUserInfo->m_pxPlayerObject)
  76. pUserInfo->m_pxPlayerObject->m_pMap->RemoveObject(pUserInfo->m_pxPlayerObject->m_nCurrX,
  77. pUserInfo->m_pxPlayerObject->m_nCurrY,
  78. OS_MOVINGOBJECT,
  79. pUserInfo->m_pxPlayerObject);
  80. pListNode = g_xUserInfoList.RemoveNode(pListNode);
  81. UpdateStatusBarUsers(FALSE);
  82. }
  83. else
  84. pListNode = g_xUserInfoList.GetNext(pListNode);
  85. }
  86. }
  87. closesocket(pGateInfo->m_sock);
  88. g_xGateList.RemoveNodeByData(pGateInfo);
  89. if (pGateInfo) delete pGateInfo;
  90. continue;
  91. }
  92. if (lpOverlapped->nOvFlag == OVERLAPPED_RECV)
  93. {
  94. static DWORD nLastTick = GetTickCount();
  95. static DWORD nBytes = 0;
  96. nBytes += dwBytesTransferred;
  97. if ( GetTickCount() - nLastTick >= 1000)
  98. {
  99. TCHAR buf[256];
  100. wsprintf( buf, _T("R: %d bytes/sec"), nBytes );
  101. nLastTick = GetTickCount();
  102. nBytes = 0;
  103. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(3, 0), (LPARAM)buf);
  104. }
  105. pGateInfo->OverlappedEx[0].bufLen += dwBytesTransferred;
  106. while ( pGateInfo->HasCompletionPacket() )
  107. {
  108. *(pGateInfo->ExtractPacket( completionPacket )) = '';
  109. pMsgHeader = (_LPTMSGHEADER) completionPacket;
  110. if ( pMsgHeader->nCode != 0xAA55AA55 )
  111. continue;
  112. switch ( pMsgHeader->wIdent )
  113. {
  114. case GM_OPEN:
  115. {
  116. pGateInfo->OpenNewUser( completionPacket );
  117. break;
  118. }
  119. case GM_CLOSE:
  120. {
  121. CUserInfo *pUserInfo = &g_xUserInfoArr[ pMsgHeader->wUserListIndex ];
  122. if (pUserInfo)
  123. {
  124. pUserInfo->m_btCurrentMode = USERMODE_LOGOFF;
  125. g_xLoginOutUserInfo.AddNewNode(pUserInfo);
  126. }
  127. break;
  128. }
  129. case GM_CHECKCLIENT:
  130. {
  131. pGateInfo->SendGateCheck();
  132. break;
  133. }
  134. case GM_RECEIVE_OK:
  135. {
  136. break;
  137. }
  138. case GM_DATA:
  139. {
  140. CUserInfo *pUserInfo = &g_xUserInfoArr[ pMsgHeader->wUserListIndex ];
  141. if ( !pUserInfo->IsEmpty() )
  142. {
  143. if ( pUserInfo->m_btCurrentMode == USERMODE_PLAYGAME)
  144. {
  145. if ( pMsgHeader->nSocket == pUserInfo->m_sock )
  146. pUserInfo->ProcessUserMessage(completionPacket + sizeof( _TMSGHEADER ) );
  147. }
  148. else
  149. {
  150. pUserInfo->Lock();
  151. pUserInfo->DoClientCertification( completionPacket + sizeof( _TMSGHEADER ) + sizeof(_TDEFAULTMESSAGE) );
  152. pUserInfo->Unlock();
  153. }
  154. }
  155. break;
  156. }
  157. } // switch
  158. } // while
  159. if ( pGateInfo->Recv() == SOCKET_ERROR && WSAGetLastError() != ERROR_IO_PENDING )
  160. InsertLogMsg( _T("WSARecv() failed") );
  161. } // if (OVERLAPPED_RECV)
  162. else if (lpOverlapped->nOvFlag == OVERLAPPED_SEND)
  163. {
  164. static DWORD nLastTick = GetTickCount();
  165. static DWORD nBytes = 0;
  166. nBytes += dwBytesTransferred;
  167. if ( GetTickCount() - nLastTick >= 1000)
  168. {
  169. TCHAR buf[256];
  170. wsprintf( buf, _T("S: %d bytes/sec"), nBytes );
  171. nLastTick = GetTickCount();
  172. nBytes = 0;
  173. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(4, 0), (LPARAM)buf);
  174. }
  175. /* fprintf( fp, "sendedrn" );
  176. pGateInfo->m_fDoSending = FALSE;
  177. if ( pGateInfo->Send( NULL ) == SOCKET_ERROR && WSAGetLastError() != ERROR_IO_PENDING )
  178. InsertLogMsg( _T("WSASend() failed") ); */
  179. }
  180. }
  181. return 0;
  182. }