CServerHandler.h 5.0 KB

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