CServerHandler.h 5.8 KB

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