MasterServerClient.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. //
  2. // Created by Keith Johnston on 4/16/16.
  3. //
  4. #include "MasterServerClient.h"
  5. #include "../Precompiled.h"
  6. #include "../IO/Log.h"
  7. #include "../Network/NetworkPriority.h"
  8. #include "../Network/Network.h"
  9. #include "../Core/Profiler.h"
  10. #include "../Core/CoreEvents.h"
  11. #include "../Network/NetworkEvents.h"
  12. #include <rapidjson/document.h>
  13. #include <rapidjson/stringbuffer.h>
  14. #include <rapidjson/prettywriter.h>
  15. namespace Atomic
  16. {
  17. MasterServerClient::MasterServerClient(Context *context) :
  18. readingMasterMessageLength(true),
  19. Object(context),
  20. udpSecondsTillRetry_(0.5f),
  21. connectToMasterState_(MASTER_NOT_CONNECTED),
  22. clientConnectToGameServerState_(GAME_NOT_CONNECTED),
  23. timeBetweenClientPunchThroughAttempts_(1.0),
  24. timeTillNextPunchThroughAttempt_(0.0),
  25. timeBetweenClientConnectAttempts_(1.0),
  26. timeTillNextClientConnectAttempt_(1.0),
  27. masterTCPConnection_(NULL),
  28. clientToServerSocket_(NULL)
  29. {
  30. SubscribeToEvent(E_BEGINFRAME, ATOMIC_HANDLER(MasterServerClient, HandleBeginFrame));
  31. }
  32. MasterServerClient::~MasterServerClient()
  33. {
  34. if (masterTCPConnection_)
  35. {
  36. masterTCPConnection_->Disconnect();
  37. }
  38. }
  39. void MasterServerClient::ConnectToMaster(const String &address, unsigned short port)
  40. {
  41. ATOMIC_PROFILE(ConnectToMaster);
  42. if (connectToMasterState_ != MASTER_NOT_CONNECTED)
  43. {
  44. DisconnectFromMaster();
  45. }
  46. masterServerInfo_.address = address;
  47. masterServerInfo_.port = port;
  48. masterServerInfo_.isRegisteringServer = false;
  49. // We first make a TCP connection
  50. SetConnectToMasterState(MASTER_CONNECTING_TCP);
  51. }
  52. void MasterServerClient::DisconnectFromMaster()
  53. {
  54. masterTCPConnection_->Disconnect();
  55. masterUDPConnection_->Disconnect();
  56. SetConnectToMasterState(MASTER_NOT_CONNECTED);
  57. }
  58. void MasterServerClient::ConnectToMasterAndRegister(const String &address, unsigned short port, const String& serverName)
  59. {
  60. ATOMIC_PROFILE(ConnectToMaster);
  61. if (connectToMasterState_ != MASTER_NOT_CONNECTED)
  62. {
  63. DisconnectFromMaster();
  64. }
  65. masterServerInfo_.address = address;
  66. masterServerInfo_.port = port;
  67. masterServerInfo_.serverName = serverName;
  68. masterServerInfo_.isRegisteringServer = true;
  69. // We first make a TCP connection
  70. SetConnectToMasterState(MASTER_CONNECTING_TCP);
  71. }
  72. void MasterServerClient::RequestServerListFromMaster()
  73. {
  74. SendMessageToMasterServer("{ \"cmd\": \"getServerList\" }");
  75. }
  76. void MasterServerClient::ConnectToServerViaMaster(const String &serverId,
  77. const String &internalAddress, unsigned short internalPort,
  78. const String &externalAddress, unsigned short externalPort,
  79. Scene* scene)
  80. {
  81. remoteGameServerInfo_.serverId = serverId;
  82. remoteGameServerInfo_.internalAddress = internalAddress;
  83. remoteGameServerInfo_.internalPort = internalPort;
  84. remoteGameServerInfo_.externalAddress = externalAddress;
  85. remoteGameServerInfo_.externalPort = externalPort;
  86. remoteGameServerInfo_.clientScene = scene;
  87. SetConnectToGameServerState(GAME_CONNECTING_INTERNAL_IP);
  88. }
  89. void MasterServerClient::RegisterServerWithMaster(const String &name)
  90. {
  91. // Get the internal IP and port
  92. kNet::EndPoint localEndPoint = masterTCPConnection_->LocalEndPoint();
  93. unsigned char* ip = localEndPoint.ip;
  94. char str[256];
  95. sprintf(str, "%d.%d.%d.%d", (unsigned int)ip[0], (unsigned int)ip[1], (unsigned int)ip[2], (unsigned int)ip[3]);
  96. Atomic::Network* network = GetSubsystem<Network>();
  97. unsigned int localPort = network->GetServerPort();
  98. String msg = String("{") +
  99. String("\"cmd\":") + String("\"registerServer\",") +
  100. String("\"internalIP\": \"") + String(str) + String("\", ") +
  101. String("\"internalPort\": ") + String(localPort) + String(", ") +
  102. String("\"id\":\"") + masterServerConnectionId_ + String("\", ") +
  103. String("\"serverName\":\"") + name + String("\"") +
  104. String("}");
  105. SendMessageToMasterServer(msg);
  106. }
  107. void MasterServerClient::SendMessageToMasterServer(const String& msg)
  108. {
  109. if (masterTCPConnection_)
  110. {
  111. String netString = String(msg.Length()) + ":" + msg;
  112. masterTCPConnection_->Send(netString.CString(), netString.Length());
  113. }
  114. else
  115. {
  116. ATOMIC_LOGERROR("No master server connection. Cannot send message");
  117. }
  118. }
  119. void MasterServerClient::Update(float dt)
  120. {
  121. if (masterTCPConnection_==NULL)
  122. {
  123. return;
  124. }
  125. CheckForMessageFromMaster();
  126. ConnectToMasterUpdate(dt);
  127. ConnectToGameServerUpdate(dt);
  128. CheckForNatPunchThroughRequests(dt);
  129. }
  130. void MasterServerClient::CheckForNatPunchThroughRequests(float dt)
  131. {
  132. if (clientIdToPunchThroughSocketMap_.Size() > 0)
  133. {
  134. if (timeTillNextPunchThroughAttempt_ <= 0)
  135. {
  136. for (HashMap<String, kNet::Socket*>::Iterator i = clientIdToPunchThroughSocketMap_.Begin();
  137. i != clientIdToPunchThroughSocketMap_.End();)
  138. {
  139. Network* network = GetSubsystem<Network>();
  140. ATOMIC_LOGINFO("Sending packet to client");
  141. kNet::Socket* s = i->second_;
  142. if (network->IsEndPointConnected(s->RemoteEndPoint()))
  143. {
  144. i = clientIdToPunchThroughSocketMap_.Erase(i);
  145. }
  146. else
  147. {
  148. s->Send("K",1);
  149. ++i;
  150. }
  151. }
  152. // Reset the timer
  153. timeTillNextPunchThroughAttempt_ = timeBetweenClientPunchThroughAttempts_;
  154. }
  155. timeTillNextPunchThroughAttempt_ -= dt;
  156. }
  157. // See if we need to still be trying to punch through from the client
  158. if (clientConnectToGameServerState_ == GAME_CONNECTING_EXTERNAL_IP &&
  159. clientToServerSocket_ != NULL && timeTillNextClientConnectAttempt_ <= 0)
  160. {
  161. Atomic::Network* network = GetSubsystem<Network>();
  162. if (!network->GetServerConnection() || !network->GetServerConnection()->IsConnected())
  163. {
  164. ATOMIC_LOGINFO("Sending packet to server");
  165. clientToServerSocket_->Send("K",1);
  166. }
  167. timeTillNextClientConnectAttempt_ = timeBetweenClientConnectAttempts_;
  168. }
  169. timeTillNextClientConnectAttempt_ -= dt;
  170. }
  171. void MasterServerClient::CheckForMessageFromMaster()
  172. {
  173. kNet::OverlappedTransferBuffer *buf = masterTCPConnection_->BeginReceive();
  174. if (buf && buf->bytesContains > 0) {
  175. int n = 0;
  176. int totalBytes = buf->bytesContains;
  177. while (n < totalBytes) {
  178. char c = buf->buffer.buf[n++];
  179. // Are we still reading in the length?
  180. if (readingMasterMessageLength) {
  181. if (c == ':') {
  182. sscanf(masterMessageLengthStr.CString(), "%u" , &bytesRemainingInMasterServerMessage_);
  183. readingMasterMessageLength = false;
  184. ATOMIC_LOGINFO("Message is " + String(bytesRemainingInMasterServerMessage_) + " long");
  185. }
  186. else {
  187. masterMessageLengthStr += c;
  188. }
  189. }
  190. else {
  191. // Are we reading in the string?
  192. masterMessageStr += c;
  193. bytesRemainingInMasterServerMessage_--;
  194. // Did we hit the end of the string?
  195. if (bytesRemainingInMasterServerMessage_ == 0) {
  196. HandleMasterServerMessage(masterMessageStr);
  197. readingMasterMessageLength = true;
  198. masterMessageLengthStr = "";
  199. masterMessageStr = "";
  200. }
  201. }
  202. }
  203. masterTCPConnection_->EndReceive(buf);
  204. }
  205. }
  206. void MasterServerClient::ConnectToMasterUpdate(float dt)
  207. {
  208. if (connectToMasterState_ == MASTER_NOT_CONNECTED ||
  209. connectToMasterState_ == MASTER_CONNECTION_FAILED)
  210. {
  211. return;
  212. }
  213. if (connectToMasterState_ == MASTER_CONNECTING_UDP)
  214. {
  215. if (udpConnectionSecondsRemaining_ <= 0)
  216. {
  217. connectToMasterState_ = MASTER_CONNECTION_FAILED;
  218. // TODO - emit error event
  219. return;
  220. }
  221. if (udpSecondsTillRetry_ <= 0)
  222. {
  223. String msg = "{ \"cmd\": \"registerUDPPort\", \"id\": \"" + masterServerConnectionId_ + "\"}";
  224. masterUDPConnection_->Send(msg.CString(), msg.Length());
  225. udpSecondsTillRetry_ = 0.5;
  226. }
  227. else
  228. {
  229. udpSecondsTillRetry_ -= dt;
  230. udpConnectionSecondsRemaining_ -= dt;
  231. }
  232. }
  233. }
  234. void MasterServerClient::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  235. {
  236. using namespace BeginFrame;
  237. Update(eventData[P_TIMESTEP].GetFloat());
  238. }
  239. void MasterServerClient::SetConnectToMasterState(ConnectToMasterState state)
  240. {
  241. Atomic::Network* network = GetSubsystem<Network>();
  242. kNet::Network* kNetNetwork = network->GetKnetNetwork();
  243. if (connectToMasterState_ == MASTER_NOT_CONNECTED &&
  244. state == MASTER_CONNECTING_TCP)
  245. {
  246. masterTCPConnection_ = kNetNetwork->ConnectSocket(masterServerInfo_.address.CString(),
  247. masterServerInfo_.port, kNet::SocketOverTCP);
  248. if (!masterTCPConnection_)
  249. {
  250. SetConnectToMasterState(MASTER_CONNECTION_FAILED);
  251. }
  252. else
  253. {
  254. SendMessageToMasterServer("{ \"cmd\": \"connectRequest\" }");
  255. }
  256. }
  257. else if (connectToMasterState_ == MASTER_CONNECTING_TCP &&
  258. state == MASTER_CONNECTING_UDP)
  259. {
  260. Atomic::Network* network = GetSubsystem<Network>();
  261. kNet::Network* kNetNetwork = network->GetKnetNetwork();
  262. kNet::NetworkServer* server = kNetNetwork->GetServer().ptr();
  263. kNet::EndPoint masterEndPoint;
  264. sscanf(masterServerInfo_.address.CString(), "%hu.%hu.%hu.%hu",
  265. (unsigned short *) &masterEndPoint.ip[0],
  266. (unsigned short *) &masterEndPoint.ip[1],
  267. (unsigned short *) &masterEndPoint.ip[2],
  268. (unsigned short *) &masterEndPoint.ip[3]);
  269. masterEndPoint.port = masterServerInfo_.port;
  270. if (server)
  271. {
  272. std::vector < kNet::Socket * > listenSockets = kNetNetwork->GetServer()->ListenSockets();
  273. kNet::Socket *listenSocket = listenSockets[0];
  274. // Create a UDP and a TCP connection to the master server
  275. // UDP connection re-uses the same udp port we are listening on for the sever
  276. masterUDPConnection_ = new kNet::Socket(listenSocket->GetSocketHandle(),
  277. listenSocket->LocalEndPoint(),
  278. listenSocket->LocalAddress(), masterEndPoint, "",
  279. kNet::SocketOverUDP, kNet::ServerClientSocket, 1400);
  280. }
  281. else
  282. {
  283. masterUDPConnection_ = kNetNetwork->CreateUnconnectedUDPSocket(masterServerInfo_.address.CString(),
  284. masterServerInfo_.port);
  285. }
  286. }
  287. if (state == MASTER_CONNECTION_FAILED)
  288. {
  289. ATOMIC_LOGERROR("Could not connect to master server");
  290. SendEvent(E_MASTERCONNECTIONFAILED);
  291. }
  292. if (state == MASTER_CONNECTED)
  293. {
  294. SendEvent(E_MASTERCONNECTIONREADY);
  295. }
  296. connectToMasterState_ = state;
  297. }
  298. void MasterServerClient::SetConnectToGameServerState(ClientConnectToGameServerState state)
  299. {
  300. if (clientConnectToGameServerState_ == GAME_NOT_CONNECTED &&
  301. state == GAME_CONNECTING_INTERNAL_IP)
  302. {
  303. kNet::EndPoint serverEndPoint;
  304. sscanf(remoteGameServerInfo_.internalAddress.CString(), "%hu.%hu.%hu.%hu",
  305. (unsigned short *) &serverEndPoint.ip[0],
  306. (unsigned short *) &serverEndPoint.ip[1],
  307. (unsigned short *) &serverEndPoint.ip[2],
  308. (unsigned short *) &serverEndPoint.ip[3]);
  309. serverEndPoint.port = remoteGameServerInfo_.internalPort;
  310. clientToServerSocket_ = new kNet::Socket(masterUDPConnection_->GetSocketHandle(),
  311. masterUDPConnection_->LocalEndPoint(),
  312. masterUDPConnection_->LocalAddress(), serverEndPoint, "",
  313. kNet::SocketOverUDP, kNet::ClientConnectionLessSocket, 1400);
  314. Atomic::Network* network = GetSubsystem<Network>();
  315. network->ConnectWithExistingSocket(clientToServerSocket_, remoteGameServerInfo_.clientScene);
  316. connectToGameServerSecondsRemaining_ = 5.0f;
  317. ATOMIC_LOGINFO("Connecting to Game Server on Internal IP: " +
  318. remoteGameServerInfo_.internalAddress + ":" +
  319. String(remoteGameServerInfo_.internalPort));
  320. }
  321. else if (clientConnectToGameServerState_ == GAME_CONNECTING_INTERNAL_IP &&
  322. state == GAME_CONNECTING_EXTERNAL_IP)
  323. {
  324. // Ask the master server to tell the game server
  325. // we want to connect to it.
  326. String msg = String("{") +
  327. String("\"cmd\":") + String("\"requestIntroduction\",") +
  328. String("\"id\":\"") + masterServerConnectionId_ + String("\", ") +
  329. String("\"serverId\":\"") + remoteGameServerInfo_.serverId + String("\"") +
  330. String("}");
  331. SendMessageToMasterServer(msg);
  332. kNet::EndPoint serverEndPoint;
  333. sscanf(remoteGameServerInfo_.externalAddress.CString(), "%hu.%hu.%hu.%hu",
  334. (unsigned short *) &serverEndPoint.ip[0],
  335. (unsigned short *) &serverEndPoint.ip[1],
  336. (unsigned short *) &serverEndPoint.ip[2],
  337. (unsigned short *) &serverEndPoint.ip[3]);
  338. serverEndPoint.port = remoteGameServerInfo_.externalPort;
  339. clientToServerSocket_ = new kNet::Socket(masterUDPConnection_->GetSocketHandle(),
  340. masterUDPConnection_->LocalEndPoint(),
  341. masterUDPConnection_->LocalAddress(), serverEndPoint, "",
  342. kNet::SocketOverUDP, kNet::ClientConnectionLessSocket, 1400);
  343. Atomic::Network* network = GetSubsystem<Network>();
  344. network->ConnectWithExistingSocket(clientToServerSocket_, remoteGameServerInfo_.clientScene);
  345. connectToGameServerSecondsRemaining_ = 5.0f;
  346. ATOMIC_LOGINFO("Connecting to Game Server on External IP: " +
  347. remoteGameServerInfo_.externalAddress + ":" +
  348. String(remoteGameServerInfo_.externalPort));
  349. }
  350. if (state == GAME_CONNECTION_FAILED)
  351. {
  352. SendEvent(E_CONNECTFAILED);
  353. }
  354. clientConnectToGameServerState_ = state;
  355. }
  356. void MasterServerClient::ConnectToGameServerUpdate(float dt)
  357. {
  358. if (clientConnectToGameServerState_ == GAME_NOT_CONNECTED ||
  359. clientConnectToGameServerState_ == GAME_CONNECTED ||
  360. clientConnectToGameServerState_ == GAME_CONNECTION_FAILED)
  361. {
  362. return;
  363. }
  364. Atomic::Network* network = GetSubsystem<Network>();
  365. // If we are connected then set the final state
  366. if (network->GetServerConnection() && network->GetServerConnection()->IsConnected())
  367. {
  368. if (clientConnectToGameServerState_ == GAME_CONNECTING_INTERNAL_IP)
  369. {
  370. ATOMIC_LOGINFO("Successfully connected using internal IP");
  371. }
  372. else if (clientConnectToGameServerState_ == GAME_CONNECTING_EXTERNAL_IP)
  373. {
  374. ATOMIC_LOGINFO("Successfully connected using external IP");
  375. }
  376. SetConnectToGameServerState(GAME_CONNECTED);
  377. return;
  378. }
  379. if (connectToGameServerSecondsRemaining_ <= 0)
  380. {
  381. if (clientConnectToGameServerState_ == GAME_CONNECTING_INTERNAL_IP)
  382. {
  383. ATOMIC_LOGINFO("Unable to connect via internal IP, trying external IP");
  384. SetConnectToGameServerState(GAME_CONNECTING_EXTERNAL_IP);
  385. }
  386. else
  387. {
  388. SetConnectToGameServerState(GAME_CONNECTION_FAILED);
  389. }
  390. return;
  391. }
  392. connectToGameServerSecondsRemaining_ -= dt;
  393. }
  394. void MasterServerClient::HandleMasterServerMessage(const String &msg)
  395. {
  396. ATOMIC_LOGINFO("Got master server message: " + msg);
  397. rapidjson::Document document;
  398. if (document.Parse<0>(msg.CString()).HasParseError())
  399. {
  400. ATOMIC_LOGERROR("Could not parse JSON data from string");
  401. return;
  402. }
  403. String cmd = document["cmd"].GetString();
  404. bool sendEvent = false;
  405. if (cmd == "connectTCPSuccess")
  406. {
  407. udpSecondsTillRetry_ = 0;
  408. udpConnectionSecondsRemaining_ = 5.0;
  409. masterServerConnectionId_ = document["id"].GetString();
  410. // Now connect with UDP
  411. SetConnectToMasterState(MASTER_CONNECTING_UDP);
  412. ATOMIC_LOGINFO("TCP Connected");
  413. }
  414. else if (cmd == "connectUDPSuccess")
  415. {
  416. SetConnectToMasterState(MASTER_CONNECTED);
  417. // Register server if needed
  418. if (masterServerInfo_.isRegisteringServer)
  419. {
  420. RegisterServerWithMaster(masterServerInfo_.serverName);
  421. }
  422. ATOMIC_LOGINFO("UDP Connected");
  423. }
  424. else if (cmd == "sendPacketToClient")
  425. {
  426. String clientIP = document["clientIP"].GetString();
  427. int clientPort = document["clientPort"].GetInt();
  428. ATOMIC_LOGINFO("Got request to send packet to client at "+clientIP+":"+String(clientPort));
  429. kNet::EndPoint clientEndPoint;
  430. sscanf(clientIP.CString(), "%hu.%hu.%hu.%hu",
  431. (unsigned short *) &clientEndPoint.ip[0],
  432. (unsigned short *) &clientEndPoint.ip[1],
  433. (unsigned short *) &clientEndPoint.ip[2],
  434. (unsigned short *) &clientEndPoint.ip[3]);
  435. clientEndPoint.port = clientPort;
  436. // Create a socket that goes out the same port we are listening on to the client.
  437. // This will be used until the client actually connects.
  438. kNet::Socket* s = new kNet::Socket(masterUDPConnection_->GetSocketHandle(),
  439. masterUDPConnection_->LocalEndPoint(),
  440. masterUDPConnection_->LocalAddress(), clientEndPoint, "",
  441. kNet::SocketOverUDP, kNet::ServerClientSocket, 1400);
  442. String clientId = document["clientId"].GetString();
  443. clientIdToPunchThroughSocketMap_.Insert(MakePair(clientId, s));
  444. }
  445. else if (cmd == "serverList")
  446. {
  447. sendEvent = true;
  448. }
  449. if (sendEvent)
  450. {
  451. using namespace NetworkMessage;
  452. VariantMap& eventData = GetEventDataMap();
  453. eventData[P_DATA] = msg;
  454. SendEvent(E_MASTERMESSAGE, eventData);
  455. }
  456. }
  457. bool MasterServerClient::StartServerAndRegisterWithMaster(unsigned short serverPort, const String &masterAddress,
  458. unsigned short masterPort, const String &serverName)
  459. {
  460. Network* network = GetSubsystem<Network>();
  461. if (!network)
  462. {
  463. ATOMIC_LOGERROR("MasterServerClient::StartServerAndRegisterWithMaster - Unable to get Network subsystem");
  464. return false;
  465. }
  466. // First start the server
  467. bool rc = network->StartServer(serverPort, kNet::SocketOverUDP);
  468. if (!rc)
  469. {
  470. ATOMIC_LOGERROR("MasterServerClient::StartServerAndRegisterWithMaster - Unable to start server");
  471. return false;
  472. }
  473. // Connect to the master server
  474. ConnectToMasterAndRegister(masterAddress, masterPort, serverName);
  475. return true;
  476. }
  477. }