| 1 | #ifndef ENGINE_SHARED_HTTP_EMSCRIPTEN_H |
| 2 | #define ENGINE_SHARED_HTTP_EMSCRIPTEN_H |
| 3 | |
| 4 | #include <base/detect.h> |
| 5 | #if defined(CONF_PLATFORM_EMSCRIPTEN) |
| 6 | |
| 7 | #include <engine/http.h> |
| 8 | |
| 9 | #include <atomic> |
| 10 | #include <chrono> |
| 11 | #include <condition_variable> |
| 12 | #include <deque> |
| 13 | #include <mutex> |
| 14 | #include <optional> |
| 15 | #include <string> |
| 16 | #include <unordered_map> |
| 17 | #include <utility> |
| 18 | #include <vector> |
| 19 | |
| 20 | typedef struct emscripten_fetch_t emscripten_fetch_t; |
| 21 | |
| 22 | class CHttpEmscripten; |
| 23 | |
| 24 | class CHttpRequestEmscripten : public IHttpRequest |
| 25 | { |
| 26 | friend CHttpEmscripten; |
| 27 | |
| 28 | public: |
| 29 | CHttpRequestEmscripten(const char *pUrl); |
| 30 | ~CHttpRequestEmscripten() override; |
| 31 | |
| 32 | void Header(const char *pNameColonValue) override; |
| 33 | |
| 34 | void Abort() override; |
| 35 | |
| 36 | private: |
| 37 | CHttpEmscripten *m_pHttp; |
| 38 | |
| 39 | std::vector<std::pair<std::string, std::string>> m_vRequestHeaders; |
| 40 | |
| 41 | emscripten_fetch_t *m_pFetch = nullptr; |
| 42 | bool m_CallbackFinished = false; |
| 43 | |
| 44 | bool ConfigureAndRun(); |
| 45 | void OnSuccess(); |
| 46 | void OnFailure(); |
| 47 | void OnProgress(); |
| 48 | void OnHeader(const char *pName, const char *pValue); |
| 49 | void OnCompletionInternal(EHttpState State, const char *pErrorDetail); |
| 50 | |
| 51 | static void FetchCallbackSuccess(emscripten_fetch_t *pFetch); |
| 52 | static void FetchCallbackFailure(emscripten_fetch_t *pFetch); |
| 53 | static void FetchCallbackProgress(emscripten_fetch_t *pFetch); |
| 54 | }; |
| 55 | |
| 56 | class CHttpEmscripten : public IEngineHttp |
| 57 | { |
| 58 | friend CHttpRequestEmscripten; |
| 59 | |
| 60 | public: |
| 61 | // Lifecycle |
| 62 | bool Init(std::chrono::milliseconds ShutdownDelay) override; |
| 63 | void Shutdown() override; |
| 64 | ~CHttpEmscripten(); |
| 65 | |
| 66 | // User |
| 67 | void Run(std::shared_ptr<IHttpRequest> pRequest) override; |
| 68 | bool HasIpresolveBug() const override; |
| 69 | |
| 70 | private: |
| 71 | void *m_pThread = nullptr; |
| 72 | |
| 73 | std::mutex m_Lock; |
| 74 | std::condition_variable m_ConditionVariableInit; |
| 75 | std::condition_variable m_ConditionVariableLoop; |
| 76 | std::atomic<bool> m_Initialized = false; |
| 77 | std::deque<std::shared_ptr<CHttpRequestEmscripten>> m_PendingRequests; |
| 78 | std::unordered_map<emscripten_fetch_t *, EHttpState> m_PendingFetchChanges; |
| 79 | std::unordered_map<emscripten_fetch_t *, std::shared_ptr<CHttpRequestEmscripten>> m_RunningRequests; |
| 80 | std::chrono::milliseconds m_ShutdownDelay{}; |
| 81 | std::optional<std::chrono::time_point<std::chrono::steady_clock>> m_ShutdownTime; |
| 82 | std::atomic<bool> m_Shutdown = false; |
| 83 | std::atomic<bool> m_StartedShutdown = false; |
| 84 | |
| 85 | static void ThreadMain(void *pUser); |
| 86 | void RunLoop(); |
| 87 | void AddPendingStateChange(emscripten_fetch_t *pFetch, EHttpState State); |
| 88 | }; |
| 89 | |
| 90 | #endif // CONF_PLATFORM_EMSCRIPTEN |
| 91 | #endif // ENGINE_SHARED_HTTP_EMSCRIPTEN_H |
| 92 | |