GlobalLobbyProcessor.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * GlobalLobbyProcessor.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "GlobalLobbyProcessor.h"
  12. #include "CVCMIServer.h"
  13. #include "../lib/CConfigHandler.h"
  14. #include "../lib/json/JsonUtils.h"
  15. #include "../lib/VCMI_Lib.h"
  16. #include "../lib/modding/CModHandler.h"
  17. #include "../lib/modding/CModInfo.h"
  18. GlobalLobbyProcessor::GlobalLobbyProcessor(CVCMIServer & owner)
  19. : owner(owner)
  20. {
  21. logGlobal->info("Connecting to lobby server");
  22. establishNewConnection();
  23. }
  24. void GlobalLobbyProcessor::establishNewConnection()
  25. {
  26. std::string hostname = settings["lobby"]["hostname"].String();
  27. uint16_t port = settings["lobby"]["port"].Integer();
  28. owner.getNetworkHandler().connectToRemote(*this, hostname, port);
  29. }
  30. void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
  31. {
  32. if (connection == controlConnection)
  33. {
  34. owner.setState(EServerState::SHUTDOWN);
  35. return;
  36. }
  37. else
  38. {
  39. for (auto const & proxy : proxyConnections)
  40. {
  41. if (proxy.second != connection)
  42. continue;
  43. if (owner.getState() == EServerState::LOBBY)
  44. {
  45. JsonNode message;
  46. message["type"].String() = "leaveGameRoom";
  47. message["accountID"].String() = proxy.first;
  48. sendMessage(controlConnection, message);
  49. }
  50. proxyConnections.erase(proxy.first);
  51. // player disconnected
  52. owner.onDisconnected(connection, errorMessage);
  53. return;
  54. }
  55. }
  56. }
  57. void GlobalLobbyProcessor::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message)
  58. {
  59. if (connection == controlConnection)
  60. {
  61. JsonNode json(message.data(), message.size());
  62. if(json["type"].String() == "operationFailed")
  63. return receiveOperationFailed(json);
  64. if(json["type"].String() == "serverLoginSuccess")
  65. return receiveServerLoginSuccess(json);
  66. if(json["type"].String() == "accountJoinsRoom")
  67. return receiveAccountJoinsRoom(json);
  68. logGlobal->error("Received unexpected message from lobby server of type '%s' ", json["type"].String());
  69. }
  70. else
  71. {
  72. // received game message via proxy connection
  73. owner.onPacketReceived(connection, message);
  74. }
  75. }
  76. void GlobalLobbyProcessor::receiveOperationFailed(const JsonNode & json)
  77. {
  78. logGlobal->info("Lobby: Failed to login into a lobby server!");
  79. owner.setState(EServerState::SHUTDOWN);
  80. }
  81. void GlobalLobbyProcessor::receiveServerLoginSuccess(const JsonNode & json)
  82. {
  83. // no-op, wait just for any new commands from lobby
  84. logGlobal->info("Lobby: Succesfully connected to lobby server");
  85. owner.startAcceptingIncomingConnections();
  86. }
  87. void GlobalLobbyProcessor::receiveAccountJoinsRoom(const JsonNode & json)
  88. {
  89. std::string accountID = json["accountID"].String();
  90. logGlobal->info("Lobby: Account %s will join our room!", accountID);
  91. assert(proxyConnections.count(accountID) == 0);
  92. proxyConnections[accountID] = nullptr;
  93. establishNewConnection();
  94. }
  95. void GlobalLobbyProcessor::onConnectionFailed(const std::string & errorMessage)
  96. {
  97. owner.setState(EServerState::SHUTDOWN);
  98. }
  99. void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
  100. {
  101. if (controlConnection == nullptr)
  102. {
  103. controlConnection = connection;
  104. logGlobal->info("Connection to lobby server established");
  105. JsonNode toSend;
  106. toSend["type"].String() = "serverLogin";
  107. toSend["gameRoomID"].String() = owner.uuid;
  108. toSend["accountID"].String() = getHostAccountID();
  109. toSend["accountCookie"].String() = getHostAccountCookie();
  110. toSend["version"].String() = VCMI_VERSION_STRING;
  111. toSend["mods"] = getHostModList();
  112. sendMessage(connection, toSend);
  113. }
  114. else
  115. {
  116. // Proxy connection for a player
  117. std::string guestAccountID;
  118. for (auto const & proxies : proxyConnections)
  119. if (proxies.second == nullptr)
  120. guestAccountID = proxies.first;
  121. JsonNode toSend;
  122. toSend["type"].String() = "serverProxyLogin";
  123. toSend["gameRoomID"].String() = owner.uuid;
  124. toSend["guestAccountID"].String() = guestAccountID;
  125. toSend["accountCookie"].String() = getHostAccountCookie();
  126. sendMessage(connection, toSend);
  127. proxyConnections[guestAccountID] = connection;
  128. owner.onNewConnection(connection);
  129. }
  130. }
  131. JsonNode GlobalLobbyProcessor::getHostModList() const
  132. {
  133. ModCompatibilityInfo info;
  134. for (auto const & modName : VLC->modh->getActiveMods())
  135. {
  136. if(VLC->modh->getModInfo(modName).checkModGameplayAffecting())
  137. info[modName] = VLC->modh->getModInfo(modName).getVerificationInfo();
  138. }
  139. return ModVerificationInfo::jsonSerializeList(info);
  140. }
  141. void GlobalLobbyProcessor::sendGameStarted()
  142. {
  143. JsonNode toSend;
  144. toSend["type"].String() = "gameStarted";
  145. sendMessage(controlConnection, toSend);
  146. }
  147. void GlobalLobbyProcessor::sendChangeRoomDescription(const std::string & description)
  148. {
  149. JsonNode toSend;
  150. toSend["type"].String() = "changeRoomDescription";
  151. toSend["description"].String() = description;
  152. sendMessage(controlConnection, toSend);
  153. }
  154. void GlobalLobbyProcessor::sendMessage(const NetworkConnectionPtr & targetConnection, const JsonNode & toSend)
  155. {
  156. assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
  157. targetConnection->sendPacket(toSend.toBytes());
  158. }
  159. //FIXME: consider whether these methods should be replaced with some other way to define our account ID / account cookie
  160. static const std::string & getServerHost()
  161. {
  162. return settings["lobby"]["hostname"].String();
  163. }
  164. const std::string & GlobalLobbyProcessor::getHostAccountID() const
  165. {
  166. return persistentStorage["lobby"][getServerHost()]["accountID"].String();
  167. }
  168. const std::string & GlobalLobbyProcessor::getHostAccountCookie() const
  169. {
  170. return persistentStorage["lobby"][getServerHost()]["accountCookie"].String();
  171. }
  172. const std::string & GlobalLobbyProcessor::getHostAccountDisplayName() const
  173. {
  174. return persistentStorage["lobby"][getServerHost()]["displayName"].String();
  175. }