CServerHandler.h 6.7 KB

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