CVCMIServer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, remoteConnectionsThread;
  47. public:
  48. std::shared_ptr<CGameHandler> gh;
  49. std::atomic<EServerState> state;
  50. ui16 port;
  51. boost::program_options::variables_map cmdLineOptions;
  52. std::set<std::shared_ptr<CConnection>> connections;
  53. std::set<std::shared_ptr<CConnection>> hangingConnections; //keep connections of players disconnected during the game
  54. std::atomic<int> currentClientId;
  55. std::atomic<ui8> currentPlayerId;
  56. std::shared_ptr<CConnection> hostClient;
  57. CVCMIServer(boost::program_options::variables_map & opts);
  58. ~CVCMIServer();
  59. void run();
  60. bool prepareToStartGame();
  61. void prepareToRestart();
  62. void startGameImmidiately();
  63. void establishRemoteConnections();
  64. void connectToRemote(const std::string & addr, int port);
  65. void startAsyncAccept();
  66. void connectionAccepted(const boost::system::error_code & ec);
  67. void threadHandleClient(std::shared_ptr<CConnection> c);
  68. void threadAnnounceLobby();
  69. void handleReceivedPack(std::unique_ptr<CPackForLobby> pack);
  70. void announcePack(std::unique_ptr<CPackForLobby> pack);
  71. bool passHost(int toConnectionId);
  72. void announceTxt(const std::string & txt, const std::string & playerName = "system");
  73. void announceMessage(const std::string & txt);
  74. void addToAnnounceQueue(std::unique_ptr<CPackForLobby> pack);
  75. void setPlayerConnectedId(PlayerSettings & pset, ui8 player) const;
  76. void updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpt = {});
  77. void clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, std::string uuid, StartInfo::EMode mode);
  78. void clientDisconnected(std::shared_ptr<CConnection> c);
  79. void reconnectPlayer(int connId);
  80. void updateAndPropagateLobbyState();
  81. // Work with LobbyInfo
  82. void setPlayer(PlayerColor clickedColor);
  83. void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
  84. int nextAllowedHero(PlayerColor player, int min, int max, int incl, int dir);
  85. bool canUseThisHero(PlayerColor player, int ID);
  86. std::vector<int> getUsedHeroes();
  87. void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
  88. void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
  89. // Campaigns
  90. void setCampaignMap(CampaignScenarioID mapId);
  91. void setCampaignBonus(int bonusId);
  92. ui8 getIdOfFirstUnallocatedPlayer() const;
  93. #if VCMI_ANDROID_DUAL_PROCESS
  94. static void create();
  95. #elif defined(SINGLE_PROCESS_APP)
  96. static void create(boost::condition_variable * cond, const std::vector<std::string> & args);
  97. # ifdef VCMI_ANDROID
  98. static void reuseClientJNIEnv(void * jniEnv);
  99. # endif // VCMI_ANDROID
  100. #endif // VCMI_ANDROID_DUAL_PROCESS
  101. };