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

模拟服务器

开发平台:

C/C++

  1. #include "GameServer.h"
  2. #include "GameServerWorkerThread.h"
  3. //[ Include in ...IPCPServer
  4. #include "IOCPServerUtils.h"
  5. #include "IOCPServerWin32Exception.h"
  6. #include "IOCPServerSocket.h"
  7. //]
  8. //[ Include in ...Protocol
  9. #include "ProtocolProtocol.h"
  10. //]
  11. /*
  12.  * Using directives
  13.  */
  14. using OnlineGameLib::Win32::CIOCompletionPort;
  15. using OnlineGameLib::Win32::CIOBuffer;
  16. using OnlineGameLib::Win32::Output;
  17. using OnlineGameLib::Win32::ToString;
  18. using OnlineGameLib::Win32::_tstring;
  19. using OnlineGameLib::Win32::CWin32Exception;
  20. using OnlineGameLib::Win32::CSocket;
  21. CGameServer::CGameServer(
  22.    const std::string &welcomeMessage,
  23.    unsigned long addressToListenOn,
  24.    unsigned short portToListenOn,
  25.    size_t maxFreeSockets,
  26.    size_t maxFreeBuffers,
  27.    size_t bufferSize /* = 1024 */,
  28.    size_t numThreads /* = 0 */)
  29.    : OnlineGameLib::Win32::CSocketServer(addressToListenOn, portToListenOn, maxFreeSockets, maxFreeBuffers, bufferSize, numThreads),
  30.  m_welcomeMessage(welcomeMessage)
  31. {
  32. }
  33. CGameServer::CGameServer(
  34. const std::string &welcomeMessage,
  35. size_t maxFreeSockets,
  36. size_t maxFreeBuffers,
  37. size_t bufferSize /* = 1024 */,
  38. size_t numThreads /* = 0 */)
  39.    : OnlineGameLib::Win32::CSocketServer(maxFreeSockets, maxFreeBuffers, bufferSize, numThreads),
  40.  m_welcomeMessage(welcomeMessage)
  41. {
  42. }
  43. CGameServer::~CGameServer()
  44. {
  45. /*
  46.  * If we want to be informed of any buffers or sockets being destroyed at destruction 
  47.  * time then we need to release these resources now, whilst we, the derived class,
  48.  * still exists. Once our destructor exits and the base destructor takes over we wont 
  49.  * get any more notifications...
  50.  */
  51. try
  52. {
  53. ReleaseSockets();
  54. ReleaseBuffers();
  55. }
  56. catch(...)
  57. {
  58. }
  59. }
  60. SOCKET CGameServer::CreateListeningSocket( unsigned long address, unsigned short port )
  61. {
  62. SOCKET s = ::WSASocket( AF_INET, SOCK_STREAM, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED );
  63. if ( INVALID_SOCKET == s )
  64. {
  65. throw CWin32Exception( _T("CSocket::CreateListeningSocket()"), ::WSAGetLastError() );
  66. }
  67. CSocket listeningSocket( s );
  68. CSocket::InternetAddress localAddress( address, port );
  69. listeningSocket.Bind( localAddress );
  70. listeningSocket.Listen( 5 );
  71. return listeningSocket.Detatch();
  72. }
  73. OnlineGameLib::Win32::CSocketServer::WorkerThread *CGameServer::CreateWorkerThread( 
  74. OnlineGameLib::Win32::CIOCompletionPort &iocp )
  75. {
  76. Output( _T("CreateWorkerThread") );
  77. return new CGameServerWorkerThread( iocp );
  78. }
  79. void CGameServer::OnStartAcceptingConnections()
  80. {
  81. Output( _T("OnStartAcceptingConnections") );
  82. }
  83. void CGameServer::OnStopAcceptingConnections()
  84. {
  85. Output( _T("OnStopAcceptingConnections") );
  86. }
  87.       
  88. void CGameServer::OnShutdownInitiated()
  89. {
  90. Output( _T("OnShutdownInitiated") );
  91. }
  92.       
  93. void CGameServer::OnShutdownComplete()
  94. {
  95. Output( _T("OnShutdownComplete") );
  96. }
  97. void CGameServer::OnConnectionEstablished(
  98. OnlineGameLib::Win32::CSocketServer::Socket *pSocket, 
  99. OnlineGameLib::Win32::CIOBuffer * pAddress )
  100. {
  101. Output( _T("OnConnectionEstablished") );
  102. pSocket->Write( m_welcomeMessage.c_str(), m_welcomeMessage.length() );
  103. pSocket->Read();
  104. }
  105. void CGameServer::OnConnectionClosed( Socket * pSocket )
  106. {
  107. Output( _T("OnConnectionClosed") );
  108. }
  109. void CGameServer::OnConnectionCreated()
  110. {
  111. Output( _T("OnConnectionCreated") );
  112. }
  113. void CGameServer::OnConnectionDestroyed()
  114. {
  115. Output( _T("OnConnectionDestroyed") );
  116. }
  117. void CGameServer::OnError( const OnlineGameLib::Win32::_tstring &message )
  118. {
  119. Output( _T("OnError - ") + message );
  120. }
  121. void CGameServer::PreWrite( 
  122. OnlineGameLib::Win32::CSocketServer::Socket *pSocket, 
  123. OnlineGameLib::Win32::CIOBuffer *pBuffer, 
  124. const char *pData, 
  125. size_t dataLength )
  126. {
  127. /*
  128.  * TODO : You can change protocol that it is used to split package
  129.  */
  130. if ( pBuffer && dataLength > 0 )
  131. {
  132. PACK_HEADER ph = {0};
  133. ph.cPackBeginFlag = PACK_BEGIN_FLAG;
  134. ph.cPackEndFlag = PACK_END_FLAG;
  135. ph.wDataLen = dataLength;
  136. ph.wCRCData = MAKE_CRC_DATE( PACK_BEGIN_FLAG, PACK_END_FLAG, dataLength );
  137. pBuffer->AddData( reinterpret_cast<const char *>(&ph), PACK_HEADER_LEN );
  138. }
  139. }