2018-01-05 19:21:07 +02:00
|
|
|
/*
|
|
|
|
* CServerHandler.cpp, 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
|
|
|
|
#include "CServerHandler.h"
|
|
|
|
#include "Client.h"
|
|
|
|
#include "CGameInfo.h"
|
|
|
|
#include "CPlayerInterface.h"
|
|
|
|
#include "gui/CGuiHandler.h"
|
2023-05-16 14:10:26 +02:00
|
|
|
#include "gui/WindowHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
#include "lobby/CSelectionBase.h"
|
|
|
|
#include "lobby/CLobbyScreen.h"
|
2022-09-22 13:39:43 +02:00
|
|
|
#include "windows/InfoWindows.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
#include "mainmenu/CMainMenu.h"
|
|
|
|
|
2021-03-02 05:56:57 +02:00
|
|
|
#ifdef VCMI_ANDROID
|
2018-05-25 21:08:14 +02:00
|
|
|
#include "../lib/CAndroidVMHelper.h"
|
2022-07-26 15:16:37 +02:00
|
|
|
#elif defined(VCMI_IOS)
|
2022-08-16 11:54:44 +02:00
|
|
|
#include "ios/utils.h"
|
|
|
|
#include <dispatch/dispatch.h>
|
2022-07-26 15:16:37 +02:00
|
|
|
#else
|
2021-03-02 05:56:57 +02:00
|
|
|
#include "../lib/Interprocess.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#endif
|
2022-12-05 21:36:02 +02:00
|
|
|
|
|
|
|
#ifdef SINGLE_PROCESS_APP
|
|
|
|
#include "../server/CVCMIServer.h"
|
|
|
|
#endif
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/CConfigHandler.h"
|
|
|
|
#include "../lib/CGeneralTextHandler.h"
|
|
|
|
#include "../lib/CThreadHelper.h"
|
2023-02-12 09:23:39 +02:00
|
|
|
#include "../lib/NetPackVisitor.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/StartInfo.h"
|
|
|
|
#include "../lib/VCMIDirs.h"
|
|
|
|
#include "../lib/mapping/CCampaignHandler.h"
|
|
|
|
#include "../lib/mapping/CMapInfo.h"
|
|
|
|
#include "../lib/mapObjects/MiscObjects.h"
|
|
|
|
#include "../lib/rmg/CMapGenOptions.h"
|
2023-03-15 21:34:29 +02:00
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/registerTypes/RegisterTypes.h"
|
|
|
|
#include "../lib/serializer/Connection.h"
|
|
|
|
#include "../lib/serializer/CMemorySerializer.h"
|
|
|
|
|
|
|
|
#include <boost/uuid/uuid.hpp>
|
|
|
|
#include <boost/uuid/uuid_io.hpp>
|
|
|
|
#include <boost/uuid/uuid_generators.hpp>
|
2018-08-13 23:48:00 +02:00
|
|
|
#include "../lib/serializer/Cast.h"
|
2023-02-12 09:23:39 +02:00
|
|
|
#include "LobbyClientNetPackVisitors.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
#include <vcmi/events/EventBus.h>
|
|
|
|
|
2022-09-10 15:03:54 +02:00
|
|
|
#ifdef VCMI_WINDOWS
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
template<typename T> class CApplyOnLobby;
|
|
|
|
|
2022-10-01 16:41:12 +02:00
|
|
|
const std::string CServerHandler::localhostAddress{"127.0.0.1"};
|
|
|
|
|
2023-02-26 11:18:24 +02:00
|
|
|
#if defined(VCMI_ANDROID) && !defined(SINGLE_PROCESS_APP)
|
2018-05-25 21:08:14 +02:00
|
|
|
extern std::atomic_bool androidTestServerReadyFlag;
|
|
|
|
#endif
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
class CBaseForLobbyApply
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool applyOnLobbyHandler(CServerHandler * handler, void * pack) const = 0;
|
|
|
|
virtual void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler, void * pack) const = 0;
|
|
|
|
virtual ~CBaseForLobbyApply(){};
|
|
|
|
template<typename U> static CBaseForLobbyApply * getApplier(const U * t = nullptr)
|
|
|
|
{
|
|
|
|
return new CApplyOnLobby<U>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> class CApplyOnLobby : public CBaseForLobbyApply
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool applyOnLobbyHandler(CServerHandler * handler, void * pack) const override
|
|
|
|
{
|
|
|
|
T * ptr = static_cast<T *>(pack);
|
2023-02-12 09:23:39 +02:00
|
|
|
ApplyOnLobbyHandlerNetPackVisitor visitor(*handler);
|
|
|
|
|
2022-07-28 11:29:39 +02:00
|
|
|
logNetwork->trace("\tImmediately apply on lobby: %s", typeList.getTypeInfo(ptr)->name());
|
2023-02-12 09:23:39 +02:00
|
|
|
ptr->visit(visitor);
|
|
|
|
|
|
|
|
return visitor.getResult();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler, void * pack) const override
|
|
|
|
{
|
|
|
|
T * ptr = static_cast<T *>(pack);
|
2023-02-12 09:23:39 +02:00
|
|
|
ApplyOnLobbyScreenNetPackVisitor visitor(*handler, lobby);
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
logNetwork->trace("\tApply on lobby from queue: %s", typeList.getTypeInfo(ptr)->name());
|
2023-02-12 09:23:39 +02:00
|
|
|
ptr->visit(visitor);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> class CApplyOnLobby<CPack>: public CBaseForLobbyApply
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool applyOnLobbyHandler(CServerHandler * handler, void * pack) const override
|
|
|
|
{
|
|
|
|
logGlobal->error("Cannot apply plain CPack!");
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler, void * pack) const override
|
|
|
|
{
|
|
|
|
logGlobal->error("Cannot apply plain CPack!");
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-30 00:03:50 +02:00
|
|
|
static const std::string NAME_AFFIX = "client";
|
|
|
|
static const std::string NAME = GameConstants::VCMI_VERSION + std::string(" (") + NAME_AFFIX + ')'; //application name
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
CServerHandler::CServerHandler()
|
2019-03-30 01:28:33 +02:00
|
|
|
: state(EClientState::NONE), mx(std::make_shared<boost::recursive_mutex>()), client(nullptr), loadMode(0), campaignStateToSend(nullptr), campaignServerRestartLock(false)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2022-11-08 02:44:34 +02:00
|
|
|
uuid = boost::uuids::to_string(boost::uuids::random_generator()());
|
|
|
|
//read from file to restore last session
|
|
|
|
if(!settings["server"]["uuid"].isNull() && !settings["server"]["uuid"].String().empty())
|
2022-10-25 03:27:53 +02:00
|
|
|
uuid = settings["server"]["uuid"].String();
|
2018-01-05 19:21:07 +02:00
|
|
|
applier = std::make_shared<CApplier<CBaseForLobbyApply>>();
|
|
|
|
registerTypesLobbyPacks(*applier);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::resetStateForLobby(const StartInfo::EMode mode, const std::vector<std::string> * names)
|
|
|
|
{
|
|
|
|
hostClientId = -1;
|
|
|
|
state = EClientState::NONE;
|
2022-12-07 23:36:20 +02:00
|
|
|
th = std::make_unique<CStopWatch>();
|
2018-01-05 19:21:07 +02:00
|
|
|
packsForLobbyScreen.clear();
|
|
|
|
c.reset();
|
2018-04-30 17:09:48 +02:00
|
|
|
si = std::make_shared<StartInfo>();
|
2018-01-05 19:21:07 +02:00
|
|
|
playerNames.clear();
|
|
|
|
si->difficulty = 1;
|
|
|
|
si->mode = mode;
|
|
|
|
myNames.clear();
|
|
|
|
if(names && !names->empty()) //if have custom set of player names - use it
|
|
|
|
myNames = *names;
|
|
|
|
else
|
|
|
|
myNames.push_back(settings["general"]["playerName"].String());
|
|
|
|
|
2022-12-05 21:36:02 +02:00
|
|
|
#if !defined(VCMI_ANDROID) && !defined(SINGLE_PROCESS_APP)
|
2018-01-05 19:21:07 +02:00
|
|
|
shm.reset();
|
|
|
|
|
|
|
|
if(!settings["session"]["disable-shm"].Bool())
|
|
|
|
{
|
|
|
|
std::string sharedMemoryName = "vcmi_memory";
|
|
|
|
if(settings["session"]["enable-shm-uuid"].Bool())
|
|
|
|
{
|
|
|
|
//used or automated testing when multiple clients start simultaneously
|
|
|
|
sharedMemoryName += "_" + uuid;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
shm = std::make_shared<SharedMemory>(sharedMemoryName, true);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
shm.reset();
|
|
|
|
logNetwork->error("Cannot open interprocess memory. Continue without it...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::startLocalServerAndConnect()
|
|
|
|
{
|
|
|
|
if(threadRunLocalServer)
|
|
|
|
threadRunLocalServer->join();
|
|
|
|
|
|
|
|
th->update();
|
2022-09-22 03:06:49 +02:00
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
auto errorMsg = CGI->generaltexth->translate("vcmi.server.errors.existingProcess");
|
2022-09-22 03:06:49 +02:00
|
|
|
try
|
|
|
|
{
|
2022-10-01 16:41:12 +02:00
|
|
|
CConnection testConnection(localhostAddress, getDefaultPort(), NAME, uuid);
|
2022-09-22 18:06:14 +02:00
|
|
|
logNetwork->error("Port is busy, check if another instance of vcmiserver is working");
|
|
|
|
CInfoWindow::showInfoDialog(errorMsg, {});
|
2022-09-22 03:06:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
//no connection means that port is not busy and we can start local server
|
|
|
|
}
|
|
|
|
|
2023-02-26 11:18:24 +02:00
|
|
|
#if defined(SINGLE_PROCESS_APP)
|
2022-08-15 15:31:58 +02:00
|
|
|
boost::condition_variable cond;
|
2023-03-09 15:36:46 +02:00
|
|
|
std::vector<std::string> args{"--uuid=" + uuid, "--port=" + std::to_string(getHostPort())};
|
2022-11-08 16:05:47 +02:00
|
|
|
if(settings["session"]["lobby"].Bool() && settings["session"]["host"].Bool())
|
|
|
|
{
|
|
|
|
args.push_back("--lobby=" + settings["session"]["address"].String());
|
|
|
|
args.push_back("--connections=" + settings["session"]["hostConnections"].String());
|
2023-03-09 15:36:46 +02:00
|
|
|
args.push_back("--lobby-port=" + std::to_string(settings["session"]["port"].Integer()));
|
2022-11-08 16:05:47 +02:00
|
|
|
args.push_back("--lobby-uuid=" + settings["session"]["hostUuid"].String());
|
|
|
|
}
|
|
|
|
threadRunLocalServer = std::make_shared<boost::thread>([&cond, args, this] {
|
2022-08-15 15:31:58 +02:00
|
|
|
setThreadName("CVCMIServer");
|
2022-11-08 16:05:47 +02:00
|
|
|
CVCMIServer::create(&cond, args);
|
2022-09-16 15:43:21 +02:00
|
|
|
onServerFinished();
|
2022-08-15 15:31:58 +02:00
|
|
|
});
|
|
|
|
threadRunLocalServer->detach();
|
2023-02-26 11:18:24 +02:00
|
|
|
#elif defined(VCMI_ANDROID)
|
|
|
|
{
|
|
|
|
CAndroidVMHelper envHelper;
|
|
|
|
envHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "startServer", true);
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
#else
|
|
|
|
threadRunLocalServer = std::make_shared<boost::thread>(&CServerHandler::threadRunServer, this); //runs server executable;
|
|
|
|
#endif
|
|
|
|
logNetwork->trace("Setting up thread calling server: %d ms", th->getDiff());
|
|
|
|
|
|
|
|
th->update();
|
|
|
|
|
2023-02-26 11:18:24 +02:00
|
|
|
#ifdef SINGLE_PROCESS_APP
|
2022-08-15 15:31:58 +02:00
|
|
|
{
|
2022-09-16 15:43:21 +02:00
|
|
|
#ifdef VCMI_IOS
|
2022-08-16 11:54:44 +02:00
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
iOS_utils::showLoadingIndicator();
|
|
|
|
});
|
2022-09-16 15:43:21 +02:00
|
|
|
#endif
|
2022-08-16 11:54:44 +02:00
|
|
|
|
2022-08-15 15:31:58 +02:00
|
|
|
boost::mutex m;
|
|
|
|
boost::unique_lock<boost::mutex> lock{m};
|
|
|
|
logNetwork->info("waiting for server");
|
|
|
|
cond.wait(lock);
|
|
|
|
logNetwork->info("server is ready");
|
2022-08-16 11:54:44 +02:00
|
|
|
|
2022-09-16 15:43:21 +02:00
|
|
|
#ifdef VCMI_IOS
|
2022-08-16 11:54:44 +02:00
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
iOS_utils::hideLoadingIndicator();
|
|
|
|
});
|
2022-09-16 15:43:21 +02:00
|
|
|
#endif
|
2022-08-15 15:31:58 +02:00
|
|
|
}
|
2023-02-26 11:18:24 +02:00
|
|
|
#elif defined(VCMI_ANDROID)
|
|
|
|
logNetwork->info("waiting for server");
|
|
|
|
while(!androidTestServerReadyFlag.load())
|
|
|
|
{
|
|
|
|
logNetwork->info("still waiting...");
|
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
|
|
|
|
}
|
|
|
|
logNetwork->info("waiting for server finished...");
|
|
|
|
androidTestServerReadyFlag = false;
|
2021-03-02 05:56:57 +02:00
|
|
|
#else
|
|
|
|
if(shm)
|
|
|
|
shm->sr->waitTillReady();
|
2018-01-05 19:21:07 +02:00
|
|
|
#endif
|
|
|
|
logNetwork->trace("Waiting for server: %d ms", th->getDiff());
|
|
|
|
|
|
|
|
th->update(); //put breakpoint here to attach to server before it does something stupid
|
|
|
|
|
2023-02-27 12:00:13 +02:00
|
|
|
#if !defined(VCMI_MOBILE)
|
2022-07-28 11:31:26 +02:00
|
|
|
const ui16 port = shm ? shm->sr->port : 0;
|
2018-01-05 19:21:07 +02:00
|
|
|
#else
|
2022-07-28 11:31:26 +02:00
|
|
|
const ui16 port = 0;
|
2018-01-05 19:21:07 +02:00
|
|
|
#endif
|
2022-10-01 16:41:12 +02:00
|
|
|
justConnectToServer(localhostAddress, port);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
logNetwork->trace("\tConnecting to the server: %d ms", th->getDiff());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::justConnectToServer(const std::string & addr, const ui16 port)
|
|
|
|
{
|
|
|
|
state = EClientState::CONNECTING;
|
|
|
|
while(!c && state != EClientState::CONNECTION_CANCELLED)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
logNetwork->info("Establishing connection...");
|
|
|
|
c = std::make_shared<CConnection>(
|
2022-11-08 02:44:34 +02:00
|
|
|
addr.size() ? addr : getHostAddress(),
|
|
|
|
port ? port : getHostPort(),
|
2018-01-05 19:21:07 +02:00
|
|
|
NAME, uuid);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
logNetwork->error("\nCannot establish connection! Retrying within 1 second");
|
|
|
|
boost::this_thread::sleep(boost::posix_time::seconds(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(state == EClientState::CONNECTION_CANCELLED)
|
2022-10-01 16:41:12 +02:00
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
logNetwork->info("Connection aborted by player!");
|
2022-10-01 16:41:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->handler = std::make_shared<boost::thread>(&CServerHandler::threadHandleConnection, this);
|
|
|
|
|
2022-11-08 02:44:34 +02:00
|
|
|
if(!addr.empty() && addr != getHostAddress())
|
2022-10-30 17:59:43 +02:00
|
|
|
{
|
|
|
|
Settings serverAddress = settings.write["server"]["server"];
|
|
|
|
serverAddress->String() = addr;
|
|
|
|
}
|
2022-11-08 02:44:34 +02:00
|
|
|
if(port && port != getHostPort())
|
2022-10-30 17:59:43 +02:00
|
|
|
{
|
|
|
|
Settings serverPort = settings.write["server"]["port"];
|
|
|
|
serverPort->Integer() = port;
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::applyPacksOnLobbyScreen()
|
|
|
|
{
|
|
|
|
if(!c || !c->handler)
|
|
|
|
return;
|
|
|
|
|
|
|
|
boost::unique_lock<boost::recursive_mutex> lock(*mx);
|
|
|
|
while(!packsForLobbyScreen.empty())
|
|
|
|
{
|
|
|
|
CPackForLobby * pack = packsForLobbyScreen.front();
|
|
|
|
packsForLobbyScreen.pop_front();
|
|
|
|
CBaseForLobbyApply * apply = applier->getApplier(typeList.getTypeID(pack)); //find the applier
|
|
|
|
apply->applyOnLobbyScreen(static_cast<CLobbyScreen *>(SEL), this, pack);
|
2023-05-16 14:10:26 +02:00
|
|
|
GH.windows().totalRedraw();
|
2018-01-05 19:21:07 +02:00
|
|
|
delete pack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::stopServerConnection()
|
|
|
|
{
|
|
|
|
if(c->handler)
|
|
|
|
{
|
|
|
|
while(!c->handler->timed_join(boost::posix_time::milliseconds(50)))
|
|
|
|
applyPacksOnLobbyScreen();
|
|
|
|
c->handler->join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<PlayerColor> CServerHandler::getHumanColors()
|
|
|
|
{
|
|
|
|
return clientHumanColors(c->connectionID);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PlayerColor CServerHandler::myFirstColor() const
|
|
|
|
{
|
|
|
|
return clientFirstColor(c->connectionID);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CServerHandler::isMyColor(PlayerColor color) const
|
|
|
|
{
|
|
|
|
return isClientColor(c->connectionID, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui8 CServerHandler::myFirstId() const
|
|
|
|
{
|
|
|
|
return clientFirstId(c->connectionID);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CServerHandler::isServerLocal() const
|
|
|
|
{
|
|
|
|
if(threadRunLocalServer)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CServerHandler::isHost() const
|
|
|
|
{
|
|
|
|
return c && hostClientId == c->connectionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CServerHandler::isGuest() const
|
|
|
|
{
|
|
|
|
return !c || hostClientId != c->connectionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui16 CServerHandler::getDefaultPort()
|
|
|
|
{
|
2022-10-25 03:27:53 +02:00
|
|
|
return static_cast<ui16>(settings["server"]["port"].Integer());
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CServerHandler::getDefaultPortStr()
|
|
|
|
{
|
2023-03-09 15:36:46 +02:00
|
|
|
return std::to_string(getDefaultPort());
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 02:44:34 +02:00
|
|
|
std::string CServerHandler::getHostAddress() const
|
|
|
|
{
|
|
|
|
if(settings["session"]["lobby"].isNull() || !settings["session"]["lobby"].Bool())
|
|
|
|
return settings["server"]["server"].String();
|
|
|
|
|
|
|
|
if(settings["session"]["host"].Bool())
|
|
|
|
return localhostAddress;
|
|
|
|
|
|
|
|
return settings["session"]["address"].String();
|
|
|
|
}
|
|
|
|
|
|
|
|
ui16 CServerHandler::getHostPort() const
|
|
|
|
{
|
|
|
|
if(settings["session"]["lobby"].isNull() || !settings["session"]["lobby"].Bool())
|
|
|
|
return getDefaultPort();
|
|
|
|
|
|
|
|
if(settings["session"]["host"].Bool())
|
|
|
|
return getDefaultPort();
|
|
|
|
|
|
|
|
return settings["session"]["port"].Integer();
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CServerHandler::sendClientConnecting() const
|
|
|
|
{
|
|
|
|
LobbyClientConnected lcc;
|
|
|
|
lcc.uuid = uuid;
|
|
|
|
lcc.names = myNames;
|
|
|
|
lcc.mode = si->mode;
|
|
|
|
sendLobbyPack(lcc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::sendClientDisconnecting()
|
|
|
|
{
|
|
|
|
// FIXME: This is workaround needed to make sure client not trying to sent anything to non existed server
|
|
|
|
if(state == EClientState::DISCONNECTING)
|
|
|
|
return;
|
|
|
|
|
|
|
|
state = EClientState::DISCONNECTING;
|
|
|
|
LobbyClientDisconnected lcd;
|
|
|
|
lcd.clientId = c->connectionID;
|
|
|
|
logNetwork->info("Connection has been requested to be closed.");
|
|
|
|
if(isServerLocal())
|
|
|
|
{
|
|
|
|
lcd.shutdownServer = true;
|
|
|
|
logNetwork->info("Sent closing signal to the server");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logNetwork->info("Sent leaving signal to the server");
|
|
|
|
}
|
|
|
|
sendLobbyPack(lcd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::setCampaignState(std::shared_ptr<CCampaignState> newCampaign)
|
|
|
|
{
|
|
|
|
state = EClientState::LOBBY_CAMPAIGN;
|
|
|
|
LobbySetCampaign lsc;
|
|
|
|
lsc.ourCampaign = newCampaign;
|
|
|
|
sendLobbyPack(lsc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::setCampaignMap(int mapId) const
|
|
|
|
{
|
|
|
|
if(state == EClientState::GAMEPLAY) // FIXME: UI shouldn't sent commands in first place
|
|
|
|
return;
|
|
|
|
|
|
|
|
LobbySetCampaignMap lscm;
|
|
|
|
lscm.mapId = mapId;
|
|
|
|
sendLobbyPack(lscm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::setCampaignBonus(int bonusId) const
|
|
|
|
{
|
|
|
|
if(state == EClientState::GAMEPLAY) // FIXME: UI shouldn't sent commands in first place
|
|
|
|
return;
|
|
|
|
|
|
|
|
LobbySetCampaignBonus lscb;
|
|
|
|
lscb.bonusId = bonusId;
|
|
|
|
sendLobbyPack(lscb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::setMapInfo(std::shared_ptr<CMapInfo> to, std::shared_ptr<CMapGenOptions> mapGenOpts) const
|
|
|
|
{
|
|
|
|
LobbySetMap lsm;
|
|
|
|
lsm.mapInfo = to;
|
|
|
|
lsm.mapGenOpts = mapGenOpts;
|
|
|
|
sendLobbyPack(lsm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::setPlayer(PlayerColor color) const
|
|
|
|
{
|
|
|
|
LobbySetPlayer lsp;
|
|
|
|
lsp.clickedColor = color;
|
|
|
|
sendLobbyPack(lsp);
|
|
|
|
}
|
|
|
|
|
2023-05-29 18:02:23 +02:00
|
|
|
void CServerHandler::setPlayerOption(ui8 what, si8 dir, PlayerColor player) const
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
LobbyChangePlayerOption lcpo;
|
|
|
|
lcpo.what = what;
|
|
|
|
lcpo.direction = dir;
|
|
|
|
lcpo.color = player;
|
|
|
|
sendLobbyPack(lcpo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::setDifficulty(int to) const
|
|
|
|
{
|
|
|
|
LobbySetDifficulty lsd;
|
|
|
|
lsd.difficulty = to;
|
|
|
|
sendLobbyPack(lsd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::setTurnLength(int npos) const
|
|
|
|
{
|
|
|
|
vstd::amin(npos, GameConstants::POSSIBLE_TURNTIME.size() - 1);
|
|
|
|
LobbySetTurnTime lstt;
|
|
|
|
lstt.turnTime = GameConstants::POSSIBLE_TURNTIME[npos];
|
|
|
|
sendLobbyPack(lstt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::sendMessage(const std::string & txt) const
|
|
|
|
{
|
|
|
|
std::istringstream readed;
|
|
|
|
readed.str(txt);
|
|
|
|
std::string command;
|
|
|
|
readed >> command;
|
|
|
|
if(command == "!passhost")
|
|
|
|
{
|
|
|
|
std::string id;
|
|
|
|
readed >> id;
|
|
|
|
if(id.length())
|
|
|
|
{
|
|
|
|
LobbyChangeHost lch;
|
|
|
|
lch.newHostConnectionId = boost::lexical_cast<int>(id);
|
|
|
|
sendLobbyPack(lch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(command == "!forcep")
|
|
|
|
{
|
|
|
|
std::string connectedId, playerColorId;
|
|
|
|
readed >> connectedId;
|
|
|
|
readed >> playerColorId;
|
2022-11-13 04:35:16 +02:00
|
|
|
if(connectedId.length() && playerColorId.length())
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
ui8 connected = boost::lexical_cast<int>(connectedId);
|
|
|
|
auto color = PlayerColor(boost::lexical_cast<int>(playerColorId));
|
|
|
|
if(color.isValidPlayer() && playerNames.find(connected) != playerNames.end())
|
|
|
|
{
|
|
|
|
LobbyForceSetPlayer lfsp;
|
|
|
|
lfsp.targetConnectedPlayer = connected;
|
|
|
|
lfsp.targetPlayerColor = color;
|
|
|
|
sendLobbyPack(lfsp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LobbyChatMessage lcm;
|
|
|
|
lcm.message = txt;
|
|
|
|
lcm.playerName = playerNames.find(myFirstId())->second.name;
|
|
|
|
sendLobbyPack(lcm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::sendGuiAction(ui8 action) const
|
|
|
|
{
|
|
|
|
LobbyGuiAction lga;
|
|
|
|
lga.action = static_cast<LobbyGuiAction::EAction>(action);
|
|
|
|
sendLobbyPack(lga);
|
|
|
|
}
|
|
|
|
|
2022-09-29 19:08:05 +02:00
|
|
|
void CServerHandler::sendRestartGame() const
|
|
|
|
{
|
|
|
|
LobbyEndGame endGame;
|
|
|
|
endGame.closeConnection = false;
|
|
|
|
endGame.restart = true;
|
|
|
|
sendLobbyPack(endGame);
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CServerHandler::sendStartGame(bool allowOnlyAI) const
|
|
|
|
{
|
|
|
|
verifyStateBeforeStart(allowOnlyAI ? true : settings["session"]["onlyai"].Bool());
|
|
|
|
LobbyStartGame lsg;
|
2021-01-20 20:29:50 +02:00
|
|
|
if(client)
|
|
|
|
{
|
|
|
|
lsg.initializedStartInfo = std::make_shared<StartInfo>(* const_cast<StartInfo *>(client->getStartInfo(true)));
|
|
|
|
lsg.initializedStartInfo->mode = StartInfo::NEW_GAME;
|
|
|
|
lsg.initializedStartInfo->seedToBeUsed = lsg.initializedStartInfo->seedPostInit = 0;
|
|
|
|
* si = * lsg.initializedStartInfo;
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
sendLobbyPack(lsg);
|
2022-09-28 21:38:41 +02:00
|
|
|
c->enterLobbyConnectionMode();
|
|
|
|
c->disableStackSendingByID();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void CServerHandler::startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2018-04-07 13:42:11 +02:00
|
|
|
if(CMM)
|
|
|
|
CMM->disable();
|
2018-01-05 19:21:07 +02:00
|
|
|
client = new CClient();
|
|
|
|
|
|
|
|
switch(si->mode)
|
|
|
|
{
|
|
|
|
case StartInfo::NEW_GAME:
|
2022-09-28 21:15:05 +02:00
|
|
|
client->newGame(gameState);
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
|
|
|
case StartInfo::CAMPAIGN:
|
2022-09-28 21:15:05 +02:00
|
|
|
client->newGame(gameState);
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
|
|
|
case StartInfo::LOAD_GAME:
|
2022-09-29 19:33:44 +02:00
|
|
|
client->loadGame(gameState);
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("Invalid mode");
|
|
|
|
}
|
|
|
|
// After everything initialized we can accept CPackToClient netpacks
|
|
|
|
c->enterGameplayConnectionMode(client->gameState());
|
|
|
|
state = EClientState::GAMEPLAY;
|
2022-10-01 16:28:45 +02:00
|
|
|
|
|
|
|
//store settings to continue game
|
|
|
|
if(!isServerLocal() && isGuest())
|
|
|
|
{
|
|
|
|
Settings saveSession = settings.write["server"]["reconnect"];
|
|
|
|
saveSession->Bool() = true;
|
|
|
|
Settings saveUuid = settings.write["server"]["uuid"];
|
|
|
|
saveUuid->String() = uuid;
|
2022-10-04 03:09:03 +02:00
|
|
|
Settings saveNames = settings.write["server"]["names"];
|
2022-10-04 03:42:14 +02:00
|
|
|
saveNames->Vector().clear();
|
2022-10-04 03:09:03 +02:00
|
|
|
for(auto & name : myNames)
|
|
|
|
{
|
|
|
|
JsonNode jsonName;
|
|
|
|
jsonName.String() = name;
|
|
|
|
saveNames->Vector().push_back(jsonName);
|
|
|
|
}
|
2022-10-01 16:28:45 +02:00
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 23:58:38 +02:00
|
|
|
void CServerHandler::endGameplay(bool closeConnection, bool restart)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
client->endGame();
|
|
|
|
vstd::clear_pointer(client);
|
|
|
|
|
2018-04-30 17:09:48 +02:00
|
|
|
if(closeConnection)
|
|
|
|
{
|
|
|
|
// Game is ending
|
|
|
|
// Tell the network thread to reach a stable state
|
|
|
|
CSH->sendClientDisconnecting();
|
|
|
|
logNetwork->info("Closed connection.");
|
|
|
|
}
|
2018-07-18 23:58:38 +02:00
|
|
|
if(!restart)
|
2018-04-07 13:42:11 +02:00
|
|
|
{
|
2018-07-18 23:58:38 +02:00
|
|
|
if(CMM)
|
|
|
|
{
|
2019-03-29 23:01:51 +02:00
|
|
|
GH.terminate_cond->setn(false);
|
2018-07-25 00:36:48 +02:00
|
|
|
GH.curInt = CMM.get();
|
2018-07-18 23:58:38 +02:00
|
|
|
CMM->enable();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-25 00:36:48 +02:00
|
|
|
GH.curInt = CMainMenu::create().get();
|
2018-07-18 23:58:38 +02:00
|
|
|
}
|
2018-04-07 13:42:11 +02:00
|
|
|
}
|
2022-09-29 19:08:05 +02:00
|
|
|
|
2022-09-29 19:18:30 +02:00
|
|
|
c->enterLobbyConnectionMode();
|
|
|
|
c->disableStackSendingByID();
|
2022-10-01 16:28:45 +02:00
|
|
|
|
|
|
|
//reset settings
|
|
|
|
Settings saveSession = settings.write["server"]["reconnect"];
|
|
|
|
saveSession->Bool() = false;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::startCampaignScenario(std::shared_ptr<CCampaignState> cs)
|
|
|
|
{
|
|
|
|
if(cs)
|
2023-02-02 18:35:01 +02:00
|
|
|
GH.pushUserEvent(EUserEvent::CAMPAIGN_START_SCENARIO, CMemorySerializer::deepCopy(*cs.get()).release());
|
2018-01-05 19:21:07 +02:00
|
|
|
else
|
2023-02-02 18:35:01 +02:00
|
|
|
GH.pushUserEvent(EUserEvent::CAMPAIGN_START_SCENARIO, CMemorySerializer::deepCopy(*si->campState.get()).release());
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 13:02:19 +02:00
|
|
|
void CServerHandler::showServerError(std::string txt)
|
|
|
|
{
|
|
|
|
CInfoWindow::showInfoDialog(txt, {});
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
int CServerHandler::howManyPlayerInterfaces()
|
|
|
|
{
|
|
|
|
int playerInts = 0;
|
|
|
|
for(auto pint : client->playerint)
|
|
|
|
{
|
|
|
|
if(dynamic_cast<CPlayerInterface *>(pint.second.get()))
|
|
|
|
playerInts++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return playerInts;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui8 CServerHandler::getLoadMode()
|
|
|
|
{
|
|
|
|
if(state == EClientState::GAMEPLAY)
|
|
|
|
{
|
|
|
|
if(si->campState)
|
|
|
|
return ELoadMode::CAMPAIGN;
|
|
|
|
for(auto pn : playerNames)
|
|
|
|
{
|
|
|
|
if(pn.second.connection != c->connectionID)
|
|
|
|
return ELoadMode::MULTI;
|
|
|
|
}
|
2019-05-04 15:56:17 +02:00
|
|
|
if(howManyPlayerInterfaces() > 1) //this condition will work for hotseat mode OR multiplayer with allowed more than 1 color per player to control
|
|
|
|
return ELoadMode::MULTI;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
return ELoadMode::SINGLE;
|
|
|
|
}
|
|
|
|
return loadMode;
|
|
|
|
}
|
|
|
|
|
2022-10-01 16:28:45 +02:00
|
|
|
void CServerHandler::restoreLastSession()
|
|
|
|
{
|
2022-10-04 03:09:03 +02:00
|
|
|
auto loadSession = [this]()
|
|
|
|
{
|
2022-10-04 18:54:40 +02:00
|
|
|
uuid = settings["server"]["uuid"].String();
|
2022-10-04 03:09:03 +02:00
|
|
|
for(auto & name : settings["server"]["names"].Vector())
|
|
|
|
myNames.push_back(name.String());
|
|
|
|
resetStateForLobby(StartInfo::LOAD_GAME, &myNames);
|
|
|
|
screenType = ESelectionScreen::loadGame;
|
2022-10-04 16:45:54 +02:00
|
|
|
justConnectToServer(settings["server"]["server"].String(), settings["server"]["port"].Integer());
|
2022-10-04 03:09:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto cleanUpSession = []()
|
|
|
|
{
|
|
|
|
//reset settings
|
|
|
|
Settings saveSession = settings.write["server"]["reconnect"];
|
|
|
|
saveSession->Bool() = false;
|
|
|
|
};
|
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
CInfoWindow::showYesNoDialog(VLC->generaltexth->translate("vcmi.server.confirmReconnect"), {}, loadSession, cleanUpSession);
|
2022-10-01 16:28:45 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CServerHandler::debugStartTest(std::string filename, bool save)
|
|
|
|
{
|
|
|
|
logGlobal->info("Starting debug test with file: %s", filename);
|
|
|
|
auto mapInfo = std::make_shared<CMapInfo>();
|
|
|
|
if(save)
|
|
|
|
{
|
|
|
|
resetStateForLobby(StartInfo::LOAD_GAME);
|
|
|
|
mapInfo->saveInit(ResourceID(filename, EResType::CLIENT_SAVEGAME));
|
|
|
|
screenType = ESelectionScreen::loadGame;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resetStateForLobby(StartInfo::NEW_GAME);
|
|
|
|
mapInfo->mapInit(filename);
|
|
|
|
screenType = ESelectionScreen::newGame;
|
|
|
|
}
|
|
|
|
if(settings["session"]["donotstartserver"].Bool())
|
2022-10-01 16:41:12 +02:00
|
|
|
justConnectToServer(localhostAddress, 3030);
|
2018-01-05 19:21:07 +02:00
|
|
|
else
|
|
|
|
startLocalServerAndConnect();
|
|
|
|
|
2021-05-16 14:39:38 +02:00
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
|
|
|
|
|
2023-05-16 17:34:23 +02:00
|
|
|
while(!settings["session"]["headless"].Bool() && !GH.windows().topWindow<CLobbyScreen>())
|
2018-01-05 19:21:07 +02:00
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
|
2023-05-16 17:34:23 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
while(!mi || mapInfo->fileURI != CSH->mi->fileURI)
|
|
|
|
{
|
|
|
|
setMapInfo(mapInfo);
|
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
|
|
|
|
}
|
|
|
|
// "Click" on color to remove us from it
|
|
|
|
setPlayer(myFirstColor());
|
|
|
|
while(myFirstColor() != PlayerColor::CANNOT_DETERMINE)
|
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sendStartGame();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
class ServerHandlerCPackVisitor : public VCMI_LIB_WRAP_NAMESPACE(ICPackVisitor)
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
CServerHandler & handler;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ServerHandlerCPackVisitor(CServerHandler & handler)
|
|
|
|
:handler(handler)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool callTyped() override { return false; }
|
|
|
|
|
|
|
|
virtual void visitForLobby(CPackForLobby & lobbyPack) override
|
|
|
|
{
|
|
|
|
handler.visitForLobby(lobbyPack);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visitForClient(CPackForClient & clientPack) override
|
|
|
|
{
|
|
|
|
handler.visitForClient(clientPack);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CServerHandler::threadHandleConnection()
|
|
|
|
{
|
|
|
|
setThreadName("CServerHandler::threadHandleConnection");
|
|
|
|
c->enterLobbyConnectionMode();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sendClientConnecting();
|
|
|
|
while(c->connected)
|
|
|
|
{
|
|
|
|
while(state == EClientState::STARTING)
|
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
|
|
|
|
|
|
|
|
CPack * pack = c->retrievePack();
|
|
|
|
if(state == EClientState::DISCONNECTING)
|
|
|
|
{
|
|
|
|
// FIXME: server shouldn't really send netpacks after it's tells client to disconnect
|
|
|
|
// Though currently they'll be delivered and might cause crash.
|
|
|
|
vstd::clear_pointer(pack);
|
|
|
|
}
|
2023-02-12 09:23:39 +02:00
|
|
|
else
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-02-12 09:23:39 +02:00
|
|
|
ServerHandlerCPackVisitor visitor(*this);
|
|
|
|
pack->visit(visitor);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//catch only asio exceptions
|
|
|
|
catch(const boost::system::system_error & e)
|
|
|
|
{
|
|
|
|
if(state == EClientState::DISCONNECTING)
|
|
|
|
{
|
|
|
|
logNetwork->info("Successfully closed connection to server, ending listening thread!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logNetwork->error("Lost connection to server, ending listening thread!");
|
|
|
|
logNetwork->error(e.what());
|
|
|
|
if(client)
|
|
|
|
{
|
2019-03-29 23:01:51 +02:00
|
|
|
state = EClientState::DISCONNECTING;
|
2023-05-18 19:32:29 +02:00
|
|
|
GH.pushUserEvent(EUserEvent::RETURN_TO_MAIN_MENU);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto lcd = new LobbyClientDisconnected();
|
|
|
|
lcd->clientId = c->connectionID;
|
|
|
|
boost::unique_lock<boost::recursive_mutex> lock(*mx);
|
|
|
|
packsForLobbyScreen.push_back(lcd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
handleException();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void CServerHandler::visitForLobby(CPackForLobby & lobbyPack)
|
|
|
|
{
|
|
|
|
if(applier->getApplier(typeList.getTypeID(&lobbyPack))->applyOnLobbyHandler(this, &lobbyPack))
|
|
|
|
{
|
|
|
|
if(!settings["session"]["headless"].Bool())
|
|
|
|
{
|
|
|
|
boost::unique_lock<boost::recursive_mutex> lock(*mx);
|
|
|
|
packsForLobbyScreen.push_back(&lobbyPack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::visitForClient(CPackForClient & clientPack)
|
|
|
|
{
|
|
|
|
client->handlePack(&clientPack);
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CServerHandler::threadRunServer()
|
|
|
|
{
|
2023-02-27 12:00:13 +02:00
|
|
|
#if !defined(VCMI_MOBILE)
|
2018-01-05 19:21:07 +02:00
|
|
|
setThreadName("CServerHandler::threadRunServer");
|
2022-09-17 15:56:01 +02:00
|
|
|
const std::string logName = (VCMIDirs::get().userLogsPath() / "server_log.txt").string();
|
2018-01-05 19:21:07 +02:00
|
|
|
std::string comm = VCMIDirs::get().serverPath().string()
|
2023-03-09 15:36:46 +02:00
|
|
|
+ " --port=" + std::to_string(getHostPort())
|
2018-01-05 19:21:07 +02:00
|
|
|
+ " --run-by-client"
|
|
|
|
+ " --uuid=" + uuid;
|
2022-11-08 02:44:34 +02:00
|
|
|
if(settings["session"]["lobby"].Bool() && settings["session"]["host"].Bool())
|
|
|
|
{
|
|
|
|
comm += " --lobby=" + settings["session"]["address"].String();
|
|
|
|
comm += " --connections=" + settings["session"]["hostConnections"].String();
|
2023-03-09 15:36:46 +02:00
|
|
|
comm += " --lobby-port=" + std::to_string(settings["session"]["port"].Integer());
|
2022-11-08 02:44:34 +02:00
|
|
|
comm += " --lobby-uuid=" + settings["session"]["hostUuid"].String();
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
if(shm)
|
|
|
|
{
|
|
|
|
comm += " --enable-shm";
|
|
|
|
if(settings["session"]["enable-shm-uuid"].Bool())
|
|
|
|
comm += " --enable-shm-uuid";
|
|
|
|
}
|
|
|
|
comm += " > \"" + logName + '\"';
|
2022-11-13 23:48:13 +02:00
|
|
|
logGlobal->info("Server command line: %s", comm);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2022-09-10 15:03:54 +02:00
|
|
|
#ifdef VCMI_WINDOWS
|
|
|
|
int result = -1;
|
|
|
|
const auto bufSize = ::MultiByteToWideChar(CP_UTF8, 0, comm.c_str(), comm.size(), nullptr, 0);
|
|
|
|
if(bufSize > 0)
|
|
|
|
{
|
|
|
|
std::wstring wComm(bufSize, {});
|
|
|
|
const auto convertResult = ::MultiByteToWideChar(CP_UTF8, 0, comm.c_str(), comm.size(), &wComm[0], bufSize);
|
|
|
|
if(convertResult > 0)
|
|
|
|
result = ::_wsystem(wComm.c_str());
|
|
|
|
else
|
|
|
|
logNetwork->error("Error " + std::to_string(GetLastError()) + ": failed to convert server launch command to wide string: " + comm);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
logNetwork->error("Error " + std::to_string(GetLastError()) + ": failed to obtain buffer length to convert server launch command to wide string : " + comm);
|
|
|
|
#else
|
2018-01-05 19:21:07 +02:00
|
|
|
int result = std::system(comm.c_str());
|
2022-09-10 15:03:54 +02:00
|
|
|
#endif
|
2018-01-05 19:21:07 +02:00
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
logNetwork->info("Server closed correctly");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logNetwork->error("Error: server failed to close correctly or crashed!");
|
|
|
|
logNetwork->error("Check %s for more info", logName);
|
|
|
|
}
|
2022-09-16 15:43:21 +02:00
|
|
|
onServerFinished();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::onServerFinished()
|
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
threadRunLocalServer.reset();
|
2019-03-30 01:28:33 +02:00
|
|
|
CSH->campaignServerRestartLock.setn(false);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::sendLobbyPack(const CPackForLobby & pack) const
|
|
|
|
{
|
|
|
|
if(state != EClientState::STARTING)
|
|
|
|
c->sendPack(&pack);
|
|
|
|
}
|