HttpRequestManager.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/std/containers/queue.h>
  10. #include <AzCore/std/smart_ptr/make_shared.h>
  11. #include <AzCore/std/parallel/atomic.h>
  12. #include <AzCore/std/parallel/mutex.h>
  13. #include <AzCore/std/parallel/condition_variable.h>
  14. #include <AzCore/std/parallel/thread.h>
  15. #include <HttpRequestor/HttpRequestParameters.h>
  16. #include <HttpRequestor/HttpTextRequestParameters.h>
  17. namespace HttpRequestor
  18. {
  19. class Manager
  20. {
  21. public:
  22. Manager();
  23. virtual ~Manager();
  24. // Add these parameters to a queue of request parameters to send off as an HTTP request as soon as they reach the head of the queue
  25. void AddRequest(Parameters && httpRequestParameters);
  26. // Add these parameters to a queue of request parameters to send off as an HTTP TEXT request as soon as they reach the head of the queue
  27. void AddTextRequest(TextParameters && httpTextRequestParameters);
  28. // The last round trip time taken to make the http request and get a response.
  29. AZStd::chrono::milliseconds GetLastRoundTripTime() const;
  30. private:
  31. // RequestManager thread loop.
  32. void ThreadFunction();
  33. // Called by ThreadFunction. Waits for timeout or until notified and processes any requests queued up.
  34. void HandleRequestBatch();
  35. // Perform an HTTP request, block until a response is received, then give the returned JSON to the callback to parse. Returns the HTTPResponseCode to the callback to handle any errors.
  36. void HandleRequest(const Parameters & httpRequestParameters);
  37. // Perform an HTTP request, block until a response is received, then give the returned TEXT to the callback to parse. Returns the HTTPResponseCode to the callback to handle any errors.
  38. void HandleTextRequest(const TextParameters & httpTextRequestParameters);
  39. private:
  40. AZStd::queue<Parameters> m_requestsToHandle; // Queue of requests that will be made in order of time received
  41. AZStd::queue<TextParameters> m_textRequestsToHandle; // Queue of requests for TEXT blobs that will be made in order of time received
  42. AZStd::mutex m_requestMutex; // Member variables for synchronization
  43. AZStd::condition_variable m_requestConditionVar;
  44. AZStd::atomic<bool> m_runThread; // Run flag used to signal the worker thread
  45. AZStd::thread m_thread; // This is the thread that will be used for all async operations
  46. static const char* s_loggingName; // Name to use for log messages etc...
  47. bool m_ownsAwsNativeInitialization = false; // Whether or not this module initialized the native layer
  48. AZStd::atomic<AZStd::chrono::milliseconds> m_lastRoundTripTime; // The last round trip time taken to make the http request and get a response.
  49. };
  50. using ManagerPtr = AZStd::shared_ptr<Manager>;
  51. }