CServerHandler.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * CServerHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../lib/CStopWatch.h"
  12. #include "../lib/network/NetworkInterface.h"
  13. #include "../lib/StartInfo.h"
  14. #include "../lib/gameState/GameStatistics.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CConnection;
  17. class PlayerColor;
  18. struct StartInfo;
  19. struct TurnTimerInfo;
  20. class CMapInfo;
  21. class CGameState;
  22. struct ClientPlayer;
  23. struct CPack;
  24. struct CPackForLobby;
  25. struct CPackForClient;
  26. class HighScoreParameter;
  27. VCMI_LIB_NAMESPACE_END
  28. class CClient;
  29. class CBaseForLobbyApply;
  30. class GlobalLobbyClient;
  31. class GameChatHandler;
  32. class IServerRunner;
  33. enum class ESelectionScreen : ui8;
  34. enum class ELoadMode : ui8;
  35. // TODO: Add mutex so we can't set CONNECTION_CANCELLED if client already connected, but thread not setup yet
  36. enum class EClientState : ui8
  37. {
  38. NONE = 0,
  39. CONNECTING, // Trying to connect to server
  40. CONNECTION_CANCELLED, // Connection cancelled by player, stop attempts to connect
  41. LOBBY, // Client is connected to lobby
  42. LOBBY_CAMPAIGN, // Client is on scenario bonus selection screen
  43. STARTING, // Gameplay interfaces being created, we pause netpacks retrieving
  44. GAMEPLAY, // In-game, used by some UI
  45. DISCONNECTING, // We disconnecting, drop all netpacks
  46. };
  47. enum class EServerMode : uint8_t
  48. {
  49. NONE = 0,
  50. LOCAL, // no global lobby
  51. LOBBY_HOST, // We are hosting global server available via global lobby
  52. LOBBY_GUEST // Connecting to a remote server via proxy provided by global lobby
  53. };
  54. class IServerAPI
  55. {
  56. protected:
  57. virtual void sendLobbyPack(const CPackForLobby & pack) const = 0;
  58. public:
  59. virtual ~IServerAPI() {}
  60. virtual void sendClientConnecting() const = 0;
  61. virtual void sendClientDisconnecting() = 0;
  62. virtual void setCampaignState(std::shared_ptr<CampaignState> newCampaign) = 0;
  63. virtual void setCampaignMap(CampaignScenarioID mapId) const = 0;
  64. virtual void setCampaignBonus(int bonusId) const = 0;
  65. virtual void setMapInfo(std::shared_ptr<CMapInfo> to, std::shared_ptr<CMapGenOptions> mapGenOpts = {}) const = 0;
  66. virtual void setPlayer(PlayerColor color) const = 0;
  67. virtual void setPlayerName(PlayerColor color, const std::string & name) const = 0;
  68. virtual void setPlayerHandicap(PlayerColor color, Handicap handicap) const = 0;
  69. virtual void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const = 0;
  70. virtual void setDifficulty(int to) const = 0;
  71. virtual void setTurnTimerInfo(const TurnTimerInfo &) const = 0;
  72. virtual void setSimturnsInfo(const SimturnsInfo &) const = 0;
  73. virtual void setExtraOptionsInfo(const ExtraOptionsInfo & info) const = 0;
  74. virtual void sendMessage(const std::string & txt) const = 0;
  75. virtual void sendGuiAction(ui8 action) const = 0; // TODO: possibly get rid of it?
  76. virtual void sendStartGame(bool allowOnlyAI = false) const = 0;
  77. virtual void sendRestartGame() const = 0;
  78. };
  79. /// structure to handle running server and connecting to it
  80. class CServerHandler final : public IServerAPI, public LobbyInfo, public INetworkClientListener, public INetworkTimerListener, boost::noncopyable
  81. {
  82. friend class ApplyOnLobbyHandlerNetPackVisitor;
  83. std::unique_ptr<INetworkHandler> networkHandler;
  84. std::shared_ptr<INetworkConnection> networkConnection;
  85. std::unique_ptr<GlobalLobbyClient> lobbyClient;
  86. std::unique_ptr<GameChatHandler> gameChat;
  87. std::unique_ptr<IServerRunner> serverRunner;
  88. std::shared_ptr<CMapInfo> mapToStart;
  89. std::vector<std::string> localPlayerNames;
  90. boost::thread threadNetwork;
  91. std::atomic<EClientState> state;
  92. void threadRunNetwork();
  93. void waitForServerShutdown();
  94. void onPacketReceived(const NetworkConnectionPtr &, const std::vector<std::byte> & message) override;
  95. void onConnectionFailed(const std::string & errorMessage) override;
  96. void onConnectionEstablished(const NetworkConnectionPtr &) override;
  97. void onDisconnected(const NetworkConnectionPtr &, const std::string & errorMessage) override;
  98. void onTimer() override;
  99. void applyPackOnLobbyScreen(CPackForLobby & pack);
  100. std::string serverHostname;
  101. ui16 serverPort;
  102. bool isServerLocal() const;
  103. public:
  104. /// High-level connection overlay that is capable of (de)serializing network data
  105. std::shared_ptr<CConnection> logicConnection;
  106. ////////////////////
  107. // FIXME: Bunch of crutches to glue it all together
  108. // For starting non-custom campaign and continue to next mission
  109. std::shared_ptr<CampaignState> campaignStateToSend;
  110. ESelectionScreen screenType; // To create lobby UI only after server is setup
  111. EServerMode serverMode;
  112. ELoadMode loadMode; // For saves filtering in SelectionTab
  113. ////////////////////
  114. std::unique_ptr<CStopWatch> th;
  115. std::unique_ptr<CClient> client;
  116. CServerHandler();
  117. ~CServerHandler();
  118. void resetStateForLobby(EStartMode mode, ESelectionScreen screen, EServerMode serverMode, const std::vector<std::string> & playerNames);
  119. void startLocalServerAndConnect(bool connectToLobby);
  120. void connectToServer(const std::string & addr, const ui16 port);
  121. GameChatHandler & getGameChat();
  122. GlobalLobbyClient & getGlobalLobby();
  123. INetworkHandler & getNetworkHandler();
  124. // Helpers for lobby state access
  125. std::set<PlayerColor> getHumanColors();
  126. PlayerColor myFirstColor() const;
  127. bool isMyColor(PlayerColor color) const;
  128. ui8 myFirstId() const; // Used by chat only!
  129. EClientState getState() const;
  130. void setState(EClientState newState);
  131. bool isHost() const;
  132. bool isGuest() const;
  133. bool inLobbyRoom() const;
  134. bool inGame() const;
  135. const std::string & getCurrentHostname() const;
  136. const std::string & getLocalHostname() const;
  137. const std::string & getRemoteHostname() const;
  138. ui16 getCurrentPort() const;
  139. ui16 getLocalPort() const;
  140. ui16 getRemotePort() const;
  141. // Lobby server API for UI
  142. void sendLobbyPack(const CPackForLobby & pack) const override;
  143. void sendClientConnecting() const override;
  144. void sendClientDisconnecting() override;
  145. void setCampaignState(std::shared_ptr<CampaignState> newCampaign) override;
  146. void setCampaignMap(CampaignScenarioID mapId) const override;
  147. void setCampaignBonus(int bonusId) const override;
  148. void setMapInfo(std::shared_ptr<CMapInfo> to, std::shared_ptr<CMapGenOptions> mapGenOpts = {}) const override;
  149. void setPlayer(PlayerColor color) const override;
  150. void setPlayerName(PlayerColor color, const std::string & name) const override;
  151. void setPlayerHandicap(PlayerColor color, Handicap handicap) const override;
  152. void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const override;
  153. void setDifficulty(int to) const override;
  154. void setTurnTimerInfo(const TurnTimerInfo &) const override;
  155. void setSimturnsInfo(const SimturnsInfo &) const override;
  156. void setExtraOptionsInfo(const ExtraOptionsInfo &) const override;
  157. void sendMessage(const std::string & txt) const override;
  158. void sendGuiAction(ui8 action) const override;
  159. void sendRestartGame() const override;
  160. void sendStartGame(bool allowOnlyAI = false) const override;
  161. void startMapAfterConnection(std::shared_ptr<CMapInfo> to);
  162. bool validateGameStart(bool allowOnlyAI = false) const;
  163. void debugStartTest(std::string filename, bool save = false);
  164. void startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState = nullptr);
  165. void showHighScoresAndEndGameplay(PlayerColor player, bool victory, const StatisticDataSet & statistic);
  166. void endNetwork();
  167. void endGameplay();
  168. void restartGameplay();
  169. void startCampaignScenario(HighScoreParameter param, std::shared_ptr<CampaignState> cs, const StatisticDataSet & statistic);
  170. void showServerError(const std::string & txt) const;
  171. // TODO: LobbyState must be updated within game so we should always know how many player interfaces our client handle
  172. int howManyPlayerInterfaces();
  173. ELoadMode getLoadMode();
  174. void visitForLobby(CPackForLobby & lobbyPack);
  175. void visitForClient(CPackForClient & clientPack);
  176. };
  177. extern CServerHandler * CSH;