1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* 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"
#include "../lib/json/JsonUtils.h"
#include "../lib/VCMI_Lib.h"
#include "../lib/modding/CModHandler.h"
#include "../lib/modding/ModDescription.h"
#include "../lib/modding/ModVerificationInfo.h"
GlobalLobbyProcessor::GlobalLobbyProcessor(CVCMIServer & owner)
: owner(owner)
{
logGlobal->info("Connecting to lobby server");
establishNewConnection();
}
void GlobalLobbyProcessor::establishNewConnection()
{
std::string hostname = settings["lobby"]["hostname"].String();
uint16_t port = settings["lobby"]["port"].Integer();
owner.getNetworkHandler().connectToRemote(*this, hostname, port);
}
void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
{
if (connection == controlConnection)
{
owner.setState(EServerState::SHUTDOWN);
return;
}
else
{
for (auto const & proxy : proxyConnections)
{
if (proxy.second != connection)
continue;
if (owner.getState() == EServerState::LOBBY)
{
JsonNode message;
message["type"].String() = "leaveGameRoom";
message["accountID"].String() = proxy.first;
sendMessage(controlConnection, message);
}
proxyConnections.erase(proxy.first);
// player disconnected
owner.onDisconnected(connection, errorMessage);
return;
}
}
}
void GlobalLobbyProcessor::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message)
{
if (connection == controlConnection)
{
JsonNode json(message.data(), message.size(), "<lobby network packet>");
if(json["type"].String() == "operationFailed")
return receiveOperationFailed(json);
if(json["type"].String() == "serverLoginSuccess")
return receiveServerLoginSuccess(json);
if(json["type"].String() == "accountJoinsRoom")
return receiveAccountJoinsRoom(json);
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);
}
}
void GlobalLobbyProcessor::receiveOperationFailed(const JsonNode & json)
{
logGlobal->info("Lobby: Failed to login into a lobby server!");
owner.setState(EServerState::SHUTDOWN);
}
void GlobalLobbyProcessor::receiveServerLoginSuccess(const JsonNode & json)
{
// no-op, wait just for any new commands from lobby
logGlobal->info("Lobby: Successfully connected to lobby server");
owner.startAcceptingIncomingConnections();
}
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();
}
void GlobalLobbyProcessor::onConnectionFailed(const std::string & errorMessage)
{
owner.setState(EServerState::SHUTDOWN);
}
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"].String() = getHostAccountID();
toSend["accountCookie"].String() = getHostAccountCookie();
toSend["version"].String() = VCMI_VERSION_STRING;
toSend["mods"] = getHostModList();
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"].String() = getHostAccountCookie();
sendMessage(connection, toSend);
proxyConnections[guestAccountID] = connection;
owner.onNewConnection(connection);
}
}
JsonNode GlobalLobbyProcessor::getHostModList() const
{
ModCompatibilityInfo info;
for (auto const & modName : VLC->modh->getActiveMods())
{
if(VLC->modh->getModInfo(modName).affectsGameplay())
info[modName] = VLC->modh->getModInfo(modName).getVerificationInfo();
}
return ModVerificationInfo::jsonSerializeList(info);
}
void GlobalLobbyProcessor::sendGameStarted()
{
JsonNode toSend;
toSend["type"].String() = "gameStarted";
sendMessage(controlConnection, toSend);
}
void GlobalLobbyProcessor::sendChangeRoomDescription(const std::string & description)
{
JsonNode toSend;
toSend["type"].String() = "changeRoomDescription";
toSend["description"].String() = description;
sendMessage(controlConnection, toSend);
}
void GlobalLobbyProcessor::sendMessage(const NetworkConnectionPtr & targetConnection, const JsonNode & toSend)
{
assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
targetConnection->sendPacket(toSend.toBytes());
}
//FIXME: consider whether these methods should be replaced with some other way to define our account ID / account cookie
static const std::string & getServerHost()
{
return settings["lobby"]["hostname"].String();
}
const std::string & GlobalLobbyProcessor::getHostAccountID() const
{
return persistentStorage["lobby"][getServerHost()]["accountID"].String();
}
const std::string & GlobalLobbyProcessor::getHostAccountCookie() const
{
return persistentStorage["lobby"][getServerHost()]["accountCookie"].String();
}
const std::string & GlobalLobbyProcessor::getHostAccountDisplayName() const
{
return persistentStorage["lobby"][getServerHost()]["displayName"].String();
}
|