CVCMIServer.h 3.1 KB

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