| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 | 
							- /*
 
-  * 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.networkHandler->connectToRemote(*this, hostname, port);
 
- }
 
- void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnection> & connection)
 
- {
 
- 	throw std::runtime_error("Lost connection to a lobby server!");
 
- }
 
- void GlobalLobbyProcessor::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message)
 
- {
 
- 	JsonNode json(message.data(), message.size());
 
- 	if(json["type"].String() == "loginFailed")
 
- 		return receiveLoginFailed(json);
 
- 	if(json["type"].String() == "loginSuccess")
 
- 		return receiveLoginSuccess(json);
 
- 	if(json["type"].String() == "accountJoinsRoom")
 
- 		return receiveAccountJoinsRoom(json);
 
- 	throw std::runtime_error("Received unexpected message from lobby server: " + json["type"].String());
 
- }
 
- void GlobalLobbyProcessor::receiveLoginFailed(const JsonNode & json)
 
- {
 
- 	throw std::runtime_error("Failed to login into a lobby server!");
 
- }
 
- void GlobalLobbyProcessor::receiveLoginSuccess(const JsonNode & json)
 
- {
 
- 	// no-op, wait just for any new commands from lobby
 
- 	logGlobal->info("Succesfully connected to lobby server");
 
- 	owner.startAcceptingIncomingConnections();
 
- }
 
- void GlobalLobbyProcessor::receiveAccountJoinsRoom(const JsonNode & json)
 
- {
 
- 	std::string accountID = json["accountID"].String();
 
- 	assert(proxyConnections.count(accountID) == 0);
 
- 	proxyConnections[accountID] = nullptr;
 
- 	establishNewConnection();
 
- }
 
- void GlobalLobbyProcessor::onConnectionFailed(const std::string & errorMessage)
 
- {
 
- 	throw std::runtime_error("Failed to connect to a lobby server!");
 
- }
 
- 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(toSend);
 
- 	}
 
- 	else
 
- 	{
 
- 		// Proxy connection for a player
 
- 		std::string accountID;
 
- 		for (auto const & proxies : proxyConnections)
 
- 			if (proxies.second == nullptr)
 
- 				accountID = proxies.first;
 
- 		JsonNode toSend;
 
- 		toSend["type"].String() = "serverProxyLogin";
 
- 		toSend["gameRoomID"].String() = owner.uuid;
 
- 		toSend["accountID"].String() = accountID;
 
- 		toSend["accountCookie"] = settings["lobby"]["accountCookie"];
 
- 		sendMessage(toSend);
 
- 		proxyConnections[accountID] = connection;
 
- 		owner.onNewConnection(connection);
 
- 	}
 
- }
 
- void GlobalLobbyProcessor::sendMessage(const JsonNode & data)
 
- {
 
- 	std::string payloadString = data.toJson(true);
 
- 	// FIXME: find better approach
 
- 	uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
 
- 	uint8_t * payloadEnd = payloadBegin + payloadString.size();
 
- 	std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
 
- 	controlConnection->sendPacket(payloadBuffer);
 
- }
 
 
  |