1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-26 08:41:13 +02:00
vcmi/client/CServerHandler.h

195 lines
6.8 KiB
C++
Raw Normal View History

/*
* 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/network/NetworkListener.h"
#include "../lib/StartInfo.h"
#include "../lib/CondSh.h"
VCMI_LIB_NAMESPACE_BEGIN
class CConnection;
class PlayerColor;
struct StartInfo;
2023-08-25 18:20:26 +02:00
struct TurnTimerInfo;
class CMapInfo;
2022-09-28 21:15:05 +02:00
class CGameState;
struct ClientPlayer;
struct CPack;
struct CPackForLobby;
struct CPackForClient;
template<typename T> class CApplier;
VCMI_LIB_NAMESPACE_END
class CClient;
class CBaseForLobbyApply;
2023-09-23 02:14:45 +02:00
class HighScoreCalculation;
class HighScoreParameter;
// 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
CONNECTION_FAILED // We could not connect to server
};
class IServerAPI
{
protected:
virtual void sendLobbyPack(const CPackForLobby & pack) const = 0;
public:
virtual ~IServerAPI() {}
virtual void sendClientConnecting() const = 0;
virtual void sendClientDisconnecting() = 0;
virtual void setCampaignState(std::shared_ptr<CampaignState> newCampaign) = 0;
virtual void setCampaignMap(CampaignScenarioID mapId) const = 0;
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;
virtual void setPlayerName(PlayerColor color, const std::string & name) const = 0;
2023-08-17 17:54:53 +02:00
virtual void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const = 0;
virtual void setDifficulty(int to) const = 0;
2023-08-25 18:20:26 +02:00
virtual void setTurnTimerInfo(const TurnTimerInfo &) const = 0;
virtual void setSimturnsInfo(const SimturnsInfo &) const = 0;
2023-12-28 21:48:19 +02:00
virtual void setExtraOptionsInfo(const ExtraOptionsInfo & info) 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;
virtual void sendRestartGame() const = 0;
};
/// structure to handle running server and connecting to it
class CServerHandler : public IServerAPI, public LobbyInfo, public INetworkClientListener, boost::noncopyable
{
friend class ApplyOnLobbyHandlerNetPackVisitor;
2023-12-25 22:26:59 +02:00
std::unique_ptr<NetworkClient> networkClient;
std::shared_ptr<CApplier<CBaseForLobbyApply>> applier;
std::shared_ptr<CMapInfo> mapToStart;
std::vector<std::string> myNames;
2023-09-23 20:41:30 +02:00
std::shared_ptr<HighScoreCalculation> highScoreCalc;
void threadRunNetwork();
void threadRunServer();
void onServerFinished();
void sendLobbyPack(const CPackForLobby & pack) const override;
void onPacketReceived(const std::shared_ptr<NetworkConnection> &, const std::vector<uint8_t> & message) override;
void onConnectionFailed(const std::string & errorMessage) override;
void onConnectionEstablished(const std::shared_ptr<NetworkConnection> &) override;
void onDisconnected(const std::shared_ptr<NetworkConnection> &) override;
void onTimer() override;
2023-12-25 22:26:59 +02:00
void applyPackOnLobbyScreen(CPackForLobby & pack);
std::string serverHostname;
ui16 serverPort;
bool isServerLocal() const;
public:
std::shared_ptr<CConnection> c;
std::atomic<EClientState> state;
////////////////////
// FIXME: Bunch of crutches to glue it all together
// For starting non-custom campaign and continue to next mission
std::shared_ptr<CampaignState> campaignStateToSend;
ui8 screenType; // To create lobby UI only after server is setup
ui8 loadMode; // For saves filtering in SelectionTab
////////////////////
std::unique_ptr<CStopWatch> th;
std::unique_ptr<boost::thread> threadRunLocalServer;
std::unique_ptr<boost::thread> threadNetwork;
CClient * client;
CondSh<bool> campaignServerRestartLock;
CServerHandler();
~CServerHandler();
2022-11-08 02:44:34 +02:00
void resetStateForLobby(const StartInfo::EMode mode, const std::vector<std::string> * names = nullptr);
2023-12-25 22:56:55 +02:00
void startLocalServerAndConnect();
void connectToServer(const std::string & addr, const ui16 port);
// 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 isHost() const;
bool isGuest() const;
const std::string & getCurrentHostname() const;
const std::string & getLocalHostname() const;
const std::string & getRemoteHostname() const;
ui16 getCurrentPort() const;
ui16 getLocalPort() const;
ui16 getRemotePort() const;
// Lobby server API for UI
void sendClientConnecting() const override;
void sendClientDisconnecting() override;
void setCampaignState(std::shared_ptr<CampaignState> newCampaign) override;
void setCampaignMap(CampaignScenarioID mapId) const override;
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;
void setPlayerName(PlayerColor color, const std::string & name) const override;
2023-08-17 18:15:42 +02:00
void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const override;
void setDifficulty(int to) const override;
2023-08-25 18:20:26 +02:00
void setTurnTimerInfo(const TurnTimerInfo &) const override;
void setSimturnsInfo(const SimturnsInfo &) const override;
2023-12-28 21:48:19 +02:00
void setExtraOptionsInfo(const ExtraOptionsInfo &) const override;
void sendMessage(const std::string & txt) const override;
void sendGuiAction(ui8 action) const override;
void sendRestartGame() const override;
void sendStartGame(bool allowOnlyAI = false) const override;
void startMapAfterConnection(std::shared_ptr<CMapInfo> to);
bool validateGameStart(bool allowOnlyAI = false) const;
2023-09-11 18:49:06 +02:00
void debugStartTest(std::string filename, bool save = false);
void startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState = nullptr);
void endGameplay(bool closeConnection = true, bool restart = false);
2023-09-23 00:21:36 +02:00
void startCampaignScenario(HighScoreParameter param, std::shared_ptr<CampaignState> cs = {});
void showServerError(const std::string & txt) const;
// TODO: LobbyState must be updated within game so we should always know how many player interfaces our client handle
int howManyPlayerInterfaces();
ui8 getLoadMode();
void visitForLobby(CPackForLobby & lobbyPack);
void visitForClient(CPackForClient & clientPack);
};
extern CServerHandler * CSH;