GlobalLobbyProcessor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. JsonNode json(message.data(), message.size());
  33. if(json["type"].String() == "loginFailed")
  34. return receiveLoginFailed(json);
  35. if(json["type"].String() == "loginSuccess")
  36. return receiveLoginSuccess(json);
  37. if(json["type"].String() == "accountJoinsRoom")
  38. return receiveAccountJoinsRoom(json);
  39. throw std::runtime_error("Received unexpected message from lobby server: " + json["type"].String());
  40. }
  41. void GlobalLobbyProcessor::receiveLoginFailed(const JsonNode & json)
  42. {
  43. throw std::runtime_error("Failed to login into a lobby server!");
  44. }
  45. void GlobalLobbyProcessor::receiveLoginSuccess(const JsonNode & json)
  46. {
  47. // no-op, wait just for any new commands from lobby
  48. logGlobal->info("Succesfully connected to lobby server");
  49. owner.startAcceptingIncomingConnections();
  50. }
  51. void GlobalLobbyProcessor::receiveAccountJoinsRoom(const JsonNode & json)
  52. {
  53. std::string accountID = json["accountID"].String();
  54. assert(proxyConnections.count(accountID) == 0);
  55. proxyConnections[accountID] = nullptr;
  56. establishNewConnection();
  57. }
  58. void GlobalLobbyProcessor::onConnectionFailed(const std::string & errorMessage)
  59. {
  60. throw std::runtime_error("Failed to connect to a lobby server!");
  61. }
  62. void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
  63. {
  64. if (controlConnection == nullptr)
  65. {
  66. controlConnection = connection;
  67. logGlobal->info("Connection to lobby server established");
  68. JsonNode toSend;
  69. toSend["type"].String() = "serverLogin";
  70. toSend["gameRoomID"].String() = owner.uuid;
  71. toSend["accountID"] = settings["lobby"]["accountID"];
  72. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  73. sendMessage(toSend);
  74. }
  75. else
  76. {
  77. // Proxy connection for a player
  78. std::string accountID;
  79. for (auto const & proxies : proxyConnections)
  80. if (proxies.second == nullptr)
  81. accountID = proxies.first;
  82. JsonNode toSend;
  83. toSend["type"].String() = "serverProxyLogin";
  84. toSend["gameRoomID"].String() = owner.uuid;
  85. toSend["accountID"].String() = accountID;
  86. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  87. sendMessage(toSend);
  88. proxyConnections[accountID] = connection;
  89. owner.onNewConnection(connection);
  90. }
  91. }
  92. void GlobalLobbyProcessor::sendMessage(const JsonNode & data)
  93. {
  94. std::string payloadString = data.toJson(true);
  95. // FIXME: find better approach
  96. uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
  97. uint8_t * payloadEnd = payloadBegin + payloadString.size();
  98. std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
  99. controlConnection->sendPacket(payloadBuffer);
  100. }