1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/server/GlobalLobbyProcessor.cpp

136 lines
3.8 KiB
C++
Raw Normal View History

/*
* GlobalLobbyProcessor.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 "GlobalLobbyProcessor.h"
#include "CVCMIServer.h"
#include "../lib/CConfigHandler.h"
GlobalLobbyProcessor::GlobalLobbyProcessor(CVCMIServer & owner)
: owner(owner)
{
logGlobal->info("Connecting to lobby server");
establishNewConnection();
}
void GlobalLobbyProcessor::establishNewConnection()
{
std::string hostname = settings["lobby"]["hostname"].String();
int16_t port = settings["lobby"]["port"].Integer();
owner.getNetworkHandler().connectToRemote(*this, hostname, port);
}
2024-02-02 01:27:19 +02:00
void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
{
if (connection == controlConnection)
{
owner.setState(EServerState::SHUTDOWN);
return;
}
else
{
// player disconnected
owner.onDisconnected(connection, errorMessage);
}
}
2024-02-02 01:27:19 +02:00
void GlobalLobbyProcessor::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message)
{
if (connection == controlConnection)
{
JsonNode json(message.data(), message.size());
2024-02-02 00:12:30 +02:00
if(json["type"].String() == "operationFailed")
return receiveOperationFailed(json);
if(json["type"].String() == "loginSuccess")
return receiveLoginSuccess(json);
if(json["type"].String() == "accountJoinsRoom")
return receiveAccountJoinsRoom(json);
2024-01-12 16:55:36 +02:00
logGlobal->error("Received unexpected message from lobby server of type '%s' ", json["type"].String());
}
else
{
// received game message via proxy connection
owner.onPacketReceived(connection, message);
}
}
2024-02-02 00:12:30 +02:00
void GlobalLobbyProcessor::receiveOperationFailed(const JsonNode & json)
{
logGlobal->info("Lobby: Failed to login into a lobby server!");
owner.setState(EServerState::SHUTDOWN);
}
void GlobalLobbyProcessor::receiveLoginSuccess(const JsonNode & json)
{
// no-op, wait just for any new commands from lobby
logGlobal->info("Lobby: Succesfully connected to lobby server");
owner.startAcceptingIncomingConnections();
}
2024-01-12 16:55:36 +02:00
void GlobalLobbyProcessor::receiveAccountJoinsRoom(const JsonNode & json)
{
std::string accountID = json["accountID"].String();
logGlobal->info("Lobby: Account %s will join our room!", accountID);
assert(proxyConnections.count(accountID) == 0);
proxyConnections[accountID] = nullptr;
establishNewConnection();
2024-01-12 16:55:36 +02:00
}
void GlobalLobbyProcessor::onConnectionFailed(const std::string & errorMessage)
{
owner.setState(EServerState::SHUTDOWN);
}
2024-01-12 16:55:36 +02:00
void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
{
if (controlConnection == nullptr)
{
controlConnection = connection;
logGlobal->info("Connection to lobby server established");
JsonNode toSend;
toSend["type"].String() = "serverLogin";
toSend["gameRoomID"].String() = owner.uuid;
toSend["accountID"] = settings["lobby"]["accountID"];
toSend["accountCookie"] = settings["lobby"]["accountCookie"];
sendMessage(connection, toSend);
}
else
{
// Proxy connection for a player
std::string guestAccountID;
for (auto const & proxies : proxyConnections)
if (proxies.second == nullptr)
guestAccountID = proxies.first;
JsonNode toSend;
toSend["type"].String() = "serverProxyLogin";
toSend["gameRoomID"].String() = owner.uuid;
toSend["guestAccountID"].String() = guestAccountID;
toSend["accountCookie"] = settings["lobby"]["accountCookie"];
sendMessage(connection, toSend);
proxyConnections[guestAccountID] = connection;
owner.onNewConnection(connection);
}
}
void GlobalLobbyProcessor::sendMessage(const NetworkConnectionPtr & target, const JsonNode & data)
{
2024-02-02 01:27:19 +02:00
target->sendPacket(data.toBytes(true));
}