2018-01-05 19:21:07 +02:00
|
|
|
/*
|
|
|
|
* CVCMIServer.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/serializer/Connection.h"
|
|
|
|
#include "../lib/StartInfo.h"
|
|
|
|
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
class CMapInfo;
|
|
|
|
|
|
|
|
struct CPackForLobby;
|
|
|
|
struct SharedMemory;
|
|
|
|
|
|
|
|
struct StartInfo;
|
|
|
|
struct LobbyInfo;
|
2020-10-01 10:38:06 +02:00
|
|
|
struct PlayerSettings;
|
2018-01-05 19:21:07 +02:00
|
|
|
class PlayerColor;
|
|
|
|
|
|
|
|
template<typename T> class CApplier;
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
|
|
|
class CGameHandler;
|
2018-01-05 19:21:07 +02:00
|
|
|
class CBaseForServerApply;
|
|
|
|
class CBaseForGHApply;
|
|
|
|
|
|
|
|
enum class EServerState : ui8
|
|
|
|
{
|
|
|
|
LOBBY,
|
2018-04-22 16:40:02 +02:00
|
|
|
GAMEPLAY_STARTING,
|
2018-01-05 19:21:07 +02:00
|
|
|
GAMEPLAY,
|
|
|
|
GAMEPLAY_ENDED,
|
|
|
|
SHUTDOWN
|
|
|
|
};
|
|
|
|
|
|
|
|
class CVCMIServer : public LobbyInfo
|
|
|
|
{
|
|
|
|
std::atomic<bool> restartGameplay; // FIXME: this is just a hack
|
|
|
|
std::shared_ptr<boost::asio::io_service> io;
|
|
|
|
std::shared_ptr<TAcceptor> acceptor;
|
|
|
|
std::shared_ptr<TSocket> upcomingConnection;
|
2018-04-30 17:09:48 +02:00
|
|
|
std::list<std::unique_ptr<CPackForLobby>> announceQueue;
|
2018-01-05 19:21:07 +02:00
|
|
|
boost::recursive_mutex mx;
|
|
|
|
std::shared_ptr<CApplier<CBaseForServerApply>> applier;
|
2022-10-26 22:52:39 +02:00
|
|
|
std::unique_ptr<boost::thread> announceLobbyThread, remoteConnectionsThread;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
std::shared_ptr<CGameHandler> gh;
|
|
|
|
std::atomic<EServerState> state;
|
|
|
|
ui16 port;
|
|
|
|
|
|
|
|
boost::program_options::variables_map cmdLineOptions;
|
|
|
|
std::set<std::shared_ptr<CConnection>> connections;
|
2022-10-01 16:28:45 +02:00
|
|
|
std::set<std::shared_ptr<CConnection>> hangingConnections; //keep connections of players disconnected during the game
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
std::atomic<int> currentClientId;
|
|
|
|
std::atomic<ui8> currentPlayerId;
|
|
|
|
std::shared_ptr<CConnection> hostClient;
|
|
|
|
|
|
|
|
CVCMIServer(boost::program_options::variables_map & opts);
|
|
|
|
~CVCMIServer();
|
|
|
|
void run();
|
2022-09-23 13:02:19 +02:00
|
|
|
bool prepareToStartGame();
|
2022-09-29 19:08:05 +02:00
|
|
|
void prepareToRestart();
|
2018-01-05 19:21:07 +02:00
|
|
|
void startGameImmidiately();
|
|
|
|
|
2022-10-25 03:27:53 +02:00
|
|
|
void establishRemoteConnections();
|
|
|
|
void connectToRemote(const std::string & addr, int port);
|
2018-01-05 19:21:07 +02:00
|
|
|
void startAsyncAccept();
|
|
|
|
void connectionAccepted(const boost::system::error_code & ec);
|
|
|
|
void threadHandleClient(std::shared_ptr<CConnection> c);
|
|
|
|
void threadAnnounceLobby();
|
2018-04-30 17:09:48 +02:00
|
|
|
void handleReceivedPack(std::unique_ptr<CPackForLobby> pack);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2018-04-30 17:09:48 +02:00
|
|
|
void announcePack(std::unique_ptr<CPackForLobby> pack);
|
2018-01-05 19:21:07 +02:00
|
|
|
bool passHost(int toConnectionId);
|
|
|
|
|
|
|
|
void announceTxt(const std::string & txt, const std::string & playerName = "system");
|
2022-09-23 13:02:19 +02:00
|
|
|
void announceMessage(const std::string & txt);
|
2018-04-30 17:09:48 +02:00
|
|
|
void addToAnnounceQueue(std::unique_ptr<CPackForLobby> pack);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
void setPlayerConnectedId(PlayerSettings & pset, ui8 player) const;
|
|
|
|
void updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpt = {});
|
|
|
|
|
|
|
|
void clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, std::string uuid, StartInfo::EMode mode);
|
|
|
|
void clientDisconnected(std::shared_ptr<CConnection> c);
|
2022-10-01 16:28:45 +02:00
|
|
|
void reconnectPlayer(int connId);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
void updateAndPropagateLobbyState();
|
|
|
|
|
|
|
|
// Work with LobbyInfo
|
|
|
|
void setPlayer(PlayerColor clickedColor);
|
|
|
|
void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
|
|
|
|
int nextAllowedHero(PlayerColor player, int min, int max, int incl, int dir);
|
|
|
|
bool canUseThisHero(PlayerColor player, int ID);
|
|
|
|
std::vector<int> getUsedHeroes();
|
|
|
|
void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
|
|
|
|
void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
|
|
|
|
|
|
|
|
// Campaigns
|
|
|
|
void setCampaignMap(int mapId);
|
|
|
|
void setCampaignBonus(int bonusId);
|
|
|
|
|
|
|
|
ui8 getIdOfFirstUnallocatedPlayer() const;
|
|
|
|
|
|
|
|
#ifdef VCMI_ANDROID
|
|
|
|
static void create();
|
2022-09-16 15:43:21 +02:00
|
|
|
#elif defined(SINGLE_PROCESS_APP)
|
2022-07-28 11:40:06 +02:00
|
|
|
static void create(boost::condition_variable * cond, const std::string & uuid);
|
2018-01-05 19:21:07 +02:00
|
|
|
#endif
|
|
|
|
};
|