CServerHandler.h 8.4 KB

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