CServerHandler.h 4.8 KB

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