1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00
This commit is contained in:
Ivan Savenko 2024-02-03 22:59:56 +02:00
parent 1b6ac1052a
commit 7dee24edae
13 changed files with 74 additions and 83 deletions

View File

@ -139,9 +139,9 @@ CServerHandler::~CServerHandler()
}
CServerHandler::CServerHandler()
: applier(std::make_unique<CApplier<CBaseForLobbyApply>>())
: networkHandler(INetworkHandler::createHandler())
, lobbyClient(std::make_unique<GlobalLobbyClient>())
, networkHandler(INetworkHandler::createHandler())
, applier(std::make_unique<CApplier<CBaseForLobbyApply>>())
, threadNetwork(&CServerHandler::threadRunNetwork, this)
, state(EClientState::NONE)
, campaignStateToSend(nullptr)
@ -187,6 +187,11 @@ GlobalLobbyClient & CServerHandler::getGlobalLobby()
return *lobbyClient;
}
INetworkHandler & CServerHandler::getNetworkHandler()
{
return *networkHandler;
}
void CServerHandler::startLocalServerAndConnect(bool connectToLobby)
{
if(threadRunLocalServer.joinable())

View File

@ -97,6 +97,7 @@ class CServerHandler final : public IServerAPI, public LobbyInfo, public INetwor
{
friend class ApplyOnLobbyHandlerNetPackVisitor;
std::unique_ptr<INetworkHandler> networkHandler;
std::shared_ptr<INetworkConnection> networkConnection;
std::unique_ptr<GlobalLobbyClient> lobbyClient;
std::unique_ptr<CApplier<CBaseForLobbyApply>> applier;
@ -104,6 +105,9 @@ class CServerHandler final : public IServerAPI, public LobbyInfo, public INetwor
std::vector<std::string> myNames;
std::shared_ptr<HighScoreCalculation> highScoreCalc;
boost::thread threadRunLocalServer;
boost::thread threadNetwork;
void threadRunNetwork();
void threadRunServer(bool connectToLobby);
@ -123,8 +127,6 @@ class CServerHandler final : public IServerAPI, public LobbyInfo, public INetwor
bool isServerLocal() const;
public:
std::unique_ptr<INetworkHandler> networkHandler;
std::shared_ptr<CConnection> c;
std::atomic<EClientState> state;
@ -140,9 +142,6 @@ public:
////////////////////
std::unique_ptr<CStopWatch> th;
boost::thread threadRunLocalServer;
boost::thread threadNetwork;
std::unique_ptr<CClient> client;
CServerHandler();
@ -153,6 +152,7 @@ public:
void connectToServer(const std::string & addr, const ui16 port);
GlobalLobbyClient & getGlobalLobby();
INetworkHandler & getNetworkHandler();
// Helpers for lobby state access
std::set<PlayerColor> getHumanColors();

View File

@ -32,12 +32,12 @@ public:
bool getResult() const { return result; }
virtual void visitLobbyClientConnected(LobbyClientConnected & pack) override;
virtual void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
virtual void visitLobbyRestartGame(LobbyRestartGame & pack) override;
virtual void visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack) override;
virtual void visitLobbyStartGame(LobbyStartGame & pack) override;
virtual void visitLobbyUpdateState(LobbyUpdateState & pack) override;
void visitLobbyClientConnected(LobbyClientConnected & pack) override;
void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
void visitLobbyRestartGame(LobbyRestartGame & pack) override;
void visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack) override;
void visitLobbyStartGame(LobbyStartGame & pack) override;
void visitLobbyUpdateState(LobbyUpdateState & pack) override;
};
class ApplyOnLobbyScreenNetPackVisitor : public VCMI_LIB_WRAP_NAMESPACE(ICPackVisitor)
@ -52,11 +52,11 @@ public:
{
}
virtual void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
virtual void visitLobbyChatMessage(LobbyChatMessage & pack) override;
virtual void visitLobbyGuiAction(LobbyGuiAction & pack) override;
virtual void visitLobbyStartGame(LobbyStartGame & pack) override;
virtual void visitLobbyLoadProgress(LobbyLoadProgress & pack) override;
virtual void visitLobbyUpdateState(LobbyUpdateState & pack) override;
virtual void visitLobbyShowMessage(LobbyShowMessage & pack) override;
void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
void visitLobbyChatMessage(LobbyChatMessage & pack) override;
void visitLobbyGuiAction(LobbyGuiAction & pack) override;
void visitLobbyStartGame(LobbyStartGame & pack) override;
void visitLobbyLoadProgress(LobbyLoadProgress & pack) override;
void visitLobbyUpdateState(LobbyUpdateState & pack) override;
void visitLobbyShowMessage(LobbyShowMessage & pack) override;
};

View File

