CVCMIServer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/serializer/Connection.h"
  12. #include "../lib/StartInfo.h"
  13. #include <boost/program_options.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. struct StartInfo;
  21. struct LobbyInfo;
  22. struct PlayerSettings;
  23. class PlayerColor;
  24. template<typename T> class CApplier;
  25. VCMI_LIB_NAMESPACE_END
  26. class CGameHandler;
  27. class CBaseForServerApply;
  28. class CBaseForGHApply;
  29. enum class EServerState : ui8
  30. {
  31. LOBBY,
  32. GAMEPLAY_STARTING,
  33. GAMEPLAY,
  34. GAMEPLAY_ENDED,
  35. SHUTDOWN
  36. };
  37. class CVCMIServer : public LobbyInfo
  38. {
  39. std::atomic<bool> restartGameplay; // FIXME: this is just a hack
  40. std::shared_ptr<boost::asio::io_service> io;
  41. std::shared_ptr<TAcceptor> acceptor;
  42. std::shared_ptr<TSocket> upcomingConnection;
  43. std::list<std::unique_ptr<CPackForLobby>> announceQueue;
  44. boost::recursive_mutex mx;
  45. std::shared_ptr<CApplier<CBaseForServerApply>> applier;
  46. std::unique_ptr<boost::thread> announceLobbyThread;
  47. std::unique_ptr<boost::thread> remoteConnectionsThread;
  48. std::atomic<EServerState> state;
  49. public:
  50. std::shared_ptr<CGameHandler> gh;
  51. ui16 port;
  52. boost::program_options::variables_map cmdLineOptions;
  53. std::set<std::shared_ptr<CConnection>> connections;
  54. std::set<std::shared_ptr<CConnection>> remoteConnections;
  55. std::set<std::shared_ptr<CConnection>> hangingConnections; //keep connections of players disconnected during the game
  56. std::atomic<int> currentClientId;
  57. std::atomic<ui8> currentPlayerId;
  58. std::shared_ptr<CConnection> hostClient;
  59. CVCMIServer(boost::program_options::variables_map & opts);
  60. ~CVCMIServer();
  61. void run();
  62. bool prepareToStartGame();
  63. void prepareToRestart();
  64. void startGameImmidiately();
  65. void establishRemoteConnections();
  66. void connectToRemote();
  67. void startAsyncAccept();
  68. void connectionAccepted(const boost::system::error_code & ec);
  69. void threadHandleClient(std::shared_ptr<CConnection> c);
  70. void threadAnnounceLobby();
  71. void handleReceivedPack(std::unique_ptr<CPackForLobby> pack);
  72. void announcePack(std::unique_ptr<CPackForLobby> pack);
  73. bool passHost(int toConnectionId);
  74. void announceTxt(const std::string & txt, const std::string & playerName = "system");
  75. void announceMessage(const std::string & txt);
  76. void addToAnnounceQueue(std::unique_ptr<CPackForLobby> pack);
  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. void updateAndPropagateLobbyState();
  83. void setState(EServerState value);
  84. EServerState getState() const;
  85. // Work with LobbyInfo
  86. void setPlayer(PlayerColor clickedColor);
  87. void setPlayerName(PlayerColor player, std::string name);
  88. void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
  89. void optionSetHero(PlayerColor player, HeroTypeID id);
  90. HeroTypeID nextAllowedHero(PlayerColor player, HeroTypeID id, int direction);
  91. bool canUseThisHero(PlayerColor player, HeroTypeID ID);
  92. std::vector<HeroTypeID> getUsedHeroes();
  93. void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
  94. void optionSetBonus(PlayerColor player, PlayerStartingBonus id);
  95. void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
  96. void optionSetCastle(PlayerColor player, FactionID id);
  97. // Campaigns
  98. void setCampaignMap(CampaignScenarioID mapId);
  99. void setCampaignBonus(int bonusId);
  100. ui8 getIdOfFirstUnallocatedPlayer() const;
  101. #if VCMI_ANDROID_DUAL_PROCESS
  102. static void create();
  103. #elif defined(SINGLE_PROCESS_APP)
  104. static void create(boost::condition_variable * cond, const std::vector<std::string> & args);
  105. # ifdef VCMI_ANDROID
  106. static void reuseClientJNIEnv(void * jniEnv);
  107. # endif // VCMI_ANDROID
  108. #endif // VCMI_ANDROID_DUAL_PROCESS
  109. };