ArcdpsExtension
 
Loading...
Searching...
No Matches
SimpleNetworkStack.h
Go to the documentation of this file.
1#pragma once
2
3#include "Singleton.h"
4
5#include <condition_variable>
6#include <curl/curl.h>
7#include <expected>
8#include <filesystem>
9#include <functional>
10#include <future>
11#include <queue>
12#include <thread>
13#include <utility>
14#include <variant>
15
16namespace ArcdpsExtension {
17 class SimpleNetworkStack final : public Singleton<SimpleNetworkStack> {
18 public:
20 ~SimpleNetworkStack() override;
21
22 struct Response {
23 std::string Message;
24 long Code;
25 };
34 struct Error {
36 std::string Message;
37 };
38 using Result = std::expected<Response, Error>;
39 using ResultFunc = std::function<void(const Result&)>;
40 using ResultPromise = std::promise<Result>;
41
57 void QueueGet(const std::string& pUrl, const std::filesystem::path& pFilepath = "");
58
76 void QueueGet(const std::string& pUrl, const ResultFunc& pFunc, const std::filesystem::path& pFilepath = "");
77
93 void QueueGet(const std::string& pUrl, ResultPromise pPromise, const std::filesystem::path& pFilepath = "");
94
99 void SetUserAgent(const std::string& pUserAgent) {
100 mUserAgent = pUserAgent;
101 }
102
108 [[nodiscard]] std::string UrlEncode(std::string_view pStr) const;
109
110 private:
111 struct QueueElement {
112 using Variant = std::variant<std::monostate, ResultFunc, ResultPromise>;
113 std::string Url;
114 Variant Callback;
115 // only set this when a download should take place
116 std::filesystem::path Filepath;
117
118 QueueElement(std::string pUrl, Variant pCallback, std::filesystem::path pFilepath) : Url(std::move(pUrl)), Callback(std::move(pCallback)), Filepath(std::move(pFilepath)) {}
119 ~QueueElement() = default;
120
121 QueueElement(const QueueElement&) = delete;
122 QueueElement(QueueElement&& pOther) noexcept = default;
123 QueueElement& operator=(const QueueElement&) = delete;
124 QueueElement& operator=(QueueElement&& pOther) noexcept = default;
125 };
126
127 CURL* mHandle = nullptr;
128 std::string mLastResponseBuffer;
129 long mLastResponseCode = 0;
130
131 std::jthread mThread;
132 std::queue<QueueElement> mJobQueue;
133 std::mutex mQueueMutex;
134 std::condition_variable_any mQueueCv;
135
136 std::string mUserAgent = "ArcdpsExtension/1.0";
137
138 static size_t ResponseBufferWriteFunction(void* pContent, size_t pSize, size_t pNMemb, void* pUserP);
139 SimpleNetworkStack::Result get(const QueueElement& pElement);
140 void runner(const std::stop_token& pToken);
141 };
142} // namespace ArcdpsExtension
Definition SimpleNetworkStack.h:17
SimpleNetworkStack()
Definition SimpleNetworkStack.cpp:5
~SimpleNetworkStack() override
Definition SimpleNetworkStack.cpp:64
std::promise< Result > ResultPromise
Definition SimpleNetworkStack.h:40
void QueueGet(const std::string &pUrl, const std::filesystem::path &pFilepath="")
Definition SimpleNetworkStack.cpp:103
void QueueGet(const std::string &pUrl, ResultPromise pPromise, const std::filesystem::path &pFilepath="")
void SetUserAgent(const std::string &pUserAgent)
Definition SimpleNetworkStack.h:99
std::function< void(const Result &)> ResultFunc
Definition SimpleNetworkStack.h:39
ErrorType
Definition SimpleNetworkStack.h:26
std::string UrlEncode(std::string_view pStr) const
Definition SimpleNetworkStack.cpp:124
std::expected< Response, Error > Result
Definition SimpleNetworkStack.h:38
Definition Singleton.h:47
Definition ArcdpsExtension.h:10
Definition SimpleNetworkStack.h:34
ErrorType Type
Definition SimpleNetworkStack.h:35
std::string Message
Definition SimpleNetworkStack.h:36
Definition SimpleNetworkStack.h:22
long Code
Definition SimpleNetworkStack.h:24
std::string Message
Definition SimpleNetworkStack.h:23