mirror of
				https://github.com/fedddddd/iw5-gsc-utils.git
				synced 2025-10-30 17:16:57 +00:00 
			
		
		
		
	Add httpGet function
This commit is contained in:
		
							
								
								
									
										46
									
								
								src/utils/concurrency.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/utils/concurrency.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include <mutex> | ||||
|  | ||||
| namespace utils::concurrency | ||||
| { | ||||
| 	template <typename T, typename MutexType = std::mutex> | ||||
| 	class container | ||||
| 	{ | ||||
| 	public: | ||||
| 		template <typename R = void, typename F> | ||||
| 		R access(F&& accessor) const | ||||
| 		{ | ||||
| 			std::lock_guard<MutexType> _{mutex_}; | ||||
| 			return accessor(object_); | ||||
| 		} | ||||
|  | ||||
| 		template <typename R = void, typename F> | ||||
| 		R access(F&& accessor) | ||||
| 		{ | ||||
| 			std::lock_guard<MutexType> _{mutex_}; | ||||
| 			return accessor(object_); | ||||
| 		} | ||||
|  | ||||
| 		template <typename R = void, typename F> | ||||
| 		R access_with_lock(F&& accessor) const | ||||
| 		{ | ||||
| 			std::unique_lock<MutexType> lock{mutex_}; | ||||
| 			return accessor(object_, lock); | ||||
| 		} | ||||
|  | ||||
| 		template <typename R = void, typename F> | ||||
| 		R access_with_lock(F&& accessor) | ||||
| 		{ | ||||
| 			std::unique_lock<MutexType> lock{mutex_}; | ||||
| 			return accessor(object_, lock); | ||||
| 		} | ||||
|  | ||||
| 		T& get_raw() { return object_; } | ||||
| 		const T& get_raw() const { return object_; } | ||||
|  | ||||
| 	private: | ||||
| 		mutable MutexType mutex_{}; | ||||
| 		T object_{}; | ||||
| 	}; | ||||
| } | ||||
							
								
								
									
										47
									
								
								src/utils/http.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/utils/http.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| #include <stdinc.hpp> | ||||
| #include "http.hpp" | ||||
|  | ||||
| namespace utils::http | ||||
| { | ||||
| 	std::optional<std::string> get_data(const std::string& url) | ||||
| 	{ | ||||
| 		CComPtr<IStream> stream; | ||||
|  | ||||
| 		if (FAILED(URLOpenBlockingStreamA(nullptr, url.data(), &stream, 0, nullptr))) | ||||
| 		{ | ||||
| 			return {}; | ||||
| 		} | ||||
|  | ||||
| 		char buffer[0x1000]; | ||||
| 		std::string result; | ||||
|  | ||||
| 		HRESULT status{}; | ||||
|  | ||||
| 		do | ||||
| 		{ | ||||
| 			DWORD bytes_read = 0; | ||||
| 			status = stream->Read(buffer, sizeof(buffer), &bytes_read); | ||||
|  | ||||
| 			if (bytes_read > 0) | ||||
| 			{ | ||||
| 				result.append(buffer, bytes_read); | ||||
| 			} | ||||
| 		} | ||||
| 		while (SUCCEEDED(status) && status != S_FALSE); | ||||
|  | ||||
| 		if (FAILED(status)) | ||||
| 		{ | ||||
| 			return {}; | ||||
| 		} | ||||
|  | ||||
| 		return {result}; | ||||
| 	} | ||||
|  | ||||
| 	std::future<std::optional<std::string>> get_data_async(const std::string& url) | ||||
| 	{ | ||||
| 		return std::async(std::launch::async, [url]() | ||||
| 		{ | ||||
| 			return get_data(url); | ||||
| 		}); | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										11
									
								
								src/utils/http.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/utils/http.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include <string> | ||||
| #include <optional> | ||||
| #include <future> | ||||
|  | ||||
| namespace utils::http | ||||
| { | ||||
| 	std::optional<std::string> get_data(const std::string& url); | ||||
| 	std::future<std::optional<std::string>> get_data_async(const std::string& url); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user