@ -177,7 +177,6 @@ void GlobalLobbyClient::receiveActiveGameRooms(const JsonNode & json)
room.hostAccountID = jsonEntry["hostAccountID"].String();
room.hostAccountDisplayName = jsonEntry["hostAccountDisplayName"].String();
room.description = jsonEntry["description"].String();
// room.status = jsonEntry["status"].String();
room.playersCount = jsonEntry["playersCount"].Integer();
room.playersLimit = jsonEntry["playersLimit"].Integer();
@ -289,7 +288,7 @@ void GlobalLobbyClient::connect()
{
std::string hostname = settings["lobby"]["hostname"].String();
int16_t port = settings["lobby"]["port"].Integer();
CSH->networkHandler->connectToRemote(*this, hostname, port);
CSH->getNetworkHandler().connectToRemote(*this, hostname, port);
}
bool GlobalLobbyClient::isConnected()

View File

@ -22,7 +22,6 @@ struct GlobalLobbyRoom
std::string hostAccountID;
std::string hostAccountDisplayName;
std::string description;
// std::string status;
int playersCount;
int playersLimit;
};

View File

@ -29,24 +29,14 @@ void NetworkConnection::start()
[self = shared_from_this()](const auto & ec, const auto & endpoint) { self->onHeaderReceived(ec); });
}
void NetworkConnection::onHeaderReceived(const boost::system::error_code & ec)
void NetworkConnection::onHeaderReceived(const boost::system::error_code & ecHeader)
{
if (ec)
if (ecHeader)
{
listener.onDisconnected(shared_from_this(), ec.message());
listener.onDisconnected(shared_from_this(), ecHeader.message());
return;
}
uint32_t messageSize = readPacketSize();
boost::asio::async_read(*socket,
readBuffer,
boost::asio::transfer_exactly(messageSize),
[self = shared_from_this(), messageSize](const auto & ec, const auto & endpoint) { self->onPacketReceived(ec, messageSize); });
}
uint32_t NetworkConnection::readPacketSize()
{
if (readBuffer.size() < messageHeaderSize)
throw std::runtime_error("Failed to read header!");
@ -54,9 +44,15 @@ uint32_t NetworkConnection::readPacketSize()
readBuffer.sgetn(reinterpret_cast<char *>(&messageSize), sizeof(messageSize));
if (messageSize > messageMaxSize)
{
listener.onDisconnected(shared_from_this(), "Invalid packet size!");
return;
}
return messageSize;
boost::asio::async_read(*socket,
readBuffer,
boost::asio::transfer_exactly(messageSize),
[self = shared_from_this(), messageSize](const auto & ecPayload, const auto & endpoint) { self->onPacketReceived(ecPayload, messageSize); });
}
void NetworkConnection::onPacketReceived(const boost::system::error_code & ec, uint32_t expectedPacketSize)
@ -69,7 +65,7 @@ void NetworkConnection::onPacketReceived(const boost::system::error_code & ec, u
if (readBuffer.size() < expectedPacketSize)
{
throw std::runtime_error("Failed to read header!");
throw std::runtime_error("Failed to read packet!");
}
std::vector<std::byte> message(expectedPacketSize);

View File

@ -25,7 +25,6 @@ class NetworkConnection : public INetworkConnection, public std::enable_shared_f
void onHeaderReceived(const boost::system::error_code & ec);
void onPacketReceived(const boost::system::error_code & ec, uint32_t expectedPacketSize);
uint32_t readPacketSize();
public:
NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket);

View File

