2018-01-05 19:21:07 +02:00
|
|
|
/*
|
|
|
|
* CServerHandler.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../lib/CStopWatch.h"
|
|
|
|
|
2024-01-12 01:10:41 +02:00
|
|
|
#include "../lib/network/NetworkInterface.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/StartInfo.h"
|
2024-08-12 00:02:20 +02:00
|
|
|
#include "../lib/gameState/GameStatistics.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
class CConnection;
|
|
|
|
class PlayerColor;
|
|
|
|
struct StartInfo;
|
2023-08-25 18:20:26 +02:00
|
|
|
struct TurnTimerInfo;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
class CMapInfo;
|
2022-09-28 21:15:05 +02:00
|
|
|
class CGameState;
|
2018-01-05 19:21:07 +02:00
|
|
|
struct ClientPlayer;
|
|
|
|
struct CPack;
|
|
|
|
struct CPackForLobby;
|
2023-02-12 09:23:39 +02:00
|
|
|
struct CPackForClient;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2024-07-26 20:54:44 +02:00
|
|
|
class HighScoreParameter;
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
|
|
|
class CClient;
|
2018-01-05 19:21:07 +02:00
|
|
|
class CBaseForLobbyApply;
|
2023-12-28 21:27:21 +02:00
|
|
|
class GlobalLobbyClient;
|
2024-03-18 19:52:53 +02:00
|
|
|
class GameChatHandler;
|
2024-02-10 23:56:02 +02:00
|
|
|
class IServerRunner;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
enum class ESelectionScreen : ui8;
|
|
|
|
enum class ELoadMode : ui8;
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
// TODO: Add mutex so we can't set CONNECTION_CANCELLED if client already connected, but thread not setup yet
|
|
|
|
enum class EClientState : ui8
|
|
|
|
{
|
|
|
|
NONE = 0,
|
|
|
|
CONNECTING, // Trying to connect to server
|
|
|
|
CONNECTION_CANCELLED, // Connection cancelled by player, stop attempts to connect
|
|
|
|
LOBBY, // Client is connected to lobby
|
|
|
|
LOBBY_CAMPAIGN, // Client is on scenario bonus selection screen
|
|
|
|
STARTING, // Gameplay interfaces being created, we pause netpacks retrieving
|
|
|
|
GAMEPLAY, // In-game, used by some UI
|
2023-10-08 23:07:25 +02:00
|
|
|
DISCONNECTING, // We disconnecting, drop all netpacks
|
2018-01-05 19:21:07 +02:00
|
|
|
};
|
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
enum class EServerMode : uint8_t
|
|
|
|
{
|
|
|
|
NONE = 0,
|
|
|
|
LOCAL, // no global lobby
|
|
|
|
LOBBY_HOST, // We are hosting global server available via global lobby
|
|
|
|
LOBBY_GUEST // Connecting to a remote server via proxy provided by global lobby
|
|
|
|
};
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
class IServerAPI
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
virtual void sendLobbyPack(const CPackForLobby & pack) const = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~IServerAPI() {}
|
|
|
|
|
|
|
|
virtual void sendClientConnecting() const = 0;
|
|
|
|
virtual void sendClientDisconnecting() = 0;
|
2023-06-25 20:16:03 +02:00
|
|
|
virtual void setCampaignState(std::shared_ptr<CampaignState> newCampaign) = 0;
|
|
|
|
virtual void setCampaignMap(CampaignScenarioID mapId) const = 0;
|
2018-01-05 19:21:07 +02:00
|
|
|
virtual void setCampaignBonus(int bonusId) const = 0;
|
|
|
|
virtual void setMapInfo(std::shared_ptr<CMapInfo> to, std::shared_ptr<CMapGenOptions> mapGenOpts = {}) const = 0;
|
|
|
|
virtual void setPlayer(PlayerColor color) const = 0;
|
2023-10-16 21:24:59 +02:00
|
|
|
virtual void setPlayerName(PlayerColor color, const std::string & name) const = 0;
|
2024-07-29 22:54:42 +02:00
|
|
|
virtual void setPlayerHandicap(PlayerColor color, Handicap handicap) const = 0;
|
2023-08-17 17:54:53 +02:00
|
|
|
virtual void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const = 0;
|
2018-01-05 19:21:07 +02:00
|
|
|
virtual void setDifficulty(int to) const = 0;
|
2023-08-25 18:20:26 +02:00
|
|
|
virtual void setTurnTimerInfo(const TurnTimerInfo &) const = 0;
|
2023-09-22 18:57:43 +02:00
|
|
|
virtual void setSimturnsInfo(const SimturnsInfo &) const = 0;
|
2023-12-28 21:48:19 +02:00
|
|
|
virtual void setExtraOptionsInfo(const ExtraOptionsInfo & info) const = 0;
|
2018-01-05 19:21:07 +02:00
|
|
|
virtual void sendMessage(const std::string & txt) const = 0;
|
|
|
|
virtual void sendGuiAction(ui8 action) const = 0; // TODO: possibly get rid of it?
|
|
|
|
virtual void sendStartGame(bool allowOnlyAI = false) const = 0;
|
2022-09-29 19:08:05 +02:00
|
|
|
virtual void sendRestartGame() const = 0;
|
2018-01-05 19:21:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// structure to handle running server and connecting to it
|
2024-01-20 00:26:25 +02:00
|
|
|
class CServerHandler final : public IServerAPI, public LobbyInfo, public INetworkClientListener, public INetworkTimerListener, boost::noncopyable
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-08-11 18:04:14 +02:00
|
|
|
friend class ApplyOnLobbyHandlerNetPackVisitor;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2024-02-03 22:59:56 +02:00
|
|
|
std::unique_ptr<INetworkHandler> networkHandler;
|
2024-01-12 16:55:36 +02:00
|
|
|
std::shared_ptr<INetworkConnection> networkConnection;
|
2023-12-28 21:27:21 +02:00
|
|
|
std::unique_ptr<GlobalLobbyClient> lobbyClient;
|
2024-03-18 19:52:53 +02:00
|
|
|
std::unique_ptr<GameChatHandler> gameChat;
|
2024-02-10 23:56:02 +02:00
|
|
|
std::unique_ptr<IServerRunner> serverRunner;
|
2023-08-11 18:04:14 +02:00
|
|
|
std::shared_ptr<CMapInfo> mapToStart;
|
2024-02-12 18:57:20 +02:00
|
|
|
std::vector<std::string> localPlayerNames;
|
2023-12-25 21:23:27 +02:00
|
|
|
|
2024-02-03 22:59:56 +02:00
|
|
|
boost::thread threadNetwork;
|
|
|
|
|
2024-02-04 19:56:04 +02:00
|
|
|
std::atomic<EClientState> state;
|
|
|
|
|
2023-12-25 21:23:27 +02:00
|
|
|
void threadRunNetwork();
|
2024-02-11 15:38:29 +02:00
|
|
|
void waitForServerShutdown();
|
2024-01-20 23:01:49 +02:00
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
void onPacketReceived(const NetworkConnectionPtr &, const std::vector<std::byte> & message) override;
|
2023-11-18 16:34:18 +02:00
|
|
|
void onConnectionFailed(const std::string & errorMessage) override;
|
2024-01-29 22:05:11 +02:00
|
|
|
void onConnectionEstablished(const NetworkConnectionPtr &) override;
|
2024-02-02 01:27:19 +02:00
|
|
|
void onDisconnected(const NetworkConnectionPtr &, const std::string & errorMessage) override;
|
2023-12-26 18:30:57 +02:00
|
|
|
void onTimer() override;
|
2023-11-18 16:34:18 +02:00
|
|
|
|
2023-12-25 22:26:59 +02:00
|
|
|
void applyPackOnLobbyScreen(CPackForLobby & pack);
|
2023-12-26 19:27:47 +02:00
|
|
|
|
|
|
|
std::string serverHostname;
|
|
|
|
ui16 serverPort;
|
|
|
|
|
|
|
|
bool isServerLocal() const;
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
public:
|
2024-02-11 16:26:27 +02:00
|
|
|
/// High-level connection overlay that is capable of (de)serializing network data
|
|
|
|
std::shared_ptr<CConnection> logicConnection;
|
2023-11-18 16:34:18 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
////////////////////
|
|
|
|
// FIXME: Bunch of crutches to glue it all together
|
|
|
|
|
|
|
|
// For starting non-custom campaign and continue to next mission
|
2023-06-25 20:16:03 +02:00
|
|
|
std::shared_ptr<CampaignState> campaignStateToSend;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
ESelectionScreen screenType; // To create lobby UI only after server is setup
|
2024-01-29 22:05:11 +02:00
|
|
|
EServerMode serverMode;
|
2024-01-21 16:48:36 +02:00
|
|
|
ELoadMode loadMode; // For saves filtering in SelectionTab
|
2018-01-05 19:21:07 +02:00
|
|
|
////////////////////
|
|
|
|
|
|
|
|
std::unique_ptr<CStopWatch> th;
|
2024-01-10 19:43:34 +02:00
|
|
|
std::unique_ptr<CClient> client;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
CServerHandler();
|
2023-11-18 16:34:18 +02:00
|
|
|
~CServerHandler();
|
2022-11-08 02:44:34 +02:00
|
|
|
|
2024-02-12 18:57:20 +02:00
|
|
|
void resetStateForLobby(EStartMode mode, ESelectionScreen screen, EServerMode serverMode, const std::vector<std::string> & playerNames);
|
2024-01-10 15:31:11 +02:00
|
|
|
void startLocalServerAndConnect(bool connectToLobby);
|
2023-12-26 19:27:47 +02:00
|
|
|
void connectToServer(const std::string & addr, const ui16 port);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2024-03-18 19:52:53 +02:00
|
|
|
GameChatHandler & getGameChat();
|
2023-12-28 21:27:21 +02:00
|
|
|
GlobalLobbyClient & getGlobalLobby();
|
2024-02-03 22:59:56 +02:00
|
|
|
INetworkHandler & getNetworkHandler();
|
2023-12-28 21:27:21 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
// Helpers for lobby state access
|
|
|
|
std::set<PlayerColor> getHumanColors();
|
|
|
|
PlayerColor myFirstColor() const;
|
|
|
|
bool isMyColor(PlayerColor color) const;
|
|
|
|
ui8 myFirstId() const; // Used by chat only!
|
|
|
|
|
2024-02-04 19:56:04 +02:00
|
|
|
EClientState getState() const;
|
|
|
|
void setState(EClientState newState);
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
bool isHost() const;
|
|
|
|
bool isGuest() const;
|
2024-02-10 19:02:25 +02:00
|
|
|
bool inLobbyRoom() const;
|
|
|
|
bool inGame() const;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-12-26 19:27:47 +02:00
|
|
|
const std::string & getCurrentHostname() const;
|
|
|
|
const std::string & getLocalHostname() const;
|
|
|
|
const std::string & getRemoteHostname() const;
|
|
|
|
|
|
|
|
ui16 getCurrentPort() const;
|
|
|
|
ui16 getLocalPort() const;
|
|
|
|
ui16 getRemotePort() const;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
// Lobby server API for UI
|
2024-03-18 19:52:53 +02:00
|
|
|
void sendLobbyPack(const CPackForLobby & pack) const override;
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void sendClientConnecting() const override;
|
|
|
|
void sendClientDisconnecting() override;
|
2023-06-25 20:16:03 +02:00
|
|
|
void setCampaignState(std::shared_ptr<CampaignState> newCampaign) override;
|
|
|
|
void setCampaignMap(CampaignScenarioID mapId) const override;
|
2018-01-05 19:21:07 +02:00
|
|
|
void setCampaignBonus(int bonusId) const override;
|
|
|
|
void setMapInfo(std::shared_ptr<CMapInfo> to, std::shared_ptr<CMapGenOptions> mapGenOpts = {}) const override;
|
|
|
|
void setPlayer(PlayerColor color) const override;
|
2023-10-16 21:24:59 +02:00
|
|
|
void setPlayerName(PlayerColor color, const std::string & name) const override;
|
2024-07-29 22:54:42 +02:00
|
|
|
void setPlayerHandicap(PlayerColor color, Handicap handicap) const override;
|
2023-08-17 18:15:42 +02:00
|
|
|
void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const override;
|
2018-01-05 19:21:07 +02:00
|
|
|
void setDifficulty(int to) const override;
|
2023-08-25 18:20:26 +02:00
|
|
|
void setTurnTimerInfo(const TurnTimerInfo &) const override;
|
2023-09-22 18:57:43 +02:00
|
|
|
void setSimturnsInfo(const SimturnsInfo &) const override;
|
2023-12-28 21:48:19 +02:00
|
|
|
void setExtraOptionsInfo(const ExtraOptionsInfo &) const override;
|
2018-01-05 19:21:07 +02:00
|
|
|
void sendMessage(const std::string & txt) const override;
|
|
|
|
void sendGuiAction(ui8 action) const override;
|
2022-09-29 19:08:05 +02:00
|
|
|
void sendRestartGame() const override;
|
2018-01-05 19:21:07 +02:00
|
|
|
void sendStartGame(bool allowOnlyAI = false) const override;
|
|
|
|
|
2023-08-11 18:04:14 +02:00
|
|
|
void startMapAfterConnection(std::shared_ptr<CMapInfo> to);
|
2023-09-11 18:39:32 +02:00
|
|
|
bool validateGameStart(bool allowOnlyAI = false) const;
|
2023-09-11 18:49:06 +02:00
|
|
|
void debugStartTest(std::string filename, bool save = false);
|
2023-09-11 18:39:32 +02:00
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState = nullptr);
|
2024-08-14 21:51:08 +02:00
|
|
|
void showHighScoresAndEndGameplay(PlayerColor player, bool victory, const StatisticDataSet & statistic);
|
2024-05-18 13:04:10 +02:00
|
|
|
void endNetwork();
|
2024-02-03 19:08:45 +02:00
|
|
|
void endGameplay();
|
|
|
|
void restartGameplay();
|
2024-08-14 21:51:08 +02:00
|
|
|
void startCampaignScenario(HighScoreParameter param, std::shared_ptr<CampaignState> cs, const StatisticDataSet & statistic);
|
2023-09-11 18:39:32 +02:00
|
|
|
void showServerError(const std::string & txt) const;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
// TODO: LobbyState must be updated within game so we should always know how many player interfaces our client handle
|
|
|
|
int howManyPlayerInterfaces();
|
2024-01-21 16:48:36 +02:00
|
|
|
ELoadMode getLoadMode();
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void visitForLobby(CPackForLobby & lobbyPack);
|
|
|
|
void visitForClient(CPackForClient & clientPack);
|
2018-01-05 19:21:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern CServerHandler * CSH;
|