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

模拟服务器

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. void UpdateStatusBarUsers(BOOL fGrow);
  3. CGateInfo::CGateInfo()
  4. {
  5. m_fDoSending = FALSE;
  6. memset( &OverlappedEx, 0, sizeof( OverlappedEx ) );
  7. }
  8. void CGateInfo::SendGateCheck()
  9. {
  10. _LPTSENDBUFF lpSendBuff = new _TSENDBUFF;
  11. if (lpSendBuff)
  12. {
  13. _TMSGHEADER MsgHdr;
  14. MsgHdr.nCode = 0xAA55AA55;
  15. MsgHdr.nSocket = 0;
  16. MsgHdr.wUserGateIndex = 0;
  17. MsgHdr.wIdent = GM_CHECKSERVER;
  18. MsgHdr.wUserListIndex = 0;
  19. MsgHdr.wTemp = 0;
  20. MsgHdr.nLength = 0;
  21. lpSendBuff->nLen = sizeof(_TMSGHEADER);
  22. memmove(lpSendBuff->szData, (char *)&MsgHdr, sizeof(_TMSGHEADER));
  23. lpSendBuff->szData[sizeof(_TMSGHEADER) + 1] = '';
  24. // Send(lpSendBuff);
  25. m_xSendBuffQ.PushQ((BYTE *)lpSendBuff);
  26. }
  27. }
  28. void CGateInfo::OpenNewUser(char *pszPacket)
  29. {
  30. int nIndex;
  31. _TMSGHEADER MsgHdr;
  32. _LPTMSGHEADER lpMsgHeader;
  33. nIndex = g_xUserInfoArr.GetFreeKey();
  34. if (nIndex >= 0)
  35. {
  36. CUserInfo * pUserInfo = &g_xUserInfoArr[nIndex];
  37. pUserInfo->Lock();
  38. g_xLoginOutUserInfo.AddNewNode(pUserInfo);
  39. lpMsgHeader = (_LPTMSGHEADER)pszPacket;
  40. pUserInfo->m_sock = lpMsgHeader->nSocket;
  41. pUserInfo->m_pxPlayerObject = NULL;
  42. ZeroMemory(pUserInfo->m_szUserID, sizeof(pUserInfo->m_szUserID));
  43. ZeroMemory(pUserInfo->m_szCharName, sizeof(pUserInfo->m_szCharName));
  44. pUserInfo->m_nCertification = 0;
  45. pUserInfo->m_nClientVersion = 0;
  46. pUserInfo->m_nUserGateIndex = lpMsgHeader->wUserGateIndex;
  47. pUserInfo->m_nUserServerIndex = nIndex;
  48. pUserInfo->m_pGateInfo = this;
  49. pUserInfo->m_btCurrentMode = USERMODE_NOTICE;
  50. pUserInfo->m_lpTHorseRcd = NULL;
  51. pUserInfo->m_bEmpty = false;
  52. pUserInfo->Unlock();
  53. _LPTSENDBUFF lpSendBuff = new _TSENDBUFF;
  54. MsgHdr.nCode = 0xAA55AA55;
  55. MsgHdr.nSocket = lpMsgHeader->nSocket;
  56. MsgHdr.wUserGateIndex = lpMsgHeader->wUserGateIndex;
  57. MsgHdr.wIdent = GM_SERVERUSERINDEX;
  58. MsgHdr.wUserListIndex = nIndex;
  59. MsgHdr.wTemp = 0;
  60. MsgHdr.nLength = 0;
  61. lpSendBuff->nLen = sizeof(_TMSGHEADER);
  62. memmove(lpSendBuff->szData, (char *)&MsgHdr, sizeof(_TMSGHEADER));
  63. m_xSendBuffQ.PushQ((BYTE *)lpSendBuff);
  64. // Send(lpSendBuff);
  65. /* DWORD dwBytesSends = 0;
  66. OverlappedEx[1].Buffer[sizeof(_TMSGHEADER)] = '';
  67. OverlappedEx[1].nOvFlag  = OVERLAPPED_SEND;
  68. OverlappedEx[1].DataBuf.len = sizeof(_TMSGHEADER);
  69. OverlappedEx[1].DataBuf.buf = OverlappedEx[1].Buffer;
  70. WSASend(m_sock, &OverlappedEx[1].DataBuf, 1, &dwBytesSends, 0, (OVERLAPPED *)&OverlappedEx[1], NULL);
  71. WSASend(m_sock, &OverlappedEx[1].DataBuf, 1, &dwBytesSends, 0, NULL, NULL);
  72. */
  73. UpdateStatusBarUsers(TRUE);
  74. }
  75. }
  76. void CGateInfo::xSend()
  77. {
  78. if (m_xSendBuffQ.GetCount())
  79. {
  80. DWORD dwBytesSends = 0;
  81. int nPos = 0;
  82. _LPTSENDBUFF lpSendBuff = (_LPTSENDBUFF)m_xSendBuffQ.PopQ();
  83. while (lpSendBuff)
  84. {
  85. memmove(&OverlappedEx[1].Buffer[nPos], lpSendBuff->szData, lpSendBuff->nLen);
  86. nPos += lpSendBuff->nLen;
  87. delete lpSendBuff;
  88. lpSendBuff = NULL;
  89. if (nPos >= 8192)
  90. break;
  91. lpSendBuff = (_LPTSENDBUFF)m_xSendBuffQ.PopQ();
  92. }
  93. if (nPos)
  94. {
  95. memset( &OverlappedEx[1].Overlapped, 0, sizeof( OVERLAPPED ) );
  96. OverlappedEx[1].nOvFlag = OVERLAPPED_SEND;
  97. OverlappedEx[1].DataBuf.len = nPos;
  98. OverlappedEx[1].DataBuf.buf = OverlappedEx[1].Buffer;
  99. WSASend(
  100. m_sock, 
  101. &OverlappedEx[1].DataBuf, 
  102. 1, 
  103. &dwBytesSends, 
  104. 0, 
  105. (OVERLAPPED *) &OverlappedEx[1], 
  106. // NULL, 
  107. NULL
  108. );
  109. }
  110. }
  111. }
  112. int CGateInfo::Send(_LPTSENDBUFF lpSendBuff)
  113. {
  114. DWORD dwBytesSends = 0;
  115. int nPos = 0;  
  116. int nRet = 0;
  117. if ( lpSendBuff )
  118. m_xSendBuffQ.PushQ((BYTE *)lpSendBuff);
  119. if (m_fDoSending)
  120. {   
  121. return 0;    
  122. }
  123. _LPTSENDBUFF lpSBuff = (_LPTSENDBUFF)m_xSendBuffQ.PopQ();
  124. if ( !lpSBuff )  
  125. {   
  126. return 0;    
  127. }
  128. while (lpSBuff)
  129. {
  130. memmove(&OverlappedEx[1].Buffer[nPos], lpSBuff->szData, lpSBuff->nLen);
  131. nPos += lpSBuff->nLen;
  132. delete lpSBuff;
  133. if (nPos >= 4096)
  134. break;
  135. lpSBuff = (_LPTSENDBUFF)m_xSendBuffQ.PopQ();
  136. }
  137. if ( nPos )
  138. {
  139. memset( &OverlappedEx[1].Overlapped, 0, sizeof( OVERLAPPED ) );
  140. OverlappedEx[1].nOvFlag = OVERLAPPED_SEND;
  141. OverlappedEx[1].DataBuf.len = nPos;
  142. OverlappedEx[1].DataBuf.buf = OverlappedEx[1].Buffer;
  143. int nRet =WSASend(m_sock, &OverlappedEx[1].DataBuf, 1, 
  144. &dwBytesSends, 0, (OVERLAPPED *) &OverlappedEx[1], NULL);
  145. m_fDoSending = TRUE;
  146. }
  147. return nRet;
  148. }
  149. int CGateInfo::Recv()
  150. {
  151. DWORD nRecvBytes = 0, nFlags = 0;
  152. OverlappedEx[0].nOvFlag = OVERLAPPED_RECV;
  153. OverlappedEx[0].DataBuf.len = DATA_BUFSIZE - OverlappedEx[0].bufLen;
  154. OverlappedEx[0].DataBuf.buf = OverlappedEx[0].Buffer + OverlappedEx[0].bufLen;
  155. memset( &OverlappedEx[0].Overlapped, 0, sizeof( OverlappedEx[0].Overlapped ) );
  156. return WSARecv( m_sock, &OverlappedEx[0].DataBuf, 1, &nRecvBytes, &nFlags, &OverlappedEx[0].Overlapped, 0 );
  157. }
  158. bool CGateInfo::HasCompletionPacket()
  159. {
  160. if ( OverlappedEx[0].bufLen < sizeof( _TMSGHEADER ) )
  161. return false;
  162. _LPTMSGHEADER pMsgHeader = (_LPTMSGHEADER) &OverlappedEx[0].Buffer;
  163. return (unsigned) OverlappedEx[0].bufLen >= sizeof( _TMSGHEADER ) + pMsgHeader->nLength;
  164. }
  165. char * CGateInfo::ExtractPacket( char *pPacket )
  166. {
  167. int packetLen = sizeof( _TMSGHEADER ) + ((_LPTMSGHEADER) &OverlappedEx[0].Buffer)->nLength;
  168. memcpy( pPacket, OverlappedEx[0].Buffer, packetLen );
  169. memmove( OverlappedEx[0].Buffer, OverlappedEx[0].Buffer + packetLen, DATA_BUFSIZE - packetLen );
  170. OverlappedEx[0].bufLen -= packetLen;
  171. return pPacket + packetLen;
  172. }