@ -397,11 +397,6 @@ std::string LobbyDatabase::getAccountDisplayName(const std::string & accountID)
return result;
}
//LobbyCookieStatus LobbyDatabase::getGameRoomCookieStatus(const std::string & accountID, const std::string & accessCookieUUID)
//{
// return {};
//}
LobbyCookieStatus LobbyDatabase::getAccountCookieStatus(const std::string & accountID, const std::string & accessCookieUUID)
{
bool result = false;

View File

@ -78,14 +78,12 @@ public:
std::vector<LobbyGameRoom> getActiveGameRooms();
std::vector<LobbyAccount> getActiveAccounts();
// std::vector<LobbyAccount> getAccountsInRoom(const std::string & roomID);
std::vector<LobbyChatMessage> getRecentMessageHistory();
std::string getIdleGameRoom(const std::string & hostAccountID);
std::string getAccountGameRoom(const std::string & accountID);
std::string getAccountDisplayName(const std::string & accountID);
// LobbyCookieStatus getGameRoomCookieStatus(const std::string & accountID, const std::string & accessCookieUUID);
LobbyCookieStatus getAccountCookieStatus(const std::string & accountID, const std::string & accessCookieUUID);
LobbyInviteStatus getAccountInviteStatus(const std::string & accountID, const std::string & roomID);
LobbyRoomState getGameRoomStatus(const std::string & roomID);

View File

@ -217,7 +217,7 @@ void LobbyServer::onDisconnected(const NetworkConnectionPtr & connection, const
if(activeProxies.count(connection))
{
auto & otherConnection = activeProxies.at(connection);
const auto & otherConnection = activeProxies.at(connection);
if (otherConnection)
otherConnection->close();

View File

@ -37,5 +37,5 @@ class GlobalLobbyProcessor : public INetworkClientListener
void establishNewConnection();
public:
GlobalLobbyProcessor(CVCMIServer & owner);
explicit GlobalLobbyProcessor(CVCMIServer & owner);
};

View File

@ -28,16 +28,16 @@ public:
return result;
}
virtual void visitForLobby(CPackForLobby & pack) override;
virtual void visitLobbyClientConnected(LobbyClientConnected & pack) override;
virtual void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
virtual void visitLobbyRestartGame(LobbyRestartGame & pack) override;
virtual void visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack) override;
virtual void visitLobbyStartGame(LobbyStartGame & pack) override;
virtual void visitLobbyChangeHost(LobbyChangeHost & pack) override;
virtual void visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack) override;
virtual void visitLobbyChatMessage(LobbyChatMessage & pack) override;
virtual void visitLobbyGuiAction(LobbyGuiAction & pack) override;
void visitForLobby(CPackForLobby & pack) override;
void visitLobbyClientConnected(LobbyClientConnected & pack) override;
void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
void visitLobbyRestartGame(LobbyRestartGame & pack) override;
void visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack) override;
void visitLobbyStartGame(LobbyStartGame & pack) override;
void visitLobbyChangeHost(LobbyChangeHost & pack) override;
void visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack) override;
void visitLobbyChatMessage(LobbyChatMessage & pack) override;
void visitLobbyGuiAction(LobbyGuiAction & pack) override;
};
class ApplyOnServerAfterAnnounceNetPackVisitor : public VCMI_LIB_WRAP_NAMESPACE(ICPackVisitor)
@ -51,12 +51,12 @@ public:
{
}
virtual void visitForLobby(CPackForLobby & pack) override;
virtual void visitLobbyClientConnected(LobbyClientConnected & pack) override;
virtual void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
virtual void visitLobbyRestartGame(LobbyRestartGame & pack) override;
virtual void visitLobbyStartGame(LobbyStartGame & pack) override;
virtual void visitLobbyChangeHost(LobbyChangeHost & pack) override;
void visitForLobby(CPackForLobby & pack) override;
void visitLobbyClientConnected(LobbyClientConnected & pack) override;
void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
void visitLobbyRestartGame(LobbyRestartGame & pack) override;
void visitLobbyStartGame(LobbyStartGame & pack) override;
void visitLobbyChangeHost(LobbyChangeHost & pack) override;
};
class ApplyOnServerNetPackVisitor : public VCMI_LIB_WRAP_NAMESPACE(ICPackVisitor)
@ -76,21 +76,21 @@ public:
return result;
}
virtual void visitLobbyClientConnected(LobbyClientConnected & pack) override;
virtual void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
virtual void visitLobbySetMap(LobbySetMap & pack) override;
virtual void visitLobbySetCampaign(LobbySetCampaign & pack) override;
virtual void visitLobbySetCampaignMap(LobbySetCampaignMap & pack) override;
virtual void visitLobbySetCampaignBonus(LobbySetCampaignBonus & pack) override;
virtual void visitLobbyRestartGame(LobbyRestartGame & pack) override;
virtual void visitLobbyStartGame(LobbyStartGame & pack) override;
virtual void visitLobbyChangeHost(LobbyChangeHost & pack) override;
virtual void visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack) override;
virtual void visitLobbySetPlayer(LobbySetPlayer & pack) override;
virtual void visitLobbySetPlayerName(LobbySetPlayerName & pack) override;
virtual void visitLobbySetTurnTime(LobbySetTurnTime & pack) override;
virtual void visitLobbySetExtraOptions(LobbySetExtraOptions & pack) override;
virtual void visitLobbySetSimturns(LobbySetSimturns & pack) override;
virtual void visitLobbySetDifficulty(LobbySetDifficulty & pack) override;
virtual void visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack) override;
void visitLobbyClientConnected(LobbyClientConnected & pack) override;
void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
void visitLobbySetMap(LobbySetMap & pack) override;
void visitLobbySetCampaign(LobbySetCampaign & pack) override;
void visitLobbySetCampaignMap(LobbySetCampaignMap & pack) override;
void visitLobbySetCampaignBonus(LobbySetCampaignBonus & pack) override;
void visitLobbyRestartGame(LobbyRestartGame & pack) override;
void visitLobbyStartGame(LobbyStartGame & pack) override;
void visitLobbyChangeHost(LobbyChangeHost & pack) override;
void visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack) override;
void visitLobbySetPlayer(LobbySetPlayer & pack) override;
void visitLobbySetPlayerName(LobbySetPlayerName & pack) override;
void visitLobbySetTurnTime(LobbySetTurnTime & pack) override;
void visitLobbySetExtraOptions(LobbySetExtraOptions & pack) override;
void visitLobbySetSimturns(LobbySetSimturns & pack) override;
void visitLobbySetDifficulty(LobbySetDifficulty & pack) override;
void visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack) override;
};

View File

@ -206,7 +206,7 @@ void ApplyOnServerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
{
for(auto & c : srv.activeConnections)
for(const auto & c : srv.activeConnections)
c->enterLobbyConnectionMode();
}
@ -250,7 +250,7 @@ void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyStartGame(LobbyStartGam
srv.startGameImmediately();
else
{
for(auto & c : srv.activeConnections)
for(const auto & c : srv.activeConnections)
{
if(c->connectionID == pack.clientId)
{