CVCMIServer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. VCMI_LIB_NAMESPACE_BEGIN
  15. class CMapInfo;
  16. struct CPackForLobby;
  17. struct SharedMemory;
  18. struct StartInfo;
  19. struct LobbyInfo;
  20. struct PlayerSettings;
  21. class PlayerColor;
  22. template<typename T> class CApplier;
  23. VCMI_LIB_NAMESPACE_END
  24. class CGameHandler;
  25. class CBaseForServerApply;
  26. class CBaseForGHApply;
  27. enum class EServerState : ui8
  28. {
  29. LOBBY,
  30. GAMEPLAY_STARTING,
  31. GAMEPLAY,
  32. GAMEPLAY_ENDED,
  33. SHUTDOWN
  34. };
  35. class CVCMIServer : public LobbyInfo
  36. {
  37. std::atomic<bool> restartGameplay; // FIXME: this is just a hack
  38. std::shared_ptr<boost::asio::io_service> io;
  39. std::shared_ptr<TAcceptor> acceptor;
  40. std::shared_ptr<TSocket> upcomingConnection;
  41. std::list<std::unique_ptr<CPackForLobby>> announceQueue;
  42. boost::recursive_mutex mx;
  43. std::shared_ptr<CApplier<CBaseForServerApply>> applier;
  44. std::unique_ptr<boost::thread> announceLobbyThread;
  45. public:
  46. std::shared_ptr<CGameHandler> gh;
  47. std::atomic<EServerState> state;
  48. ui16 port;
  49. boost::program_options::variables_map cmdLineOptions;
  50. std::set<std::shared_ptr<CConnection>> connections;
  51. std::atomic<int> currentClientId;
  52. std::atomic<ui8> currentPlayerId;
  53. std::shared_ptr<CConnection> hostClient;
  54. CVCMIServer(boost::program_options::variables_map & opts);
  55. ~CVCMIServer();
  56. void run();
  57. bool prepareToStartGame();
  58. void prepareToRestart();
  59. void startGameImmidiately();
  60. void startAsyncAccept();
  61. void connectionAccepted(const boost::system::error_code & ec);
  62. void threadHandleClient(std::shared_ptr<CConnection> c);
  63. void threadAnnounceLobby();
  64. void handleReceivedPack(std::unique_ptr<CPackForLobby> pack);
  65. void announcePack(std::unique_ptr<CPackForLobby> pack);
  66. bool passHost(int toConnectionId);
  67. void announceTxt(const std::string & txt, const std::string & playerName = "system");
  68. void announceMessage(const std::string & txt);
  69. void addToAnnounceQueue(std::unique_ptr<CPackForLobby> pack);
  70. void setPlayerConnectedId(PlayerSettings & pset, ui8 player) const;
  71. void updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpt = {});
  72. void clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, std::string uuid, StartInfo::EMode mode);
  73. void clientDisconnected(std::shared_ptr<CConnection> c);
  74. void updateAndPropagateLobbyState();
  75. // Work with LobbyInfo
  76. void setPlayer(PlayerColor clickedColor);
  77. void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
  78. int nextAllowedHero(PlayerColor player, int min, int max, int incl, int dir);
  79. bool canUseThisHero(PlayerColor player, int ID);
  80. std::vector<int> getUsedHeroes();
  81. void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
  82. void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
  83. // Campaigns
  84. void setCampaignMap(int mapId);
  85. void setCampaignBonus(int bonusId);
  86. ui8 getIdOfFirstUnallocatedPlayer() const;
  87. #ifdef VCMI_ANDROID
  88. static void create();
  89. #elif defined(SINGLE_PROCESS_APP)
  90. static void create(boost::condition_variable * cond, const std::string & uuid);
  91. #endif
  92. };