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