GlobalLobbyProcessor.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. GlobalLobbyProcessor::GlobalLobbyProcessor(CVCMIServer & owner)
  15. : owner(owner)
  16. {
  17. logGlobal->info("Connecting to lobby server");
  18. establishNewConnection();
  19. }
  20. void GlobalLobbyProcessor::establishNewConnection()
  21. {
  22. std::string hostname = settings["lobby"]["hostname"].String();
  23. int16_t port = settings["lobby"]["port"].Integer();
  24. owner.networkHandler->connectToRemote(*this, hostname, port);
  25. }
  26. void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnection> & connection)
  27. {
  28. throw std::runtime_error("Lost connection to a lobby server!");
  29. }
  30. void GlobalLobbyProcessor::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message)
  31. {
  32. if (connection == controlConnection)
  33. {
  34. JsonNode json(message.data(), message.size());
  35. if(json["type"].String() == "loginFailed")
  36. return receiveLoginFailed(json);
  37. if(json["type"].String() == "loginSuccess")
  38. return receiveLoginSuccess(json);
  39. if(json["type"].String() == "accountJoinsRoom")
  40. return receiveAccountJoinsRoom(json);
  41. throw std::runtime_error("Received unexpected message from lobby server: " + json["type"].String());
  42. }
  43. else
  44. {
  45. // received game message via proxy connection
  46. owner.onPacketReceived(connection, message);
  47. }
  48. }
  49. void GlobalLobbyProcessor::receiveLoginFailed(const JsonNode & json)
  50. {
  51. logGlobal->info("Lobby: Failed to login into a lobby server!");
  52. throw std::runtime_error("Failed to login into a lobby server!");
  53. }
  54. void GlobalLobbyProcessor::receiveLoginSuccess(const JsonNode & json)
  55. {
  56. // no-op, wait just for any new commands from lobby
  57. logGlobal->info("Lobby: Succesfully connected to lobby server");
  58. owner.startAcceptingIncomingConnections();
  59. }
  60. void GlobalLobbyProcessor::receiveAccountJoinsRoom(const JsonNode & json)
  61. {
  62. std::string accountID = json["accountID"].String();
  63. logGlobal->info("Lobby: Account %s will join our room!", accountID);
  64. assert(proxyConnections.count(accountID) == 0);
  65. proxyConnections[accountID] = nullptr;
  66. establishNewConnection();
  67. }
  68. void GlobalLobbyProcessor::onConnectionFailed(const std::string & errorMessage)
  69. {
  70. throw std::runtime_error("Failed to connect to a lobby server!");
  71. }
  72. void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
  73. {
  74. if (controlConnection == nullptr)
  75. {
  76. controlConnection = connection;
  77. logGlobal->info("Connection to lobby server established");
  78. JsonNode toSend;
  79. toSend["type"].String() = "serverLogin";
  80. toSend["gameRoomID"].String() = owner.uuid;
  81. toSend["accountID"] = settings["lobby"]["accountID"];
  82. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  83. sendMessage(connection, toSend);
  84. }
  85. else
  86. {
  87. // Proxy connection for a player
  88. std::string guestAccountID;
  89. for (auto const & proxies : proxyConnections)
  90. if (proxies.second == nullptr)
  91. guestAccountID = proxies.first;
  92. JsonNode toSend;
  93. toSend["type"].String() = "serverProxyLogin";
  94. toSend["gameRoomID"].String() = owner.uuid;
  95. toSend["guestAccountID"].String() = guestAccountID;
  96. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  97. sendMessage(connection, toSend);
  98. proxyConnections[guestAccountID] = connection;
  99. owner.onNewConnection(connection);
  100. }
  101. }
  102. void GlobalLobbyProcessor::sendMessage(const NetworkConnectionPtr & target, const JsonNode & data)
  103. {
  104. std::string payloadString = data.toJson(true);
  105. // FIXME: find better approach
  106. uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
  107. uint8_t * payloadEnd = payloadBegin + payloadString.size();
  108. std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
  109. target->sendPacket(payloadBuffer);
  110. }