CVCMIServer.h 3.6 KB

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