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 <cstddef>
7#include <curl/curl.h>
8#include <expected>
9#include <filesystem>
10#include <functional>
11#include <future>
12#include <mutex>
13#include <queue>
14#include <stop_token>
15#include <string>
16#include <string_view>
17#include <thread>
18#include <type_traits>
19#include <utility>
20#include <variant>
21
22namespace ArcdpsExtension {
23 class SimpleNetworkStack final : public Singleton<SimpleNetworkStack> {
24 public:
26 ~SimpleNetworkStack() override;
27
28 struct Response {
29 std::string Message;
30 long Code;
31 };
40 struct Error {
42 std::string Message;
43 };
44 using Result = std::expected<Response, Error>;
45 using ResultFunc = std::function<void(const Result&)>;
46 using ResultPromise = std::promise<Result>;
47
63 void QueueGet(const std::string& pUrl, const std::filesystem::path& pFilepath = "");
64
82 void QueueGet(const std::string& pUrl, const ResultFunc& pFunc, const std::filesystem::path& pFilepath = "");
83
99 void QueueGet(const std::string& pUrl, ResultPromise pPromise, const std::filesystem::path& pFilepath = "");
100
105 void SetUserAgent(const std::string& pUserAgent) {
106 mUserAgent = pUserAgent;
107 }
108
114 [[nodiscard]] std::string UrlEncode(std::string_view pStr) const;
115
116 private:
117 struct QueueElement {
118 using Variant = std::variant<std::monostate, ResultFunc, ResultPromise>;
119 std::string Url;
120 Variant Callback;
121 // only set this when a download should take place
122 std::filesystem::path Filepath;
123
124 QueueElement(std::string pUrl, Variant pCallback, std::filesystem::path pFilepath) : Url(std::move(pUrl)), Callback(std::move(pCallback)), Filepath(std::move(pFilepath)) {}
125 ~QueueElement() = default;
126
127 QueueElement(const QueueElement&) = delete;
128 QueueElement(QueueElement&& pOther) noexcept = default;
129 QueueElement& operator=(const QueueElement&) = delete;
130 QueueElement& operator=(QueueElement&& pOther) noexcept = default;
131 };
132
133 CURL* mHandle = nullptr;
134 std::string mLastResponseBuffer;
135 long mLastResponseCode = 0;
136
137 std::jthread mThread;
138 std::queue<QueueElement> mJobQueue;
139 std::mutex mQueueMutex;
140 std::condition_variable_any mQueueCv;
141
142 std::string mUserAgent = "ArcdpsExtension/1.0";
143
144 static size_t ResponseBufferWriteFunction(void* pContent, size_t pSize, size_t pNMemb, void* pUserP);
145 SimpleNetworkStack::Result get(const QueueElement& pElement);
146 void runner(const std::stop_token& pToken);
147 };
148} // namespace ArcdpsExtension
Definition SimpleNetworkStack.h:23
SimpleNetworkStack()
Definition SimpleNetworkStack.cpp:7
~SimpleNetworkStack() override
Definition SimpleNetworkStack.cpp:72
std::promise< Result > ResultPromise
Definition SimpleNetworkStack.h:46
void QueueGet(const std::string &pUrl, const std::filesystem::path &pFilepath="")
Definition SimpleNetworkStack.cpp:111
void QueueGet(const std::string &pUrl, ResultPromise pPromise, const std::filesystem::path &pFilepath="")
void SetUserAgent(const std::string &pUserAgent)
Definition SimpleNetworkStack.h:105
std::function< void(const Result &)> ResultFunc
Definition SimpleNetworkStack.h:45
ErrorType
Definition SimpleNetworkStack.h:32
std::string UrlEncode(std::string_view pStr) const
Definition SimpleNetworkStack.cpp:132
std::expected< Response, Error > Result
Definition SimpleNetworkStack.h:44
Definition Singleton.h:48
Definition ArcdpsExtension.h:10
Definition SimpleNetworkStack.h:40
ErrorType Type
Definition SimpleNetworkStack.h:41
std::string Message
Definition SimpleNetworkStack.h:42
Definition SimpleNetworkStack.h:28
long Code
Definition SimpleNetworkStack.h:30
std::string Message
Definition SimpleNetworkStack.h:29