CVCMIServer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 SharedMemory;
  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. enum class EServerState : ui8
  31. {
  32. LOBBY,
  33. GAMEPLAY_STARTING,
  34. GAMEPLAY,
  35. GAMEPLAY_ENDED,
  36. SHUTDOWN
  37. };
  38. class CVCMIServer : public LobbyInfo
  39. {
  40. std::atomic<bool> restartGameplay; // FIXME: this is just a hack
  41. std::shared_ptr<boost::asio::io_service> io;
  42. std::shared_ptr<TAcceptor> acceptor;
  43. std::shared_ptr<TSocket> upcomingConnection;
  44. std::list<std::unique_ptr<CPackForLobby>> announceQueue;
  45. boost::recursive_mutex mx;
  46. std::shared_ptr<CApplier<CBaseForServerApply>> applier;
  47. std::unique_ptr<boost::thread> announceLobbyThread, remoteConnectionsThread;
  48. public:
  49. std::shared_ptr<CGameHandler> gh;
  50. std::atomic<EServerState> state;
  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>> hangingConnections; //keep connections of players disconnected during the game
  55. std::atomic<int> currentClientId;
  56. std::atomic<ui8> currentPlayerId;
  57. std::shared_ptr<CConnection> hostClient;
  58. CVCMIServer(boost::program_options::variables_map & opts);
  59. ~CVCMIServer();
  60. void run();
  61. bool prepareToStartGame();
  62. void prepareToRestart();
  63. void startGameImmidiately();
  64. void establishRemoteConnections();
  65. void connectToRemote(const std::string & addr, int port);
  66. void startAsyncAccept();
  67. void connectionAccepted(const boost::system::error_code & ec);
  68. void threadHandleClient(std::shared_ptr<CConnection> c);
  69. void threadAnnounceLobby();
  70. void handleReceivedPack(std::unique_ptr<CPackForLobby> pack);
  71. void announcePack(std::unique_ptr<CPackForLobby> pack);
  72. bool passHost(int toConnectionId);
  73. void announceTxt(const std::string & txt, const std::string & playerName = "system");
  74. void announceMessage(const std::string & txt);
  75. void addToAnnounceQueue(std::unique_ptr<CPackForLobby> pack);
  76. void setPlayerConnectedId(PlayerSettings & pset, ui8 player) const;
  77. void updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpt = {});
  78. void clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, std::string uuid, StartInfo::EMode mode);
  79. void clientDisconnected(std::shared_ptr<CConnection> c);
  80. void reconnectPlayer(int connId);
  81. void updateAndPropagateLobbyState();
  82. // Work with LobbyInfo
  83. void setPlayer(PlayerColor clickedColor);
  84. void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
  85. int nextAllowedHero(PlayerColor player, int min, int max, int incl, int dir);
  86. bool canUseThisHero(PlayerColor player, int ID);
  87. std::vector<int> getUsedHeroes();
  88. void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
  89. void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
  90. // Campaigns
  91. void setCampaignMap(int mapId);
  92. void setCampaignBonus(int bonusId);
  93. ui8 getIdOfFirstUnallocatedPlayer() const;
  94. #if VCMI_ANDROID_DUAL_PROCESS
  95. static void create();
  96. #elif defined(SINGLE_PROCESS_APP)
  97. static void create(boost::condition_variable * cond, const std::vector<std::string> & args);
  98. # ifdef VCMI_ANDROID
  99. static void reuseClientJNIEnv(void * jniEnv);
  100. # endif // VCMI_ANDROID
  101. #endif // VCMI_ANDROID_DUAL_PROCESS
  102. };