MasterClient.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "MasterClient.h"
  11. #include "RakPeerInterface.h"
  12. #include "PacketEnumerations.h"
  13. #include "RakNetworkFactory.h"
  14. #include "StringCompressor.h"
  15. #include "GetTime.h"
  16. #include <cstring>
  17. // Uncomment this define for debugging printfs
  18. #define _SHOW_MASTER_SERVER_PRINTF
  19. #ifdef _SHOW_MASTER_SERVER_PRINTF
  20. #include <cstdio>
  21. #endif
  22. MasterClient::MasterClient()
  23. {
  24. }
  25. MasterClient::~MasterClient()
  26. {
  27. ClearServerList();
  28. }
  29. bool MasterClient::Connect(char* host, int masterServerPort)
  30. {
  31. localServer.Clear();
  32. listServer=serverListed=localServerModified=false;
  33. localServer.connectionIdentifier.port=rakPeer->GetInternalID().port;
  34. ruleIdentifierList.Reset();
  35. return rakPeer->Connect(host, masterServerPort, 0, 0);
  36. }
  37. void MasterClient::Disconnect(void)
  38. {
  39. if (IsConnected())
  40. DelistServer();
  41. rakPeer->Disconnect(100);
  42. }
  43. bool MasterClient::IsConnected(void)
  44. {
  45. unsigned short numberOfSystems;
  46. rakPeer->GetConnectionList(0, &numberOfSystems);
  47. return numberOfSystems==1;
  48. }
  49. void MasterClient::AddQueryRule(char *ruleIdentifier)
  50. {
  51. if (ruleIdentifier && IsReservedRuleIdentifier(ruleIdentifier)==false)
  52. stringCompressor->EncodeString(ruleIdentifier, 256, &ruleIdentifierList);
  53. }
  54. void MasterClient::ClearQueryRules(void)
  55. {
  56. ruleIdentifierList.Reset();
  57. }
  58. void MasterClient::QueryMasterServer(void)
  59. {
  60. BitStream outgoingBitStream;
  61. // Request to the master server for the list of servers that contain at least one of the specified keys
  62. outgoingBitStream.Write((unsigned char)ID_QUERY_MASTER_SERVER);
  63. if (ruleIdentifierList.GetNumberOfBitsUsed()>0)
  64. outgoingBitStream.WriteBits(ruleIdentifierList.GetData(), ruleIdentifierList.GetNumberOfBitsUsed(), false);
  65. rakPeer->Send(&outgoingBitStream, HIGH_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_PLAYER_ID, true);
  66. }
  67. void MasterClient::PingServers(void)
  68. {
  69. unsigned serverIndex;
  70. for (serverIndex=0; serverIndex < gameServerList.serverList.Size(); serverIndex++)
  71. {
  72. rakPeer->Ping((char*)rakPeer->PlayerIDToDottedIP(gameServerList.serverList[serverIndex]->connectionIdentifier),
  73. gameServerList.serverList[serverIndex]->connectionIdentifier.port, false);
  74. }
  75. }
  76. void MasterClient::Update(RakPeerInterface *peer)
  77. {
  78. BitStream outgoingBitStream;
  79. if (listServer && ((serverListed && localServerModified) || (serverListed==false)))
  80. {
  81. outgoingBitStream.Write((unsigned char)ID_MASTER_SERVER_SET_SERVER);
  82. SerializeServer(&localServer, &outgoingBitStream);
  83. rakPeer->Send(&outgoingBitStream, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_PLAYER_ID, true);
  84. serverListed=true;
  85. localServerModified=false;
  86. }
  87. }
  88. bool MasterClient::OnReceive(RakPeerInterface *peer, Packet *packet)
  89. {
  90. switch(packet->data[0])
  91. {
  92. case ID_NO_FREE_INCOMING_CONNECTIONS:
  93. OnMasterServerFull();
  94. return false; // Do not absorb packet
  95. case ID_DISCONNECTION_NOTIFICATION:
  96. OnLostConnection();
  97. return false; // Do not absorb packet
  98. case ID_CONNECTION_LOST:
  99. OnLostConnection();
  100. return false; // Do not absorb packet
  101. case ID_MODIFIED_PACKET:
  102. OnModifiedPacket();
  103. return false;
  104. case ID_CONNECTION_ATTEMPT_FAILED:
  105. OnConnectionAttemptFailed();
  106. return false; // Do not absorb packet
  107. case ID_MASTER_SERVER_UPDATE_SERVER:
  108. HandleServerListResponse(packet, false);
  109. return true; // Absorb packet
  110. case ID_MASTER_SERVER_SET_SERVER:
  111. HandleServerListResponse(packet, true);
  112. return true; // Absorb packet
  113. case ID_PONG:
  114. HandlePong(packet);
  115. return false; // Absorb packet
  116. case ID_RELAYED_CONNECTION_NOTIFICATION:
  117. HandleRelayedConnectionNotification(packet);
  118. return true; // Absorb packet
  119. }
  120. return 0;
  121. }
  122. void MasterClient::ConnectionAttemptNotification(char *serverIP, unsigned short serverPort)
  123. {
  124. if (serverIP==0)
  125. return;
  126. BitStream bitStream(23);
  127. bitStream.Write((unsigned char)ID_RELAYED_CONNECTION_NOTIFICATION);
  128. bitStream.Write(localServer.connectionIdentifier.port); // Your own game client port
  129. bitStream.Write(serverPort); // The game server you are connecting to port
  130. stringCompressor->EncodeString(serverIP, 22, &bitStream); // The game server IP you are connecting to
  131. rakPeer->Send(&bitStream, HIGH_PRIORITY, RELIABLE, 0, UNASSIGNED_PLAYER_ID, true);
  132. }
  133. void MasterClient::ListServer(void)
  134. {
  135. listServer=true;
  136. }
  137. void MasterClient::DelistServer(void)
  138. {
  139. BitStream bitStream;
  140. listServer=false;
  141. if (serverListed)
  142. {
  143. bitStream.Write((unsigned char)ID_MASTER_SERVER_DELIST_SERVER);
  144. bitStream.Write(localServer.connectionIdentifier.port);
  145. rakPeer->Send(&bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_PLAYER_ID, true);
  146. serverListed=false;
  147. }
  148. }
  149. void MasterClient::HandleServerListResponse(Packet *packet, bool overwriteExisting)
  150. {
  151. int serverIndex;
  152. bool newServerAdded;
  153. unsigned short numberOfServers;
  154. GameServer *gameServer;
  155. RakNetTime currentTime;
  156. BitStream inputBitStream(packet->data, packet->length, false);
  157. inputBitStream.IgnoreBits(8*sizeof(unsigned char));
  158. if (inputBitStream.ReadCompressed(numberOfServers)==false)
  159. return;
  160. currentTime=RakNet::GetTime();
  161. for (serverIndex=0; serverIndex < numberOfServers; serverIndex++)
  162. {
  163. gameServer = DeserializeServer(&inputBitStream);
  164. // Find the existing game server that matches this port/address.
  165. // If not found, then add it to the list.
  166. // else update it
  167. // If (overwriteExisting)
  168. // - Delete any fields that exist in the old and not in the new
  169. // Add any fields that exist in the new and do not exist in the old
  170. // Update any fields that exist in both
  171. // Unset the deletion mark
  172. gameServer=UpdateServerList(gameServer,overwriteExisting, &newServerAdded);
  173. if (newServerAdded)
  174. {
  175. // Ping the new server
  176. rakPeer->Ping((char*)rakPeer->PlayerIDToDottedIP(gameServer->connectionIdentifier),
  177. gameServer->connectionIdentifier.port, false);
  178. // Returns true if new server updated
  179. OnGameServerListAddition(gameServer);
  180. }
  181. else
  182. {
  183. // returns false if an existing server is modified
  184. OnGameServerListRuleUpdate(gameServer);
  185. }
  186. }
  187. // Any servers that were not updated on the last call to UpdateServerList
  188. // will have lastUpdateTime time as less than the current time
  189. // Delete those
  190. serverIndex=0;
  191. while (serverIndex < (int) gameServerList.serverList.Size())
  192. {
  193. if (gameServerList.serverList[serverIndex]->lastUpdateTime < currentTime)
  194. {
  195. delete gameServerList.serverList[serverIndex];
  196. gameServerList.serverList.RemoveAtIndex(serverIndex);
  197. }
  198. else
  199. serverIndex++;
  200. }
  201. OnGameServerListQueryComplete();
  202. }
  203. void MasterClient::HandleRelayedConnectionNotification(Packet *packet)
  204. {
  205. PlayerID clientSystem;
  206. BitStream incomingBitStream(packet->data, packet->length, false);
  207. incomingBitStream.IgnoreBits(8*sizeof(unsigned char));
  208. incomingBitStream.Read(clientSystem.binaryAddress);
  209. incomingBitStream.Read(clientSystem.port);
  210. OnConnectionRequest(rakPeer->PlayerIDToDottedIP(clientSystem), clientSystem.port);
  211. }
  212. void MasterClient::PostRule(char *ruleIdentifier, char *stringData, int intData)
  213. {
  214. if (ruleIdentifier)
  215. {
  216. if (IsReservedRuleIdentifier(ruleIdentifier))
  217. return;
  218. localServerModified |= UpdateServerRule(&localServer, ruleIdentifier, stringData, intData);
  219. }
  220. }
  221. void MasterClient::RemoveRule(char *ruleIdentifier)
  222. {
  223. if (ruleIdentifier)
  224. localServerModified |= RemoveServerRule(&localServer, ruleIdentifier);
  225. }
  226. void MasterClient::OnLostConnection(void)
  227. {
  228. #ifdef _SHOW_MASTER_SERVER_PRINTF
  229. printf("Connection lost.\n");
  230. #endif
  231. }
  232. void MasterClient::OnConnectionAttemptFailed(void)
  233. {
  234. #ifdef _SHOW_MASTER_SERVER_PRINTF
  235. printf("Connection attempt failed.\n");
  236. #endif
  237. }
  238. void MasterClient::OnMasterServerFull(void)
  239. {
  240. #ifdef _SHOW_MASTER_SERVER_PRINTF
  241. printf("Server full.\n");
  242. #endif
  243. }
  244. void MasterClient::OnModifiedPacket(void)
  245. {
  246. #ifdef _SHOW_MASTER_SERVER_PRINTF
  247. printf("Modified packet.\n");
  248. #endif
  249. }
  250. void MasterClient::OnGameServerListAddition(GameServer *newServer)
  251. {
  252. #ifdef _SHOW_MASTER_SERVER_PRINTF
  253. printf("Server added.\n");
  254. #endif
  255. }
  256. void MasterClient::OnGameServerListRuleUpdate(GameServer *updatedServer)
  257. {
  258. #ifdef _SHOW_MASTER_SERVER_PRINTF
  259. printf("Rules updated for a server.\n");
  260. #endif
  261. }
  262. void MasterClient::OnGameServerListQueryComplete(void)
  263. {
  264. #ifdef _SHOW_MASTER_SERVER_PRINTF
  265. printf("Query complete.\n");
  266. #endif
  267. }
  268. // Event when a game client wants to connect to our server
  269. // You should call AdvertiseSystem to the passed IP and port from your game instance
  270. void MasterClient::OnConnectionRequest(const char *clientIP, unsigned short clientPort)
  271. {
  272. #ifdef _SHOW_MASTER_SERVER_PRINTF
  273. printf("Master client indicates a connection request from %s:%i.\n", clientIP, clientPort);
  274. #endif
  275. rakPeer->AdvertiseSystem((char*)clientIP, clientPort,0,0);
  276. }
粤ICP备19079148号