GameClient.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:3k
源码类别:

模拟服务器

开发平台:

C/C++

  1. #include "GameClient.h"
  2. //[ Include in ...ESClient
  3. #include "ESClientUtils.h"
  4. #include "ESClientWin32Exception.h"
  5. #include "ESClientSocket.h"
  6. #include "ESClientException.h"
  7. //]
  8. #include "Protocol.h"
  9. #include "JXClient.h"
  10. #pragma comment (lib, "ESClient.lib")
  11. /*
  12.  * Using directives
  13.  */
  14. using OnlineGameLib::Win32::CIOBuffer;
  15. using OnlineGameLib::Win32::Output;
  16. using OnlineGameLib::Win32::OutPutInfo;
  17. using OnlineGameLib::Win32::ToString;
  18. using OnlineGameLib::Win32::_tstring;
  19. using OnlineGameLib::Win32::CException;
  20. using OnlineGameLib::Win32::CWin32Exception;
  21. using OnlineGameLib::Win32::CSocket;
  22. using OnlineGameLib::Win32::DumpData;
  23. CGameClient::CGameClient(
  24. const OnlineGameLib::Win32::_tstring &addressToConnectServer,
  25. unsigned short portToConnectServer,
  26. size_t maxFreeBuffers,
  27. size_t bufferSize /* = 1024 */
  28. )
  29.   : OnlineGameLib::Win32::CSocketClient(addressToConnectServer, portToConnectServer, maxFreeBuffers, bufferSize)
  30. {
  31. }
  32. CGameClient::CGameClient(
  33. size_t maxFreeBuffers,
  34. size_t bufferSize /* = 1024 */
  35. )
  36. : OnlineGameLib::Win32::CSocketClient(maxFreeBuffers, bufferSize)
  37. {
  38. }
  39. CGameClient::~CGameClient()
  40. {
  41. try
  42. {
  43. ReleaseBuffers();
  44. }
  45. catch(...)
  46. {
  47. }
  48. }
  49. SOCKET CGameClient::CreateConnectionSocket( 
  50.   const OnlineGameLib::Win32::_tstring &addressToConnectServer,
  51.   unsigned short port)
  52. {
  53. SOCKET s = ::WSASocket( AF_INET, SOCK_STREAM, IPPROTO_IP, NULL, 0, 0 );
  54. if ( INVALID_SOCKET == s )
  55. {
  56. throw CWin32Exception( _T("CSocket::CreateListeningSocket()"), ::WSAGetLastError() );
  57. }
  58. CSocket connectionSocket( s );
  59. CSocket::InternetAddress localAddress( addressToConnectServer, port );
  60. connectionSocket.Connect( localAddress );
  61. return connectionSocket.Detatch();
  62. }
  63. void CGameClient::OnStartConnections()
  64. {
  65. Output( _T("OnStartConnections") );
  66. }
  67. void CGameClient::OnStopConnections()
  68. {
  69. DisConnetServer();
  70. Output( _T("OnStopConnections") );
  71. }
  72. void CGameClient::OnShutdownInitiated()
  73. {
  74. Output( _T("OnShutdownInitiated") );
  75. }
  76. void CGameClient::OnShutdownComplete()
  77. {
  78. Output( _T("OnShutdownComplete") );
  79. }
  80. void CGameClient::OnError( const OnlineGameLib::Win32::_tstring &message )
  81. {
  82. Output( _T("OnError - ") + message );
  83. }
  84. void CGameClient::OnConnect()
  85. {
  86. ConnectServer();
  87. Output( _T("OnConnect") );
  88. }
  89. void CGameClient::OnClose()
  90. {
  91. Output( _T("OnClose") );
  92. }
  93. void CGameClient::ReadCompleted( OnlineGameLib::Win32::CIOBuffer *pBuffer )
  94. {
  95. try
  96. {
  97. const BYTE *pPackData = pBuffer->GetBuffer();
  98. const size_t used = pBuffer->GetUsed();
  99. RecvFromServer( reinterpret_cast<const char*>( pPackData ), used );
  100. pBuffer->Empty();
  101. }
  102. catch(const CException &e)
  103. {
  104. Output( _T("ReadCompleted - Exception - ") + e.GetWhere() + _T(" - ") + e.GetMessage() );
  105. StopConnections();
  106. }
  107. catch(...)
  108. {
  109. Output( _T("ReadCompleted - Unexpected exception") );
  110. StopConnections();
  111. }
  112. }
  113. void CGameClient::PreWrite( 
  114. OnlineGameLib::Win32::CIOBuffer *pBuffer, 
  115. const char *pData, 
  116. size_t dataLength )
  117. {
  118. /*
  119.  * TODO : You can change protocol that it is used to split package
  120.  */
  121. if ( pBuffer && dataLength > 0 )
  122. {
  123. PACK_HEADER ph = {0};
  124. ph.cPackBeginFlag = PACK_BEGIN_FLAG;
  125. ph.cPackEndFlag = PACK_END_FLAG;
  126. ph.wDataLen = dataLength;
  127. ph.wCRCData = MAKE_CRC_DATE( PACK_BEGIN_FLAG, PACK_END_FLAG, dataLength );
  128. pBuffer->AddData( reinterpret_cast<const char *>(&ph), PACK_HEADER_LEN );
  129. }
  130. }