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

模拟服务器

开发平台:

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.    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(
  29. addressToListenOn, 
  30. portToListenOn,
  31. maxFreeSockets,
  32. maxFreeBuffers, 
  33. bufferSize, 
  34. numThreads)
  35. {
  36. }
  37. CGameServer::CGameServer(
  38. size_t maxFreeSockets,
  39. size_t maxFreeBuffers,
  40. size_t bufferSize /* = 1024 */,
  41. size_t numThreads /* = 0 */)
  42.    : OnlineGameLib::Win32::CSocketServer(
  43. maxFreeSockets, 
  44. maxFreeBuffers, 
  45. bufferSize, 
  46. numThreads)
  47. {
  48. }
  49. CGameServer::~CGameServer()
  50. {
  51. /*
  52.  * If we want to be informed of any buffers or sockets being destroyed at destruction 
  53.  * time then we need to release these resources now, whilst we, the derived class,
  54.  * still exists. Once our destructor exits and the base destructor takes over we wont 
  55.  * get any more notifications...
  56.  */
  57. try
  58. {
  59. ReleaseSockets();
  60. ReleaseBuffers();
  61. }
  62. catch(...)
  63. {
  64. }
  65. }
  66. SOCKET CGameServer::CreateListeningSocket( unsigned long address, unsigned short port )
  67. {
  68. SOCKET s = ::WSASocket( AF_INET, SOCK_STREAM, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED );
  69. if ( INVALID_SOCKET == s )
  70. {
  71. throw CWin32Exception( _T("CGameServer::CreateListeningSocket()"), ::WSAGetLastError() );
  72. }
  73. CSocket listeningSocket( s );
  74. CSocket::InternetAddress localAddress( address, port );
  75. listeningSocket.Bind( localAddress );
  76. listeningSocket.Listen( 5 );
  77. return listeningSocket.Detatch();
  78. }
  79. OnlineGameLib::Win32::CSocketServer::WorkerThread *CGameServer::CreateWorkerThread( 
  80. OnlineGameLib::Win32::CIOCompletionPort &iocp )
  81. {
  82. Output( _T("CGameServer::CreateWorkerThread") );
  83. return new CGameServerWorkerThread( iocp );
  84. }
  85. void CGameServer::OnConnectionEstablished(
  86. OnlineGameLib::Win32::CSocketServer::Socket *pSocket, 
  87. OnlineGameLib::Win32::CIOBuffer * pAddress )
  88. {
  89. Output( _T("CGameServer::OnConnectionEstablished") );
  90. pSocket->Read();
  91. }
  92. void CGameServer::OnConnectionClosed( Socket * pSocket )
  93. {
  94. Output( _T("CGameServer::OnConnectionClosed") );
  95. }
  96. void CGameServer::OnError( const OnlineGameLib::Win32::_tstring &message )
  97. {
  98. Output( _T("CGameServer::OnError - ") + message );
  99. }
  100. void CGameServer::PreWrite( 
  101. OnlineGameLib::Win32::CSocketServer::Socket *pSocket, 
  102. OnlineGameLib::Win32::CIOBuffer *pBuffer, 
  103. const char *pData, 
  104. size_t dataLength )
  105. {
  106. }