MasterCommon.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /// \file
  11. #ifndef __MASTER_COMMON_H
  12. #define __MASTER_COMMON_H
  13. #include "DS_List.h"
  14. #include "NetworkTypes.h"
  15. #include "BitStream.h"
  16. #include "PluginInterface.h"
  17. using namespace RakNet;
  18. class RakPeerInterface;
  19. struct Packet;
  20. // IP, Port, Ping - case sensitive!
  21. #define NUMBER_OF_DEFAULT_MASTER_SERVER_KEYS 3
  22. // If we ping NUMBER_OF_MISSED_PINGS_TO_DROP without ever a response, that server is dropped from the list.
  23. // This includes the last ping, so actually NUMBER_OF_MISSED_PINGS_TO_DROP-1 would be truly missed
  24. #define NUMBER_OF_MISSED_PINGS_TO_DROP 4
  25. // KEEP_ALIVE_PING_FREQUENCY is how often to ping servers to make sure they are active
  26. #define KEEP_ALIVE_PING_FREQUENCY 20000
  27. // How many ms must pass per connection before we check average bytes for a flood attack
  28. #define FLOOD_ATTACK_CHECK_DELAY 5000
  29. // How many bytes per ms someone has to send on average before they are banned.
  30. #define FLOOD_ATTACK_BYTES_PER_MS 2.0f
  31. struct GameServerRule;
  32. struct GameServer;
  33. /// \defgroup MASTER_SERVER_GROUP MasterServer
  34. /// \ingroup PLUGINS_GROUP
  35. /// \ingroup MASTER_SERVER_GROUP
  36. /// \brief Just a utility class.
  37. struct GameServerList
  38. {
  39. public:
  40. GameServerList();
  41. ~GameServerList();
  42. void Clear(void);
  43. void SortOnKey(char *key, bool ascending);
  44. void QuickSort(int low, int high, bool ascending);
  45. int Partition(int low, int high, bool ascending);
  46. int GetIndexByPlayerID(PlayerID playerID);
  47. DataStructures::List<GameServer*> serverList;
  48. };
  49. /// \ingroup MASTER_SERVER_GROUP
  50. class MasterCommon : public PluginInterface
  51. {
  52. public:
  53. MasterCommon();
  54. // ---------------------------------------------------
  55. // BROWSER FUNCTIONS
  56. // ---------------------------------------------------
  57. // Sorting function
  58. // ruleIdentifier is a string used by you previously when adding rules via PostRule
  59. // It can also be "IP" "Port" or "Ping"
  60. // Set ascending to true to sort from low to high. Otherwise sorts from high to low.
  61. void SortServerListOnKey(char *ruleIdentifier, bool ascending);
  62. // serverIndex should be from 0 to GetServerListSize()-1
  63. // ruleIdentifier is a string used by you previously when adding rules via PostRule
  64. // It can also be "IP" "Port" or "Ping".
  65. // identifier found will return true if the specified rule is found AND you are reading the
  66. // correct type.
  67. // GetServerListRuleAsInt should be used for int values.
  68. // GetServerListRuleAsString should be used for string values
  69. unsigned int GetServerListSize(void);
  70. int GetServerListRuleAsInt(int serverIndex, char *ruleIdentifier, bool *identifierFound);
  71. const char* GetServerListRuleAsString(int serverIndex, char *ruleIdentifier, bool *identifierFound);
  72. protected:
  73. void OnAttach(RakPeerInterface *peer);
  74. // Delete all elements from the server list
  75. void ClearServerList(void);
  76. // Returns true if a rule is reserved
  77. bool IsReservedRuleIdentifier(char *ruleIdentifier);
  78. void HandlePong(Packet *packet);
  79. // Adds or updates the specified rule to the specified server.
  80. // Returns true if the server has been changed. False if we are adding a rule that is already the same
  81. bool UpdateServerRule(GameServer *gameServer, char *ruleIdentifier, char *stringData, int intData);
  82. // Remove the specified rule from the server.
  83. // Returns true if the rule was removed.
  84. bool RemoveServerRule(GameServer *gameServer, char *ruleIdentifier);
  85. // Encode a playerID to a bitstream
  86. void SerializePlayerID(PlayerID *playerID, BitStream *outputBitStream);
  87. // Encode a rule to a bitstream
  88. void SerializeRule(GameServerRule *gameServerRule, BitStream *outputBitStream);
  89. // Decode a playerID from a bitstream
  90. void DeserializePlayerID(PlayerID *playerID, BitStream *inputBitStream);
  91. // Decode a rule from a bitstream
  92. GameServerRule *DeserializeRule(BitStream *inputBitStream);
  93. // Encode a server to a bitstream
  94. void SerializeServer(GameServer *gameServer, BitStream *outputBitStream);
  95. // Create a server from a bitstream
  96. GameServer *DeserializeServer(BitStream *inputBitStream);
  97. // Add the default rules to a server (ip, port, ping)
  98. void AddDefaultRulesToServer(GameServer *gameServer, PlayerID playerID);
  99. // Update one server based on the information in another
  100. void UpdateServer(GameServer *destination, GameServer *source, bool deleteSingleRules);
  101. // Add the specified server to the list of servers - or if the server already exists
  102. // Update the existing server and delete the server passed
  103. // deleteSingleRules means if a match is found and a rule exists in the old
  104. // server but not the new, then delete that rule.
  105. // Returns the new or updated server
  106. GameServer * UpdateServerList(GameServer *gameServer, bool deleteSingleRules, bool *newServerAdded);
  107. RakPeerInterface *rakPeer;
  108. GameServerList gameServerList;
  109. };
  110. /// \ingroup MASTER_SERVER_GROUP
  111. struct GameServerRule
  112. {
  113. GameServerRule();
  114. ~GameServerRule();
  115. char *key;
  116. // stringValue and intValue are mutually exclusive
  117. char *stringValue;
  118. int intValue;
  119. };
  120. /// \ingroup MASTER_SERVER_GROUP
  121. struct GameServer
  122. {
  123. GameServer();
  124. ~GameServer();
  125. void Clear(void);
  126. bool FindKey(char *key);
  127. int keyIndex;
  128. int numberOfKeysFound;
  129. RakNetTime lastUpdateTime;
  130. PlayerID connectionIdentifier; // The game server
  131. PlayerID originationId; // Only used by the server - the master client PlayerID
  132. int failedPingResponses;
  133. RakNetTime nextPingTime;
  134. // When inserting rules, don't forget that IP and ping should always be added.
  135. // These are required for any game server
  136. DataStructures::List<GameServerRule*> serverRules;
  137. };
  138. #endif
粤ICP备19079148号