MasterServer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "MasterServer.h"
  11. #include "RakPeerInterface.h"
  12. #include "BitStream.h"
  13. #include "RakNetworkFactory.h"
  14. #include "PacketEnumerations.h"
  15. #include "StringCompressor.h"
  16. #include "GetTime.h"
  17. #include "RakNetStatistics.h"
  18. // Uncomment this define for debugging printfs
  19. #define _SHOW_MASTER_SERVER_PRINTF
  20. #ifdef _SHOW_MASTER_SERVER_PRINTF
  21. #include <cstdio>
  22. #endif
  23. MasterServer::MasterServer()
  24. {
  25. }
  26. MasterServer::~MasterServer()
  27. {
  28. ClearServerList();
  29. }
  30. void MasterServer::Update(RakPeerInterface *peer)
  31. {
  32. unsigned serverIndex;
  33. RakNetTime time;
  34. // TODO - should have multiple listing security
  35. time = RakNet::GetTime();
  36. serverIndex=0;
  37. while (serverIndex < gameServerList.serverList.Size())
  38. {
  39. if (time >= gameServerList.serverList[serverIndex]->nextPingTime)
  40. {
  41. if (gameServerList.serverList[serverIndex]->failedPingResponses>=NUMBER_OF_MISSED_PINGS_TO_DROP)
  42. {
  43. #ifdef _SHOW_MASTER_SERVER_PRINTF
  44. printf("Deleting %s for lack of ping response.\n", (char*)rakPeer->PlayerIDToDottedIP(gameServerList.serverList[serverIndex]->connectionIdentifier));
  45. #endif
  46. gameServerList.serverList[serverIndex]->Clear();
  47. delete gameServerList.serverList[serverIndex];
  48. gameServerList.serverList.RemoveAtIndex(serverIndex);
  49. }
  50. else
  51. {
  52. gameServerList.serverList[serverIndex]->nextPingTime = time + KEEP_ALIVE_PING_FREQUENCY;
  53. if (rakPeer->GetIndexFromPlayerID(gameServerList.serverList[serverIndex]->connectionIdentifier)==-1)
  54. {
  55. rakPeer->Ping((char*)rakPeer->PlayerIDToDottedIP(gameServerList.serverList[serverIndex]->connectionIdentifier),
  56. gameServerList.serverList[serverIndex]->connectionIdentifier.port, false);
  57. gameServerList.serverList[serverIndex]->failedPingResponses++;
  58. #ifdef _SHOW_MASTER_SERVER_PRINTF
  59. printf("Pinging %s. Waiting on %i repl(ies) so far.\n", (char*)rakPeer->PlayerIDToDottedIP(gameServerList.serverList[serverIndex]->connectionIdentifier),gameServerList.serverList[serverIndex]->failedPingResponses);
  60. #endif
  61. }
  62. else
  63. {
  64. #ifdef _SHOW_MASTER_SERVER_PRINTF
  65. printf("Not pinging %s since they are currently connected.\n", (char*)rakPeer->PlayerIDToDottedIP(gameServerList.serverList[serverIndex]->connectionIdentifier));
  66. #endif
  67. }
  68. serverIndex++;
  69. }
  70. }
  71. else
  72. serverIndex++;
  73. }
  74. }
  75. bool MasterServer::OnReceive(RakPeerInterface *peer, Packet *packet)
  76. {
  77. RakNetStatisticsStruct *rss;
  78. RakNetTime connectionTime;
  79. RakNetTime time;
  80. unsigned serverIndex;
  81. time = RakNet::GetTime();
  82. // Quick and dirty flood attack security:
  83. // If a client has been connected for more than 5 seconds,
  84. // and has sent more than 1000 bytes per second on average then ban them
  85. rss=rakPeer->GetStatistics(packet->playerId);
  86. if (rss)
  87. {
  88. connectionTime=time-rss->connectionStartTime;
  89. if (connectionTime > FLOOD_ATTACK_CHECK_DELAY &&
  90. (float)(rss->bitsReceived/8) / (float) connectionTime > FLOOD_ATTACK_BYTES_PER_MS)
  91. {
  92. rakPeer->CloseConnection(packet->playerId, true,0);
  93. #ifdef _SHOW_MASTER_SERVER_PRINTF
  94. printf("%s banned for session due to for flood attack\n", (char*)rakPeer->PlayerIDToDottedIP(packet->playerId));
  95. #endif
  96. rakPeer->AddToBanList(rakPeer->PlayerIDToDottedIP(packet->playerId));
  97. // Find all servers with this IP and kill them.
  98. serverIndex=0;
  99. while (serverIndex < gameServerList.serverList.Size())
  100. {
  101. if (gameServerList.serverList[serverIndex]->connectionIdentifier.binaryAddress==packet->playerId.binaryAddress)
  102. {
  103. delete gameServerList.serverList[serverIndex];
  104. gameServerList.serverList.RemoveAtIndex(serverIndex);
  105. }
  106. else
  107. serverIndex++;
  108. }
  109. }
  110. }
  111. switch(packet->data[0])
  112. {
  113. case ID_QUERY_MASTER_SERVER:
  114. HandleQuery(packet);
  115. return true;
  116. case ID_MASTER_SERVER_DELIST_SERVER:
  117. HandleDelistServer(packet);
  118. return true;
  119. case ID_MASTER_SERVER_SET_SERVER:
  120. HandleUpdateServer(packet);
  121. return true;
  122. case ID_PONG:
  123. HandlePong(packet);
  124. return false;
  125. case ID_RELAYED_CONNECTION_NOTIFICATION:
  126. HandleRelayedConnectionNotification(packet);
  127. return true;
  128. }
  129. return false; // Absorb packet
  130. }
  131. bool MasterServer::PropagateToGame(Packet *packet) const
  132. {
  133. unsigned char packetIdentifier = packet->data[ 0 ];
  134. return packetIdentifier!=ID_QUERY_MASTER_SERVER &&
  135. packetIdentifier!=ID_MASTER_SERVER_DELIST_SERVER &&
  136. packetIdentifier!=ID_MASTER_SERVER_SET_SERVER &&
  137. packetIdentifier!=ID_RELAYED_CONNECTION_NOTIFICATION;
  138. }
  139. void MasterServer::HandleDelistServer(Packet *packet)
  140. {
  141. PlayerID serverPlayerID;
  142. int existingServerIndex;
  143. BitStream bitStream(packet->data, packet->length, false);
  144. bitStream.IgnoreBits(sizeof(unsigned char)*8); // Ignore the packet type enum
  145. bitStream.Read(serverPlayerID.port);
  146. serverPlayerID.binaryAddress=packet->playerId.binaryAddress;
  147. existingServerIndex=gameServerList.GetIndexByPlayerID(serverPlayerID);
  148. if (existingServerIndex>=0)
  149. {
  150. gameServerList.serverList[existingServerIndex]->Clear();
  151. delete gameServerList.serverList[existingServerIndex];
  152. gameServerList.serverList.RemoveAtIndex(existingServerIndex);
  153. }
  154. //else
  155. // Server does not already exist
  156. #ifdef _SHOW_MASTER_SERVER_PRINTF
  157. printf("%i servers on the list\n", gameServerList.serverList.Size());
  158. #endif
  159. }
  160. void MasterServer::HandleQuery(Packet *packet)
  161. {
  162. DataStructures::List<GameServer*> serversWithKeysList;
  163. char ruleIdentifier[256];
  164. unsigned index, serverIndex;
  165. int key;
  166. bool queryAll;
  167. BitStream outputBitStream;
  168. BitStream compressedString(packet->data, packet->length, false);
  169. compressedString.IgnoreBits(8*sizeof(unsigned char));
  170. queryAll=true;
  171. while (compressedString.GetNumberOfUnreadBits()>0)
  172. {
  173. // Generate a list of the indices of the servers that have one or more of the specified keys.
  174. stringCompressor->DecodeString(ruleIdentifier, 256, &compressedString);
  175. if (ruleIdentifier[0]==0)
  176. // If we fail to read the first string, queryAll remains true.
  177. break;
  178. queryAll=false;
  179. if (IsReservedRuleIdentifier(ruleIdentifier))
  180. continue;
  181. for (index=0; index < gameServerList.serverList.Size(); index++)
  182. {
  183. if (gameServerList.serverList[index]->connectionIdentifier==UNASSIGNED_PLAYER_ID)
  184. continue;
  185. if (gameServerList.serverList[index]->FindKey(ruleIdentifier))
  186. {
  187. serverIndex=serversWithKeysList.GetIndexOf(gameServerList.serverList[index]);
  188. if (serverIndex==MAX_UNSIGNED_LONG)
  189. {
  190. gameServerList.serverList[index]->numberOfKeysFound=1;
  191. serversWithKeysList.Insert(gameServerList.serverList[index]);
  192. }
  193. else
  194. {
  195. serversWithKeysList[serverIndex]->numberOfKeysFound++;
  196. }
  197. }
  198. }
  199. }
  200. // Write the packet id
  201. if (queryAll)
  202. outputBitStream.Write((unsigned char) ID_MASTER_SERVER_SET_SERVER);
  203. else
  204. outputBitStream.Write((unsigned char) ID_MASTER_SERVER_UPDATE_SERVER);
  205. if (queryAll)
  206. {
  207. // Write the number of servers
  208. outputBitStream.WriteCompressed((unsigned short)gameServerList.serverList.Size());
  209. for (index=0; index < gameServerList.serverList.Size(); index++)
  210. {
  211. // Write the whole server
  212. SerializeServer(gameServerList.serverList[index], &outputBitStream);
  213. }
  214. }
  215. else
  216. {
  217. compressedString.ResetReadPointer();
  218. compressedString.IgnoreBits(8*sizeof(unsigned char));
  219. // Write the number of servers with requested keys
  220. outputBitStream.WriteCompressed((unsigned short)serversWithKeysList.Size());
  221. // For each server, write the header which consists of the IP/PORT.
  222. // Then go through the list of requested keys and write those
  223. for (index=0; index < serversWithKeysList.Size(); index++)
  224. {
  225. SerializePlayerID(&(serversWithKeysList[index]->connectionIdentifier), &outputBitStream);
  226. outputBitStream.WriteCompressed((unsigned short)serversWithKeysList[index]->numberOfKeysFound);
  227. while (compressedString.GetNumberOfUnreadBits()>0)
  228. {
  229. // Generate a list of the indices of the servers that have one or more of the specified keys.
  230. stringCompressor->DecodeString(ruleIdentifier, 256, &compressedString);
  231. if (ruleIdentifier[0]==0)
  232. break;
  233. if (IsReservedRuleIdentifier(ruleIdentifier))
  234. continue;
  235. serversWithKeysList[index]->FindKey(ruleIdentifier);
  236. key=serversWithKeysList[index]->keyIndex;
  237. if (key>=0)
  238. SerializeRule(serversWithKeysList[index]->serverRules[key], &outputBitStream);
  239. }
  240. }
  241. }
  242. rakPeer->Send(&outputBitStream, MEDIUM_PRIORITY, RELIABLE, 0, packet->playerId, false);
  243. }
  244. void MasterServer::HandleUpdateServer(Packet *packet)
  245. {
  246. GameServer *gameServer;
  247. bool newServerAdded;
  248. BitStream incomingBitStream(packet->data, packet->length, false);
  249. incomingBitStream.IgnoreBits(8*sizeof(unsigned char));
  250. gameServer = DeserializeServer(&incomingBitStream);
  251. gameServer->connectionIdentifier.binaryAddress=packet->playerId.binaryAddress;
  252. UpdateServerList(gameServer, true, &newServerAdded);
  253. if (newServerAdded)
  254. {
  255. #ifdef _SHOW_MASTER_SERVER_PRINTF
  256. printf("Server added. %i servers on the list\n", gameServerList.serverList.Size());
  257. #endif
  258. gameServer->originationId=packet->playerId;
  259. }
  260. #ifdef _SHOW_MASTER_SERVER_PRINTF
  261. else
  262. printf("Server updated. %i servers on the list\n", gameServerList.serverList.Size());
  263. #endif
  264. }
  265. void MasterServer::OnModifiedPacket(void)
  266. {
  267. #ifdef _SHOW_MASTER_SERVER_PRINTF
  268. printf("Modified packet.\n");
  269. #endif
  270. }
  271. void MasterServer::HandleRelayedConnectionNotification(Packet *packet)
  272. {
  273. char str[22];
  274. unsigned short clientGamePort, serverGamePort;
  275. BitStream incomingBitStream(packet->data, packet->length, false);
  276. incomingBitStream.IgnoreBits(8*sizeof(unsigned char));
  277. incomingBitStream.Read(clientGamePort);
  278. incomingBitStream.Read(serverGamePort);
  279. if (!stringCompressor->DecodeString(str, 22, &incomingBitStream))
  280. return;
  281. if (str[0]==0)
  282. return;
  283. BitStream outgoingBitStream;
  284. outgoingBitStream.Write((unsigned char)ID_RELAYED_CONNECTION_NOTIFICATION);
  285. // Assumes the game client is on the same computer as the master client
  286. outgoingBitStream.Write(packet->playerId.binaryAddress); // This is the public IP, which the sender doesn't necessarily know
  287. outgoingBitStream.Write(clientGamePort);
  288. PlayerID targetID;
  289. rakPeer->IPToPlayerID(str, serverGamePort, &targetID);
  290. // Given the IP and port of the game system, give me the index into the game server list
  291. int serverIndex = gameServerList.GetIndexByPlayerID(targetID);
  292. if (serverIndex>=0)
  293. {
  294. #ifdef _SHOW_MASTER_SERVER_PRINTF
  295. printf("ID_RELAYED_CONNECTION_NOTIFICATION sent to %s:%i from %s:%i\n", str, serverGamePort, rakPeer->PlayerIDToDottedIP(packet->playerId), packet->playerId.port);
  296. #endif
  297. rakPeer->Send(&outgoingBitStream, HIGH_PRIORITY, RELIABLE, 0, gameServerList.serverList[serverIndex]->originationId, false);
  298. }
  299. else
  300. {
  301. #ifdef _SHOW_MASTER_SERVER_PRINTF
  302. printf("ID_RELAYED_CONNECTION_NOTIFICATION not sent to %s:%i from %s:%i.\nMaster server does not know about target system.\n", str, serverGamePort, rakPeer->PlayerIDToDottedIP(packet->playerId), packet->playerId.port);
  303. #endif
  304. }
  305. }
粤ICP备19079148号