HTTPSClientPool.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 HTTPSCLIENTPOOL_H
  17. #define HTTPSCLIENTPOOL_H
  18. #include "HTTPSClient.h"
  19. #include <unordered_map>
  20. #include <string>
  21. #include <memory>
  22. #include <thread>
  23. #include <chrono>
  24. #include <atomic>
  25. #include <queue>
  26. #include <mutex>
  27. #include <condition_variable>
  28. #include <functional>
  29. #include <vector>
  30. #include <boost/property_tree/ptree.hpp> // Include Boost property tree
  31. struct pair_hash {
  32. template <class T1, class T2>
  33. std::size_t operator()(const std::pair<T1, T2>& pair) const {
  34. return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
  35. }
  36. };
  37. class HTTPSClientPool {
  38. public:
  39. HTTPSClientPool();
  40. ~HTTPSClientPool();
  41. // init cert and key file
  42. void init(const std::string& cert, const std::string& key);
  43. // Pre-authenticate and add a client to the pool
  44. void addPeerClient(const std::string& peerId, const std::string& server, const std::string& port, const std::string& authEndpoint);
  45. std::shared_ptr<HTTPSClient> getOrCreateClient(const std::string& id, const std::string& server, const std::string& port);
  46. // Send a request to a peer by ID and parse response as a ptree
  47. boost::property_tree::ptree sendRequestToPeer(const std::string& peerId, const std::string& target);
  48. boost::property_tree::ptree sendPostRequestToPeer(const std::string& peerId, const std::string& target, const std::string& jsonPayload);
  49. void pollPeerHealthData(auto client, const std::string& id, const std::string& server, const std::string& port);
  50. void startPolling(); // Starts asynchronous polling of peers
  51. void stopPolling(); // Stops the polling process
  52. // Sends a POST request asynchronously by adding it to the task queue
  53. void sendPostRequestToPeerAsync(const std::string& peerId, const std::string& server, const std::string& port, const std::string& target, const std::string& payload);
  54. // Worker thread function
  55. void workerFunction();
  56. bool isPolling() { return running; }
  57. private:
  58. std::shared_ptr<HTTPSClient> getClient(const std::string& peerId);
  59. std::unordered_map<std::pair<std::string, std::string>, std::shared_ptr<HTTPSClient>, pair_hash> clients;
  60. std::unordered_map<std::string, std::shared_ptr<HTTPSClient>> clientsById; // New map for ID-based lookup
  61. std::string certFile;
  62. std::string keyFile;
  63. int pollingInterval; // Polling interval in milliseconds
  64. std::queue<std::function<void()>> taskQueue; // Queue of tasks to execute
  65. std::mutex queueMutex; // Mutex to protect access to the task queue
  66. std::condition_variable condition; // Condition variable to signal worker threads
  67. std::vector<std::thread> workers; // Worker threads
  68. bool stop = false; // Flag to stop workers
  69. std::atomic<bool> running; // Flag to control polling loop
  70. void pollPeerHealth(const std::string& server, const std::string& port); // Polls individual peer
  71. };
  72. #endif // HTTPSCLIENTPOOL_H