CVCMIServer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * CVCMIServer.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/network/NetworkInterface.h"
  12. #include "../lib/StartInfo.h"
  13. #include <boost/program_options/variables_map.hpp>
  14. #if defined(VCMI_ANDROID) && !defined(SINGLE_PROCESS_APP)
  15. #define VCMI_ANDROID_DUAL_PROCESS 1
  16. #endif
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class CMapInfo;
  19. struct CPackForLobby;
  20. class CConnection;
  21. struct StartInfo;
  22. struct LobbyInfo;
  23. struct PlayerSettings;
  24. class PlayerColor;
  25. template<typename T> class CApplier;
  26. VCMI_LIB_NAMESPACE_END
  27. class CGameHandler;
  28. class CBaseForServerApply;
  29. class CBaseForGHApply;
  30. class GlobalLobbyProcessor;
  31. enum class EServerState : ui8
  32. {
  33. LOBBY,
  34. GAMEPLAY_STARTING,
  35. GAMEPLAY,
  36. GAMEPLAY_ENDED,
  37. SHUTDOWN
  38. };
  39. class CVCMIServer : public LobbyInfo, public INetworkServerListener, public INetworkTimerListener
  40. {
  41. /// Network server instance that receives and processes incoming connections on active socket
  42. std::unique_ptr<INetworkServer> networkServer;
  43. std::unique_ptr<GlobalLobbyProcessor> lobbyProcessor;
  44. std::chrono::steady_clock::time_point gameplayStartTime;
  45. std::chrono::steady_clock::time_point lastTimerUpdateTime;
  46. public:
  47. /// List of all active connections
  48. std::vector<std::shared_ptr<CConnection>> activeConnections;
  49. std::unique_ptr<INetworkHandler> networkHandler;
  50. private:
  51. bool restartGameplay; // FIXME: this is just a hack
  52. boost::recursive_mutex mx;
  53. std::shared_ptr<CApplier<CBaseForServerApply>> applier;
  54. EServerState state;
  55. void establishOutgoingConnection();
  56. std::shared_ptr<CConnection> findConnection(const std::shared_ptr<INetworkConnection> &);
  57. int currentClientId;
  58. ui8 currentPlayerId;
  59. public:
  60. // INetworkListener impl
  61. void onDisconnected(const std::shared_ptr<INetworkConnection> & connection) override;
  62. void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message) override;
  63. void onNewConnection(const std::shared_ptr<INetworkConnection> &) override;
  64. void onTimer() override;
  65. std::shared_ptr<CGameHandler> gh;
  66. boost::program_options::variables_map cmdLineOptions;
  67. CVCMIServer(boost::program_options::variables_map & opts);
  68. ~CVCMIServer();
  69. void run();
  70. bool prepareToStartGame();
  71. void prepareToRestart();
  72. void startGameImmediately();
  73. void threadHandleClient(std::shared_ptr<CConnection> c);
  74. void announcePack(std::unique_ptr<CPackForLobby> pack);
  75. bool passHost(int toConnectionId);
  76. void announceTxt(const std::string & txt, const std::string & playerName = "system");
  77. void setPlayerConnectedId(PlayerSettings & pset, ui8 player) const;
  78. void updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpt = {});
  79. void clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, std::string uuid, StartInfo::EMode mode);
  80. void clientDisconnected(std::shared_ptr<CConnection> c);
  81. void reconnectPlayer(int connId);
  82. public:
  83. void announceMessage(const std::string & txt);
  84. void handleReceivedPack(std::unique_ptr<CPackForLobby> pack);
  85. void updateAndPropagateLobbyState();
  86. void setState(EServerState value);
  87. EServerState getState() const;
  88. // Work with LobbyInfo
  89. void setPlayer(PlayerColor clickedColor);
  90. void setPlayerName(PlayerColor player, std::string name);
  91. void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
  92. void optionSetHero(PlayerColor player, HeroTypeID id);
  93. HeroTypeID nextAllowedHero(PlayerColor player, HeroTypeID id, int direction);
  94. bool canUseThisHero(PlayerColor player, HeroTypeID ID);
  95. std::vector<HeroTypeID> getUsedHeroes();
  96. void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
  97. void optionSetBonus(PlayerColor player, PlayerStartingBonus id);
  98. void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
  99. void optionSetCastle(PlayerColor player, FactionID id);
  100. // Campaigns
  101. void setCampaignMap(CampaignScenarioID mapId);
  102. void setCampaignBonus(int bonusId);
  103. ui8 getIdOfFirstUnallocatedPlayer() const;
  104. #if VCMI_ANDROID_DUAL_PROCESS
  105. static void create();
  106. #elif defined(SINGLE_PROCESS_APP)
  107. static void create(boost::condition_variable * cond, const std::vector<std::string> & args);
  108. # ifdef VCMI_ANDROID
  109. static void reuseClientJNIEnv(void * jniEnv);
  110. # endif // VCMI_ANDROID
  111. #endif // VCMI_ANDROID_DUAL_PROCESS
  112. };