CServerHandler.h 4.9 KB

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