CVCMIServer.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "IGameServer.h"
  12. #include "../lib/network/NetworkInterface.h"
  13. #include "../lib/StartInfo.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CMapInfo;
  16. struct CPackForLobby;
  17. class GameConnection;
  18. struct StartInfo;
  19. struct LobbyInfo;
  20. struct PlayerSettings;
  21. class PlayerColor;
  22. class MetaString;
  23. VCMI_LIB_NAMESPACE_END
  24. class CGameHandler;
  25. class CBaseForServerApply;
  26. class CBaseForGHApply;
  27. class GlobalLobbyProcessor;
  28. class CVCMIServer : public LobbyInfo, public INetworkServerListener, public INetworkTimerListener, public IServerDiscoveryAnnouncer, public IGameServer
  29. {
  30. std::chrono::steady_clock::time_point gameplayStartTime;
  31. std::chrono::steady_clock::time_point lastTimerUpdateTime;
  32. std::unique_ptr<INetworkHandler> networkHandler;
  33. /// Network server instance that receives and processes incoming connections on active socket
  34. std::unique_ptr<INetworkServer> networkServer;
  35. /// Handles connection with global lobby. Must be constructed and destroyed after network handler
  36. std::unique_ptr<GlobalLobbyProcessor> lobbyProcessor;
  37. EServerState state = EServerState::LOBBY;
  38. std::shared_ptr<GameConnection> findConnection(const std::shared_ptr<INetworkConnection> &);
  39. GameConnectionID currentClientId;
  40. PlayerConnectionID currentPlayerId;
  41. uint16_t port;
  42. bool runByClient;
  43. std::shared_ptr<IServerDiscoveryListener> discoveryListener;
  44. bool loadSavedGame(CGameHandler & handler, const StartInfo & info);
  45. public:
  46. uint16_t getPort() const override
  47. {
  48. return port;
  49. }
  50. bool isInLobby() const override;
  51. // IGameServer impl
  52. void setState(EServerState value) override;
  53. EServerState getState() const override;
  54. bool isPlayerHost(const PlayerColor & color) const override;
  55. bool hasPlayerAt(PlayerColor player, GameConnectionID connectionID) const override;
  56. bool hasBothPlayersAtSameConnection(PlayerColor left, PlayerColor right) const override;
  57. void applyPack(CPackForClient & pack) override;
  58. void sendPack(CPackForClient & pack, GameConnectionID connectionID) override;
  59. /// List of all active connections
  60. std::vector<std::shared_ptr<GameConnection>> activeConnections;
  61. uint16_t prepare(bool connectToLobby, bool listenForConnections);
  62. // INetworkListener impl
  63. void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) override;
  64. void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) override;
  65. void onNewConnection(const std::shared_ptr<INetworkConnection> &) override;
  66. void onTimer() override;
  67. std::shared_ptr<CGameHandler> gh;
  68. CVCMIServer(uint16_t port, bool runByClient);
  69. ~CVCMIServer();
  70. void run();
  71. bool wasStartedByClient() const;
  72. bool prepareToStartGame();
  73. void prepareToRestart();
  74. void startGameImmediately();
  75. uint16_t startAcceptingIncomingConnections(bool listenForConnections);
  76. void threadHandleClient(std::shared_ptr<GameConnection> c);
  77. void announcePack(CPackForLobby & pack);
  78. bool passHost(GameConnectionID toConnectionId);
  79. void announceTxt(const MetaString & txt, const std::string & playerName = "system");
  80. void announceTxt(const std::string & txt, const std::string & playerName = "system");
  81. void setPlayerConnectedId(PlayerSettings & pset, PlayerConnectionID player) const;
  82. void updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpt = {});
  83. void clientConnected(std::shared_ptr<GameConnection> c, std::vector<std::string> & names, const std::string & uuid, EStartMode mode);
  84. void clientDisconnected(std::shared_ptr<GameConnection> c);
  85. void announceMessage(const MetaString & txt);
  86. void announceMessage(const std::string & txt);
  87. void handleReceivedPack(std::shared_ptr<GameConnection> connection, CPackForLobby & pack);
  88. void updateAndPropagateLobbyState();
  89. INetworkHandler & getNetworkHandler();
  90. INetworkServer & getNetworkServer();
  91. // Work with LobbyInfo
  92. void setPlayer(PlayerColor clickedColor);
  93. void setPlayerName(PlayerColor player, const std::string & name);
  94. void setPlayerHandicap(PlayerColor player, Handicap handicap);
  95. void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
  96. void optionSetHero(PlayerColor player, HeroTypeID id);
  97. HeroTypeID nextAllowedHero(PlayerColor player, HeroTypeID id, int direction);
  98. bool canUseThisHero(PlayerColor player, HeroTypeID ID);
  99. std::vector<HeroTypeID> getUsedHeroes();
  100. void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
  101. void optionSetBonus(PlayerColor player, PlayerStartingBonus id);
  102. void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
  103. void optionSetCastle(PlayerColor player, FactionID id);
  104. // Campaigns
  105. void setCampaignMap(CampaignScenarioID mapId);
  106. void setCampaignBonus(int bonusId);
  107. PlayerConnectionID getIdOfFirstUnallocatedPlayer() const;
  108. void multiplayerWelcomeMessage();
  109. void startDiscoveryListener();
  110. void stopDiscoveryListener();
  111. };