CServerHandler.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/StartInfo.h"
  13. #include "../lib/CondSh.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CConnection;
  16. class PlayerColor;
  17. struct StartInfo;
  18. struct TurnTimerInfo;
  19. class CMapInfo;
  20. class CGameState;
  21. struct ClientPlayer;
  22. struct CPack;
  23. struct CPackForLobby;
  24. struct CPackForClient;
  25. template<typename T> class CApplier;
  26. VCMI_LIB_NAMESPACE_END
  27. class CClient;
  28. class CBaseForLobbyApply;
  29. class HighScoreCalculation;
  30. class HighScoreParameter;
  31. // TODO: Add mutex so we can't set CONNECTION_CANCELLED if client already connected, but thread not setup yet
  32. enum class EClientState : ui8
  33. {
  34. NONE = 0,
  35. CONNECTING, // Trying to connect to server
  36. CONNECTION_CANCELLED, // Connection cancelled by player, stop attempts to connect
  37. LOBBY, // Client is connected to lobby
  38. LOBBY_CAMPAIGN, // Client is on scenario bonus selection screen
  39. STARTING, // Gameplay interfaces being created, we pause netpacks retrieving
  40. GAMEPLAY, // In-game, used by some UI
  41. DISCONNECTING // We disconnecting, drop all netpacks
  42. };
  43. class IServerAPI
  44. {
  45. protected:
  46. virtual void sendLobbyPack(const CPackForLobby & pack) const = 0;
  47. public:
  48. virtual ~IServerAPI() {}
  49. virtual void sendClientConnecting() const = 0;
  50. virtual void sendClientDisconnecting() = 0;
  51. virtual void setCampaignState(std::shared_ptr<CampaignState> newCampaign) = 0;
  52. virtual void setCampaignMap(CampaignScenarioID mapId) const = 0;
  53. virtual void setCampaignBonus(int bonusId) const = 0;
  54. virtual void setMapInfo(std::shared_ptr<CMapInfo> to, std::shared_ptr<CMapGenOptions> mapGenOpts = {}) const = 0;
  55. virtual void setPlayer(PlayerColor color) const = 0;
  56. virtual void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const = 0;
  57. virtual void setDifficulty(int to) const = 0;
  58. virtual void setTurnTimerInfo(const TurnTimerInfo &) const = 0;
  59. virtual void setSimturnsInfo(const SimturnsInfo &) const = 0;
  60. virtual void sendMessage(const std::string & txt) const = 0;
  61. virtual void sendGuiAction(ui8 action) const = 0; // TODO: possibly get rid of it?
  62. virtual void sendStartGame(bool allowOnlyAI = false) const = 0;
  63. virtual void sendRestartGame() const = 0;
  64. };
  65. /// structure to handle running server and connecting to it
  66. class CServerHandler : public IServerAPI, public LobbyInfo
  67. {
  68. friend class ApplyOnLobbyHandlerNetPackVisitor;
  69. std::shared_ptr<CApplier<CBaseForLobbyApply>> applier;
  70. std::shared_ptr<boost::recursive_mutex> mx;
  71. std::list<CPackForLobby *> packsForLobbyScreen; //protected by mx
  72. std::shared_ptr<CMapInfo> mapToStart;
  73. std::vector<std::string> myNames;
  74. std::shared_ptr<HighScoreCalculation> highScoreCalc;
  75. void threadHandleConnection();
  76. void threadRunServer();
  77. void onServerFinished();
  78. void sendLobbyPack(const CPackForLobby & pack) const override;
  79. public:
  80. std::atomic<EClientState> state;
  81. ////////////////////
  82. // FIXME: Bunch of crutches to glue it all together
  83. // For starting non-custom campaign and continue to next mission
  84. std::shared_ptr<CampaignState> campaignStateToSend;
  85. ui8 screenType; // To create lobby UI only after server is setup
  86. ui8 loadMode; // For saves filtering in SelectionTab
  87. ////////////////////
  88. std::unique_ptr<CStopWatch> th;
  89. std::shared_ptr<boost::thread> threadRunLocalServer;
  90. std::shared_ptr<CConnection> c;
  91. CClient * client;
  92. CondSh<bool> campaignServerRestartLock;
  93. static const std::string localhostAddress;
  94. CServerHandler();
  95. std::string getHostAddress() const;
  96. ui16 getHostPort() const;
  97. void resetStateForLobby(const StartInfo::EMode mode, const std::vector<std::string> * names = nullptr);
  98. void startLocalServerAndConnect();
  99. void justConnectToServer(const std::string & addr, const ui16 port);
  100. void applyPacksOnLobbyScreen();
  101. void stopServerConnection();
  102. // Helpers for lobby state access
  103. std::set<PlayerColor> getHumanColors();
  104. PlayerColor myFirstColor() const;
  105. bool isMyColor(PlayerColor color) const;
  106. ui8 myFirstId() const; // Used by chat only!
  107. bool isServerLocal() const;
  108. bool isHost() const;
  109. bool isGuest() const;
  110. static ui16 getDefaultPort();
  111. static std::string getDefaultPortStr();
  112. // Lobby server API for UI
  113. void sendClientConnecting() const override;
  114. void sendClientDisconnecting() override;
  115. void setCampaignState(std::shared_ptr<CampaignState> newCampaign) override;
  116. void setCampaignMap(CampaignScenarioID mapId) const override;
  117. void setCampaignBonus(int bonusId) const override;
  118. void setMapInfo(std::shared_ptr<CMapInfo> to, std::shared_ptr<CMapGenOptions> mapGenOpts = {}) const override;
  119. void setPlayer(PlayerColor color) const override;
  120. void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const override;
  121. void setDifficulty(int to) const override;
  122. void setTurnTimerInfo(const TurnTimerInfo &) const override;
  123. void setSimturnsInfo(const SimturnsInfo &) const override;
  124. void sendMessage(const std::string & txt) const override;
  125. void sendGuiAction(ui8 action) const override;
  126. void sendRestartGame() const override;
  127. void sendStartGame(bool allowOnlyAI = false) const override;
  128. void startMapAfterConnection(std::shared_ptr<CMapInfo> to);
  129. bool validateGameStart(bool allowOnlyAI = false) const;
  130. void debugStartTest(std::string filename, bool save = false);
  131. void startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState = nullptr);
  132. void endGameplay(bool closeConnection = true, bool restart = false);
  133. void startCampaignScenario(HighScoreParameter param, std::shared_ptr<CampaignState> cs = {});
  134. void showServerError(const std::string & txt) const;
  135. // TODO: LobbyState must be updated within game so we should always know how many player interfaces our client handle
  136. int howManyPlayerInterfaces();
  137. ui8 getLoadMode();
  138. void restoreLastSession();
  139. void visitForLobby(CPackForLobby & lobbyPack);
  140. void visitForClient(CPackForClient & clientPack);
  141. };
  142. extern CServerHandler * CSH;