GlobalLobbyProcessor.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. GlobalLobbyProcessor::GlobalLobbyProcessor(CVCMIServer & owner)
  16. : owner(owner)
  17. {
  18. logGlobal->info("Connecting to lobby server");
  19. establishNewConnection();
  20. }
  21. void GlobalLobbyProcessor::establishNewConnection()
  22. {
  23. std::string hostname = settings["lobby"]["hostname"].String();
  24. int16_t port = settings["lobby"]["port"].Integer();
  25. owner.getNetworkHandler().connectToRemote(*this, hostname, port);
  26. }
  27. void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
  28. {
  29. if (connection == controlConnection)
  30. {
  31. owner.setState(EServerState::SHUTDOWN);
  32. return;
  33. }
  34. else
  35. {
  36. if (owner.getState() == EServerState::LOBBY)
  37. {
  38. for (auto const & proxy : proxyConnections)
  39. {
  40. if (proxy.second == connection)
  41. {
  42. JsonNode message;
  43. message["type"].String() = "leaveGameRoom";
  44. message["accountID"].String() = proxy.first;
  45. sendMessage(controlConnection, message);
  46. break;
  47. }
  48. }
  49. }
  50. // player disconnected
  51. owner.onDisconnected(connection, errorMessage);
  52. }
  53. }
  54. void GlobalLobbyProcessor::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message)
  55. {
  56. if (connection == controlConnection)
  57. {
  58. JsonNode json(message.data(), message.size());
  59. if(json["type"].String() == "operationFailed")
  60. return receiveOperationFailed(json);
  61. if(json["type"].String() == "serverLoginSuccess")
  62. return receiveServerLoginSuccess(json);
  63. if(json["type"].String() == "accountJoinsRoom")
  64. return receiveAccountJoinsRoom(json);
  65. logGlobal->error("Received unexpected message from lobby server of type '%s' ", json["type"].String());
  66. }
  67. else
  68. {
  69. // received game message via proxy connection
  70. owner.onPacketReceived(connection, message);
  71. }
  72. }
  73. void GlobalLobbyProcessor::receiveOperationFailed(const JsonNode & json)
  74. {
  75. logGlobal->info("Lobby: Failed to login into a lobby server!");
  76. owner.setState(EServerState::SHUTDOWN);
  77. }
  78. void GlobalLobbyProcessor::receiveServerLoginSuccess(const JsonNode & json)
  79. {
  80. // no-op, wait just for any new commands from lobby
  81. logGlobal->info("Lobby: Succesfully connected to lobby server");
  82. owner.startAcceptingIncomingConnections();
  83. }
  84. void GlobalLobbyProcessor::receiveAccountJoinsRoom(const JsonNode & json)
  85. {
  86. std::string accountID = json["accountID"].String();
  87. logGlobal->info("Lobby: Account %s will join our room!", accountID);
  88. assert(proxyConnections.count(accountID) == 0);
  89. proxyConnections[accountID] = nullptr;
  90. establishNewConnection();
  91. }
  92. void GlobalLobbyProcessor::onConnectionFailed(const std::string & errorMessage)
  93. {
  94. owner.setState(EServerState::SHUTDOWN);
  95. }
  96. void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
  97. {
  98. if (controlConnection == nullptr)
  99. {
  100. controlConnection = connection;
  101. logGlobal->info("Connection to lobby server established");
  102. JsonNode toSend;
  103. toSend["type"].String() = "serverLogin";
  104. toSend["gameRoomID"].String() = owner.uuid;
  105. toSend["accountID"] = settings["lobby"]["accountID"];
  106. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  107. toSend["version"].String() = VCMI_VERSION_STRING;
  108. sendMessage(connection, toSend);
  109. }
  110. else
  111. {
  112. // Proxy connection for a player
  113. std::string guestAccountID;
  114. for (auto const & proxies : proxyConnections)
  115. if (proxies.second == nullptr)
  116. guestAccountID = proxies.first;
  117. JsonNode toSend;
  118. toSend["type"].String() = "serverProxyLogin";
  119. toSend["gameRoomID"].String() = owner.uuid;
  120. toSend["guestAccountID"].String() = guestAccountID;
  121. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  122. sendMessage(connection, toSend);
  123. proxyConnections[guestAccountID] = connection;
  124. owner.onNewConnection(connection);
  125. }
  126. }
  127. void GlobalLobbyProcessor::sendChangeRoomDescription(const std::string & description)
  128. {
  129. JsonNode toSend;
  130. toSend["type"].String() = "changeRoomDescription";
  131. toSend["description"].String() = description;
  132. sendMessage(controlConnection, toSend);
  133. }
  134. void GlobalLobbyProcessor::sendMessage(const NetworkConnectionPtr & targetConnection, const JsonNode & toSend)
  135. {
  136. assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
  137. targetConnection->sendPacket(toSend.toBytes());
  138. }