MasterServerClient.cpp 19 KB

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