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"
|
|
|
|
|
|
|
|
#include "../lib/StartInfo.h"
|
2019-03-30 01:28:33 +02:00
|
|
|
#include "../lib/CondSh.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;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
template<typename T> class CApplier;
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
|
|
|
class CClient;
|
2018-01-05 19:21:07 +02:00
|
|
|
class CBaseForLobbyApply;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
DISCONNECTING // We disconnecting, drop all netpacks
|
|
|
|
};
|
|
|
|
|
|
|
|
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-05-29 18:02:23 +02:00
|
|
|
virtual void setPlayerOption(ui8 what, si8 dir, PlayerColor player) const = 0;
|
2018-01-05 19:21:07 +02:00
|
|
|
virtual void setDifficulty(int to) const = 0;
|
|
|
|
virtual void setTurnLength(int npos) const = 0;
|
|
|
|
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
|
|
|
|
class CServerHandler : public IServerAPI, public LobbyInfo
|
|
|
|
{
|
|
|
|
std::shared_ptr<CApplier<CBaseForLobbyApply>> applier;
|
|
|
|
|
|
|
|
std::shared_ptr<boost::recursive_mutex> mx;
|
|
|
|
std::list<CPackForLobby *> packsForLobbyScreen; //protected by mx
|
|
|
|
|
|
|
|
std::vector<std::string> myNames;
|
|
|
|
|
|
|
|
void threadHandleConnection();
|
|
|
|
void threadRunServer();
|
2022-09-16 15:43:21 +02:00
|
|
|
void onServerFinished();
|
2018-01-05 19:21:07 +02:00
|
|
|
void sendLobbyPack(const CPackForLobby & pack) const override;
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::atomic<EClientState> state;
|
|
|
|
////////////////////
|
|
|
|
// 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
|
|
|
|
|
|
|
ui8 screenType; // To create lobby UI only after server is setup
|
|
|
|
ui8 loadMode; // For saves filtering in SelectionTab
|
|
|
|
////////////////////
|
|
|
|
|
|
|
|
std::unique_ptr<CStopWatch> th;
|
|
|
|
std::shared_ptr<boost::thread> threadRunLocalServer;
|
|
|
|
|
|
|
|
std::shared_ptr<CConnection> c;
|
|
|
|
CClient * client;
|
|
|
|
|
2019-03-30 01:28:33 +02:00
|
|
|
CondSh<bool> campaignServerRestartLock;
|
|
|
|
|
2022-10-01 16:41:12 +02:00
|
|
|
static const std::string localhostAddress;
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
CServerHandler();
|
2022-11-08 02:44:34 +02:00
|
|
|
|
|
|
|
std::string getHostAddress() const;
|
|
|
|
ui16 getHostPort() const;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
void resetStateForLobby(const StartInfo::EMode mode, const std::vector<std::string> * names = nullptr);
|
|
|
|
void startLocalServerAndConnect();
|
2023-07-28 13:17:06 +02:00
|
|
|
void justConnectToServer(const std::string & addr, const ui16 port);
|
2018-01-05 19:21:07 +02:00
|
|
|
void applyPacksOnLobbyScreen();
|
|
|
|
void stopServerConnection();
|
|
|
|
|
|
|
|
// Helpers for lobby state access
|
|
|
|
std::set<PlayerColor> getHumanColors();
|
|
|
|
PlayerColor myFirstColor() const;
|
|
|
|
bool isMyColor(PlayerColor color) const;
|
|
|
|
ui8 myFirstId() const; // Used by chat only!
|
|
|
|
|
|
|
|
bool isServerLocal() const;
|
|
|
|
bool isHost() const;
|
|
|
|
bool isGuest() const;
|
|
|
|
|
|
|
|
static ui16 getDefaultPort();
|
|
|
|
static std::string getDefaultPortStr();
|
|
|
|
|
|
|
|
// Lobby server API for UI
|
|
|
|
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-05-29 18:02:23 +02:00
|
|
|
void setPlayerOption(ui8 what, si8 dir, PlayerColor player) const override;
|
2018-01-05 19:21:07 +02:00
|
|
|
void setDifficulty(int to) const override;
|
|
|
|
void setTurnLength(int npos) const override;
|
|
|
|
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-02-12 09:23:39 +02:00
|
|
|
void startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState = nullptr);
|
2018-07-18 23:58:38 +02:00
|
|
|
void endGameplay(bool closeConnection = true, bool restart = false);
|
2023-06-25 20:16:03 +02:00
|
|
|
void startCampaignScenario(std::shared_ptr<CampaignState> cs = {});
|
2023-08-06 16:46:49 +02:00
|
|
|
void showServerError(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();
|
|
|
|
ui8 getLoadMode();
|
|
|
|
|
|
|
|
void debugStartTest(std::string filename, bool save = false);
|
2022-10-01 16:28:45 +02:00
|
|
|
void restoreLastSession();
|
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;
|