Server.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_SERVER_H
  13. #define SE_INCL_SERVER_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #include <Engine/Base/Synchronization.h>
  18. #include <Engine/Network/NetworkMessage.h>
  19. #include <Engine/Network/SessionState.h>
  20. #include <Engine/Templates/StaticArray.h>
  21. /*
  22. * Server, manages game joining and similar, routes messages from PlayerSource to PlayerTarget
  23. */
  24. class CServer {
  25. public:
  26. BOOL srv_bActive; // set if started
  27. CStaticArray<CSessionSocket> srv_assoSessions; // client computers in game
  28. CStaticArray<CPlayerBuffer> srv_aplbPlayers; // player buffers for all clients in game
  29. CStaticArray<CSyncCheck> srv_ascChecks; // remembered sync checks
  30. TIME srv_tmLastProcessedTick; // last tick when all actions have been resent
  31. INDEX srv_iLastProcessedSequence; // sequence of last sent game stream block
  32. BOOL srv_bPause; // set while game is paused
  33. BOOL srv_bGameFinished; // set while game is finished
  34. FLOAT srv_fServerStep; // counter for smooth time slowdown/speedup
  35. public:
  36. /* Send disconnect message to some client. */
  37. void SendDisconnectMessage(INDEX iClient, const char *strExplanation, BOOL bStream = FALSE);
  38. /* Get total number of active players. */
  39. INDEX GetPlayersCount(void);
  40. /* Get number of active vip players. */
  41. INDEX GetVIPPlayersCount(void);
  42. /* Get total number of active clients. */
  43. INDEX GetClientsCount(void);
  44. /* Get number of active vip clients. */
  45. INDEX GetVIPClientsCount(void);
  46. /* Get number of active observers. */
  47. INDEX GetObserversCount(void);
  48. /* Get number of active players on one client. */
  49. INDEX GetPlayersCountForClient(INDEX iClient);
  50. /* Find first inactive player. */
  51. CPlayerBuffer *FirstInactivePlayer(void);
  52. /* Check if some character name already exists in this session. */
  53. BOOL CharacterNameIsUsed(CPlayerCharacter &pcCharacter);
  54. /* Send initialization info to local client. */
  55. void ConnectLocalSessionState(INDEX iClient, CNetworkMessage &nm);
  56. /* Send initialization info to remote client. */
  57. void ConnectRemoteSessionState(INDEX iClient, CNetworkMessage &nm);
  58. /* Send session state data to remote client. */
  59. void SendSessionStateData(INDEX iClient);
  60. /* Send one regular batch of sequences to a client. */
  61. void SendGameStreamBlocks(INDEX iClient);
  62. /* Resend a batch of game stream blocks to a client. */
  63. void ResendGameStreamBlocks(INDEX iClient, INDEX iSequence0, INDEX ctSequences);
  64. // add a new sync check to buffer
  65. void AddSyncCheck(const CSyncCheck &sc);
  66. // try to find a sync check for given time in the buffer (-1==too old, 0==found, 1==toonew)
  67. INDEX FindSyncCheck(TIME tmTick, CSyncCheck &sc);
  68. // make allaction messages for one tick
  69. void MakeAllActions(void);
  70. // add a block to streams for all sessions
  71. void AddBlockToAllSessions(CNetworkStreamBlock &nsb);
  72. // find a mask of all players on a certain client
  73. ULONG MaskOfPlayersOnClient(INDEX iClient);
  74. public:
  75. /* Constructor. */
  76. CServer(void);
  77. /* Destructor. */
  78. ~CServer();
  79. /* Start server. */
  80. void Start_t(void);
  81. /* Stop server. */
  82. void Stop(void);
  83. /* Run server loop. */
  84. void ServerLoop(void);
  85. /* Make synchronization test message and add it to game stream. */
  86. void MakeSynchronisationCheck(void);
  87. /* Handle incoming network messages. */
  88. void HandleAll();
  89. void HandleAllForAClient(INDEX iClient);
  90. void HandleClientDisconected(INDEX iClient);
  91. void Handle(INDEX iClient, CNetworkMessage &nm);
  92. };
  93. #endif /* include-once check. */