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