PeerManager.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. EQ2Emu: Everquest II Server Emulator
  3. Copyright (C) 2007-2025 EQ2Emu Development Team (https://www.eq2emu.com)
  4. This file is part of EQ2Emu.
  5. EQ2Emu is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emu is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emu. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef PEERMANAGER_H
  17. #define PEERMANAGER_H
  18. #include <iostream>
  19. #include <chrono>
  20. #include <map>
  21. #include <string>
  22. #include <optional>
  23. #include <memory>
  24. #include <mutex>
  25. #include <atomic>
  26. #include <boost/property_tree/ptree.hpp>
  27. #include <boost/property_tree/json_parser.hpp>
  28. #include "../../common/types.h"
  29. class Client;
  30. enum HealthStatus {
  31. UNKNOWN = 0,
  32. WARN = 1,
  33. ERROR = 2,
  34. STARTUP = 3,
  35. OK = 4,
  36. SHUTDOWN = 5
  37. };
  38. enum class PeeringStatus {
  39. PRIMARY,
  40. SECONDARY
  41. };
  42. struct HealthCheck {
  43. HealthStatus status;
  44. std::chrono::system_clock::time_point lastReceived;
  45. void updateStatus(HealthStatus newStatus);
  46. std::chrono::duration<double> timeSinceLastCheck() const;
  47. };
  48. struct GroupOptions;
  49. struct GroupMemberInfo;
  50. struct WhoAllPeerPlayer;
  51. struct GuildMember;
  52. struct ZoneChangeDetails {
  53. std::string peerId;
  54. std::string peerWorldAddress;
  55. std::string peerInternalWorldAddress;
  56. int16 peerWorldPort;
  57. std::string peerWebAddress;
  58. int16 peerWebPort;
  59. std::string zoneFileName;
  60. std::string zoneName;
  61. int32 zoneId;
  62. int32 instanceId;
  63. float safeX;
  64. float safeY;
  65. float safeZ;
  66. float safeHeading;
  67. bool lockState;
  68. sint16 minStatus;
  69. int16 minLevel;
  70. int16 maxLevel;
  71. int16 minVersion;
  72. int32 defaultLockoutTime;
  73. int32 defaultReenterTime;
  74. int8 instanceType;
  75. int32 numPlayers;
  76. void* zonePtr;
  77. bool peerAuthorized;
  78. int32 zoneKey;
  79. int32 authDispatchedTime;
  80. bool zoningPastAuth;
  81. ZoneChangeDetails() = default;
  82. ZoneChangeDetails(ZoneChangeDetails* copy_details);
  83. ZoneChangeDetails(std::string peer_id, std::string peer_world_address, std::string peer_internal_world_address, int16 peer_world_port,
  84. std::string peer_web_address, int16 peer_web_port, std::string zone_file_name, std::string zone_name, int32 zone_id,
  85. int32 instance_id, float safe_x, float safe_y, float safe_z, float safe_heading, bool lock_state, sint16 min_status,
  86. int16 min_level, int16 max_level, int16 min_version, int32 default_lockout_time, int32 default_reenter_time, int8 instance_type, int32 num_players);
  87. };
  88. struct Peer {
  89. std::string id;
  90. PeeringStatus peeringStatus;
  91. HealthCheck healthCheck;
  92. std::string worldAddr;
  93. std::string internalWorldAddr;
  94. int16 worldPort;
  95. int16 peerPriority;
  96. std::string webAddr;
  97. int16 webPort;
  98. std::shared_ptr<boost::property_tree::ptree> zone_tree;
  99. std::shared_ptr<boost::property_tree::ptree> client_tree;
  100. std::atomic<bool> sentInitialPeerData;
  101. std::atomic<bool> wasOffline;
  102. mutable std::mutex dataMutex; // Mutex to protect access to ptree
  103. // Default constructor
  104. Peer()
  105. : zone_tree(std::make_shared<boost::property_tree::ptree>()),
  106. client_tree(std::make_shared<boost::property_tree::ptree>()) {
  107. healthCheck.status = HealthStatus::UNKNOWN;
  108. peerPriority = 65535;
  109. sentInitialPeerData = false;
  110. wasOffline = false;
  111. }
  112. Peer(std::string peerId, PeeringStatus status, std::string client_address,
  113. std::string client_internal_address, int16 client_port,
  114. std::string web_address, int16 web_port)
  115. : id(std::move(peerId)), peeringStatus(status), worldAddr(std::move(client_address)),
  116. internalWorldAddr(std::move(client_internal_address)), worldPort(client_port),
  117. webAddr(std::move(web_address)), webPort(web_port),
  118. zone_tree(std::make_shared<boost::property_tree::ptree>()),
  119. client_tree(std::make_shared<boost::property_tree::ptree>()) {
  120. healthCheck.status = HealthStatus::STARTUP;
  121. peerPriority = 65535;
  122. sentInitialPeerData = false;
  123. wasOffline = false;
  124. }
  125. // Example function to output data as JSON string (for debug or logging)
  126. std::string getZoneDataAsJson() const {
  127. std::lock_guard<std::mutex> lock(dataMutex);
  128. std::ostringstream oss;
  129. boost::property_tree::write_json(oss, *zone_tree); // Dereference data
  130. return oss.str();
  131. }
  132. // Example function to output data as JSON string (for debug or logging)
  133. std::string getClientDataAsJson() const {
  134. std::lock_guard<std::mutex> lock(dataMutex);
  135. std::ostringstream oss;
  136. boost::property_tree::write_json(oss, *client_tree); // Dereference data
  137. return oss.str();
  138. }
  139. };
  140. class PeerManager {
  141. private:
  142. std::map<std::string, std::shared_ptr<Peer>> peers;
  143. std::atomic<int32> uniqueGroupID{ 1 }; // Shared counter for unique IDs
  144. std::mutex idMutex;
  145. public:
  146. void addPeer(std::string id, PeeringStatus status, std::string client_address, std::string client_internal_address, int16 client_port, std::string web_address, int16 web_port);
  147. void updateHealth(const std::string& id, HealthStatus newStatus);
  148. void updatePriority(const std::string& id, int16 priority);
  149. void updateZoneTree(const std::string& id, const boost::property_tree::ptree& newTree);
  150. void updateClientTree(const std::string& id, const boost::property_tree::ptree& newTree);
  151. void setZonePeerData(ZoneChangeDetails* opt_details, std::string peerId, std::string peerWorldAddress, std::string peerInternalWorldAddress, int16 peerWorldPort,
  152. std::string peerWebAddress, int16 peerWebPort, std::string zoneFileName, std::string zoneName, int32 zoneId,
  153. int32 instanceId, float safeX, float safeY, float safeZ, float safeHeading, bool lockState, sint16 minStatus,
  154. int16 minLevel, int16 maxLevel, int16 minVersion, int32 defaultLockoutTime, int32 defaultReenterTime, int8 instanceType, int32 numPlayers);
  155. void setZonePeerDataSelf(ZoneChangeDetails* opt_details, std::string zoneFileName, std::string zoneName, int32 zoneId,
  156. int32 instanceId, float safeX, float safeY, float safeZ, float safeHeading, bool lockState, sint16 minStatus,
  157. int16 minLevel, int16 maxLevel, int16 minVersion, int32 defaultLockoutTime, int32 defaultReenterTime, int8 instanceType, int32 numPlayers, void* zonePtr = nullptr);
  158. bool IsClientConnectedPeer(int32 account_id);
  159. std::string GetCharacterPeerId(std::string charName);
  160. void SendPeersChannelMessage(int32 group_id, std::string fromName, std::string message, int16 channel, int32 language_id = 0);
  161. void SendPeersGuildChannelMessage(int32 guild_id, std::string fromName, std::string message, int16 channel, int32 language_id = 0);
  162. void sendZonePeerList(Client* client);
  163. std::string getZonePeerId(const std::string& inc_zone_name, int32 inc_zone_id, int32 inc_instance_id, ZoneChangeDetails* opt_details = nullptr, bool only_always_loaded = false);
  164. void setZonePeerData(ZoneChangeDetails* opt_details);
  165. void setPrimary(const std::string& id);
  166. bool hasPrimary();
  167. bool hasPriorityPeer(int16 priority);
  168. std::string getPriorityPeer();
  169. void updatePeer(const std::string& web_address, int16 web_port, const std::string& client_address, const std::string& client_internal_address, int16 client_port, bool is_primary = false);
  170. std::string isPeer(const std::string& web_address, int16 web_port);
  171. HealthStatus getPeerStatus(const std::string& web_address, int16 web_port);
  172. bool hasPeers();
  173. std::string assignUniqueNameForSecondary(const std::string& baseName, std::string client_address, std::string client_internal_address, int16 client_port, std::string web_address, int16 web_port);
  174. std::optional<std::string> getHealthyPeer() const;
  175. std::shared_ptr<Peer> getHealthyPeerPtr() const;
  176. std::shared_ptr<Peer> getHealthyPrimaryPeerPtr() const;
  177. std::shared_ptr<Peer> getHealthyPeerWithLeastClients() const;
  178. std::shared_ptr<Peer> getPeerById(const std::string& id) const;
  179. int32 getUniqueGroupId();
  180. bool sendPrimaryNewGroupRequest(std::string leader, std::string member, int32 entity_id, GroupOptions* options);
  181. void sendPeersGroupMember(int32 group_id, GroupMemberInfo* info, bool is_update = false, std::string peerId = "");
  182. void sendPeersRemoveGroupMember(int32 group_id, std::string name, int32 char_id, bool is_client);
  183. void populateGroupOptions(boost::property_tree::ptree& root, GroupOptions* options);
  184. void sendPeersNewGroupRequest(std::string peer_creation_address, int16 peer_creation_port,
  185. int32 group_id, std::string leader, std::string member, GroupOptions* options,
  186. std::string peerId = "", std::vector<int32>* raidGroups = nullptr, bool is_update = false);
  187. void sendPeersDisbandGroup(int32 group_id);
  188. bool sendPrimaryCreateGuildRequest(std::string guild_name, std::string leader_name);
  189. void sendPeersAddGuildMember(int32 character_id, int32 guild_id, std::string invited_by, int32 join_timestamp, int8 rank);
  190. void sendPeersRemoveGuildMember(int32 character_id, int32 guild_id, std::string removed_by);
  191. void sendPeersCreateGuild(int32 guild_id);
  192. void sendPeersGuildPermission(int32 guild_id, int8 rank, int8 permission, int8 value_);
  193. void sendPeersGuildEventFilter(int32 guild_id, int8 event_id, int8 category, int8 value_);
  194. void SetPeerErrorState(std::string address, std::string port);
  195. void handlePrimaryConflict(const std::string& reconnectingPeerId);
  196. std::shared_ptr<Peer> getCurrentPrimary();
  197. void sendPeersMessage(const std::string& endpoint, int32 command, int32 sub_command = 0);
  198. void sendZonePlayerList(std::vector<string>* queries, std::vector<WhoAllPeerPlayer>* peer_list, bool isGM);
  199. bool GetClientGuildDetails(int32 matchCharID, GuildMember* member_details);
  200. };
  201. #endif // PEERMANAGER_H