Network: Implement WebService with emscripten fetch API on wasm
This commit is contained in:
parent
b28d97b1fa
commit
6bf91e10e5
|
|
@ -8,15 +8,15 @@ int main()
|
||||||
|
|
||||||
std::unique_ptr<Nz::WebService> webService = Nz::Network::Instance()->InstantiateWebService();
|
std::unique_ptr<Nz::WebService> webService = Nz::Network::Instance()->InstantiateWebService();
|
||||||
|
|
||||||
std::unique_ptr<Nz::WebRequest> webRequest = webService->CreateGetRequest("https://www.perdu.com", [&](const Nz::WebRequestResult& result)
|
std::unique_ptr<Nz::WebRequest> webRequest = webService->CreateGetRequest("https://test.digitalpulse.software", [&](const Nz::WebRequestResult& result)
|
||||||
{
|
{
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
std::cout << "Got a " << result.GetReponseCode() << " response(" << result.GetDownloadedSize() << " bytes, " << result.GetDownloadSpeed() << " bytes / s" << ")" << std::endl;
|
std::cout << "Got a " << result.GetStatusCode() << " response(" << result.GetDownloadedSize() << " bytes, " << result.GetDownloadSpeed() << " bytes / s" << ")" << std::endl;
|
||||||
std::cout << result.GetBody() << std::endl;
|
std::cout << result.GetBody() << std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout << "Web request failed (code " << result.GetReponseCode() << "): " << result.GetErrorMessage() << std::endl;
|
std::cout << "Web request failed (code " << result.GetStatusCode() << "): " << result.GetErrorMessage() << std::endl;
|
||||||
|
|
||||||
Nz::ApplicationBase::Instance()->Quit();
|
Nz::ApplicationBase::Instance()->Quit();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,9 @@ namespace Nz
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
std::unique_ptr<class CurlLibrary> m_curlLibrary;
|
std::unique_ptr<class CurlLibrary> m_curlLibrary;
|
||||||
|
#endif
|
||||||
|
|
||||||
static Network* s_instance;
|
static Network* s_instance;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@
|
||||||
#ifndef NAZARA_NETWORK_WEBREQUEST_HPP
|
#ifndef NAZARA_NETWORK_WEBREQUEST_HPP
|
||||||
#define NAZARA_NETWORK_WEBREQUEST_HPP
|
#define NAZARA_NETWORK_WEBREQUEST_HPP
|
||||||
|
|
||||||
|
#include <NazaraUtils/Prerequisites.hpp>
|
||||||
|
#include <Nazara/Core/Clock.hpp>
|
||||||
#include <Nazara/Network/Config.hpp>
|
#include <Nazara/Network/Config.hpp>
|
||||||
#include <Nazara/Network/Enums.hpp>
|
#include <Nazara/Network/Enums.hpp>
|
||||||
#include <Nazara/Network/WebRequestResult.hpp>
|
#include <Nazara/Network/WebRequestResult.hpp>
|
||||||
|
|
@ -15,7 +17,11 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
struct curl_slist;
|
struct curl_slist;
|
||||||
|
#else
|
||||||
|
struct emscripten_fetch_attr_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
|
@ -38,10 +44,10 @@ namespace Nz
|
||||||
|
|
||||||
inline void SetDataCallback(DataCallback callback);
|
inline void SetDataCallback(DataCallback callback);
|
||||||
inline void SetHeader(std::string header, std::string value);
|
inline void SetHeader(std::string header, std::string value);
|
||||||
void SetJSonContent(std::string_view encodedJSon);
|
void SetJSonContent(std::string encodedJSon);
|
||||||
void SetMaximumFileSize(UInt64 maxFileSize);
|
void SetMaximumFileSize(UInt64 maxFileSize);
|
||||||
inline void SetResultCallback(ResultCallback callback);
|
inline void SetResultCallback(ResultCallback callback);
|
||||||
void SetServiceName(const std::string_view& serviceName);
|
void SetServiceName(std::string serviceName);
|
||||||
void SetURL(const std::string& url);
|
void SetURL(const std::string& url);
|
||||||
|
|
||||||
void SetupGet();
|
void SetupGet();
|
||||||
|
|
@ -50,21 +56,36 @@ namespace Nz
|
||||||
WebRequest& operator=(const WebRequest&) = delete;
|
WebRequest& operator=(const WebRequest&) = delete;
|
||||||
WebRequest& operator=(WebRequest&&) = default;
|
WebRequest& operator=(WebRequest&&) = default;
|
||||||
|
|
||||||
static std::unique_ptr<WebRequest> Get(const std::string& url, ResultCallback callback = nullptr);
|
|
||||||
static std::unique_ptr<WebRequest> Post(const std::string& url, ResultCallback callback = nullptr);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
inline bool OnBodyResponse(const char* data, std::size_t length);
|
inline bool OnBodyResponse(const char* data, std::size_t length);
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
CURL* Prepare();
|
CURL* Prepare();
|
||||||
inline void TriggerCallback();
|
#else
|
||||||
inline void TriggerCallback(std::string errorMessage);
|
inline emscripten_fetch_t* GetFetchHandle() const;
|
||||||
|
inline Nz::Time GetRequestTime() const;
|
||||||
|
emscripten_fetch_t* Prepare(emscripten_fetch_attr_t* fetchAttr);
|
||||||
|
inline void StopClock();
|
||||||
|
#endif
|
||||||
|
inline void TriggerErrorCallback(std::string errorMessage);
|
||||||
|
inline void TriggerSuccessCallback();
|
||||||
|
|
||||||
|
#ifdef NAZARA_PLATFORM_WEB
|
||||||
|
std::string m_httpMethod;
|
||||||
|
std::string m_url;
|
||||||
|
std::vector<const char*> m_requestHeaders;
|
||||||
|
#endif
|
||||||
|
std::string m_content;
|
||||||
std::string m_responseBody;
|
std::string m_responseBody;
|
||||||
std::unordered_map<std::string, std::string> m_headers;
|
std::unordered_map<std::string, std::string> m_headers;
|
||||||
WebService& m_webService;
|
WebService& m_webService;
|
||||||
DataCallback m_dataCallback;
|
DataCallback m_dataCallback;
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
MovablePtr<CURL> m_curlHandle;
|
MovablePtr<CURL> m_curlHandle;
|
||||||
MovablePtr<curl_slist> m_headerList;
|
MovablePtr<curl_slist> m_headerList;
|
||||||
|
#else
|
||||||
|
HighPrecisionClock m_clock;
|
||||||
|
MovablePtr<emscripten_fetch_t> m_fetchHandle;
|
||||||
|
#endif
|
||||||
ResultCallback m_resultCallback;
|
ResultCallback m_resultCallback;
|
||||||
bool m_isUserAgentSet;
|
bool m_isUserAgentSet;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -32,16 +32,42 @@ namespace Nz
|
||||||
return m_dataCallback(data, length);
|
return m_dataCallback(data, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void WebRequest::TriggerCallback()
|
#ifdef NAZARA_PLATFORM_WEB
|
||||||
|
inline emscripten_fetch_t* WebRequest::GetFetchHandle() const
|
||||||
{
|
{
|
||||||
m_resultCallback(WebRequestResult(m_webService, m_curlHandle.Get(), std::move(m_responseBody)));
|
return m_fetchHandle;
|
||||||
m_responseBody.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void WebRequest::TriggerCallback(std::string errorMessage)
|
inline Time WebRequest::GetRequestTime() const
|
||||||
{
|
{
|
||||||
m_resultCallback(WebRequestResult(m_webService, std::move(errorMessage)));
|
return m_clock.GetElapsedTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void WebRequest::StopClock()
|
||||||
|
{
|
||||||
|
m_clock.Pause();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
inline void WebRequest::TriggerErrorCallback(std::string errorMessage)
|
||||||
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
|
m_resultCallback(WebRequestResult(m_webService, Nz::Err(std::move(errorMessage)), m_curlHandle.Get()));
|
||||||
|
#else
|
||||||
|
m_resultCallback(WebRequestResult(m_webService, Nz::Err(std::move(errorMessage)), m_fetchHandle.Get(), m_clock.GetElapsedTime()));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void WebRequest::TriggerSuccessCallback()
|
||||||
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
|
m_resultCallback(WebRequestResult(m_webService, Nz::Ok(std::move(m_responseBody)), m_curlHandle.Get()));
|
||||||
|
#else
|
||||||
|
m_resultCallback(WebRequestResult(m_webService, Nz::Ok(std::move(m_responseBody)), m_fetchHandle.Get(), m_clock.GetElapsedTime()));
|
||||||
|
#endif
|
||||||
|
m_responseBody.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Network/DebugOff.hpp>
|
#include <Nazara/Network/DebugOff.hpp>
|
||||||
|
#include "WebRequest.hpp"
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,18 @@
|
||||||
#define NAZARA_NETWORK_WEBREQUESTRESULT_HPP
|
#define NAZARA_NETWORK_WEBREQUESTRESULT_HPP
|
||||||
|
|
||||||
#include <NazaraUtils/Prerequisites.hpp>
|
#include <NazaraUtils/Prerequisites.hpp>
|
||||||
|
#include <Nazara/Core/Time.hpp>
|
||||||
#include <Nazara/Network/Config.hpp>
|
#include <Nazara/Network/Config.hpp>
|
||||||
#include <NazaraUtils/MovablePtr.hpp>
|
#include <NazaraUtils/MovablePtr.hpp>
|
||||||
|
#include <NazaraUtils/Result.hpp>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
using CURL = void;
|
using CURL = void;
|
||||||
|
#else
|
||||||
|
struct emscripten_fetch_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
|
@ -30,10 +36,10 @@ namespace Nz
|
||||||
|
|
||||||
inline std::string& GetBody();
|
inline std::string& GetBody();
|
||||||
inline const std::string& GetBody() const;
|
inline const std::string& GetBody() const;
|
||||||
Nz::UInt64 GetDownloadedSize() const;
|
UInt64 GetDownloadedSize() const;
|
||||||
Nz::UInt64 GetDownloadSpeed() const;
|
UInt64 GetDownloadSpeed() const;
|
||||||
inline const std::string& GetErrorMessage() const;
|
inline const std::string& GetErrorMessage() const;
|
||||||
long GetReponseCode() const;
|
UInt32 GetStatusCode() const;
|
||||||
|
|
||||||
inline bool HasSucceeded() const;
|
inline bool HasSucceeded() const;
|
||||||
|
|
||||||
|
|
@ -43,12 +49,23 @@ namespace Nz
|
||||||
WebRequestResult& operator=(WebRequestResult&&) = delete;
|
WebRequestResult& operator=(WebRequestResult&&) = delete;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
inline WebRequestResult(WebService& webService, CURL* curl, std::string body);
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
inline WebRequestResult(WebService& webService, std::string errMessage);
|
inline WebRequestResult(WebService& webService, Result<std::string, std::string>&& bodyResult, CURL* curl);
|
||||||
|
#else
|
||||||
|
inline WebRequestResult(WebService& webService, Result<std::string, std::string>&& bodyResult, emscripten_fetch_t* fetchHandle, Time downloadTime);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
CURL* m_curlHandle;
|
CURL* m_curlHandle;
|
||||||
|
#else
|
||||||
|
emscripten_fetch_t* m_fetchHandle;
|
||||||
|
#endif
|
||||||
WebService& m_webService;
|
WebService& m_webService;
|
||||||
std::string m_bodyOrErr;
|
Result<std::string, std::string> m_bodyResult;
|
||||||
|
#ifdef NAZARA_PLATFORM_WEB
|
||||||
|
Time m_downloadTime;
|
||||||
|
#endif
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,53 +2,55 @@
|
||||||
// This file is part of the "Nazara Engine - Network module"
|
// This file is part of the "Nazara Engine - Network module"
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Network/Debug.hpp>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <Nazara/Network/Debug.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
inline WebRequestResult::WebRequestResult(WebService& webService, CURL* curl, std::string body) :
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
|
inline WebRequestResult::WebRequestResult(WebService& webService, Result<std::string, std::string>&& bodyResult, CURL* curl) :
|
||||||
m_curlHandle(curl),
|
m_curlHandle(curl),
|
||||||
m_webService(webService),
|
m_webService(webService),
|
||||||
m_bodyOrErr(std::move(body))
|
m_bodyResult(std::move(bodyResult))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
inline WebRequestResult::WebRequestResult(WebService& webService, std::string errMessage) :
|
inline WebRequestResult::WebRequestResult(WebService& webService, Result<std::string, std::string>&& bodyResult, emscripten_fetch_t* fetchHandle, Time downloadTime) :
|
||||||
m_curlHandle(nullptr),
|
m_fetchHandle(fetchHandle),
|
||||||
m_webService(webService),
|
m_webService(webService),
|
||||||
m_bodyOrErr(std::move(errMessage))
|
m_bodyResult(std::move(bodyResult)),
|
||||||
|
m_downloadTime(downloadTime)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
inline std::string& WebRequestResult::GetBody()
|
inline std::string& WebRequestResult::GetBody()
|
||||||
{
|
{
|
||||||
assert(HasSucceeded());
|
assert(HasSucceeded());
|
||||||
return m_bodyOrErr;
|
return m_bodyResult.GetValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const std::string& WebRequestResult::GetBody() const
|
inline const std::string& WebRequestResult::GetBody() const
|
||||||
{
|
{
|
||||||
assert(HasSucceeded());
|
assert(HasSucceeded());
|
||||||
return m_bodyOrErr;
|
return m_bodyResult.GetValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const std::string& WebRequestResult::GetErrorMessage() const
|
inline const std::string& WebRequestResult::GetErrorMessage() const
|
||||||
{
|
{
|
||||||
assert(!HasSucceeded());
|
assert(!HasSucceeded());
|
||||||
return m_bodyOrErr;
|
return m_bodyResult.GetError();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool WebRequestResult::HasSucceeded() const
|
inline bool WebRequestResult::HasSucceeded() const
|
||||||
{
|
{
|
||||||
return m_curlHandle != nullptr;
|
return m_bodyResult.IsOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline WebRequestResult::operator bool() const
|
inline WebRequestResult::operator bool() const
|
||||||
{
|
{
|
||||||
return HasSucceeded();
|
return HasSucceeded();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Network/DebugOff.hpp>
|
#include <Nazara/Network/DebugOff.hpp>
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,12 @@
|
||||||
#include <Nazara/Network/Config.hpp>
|
#include <Nazara/Network/Config.hpp>
|
||||||
#include <Nazara/Network/WebRequest.hpp>
|
#include <Nazara/Network/WebRequest.hpp>
|
||||||
#include <NazaraUtils/MovablePtr.hpp>
|
#include <NazaraUtils/MovablePtr.hpp>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
using CURLM = void;
|
using CURLM = void;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
|
@ -24,7 +28,11 @@ namespace Nz
|
||||||
friend WebRequestResult;
|
friend WebRequestResult;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
WebService(const CurlLibrary& library);
|
WebService(const CurlLibrary& library);
|
||||||
|
#else
|
||||||
|
WebService();
|
||||||
|
#endif
|
||||||
WebService(const WebService&) = delete;
|
WebService(const WebService&) = delete;
|
||||||
WebService(WebService&&) = delete;
|
WebService(WebService&&) = delete;
|
||||||
~WebService();
|
~WebService();
|
||||||
|
|
@ -44,12 +52,25 @@ namespace Nz
|
||||||
WebService& operator=(WebService&&) = delete;
|
WebService& operator=(WebService&&) = delete;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
inline const CurlLibrary& GetCurlLibrary() const;
|
inline const CurlLibrary& GetCurlLibrary() const;
|
||||||
|
#endif
|
||||||
|
|
||||||
std::string m_userAgent;
|
std::string m_userAgent;
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
std::unordered_map<CURL*, std::unique_ptr<WebRequest>> m_activeRequests;
|
std::unordered_map<CURL*, std::unique_ptr<WebRequest>> m_activeRequests;
|
||||||
const CurlLibrary& m_curl;
|
const CurlLibrary& m_curl;
|
||||||
MovablePtr<CURLM> m_curlMulti;
|
MovablePtr<CURLM> m_curlMulti;
|
||||||
|
#else
|
||||||
|
struct FinishedRequest
|
||||||
|
{
|
||||||
|
std::unique_ptr<WebRequest> request;
|
||||||
|
bool succeeded;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unordered_map<emscripten_fetch_t*, std::unique_ptr<WebRequest>> m_activeRequests;
|
||||||
|
std::vector<FinishedRequest> m_finishedRequests;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,10 +36,12 @@ namespace Nz
|
||||||
return m_userAgent;
|
return m_userAgent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
const CurlLibrary& WebService::GetCurlLibrary() const
|
const CurlLibrary& WebService::GetCurlLibrary() const
|
||||||
{
|
{
|
||||||
return m_curl;
|
return m_curl;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Network/DebugOff.hpp>
|
#include <Nazara/Network/DebugOff.hpp>
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,15 @@
|
||||||
#include <Nazara/Core/Error.hpp>
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <Nazara/Core/Log.hpp>
|
#include <Nazara/Core/Log.hpp>
|
||||||
#include <Nazara/Network/Config.hpp>
|
#include <Nazara/Network/Config.hpp>
|
||||||
#include <Nazara/Network/CurlLibrary.hpp>
|
|
||||||
#include <Nazara/Network/NetPacket.hpp>
|
#include <Nazara/Network/NetPacket.hpp>
|
||||||
#include <Nazara/Network/WebService.hpp>
|
#include <Nazara/Network/WebService.hpp>
|
||||||
#include <NazaraUtils/CallOnExit.hpp>
|
#include <NazaraUtils/CallOnExit.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
|
#include <Nazara/Network/CurlLibrary.hpp>
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||||
#include <Nazara/Network/Win32/SocketImpl.hpp>
|
#include <Nazara/Network/Win32/SocketImpl.hpp>
|
||||||
#elif defined(NAZARA_PLATFORM_POSIX)
|
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||||
|
|
@ -42,18 +46,21 @@ namespace Nz
|
||||||
if (!NetPacket::Initialize())
|
if (!NetPacket::Initialize())
|
||||||
throw std::runtime_error("failed to initialize packets");
|
throw std::runtime_error("failed to initialize packets");
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
if (config.webServices)
|
if (config.webServices)
|
||||||
{
|
{
|
||||||
m_curlLibrary = std::make_unique<CurlLibrary>();
|
m_curlLibrary = std::make_unique<CurlLibrary>();
|
||||||
if (!m_curlLibrary->Load())
|
if (!m_curlLibrary->Load())
|
||||||
throw std::runtime_error("failed to initialize curl");
|
throw std::runtime_error("failed to initialize curl");
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
Network::~Network()
|
Network::~Network()
|
||||||
{
|
{
|
||||||
// Uninitialize module here
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
m_curlLibrary.reset();
|
m_curlLibrary.reset();
|
||||||
|
#endif
|
||||||
|
|
||||||
NetPacket::Uninitialize();
|
NetPacket::Uninitialize();
|
||||||
SocketImpl::Uninitialize();
|
SocketImpl::Uninitialize();
|
||||||
|
|
@ -61,6 +68,7 @@ namespace Nz
|
||||||
|
|
||||||
std::unique_ptr<WebService> Network::InstantiateWebService()
|
std::unique_ptr<WebService> Network::InstantiateWebService()
|
||||||
{
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
if (!m_curlLibrary)
|
if (!m_curlLibrary)
|
||||||
{
|
{
|
||||||
std::unique_ptr<CurlLibrary> curlLibrary = std::make_unique<CurlLibrary>();
|
std::unique_ptr<CurlLibrary> curlLibrary = std::make_unique<CurlLibrary>();
|
||||||
|
|
@ -71,6 +79,9 @@ namespace Nz
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::make_unique<WebService>(*m_curlLibrary);
|
return std::make_unique<WebService>(*m_curlLibrary);
|
||||||
|
#else
|
||||||
|
return std::make_unique<WebService>();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
Network* Network::s_instance = nullptr;
|
Network* Network::s_instance = nullptr;
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,20 @@
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Network/WebRequest.hpp>
|
#include <Nazara/Network/WebRequest.hpp>
|
||||||
#include <Nazara/Network/CurlLibrary.hpp> //< include last because of curl/curl.h
|
|
||||||
#include <Nazara/Network/WebService.hpp>
|
#include <Nazara/Network/WebService.hpp>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
|
#include <Nazara/Network/CurlLibrary.hpp>
|
||||||
|
#else
|
||||||
|
#include <emscripten/fetch.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <Nazara/Network/Debug.hpp>
|
#include <Nazara/Network/Debug.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
WebRequest::WebRequest(WebService& webService) :
|
WebRequest::WebRequest(WebService& webService) :
|
||||||
m_webService(webService),
|
m_webService(webService),
|
||||||
m_isUserAgentSet(false)
|
m_isUserAgentSet(false)
|
||||||
|
|
@ -17,17 +25,30 @@ namespace Nz
|
||||||
|
|
||||||
m_curlHandle = libcurl.easy_init();
|
m_curlHandle = libcurl.easy_init();
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
WebRequest::WebRequest(WebService& webService) :
|
||||||
|
m_webService(webService),
|
||||||
|
m_isUserAgentSet(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
WebRequest::~WebRequest()
|
WebRequest::~WebRequest()
|
||||||
{
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
auto& libcurl = m_webService.GetCurlLibrary();
|
auto& libcurl = m_webService.GetCurlLibrary();
|
||||||
if (m_curlHandle)
|
if (m_curlHandle)
|
||||||
libcurl.easy_cleanup(m_curlHandle);
|
libcurl.easy_cleanup(m_curlHandle);
|
||||||
|
|
||||||
if (m_headerList)
|
if (m_headerList)
|
||||||
libcurl.slist_free_all(m_headerList);
|
libcurl.slist_free_all(m_headerList);
|
||||||
|
#else
|
||||||
|
if (m_fetchHandle)
|
||||||
|
emscripten_fetch_close(m_fetchHandle);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
void WebRequest::ForceProtocol(Nz::NetProtocol protocol)
|
void WebRequest::ForceProtocol(Nz::NetProtocol protocol)
|
||||||
{
|
{
|
||||||
assert(protocol != Nz::NetProtocol::Unknown);
|
assert(protocol != Nz::NetProtocol::Unknown);
|
||||||
|
|
@ -51,28 +72,43 @@ namespace Nz
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
void WebRequest::SetJSonContent(std::string_view encodedJSon)
|
void WebRequest::ForceProtocol(Nz::NetProtocol /*protocol*/)
|
||||||
{
|
{
|
||||||
auto& libcurl = m_webService.GetCurlLibrary();
|
// Ignored
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void WebRequest::SetJSonContent(std::string encodedJSon)
|
||||||
|
{
|
||||||
SetHeader("Content-Type", "application/json");
|
SetHeader("Content-Type", "application/json");
|
||||||
libcurl.easy_setopt(m_curlHandle, CURLOPT_POSTFIELDSIZE_LARGE, curl_off_t(encodedJSon.size()));
|
|
||||||
libcurl.easy_setopt(m_curlHandle, CURLOPT_COPYPOSTFIELDS, encodedJSon.data());
|
m_content = std::move(encodedJSon);
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
|
auto& libcurl = m_webService.GetCurlLibrary();
|
||||||
|
libcurl.easy_setopt(m_curlHandle, CURLOPT_POSTFIELDSIZE_LARGE, SafeCast<curl_off_t>(m_content.size()));
|
||||||
|
libcurl.easy_setopt(m_curlHandle, CURLOPT_POSTFIELDS, m_content.data());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRequest::SetMaximumFileSize(Nz::UInt64 maxFileSize)
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
|
void WebRequest::SetMaximumFileSize(UInt64 maxFileSize)
|
||||||
{
|
{
|
||||||
auto& libcurl = m_webService.GetCurlLibrary();
|
auto& libcurl = m_webService.GetCurlLibrary();
|
||||||
|
|
||||||
curl_off_t maxSize = maxFileSize;
|
curl_off_t maxSize = SafeCast<curl_off_t>(maxFileSize);
|
||||||
libcurl.easy_setopt(m_curlHandle, CURLOPT_MAXFILESIZE_LARGE, maxSize);
|
libcurl.easy_setopt(m_curlHandle, CURLOPT_MAXFILESIZE_LARGE, maxSize);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
void WebRequest::SetServiceName(const std::string_view& serviceName)
|
void WebRequest::SetMaximumFileSize(UInt64 maxFileSize)
|
||||||
{
|
{
|
||||||
auto& libcurl = m_webService.GetCurlLibrary();
|
// TODO: Implement using EMSCRIPTEN_FETCH_STREAM_DATA
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void WebRequest::SetServiceName(std::string serviceName)
|
||||||
|
{
|
||||||
if (!serviceName.empty())
|
if (!serviceName.empty())
|
||||||
{
|
{
|
||||||
//TODO Nz::StackString?
|
//TODO Nz::StackString?
|
||||||
|
|
@ -81,37 +117,50 @@ namespace Nz
|
||||||
userAgent.append(" - ");
|
userAgent.append(" - ");
|
||||||
userAgent.append(serviceName.data(), serviceName.size());
|
userAgent.append(serviceName.data(), serviceName.size());
|
||||||
|
|
||||||
libcurl.easy_setopt(m_curlHandle, CURLOPT_USERAGENT, userAgent.data());
|
SetHeader("User-Agent", std::move(userAgent));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
libcurl.easy_setopt(m_curlHandle, CURLOPT_USERAGENT, m_webService.GetUserAgent().c_str());
|
SetHeader("User-Agent", m_webService.GetUserAgent());
|
||||||
|
|
||||||
m_isUserAgentSet = true;
|
m_isUserAgentSet = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRequest::SetURL(const std::string& url)
|
void WebRequest::SetURL(const std::string& url)
|
||||||
{
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
auto& libcurl = m_webService.GetCurlLibrary();
|
auto& libcurl = m_webService.GetCurlLibrary();
|
||||||
|
|
||||||
libcurl.easy_setopt(m_curlHandle, CURLOPT_URL, url.data());
|
libcurl.easy_setopt(m_curlHandle, CURLOPT_URL, url.data());
|
||||||
|
#else
|
||||||
|
m_url = url;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRequest::SetupGet()
|
void WebRequest::SetupGet()
|
||||||
{
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
auto& libcurl = m_webService.GetCurlLibrary();
|
auto& libcurl = m_webService.GetCurlLibrary();
|
||||||
|
|
||||||
libcurl.easy_setopt(m_curlHandle, CURLOPT_HTTPGET, long(1));
|
libcurl.easy_setopt(m_curlHandle, CURLOPT_HTTPGET, long(1));
|
||||||
|
#else
|
||||||
|
m_httpMethod = "GET";
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRequest::SetupPost()
|
void WebRequest::SetupPost()
|
||||||
{
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
auto& libcurl = m_webService.GetCurlLibrary();
|
auto& libcurl = m_webService.GetCurlLibrary();
|
||||||
|
|
||||||
libcurl.easy_setopt(m_curlHandle, CURLOPT_POST, long(1));
|
libcurl.easy_setopt(m_curlHandle, CURLOPT_POST, long(1));
|
||||||
|
#else
|
||||||
|
m_httpMethod = "POST";
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
CURL* WebRequest::Prepare()
|
CURL* WebRequest::Prepare()
|
||||||
{
|
{
|
||||||
|
if (!m_isUserAgentSet)
|
||||||
|
SetHeader("User-Agent", m_webService.GetUserAgent());
|
||||||
|
|
||||||
auto& libcurl = m_webService.GetCurlLibrary();
|
auto& libcurl = m_webService.GetCurlLibrary();
|
||||||
|
|
||||||
if (!m_headers.empty())
|
if (!m_headers.empty())
|
||||||
|
|
@ -126,8 +175,41 @@ namespace Nz
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_isUserAgentSet)
|
if (!m_isUserAgentSet)
|
||||||
libcurl.easy_setopt(m_curlHandle, CURLOPT_USERAGENT, m_webService.GetUserAgent().c_str());
|
SetHeader("User-Agent", m_webService.GetUserAgent());
|
||||||
|
|
||||||
return m_curlHandle;
|
return m_curlHandle;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
emscripten_fetch_t* WebRequest::Prepare(emscripten_fetch_attr_t* fetchAttr)
|
||||||
|
{
|
||||||
|
if (!m_isUserAgentSet)
|
||||||
|
SetHeader("User-Agent", m_webService.GetUserAgent());
|
||||||
|
|
||||||
|
if (m_httpMethod.size() >= Nz::CountOf(fetchAttr->requestMethod))
|
||||||
|
throw std::runtime_error("request method is too big");
|
||||||
|
|
||||||
|
if (m_url.empty())
|
||||||
|
throw std::runtime_error("no url set");
|
||||||
|
|
||||||
|
std::strcpy(fetchAttr->requestMethod, m_httpMethod.c_str());
|
||||||
|
|
||||||
|
fetchAttr->requestData = m_content.data();
|
||||||
|
fetchAttr->requestDataSize = m_content.size();
|
||||||
|
|
||||||
|
if (!m_requestHeaders.empty())
|
||||||
|
{
|
||||||
|
for (auto&& [header, value] : m_headers)
|
||||||
|
{
|
||||||
|
m_requestHeaders.push_back(header.c_str());
|
||||||
|
m_requestHeaders.push_back(value.c_str());
|
||||||
|
}
|
||||||
|
m_requestHeaders.push_back(nullptr);
|
||||||
|
|
||||||
|
fetchAttr->requestHeaders = m_requestHeaders.data();
|
||||||
|
}
|
||||||
|
|
||||||
|
m_fetchHandle = emscripten_fetch(fetchAttr, m_url.c_str());
|
||||||
|
return m_fetchHandle;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,38 +3,55 @@
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Network/WebRequestResult.hpp>
|
#include <Nazara/Network/WebRequestResult.hpp>
|
||||||
#include <Nazara/Network/CurlLibrary.hpp> //< include last because of curl/curl.h
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <Nazara/Network/WebService.hpp>
|
#include <Nazara/Network/WebService.hpp>
|
||||||
|
#include <NazaraUtils/Algorithm.hpp>
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
|
#include <Nazara/Network/CurlLibrary.hpp>
|
||||||
|
#else
|
||||||
|
#include <emscripten/fetch.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <Nazara/Network/Debug.hpp>
|
#include <Nazara/Network/Debug.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
Nz::UInt64 WebRequestResult::GetDownloadedSize() const
|
UInt64 WebRequestResult::GetDownloadedSize() const
|
||||||
{
|
{
|
||||||
assert(HasSucceeded());
|
NazaraAssert(HasSucceeded(), "web request failed");
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
auto& libcurl = m_webService.GetCurlLibrary();
|
auto& libcurl = m_webService.GetCurlLibrary();
|
||||||
|
|
||||||
curl_off_t downloadedSize = 0;
|
curl_off_t downloadedSize = 0;
|
||||||
libcurl.easy_getinfo(m_curlHandle, CURLINFO_SIZE_DOWNLOAD_T, &downloadedSize);
|
libcurl.easy_getinfo(m_curlHandle, CURLINFO_SIZE_DOWNLOAD_T, &downloadedSize);
|
||||||
|
|
||||||
return downloadedSize;
|
return SafeCast<UInt64>(downloadedSize);
|
||||||
|
#else
|
||||||
|
return m_fetchHandle->numBytes;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t WebRequestResult::GetDownloadSpeed() const
|
UInt64 WebRequestResult::GetDownloadSpeed() const
|
||||||
{
|
{
|
||||||
assert(HasSucceeded());
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
|
NazaraAssert(HasSucceeded(), "web request failed");
|
||||||
|
|
||||||
auto& libcurl = m_webService.GetCurlLibrary();
|
auto& libcurl = m_webService.GetCurlLibrary();
|
||||||
|
|
||||||
curl_off_t downloadSpeed = 0;
|
curl_off_t downloadSpeed = 0;
|
||||||
libcurl.easy_getinfo(m_curlHandle, CURLINFO_SPEED_DOWNLOAD_T, &downloadSpeed);
|
libcurl.easy_getinfo(m_curlHandle, CURLINFO_SPEED_DOWNLOAD_T, &downloadSpeed);
|
||||||
|
|
||||||
return downloadSpeed;
|
return SafeCast<UInt64>(downloadSpeed);
|
||||||
|
#else
|
||||||
|
return 1000u * m_bodyResult.GetValue().size() / m_downloadTime.AsMilliseconds();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
long WebRequestResult::GetReponseCode() const
|
UInt32 WebRequestResult::GetStatusCode() const
|
||||||
{
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
assert(HasSucceeded());
|
assert(HasSucceeded());
|
||||||
|
|
||||||
auto& libcurl = m_webService.GetCurlLibrary();
|
auto& libcurl = m_webService.GetCurlLibrary();
|
||||||
|
|
@ -42,6 +59,9 @@ namespace Nz
|
||||||
long responseCode;
|
long responseCode;
|
||||||
libcurl.easy_getinfo(m_curlHandle, CURLINFO_RESPONSE_CODE, &responseCode);
|
libcurl.easy_getinfo(m_curlHandle, CURLINFO_RESPONSE_CODE, &responseCode);
|
||||||
|
|
||||||
return responseCode;
|
return SafeCast<UInt32>(responseCode);
|
||||||
|
#else
|
||||||
|
return m_fetchHandle->status;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,17 @@
|
||||||
#include <Nazara/Network/WebService.hpp>
|
#include <Nazara/Network/WebService.hpp>
|
||||||
#include <Nazara/Core/Error.hpp>
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <Nazara/Core/ErrorFlags.hpp>
|
#include <Nazara/Core/ErrorFlags.hpp>
|
||||||
#include <Nazara/Network/CurlLibrary.hpp> //< include last because of curl/curl.h
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
|
#include <Nazara/Network/CurlLibrary.hpp>
|
||||||
|
#else
|
||||||
|
#include <emscripten/fetch.h>
|
||||||
|
#endif
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
#include <Nazara/Network/Debug.hpp>
|
#include <Nazara/Network/Debug.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
WebService::WebService(const CurlLibrary& curl) :
|
WebService::WebService(const CurlLibrary& curl) :
|
||||||
m_curl(curl)
|
m_curl(curl)
|
||||||
{
|
{
|
||||||
|
|
@ -20,9 +25,16 @@ namespace Nz
|
||||||
|
|
||||||
m_curlMulti = m_curl.multi_init();
|
m_curlMulti = m_curl.multi_init();
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
WebService::WebService() :
|
||||||
|
m_userAgent("Nazara WebService - emscripten_fetch")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
WebService::~WebService()
|
WebService::~WebService()
|
||||||
{
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
if (m_curlMulti)
|
if (m_curlMulti)
|
||||||
{
|
{
|
||||||
for (auto&& [handle, request] : m_activeRequests)
|
for (auto&& [handle, request] : m_activeRequests)
|
||||||
|
|
@ -30,10 +42,12 @@ namespace Nz
|
||||||
|
|
||||||
m_curl.multi_cleanup(m_curlMulti);
|
m_curl.multi_cleanup(m_curlMulti);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebService::Poll()
|
void WebService::Poll()
|
||||||
{
|
{
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
assert(m_curlMulti);
|
assert(m_curlMulti);
|
||||||
|
|
||||||
int reportedActiveRequest;
|
int reportedActiveRequest;
|
||||||
|
|
@ -59,9 +73,9 @@ namespace Nz
|
||||||
WebRequest& request = *it->second;
|
WebRequest& request = *it->second;
|
||||||
|
|
||||||
if (m->data.result == CURLE_OK)
|
if (m->data.result == CURLE_OK)
|
||||||
request.TriggerCallback();
|
request.TriggerSuccessCallback();
|
||||||
else
|
else
|
||||||
request.TriggerCallback(m_curl.easy_strerror(m->data.result));
|
request.TriggerErrorCallback(m_curl.easy_strerror(m->data.result));
|
||||||
|
|
||||||
m_curl.multi_remove_handle(m_curlMulti, handle);
|
m_curl.multi_remove_handle(m_curlMulti, handle);
|
||||||
|
|
||||||
|
|
@ -69,13 +83,29 @@ namespace Nz
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (m);
|
while (m);
|
||||||
|
#else
|
||||||
|
if (!m_finishedRequests.empty())
|
||||||
|
{
|
||||||
|
for (auto&& [request, succeeded] : m_finishedRequests)
|
||||||
|
{
|
||||||
|
if (succeeded)
|
||||||
|
request->TriggerSuccessCallback();
|
||||||
|
else
|
||||||
|
request->TriggerErrorCallback(request->GetFetchHandle()->statusText);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_finishedRequests.clear();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebService::QueueRequest(std::unique_ptr<WebRequest>&& request)
|
void WebService::QueueRequest(std::unique_ptr<WebRequest>&& request)
|
||||||
{
|
{
|
||||||
assert(m_curlMulti);
|
|
||||||
assert(request);
|
assert(request);
|
||||||
|
|
||||||
|
#ifndef NAZARA_PLATFORM_WEB
|
||||||
|
assert(m_curlMulti);
|
||||||
|
|
||||||
CURL* handle = request->Prepare();
|
CURL* handle = request->Prepare();
|
||||||
|
|
||||||
curl_write_callback writeCallback = [](char* ptr, std::size_t size, std::size_t nmemb, void* userdata) -> std::size_t
|
curl_write_callback writeCallback = [](char* ptr, std::size_t size, std::size_t nmemb, void* userdata) -> std::size_t
|
||||||
|
|
@ -95,5 +125,57 @@ namespace Nz
|
||||||
m_activeRequests.emplace(handle, std::move(request));
|
m_activeRequests.emplace(handle, std::move(request));
|
||||||
|
|
||||||
m_curl.multi_add_handle(m_curlMulti, handle);
|
m_curl.multi_add_handle(m_curlMulti, handle);
|
||||||
|
#else
|
||||||
|
emscripten_fetch_attr_t attr;
|
||||||
|
emscripten_fetch_attr_init(&attr);
|
||||||
|
|
||||||
|
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
|
||||||
|
|
||||||
|
attr.onsuccess = [](emscripten_fetch_t* fetch)
|
||||||
|
{
|
||||||
|
WebService* service = static_cast<WebService*>(fetch->userData);
|
||||||
|
|
||||||
|
auto it = service->m_activeRequests.find(fetch);
|
||||||
|
if (it == service->m_activeRequests.end())
|
||||||
|
{
|
||||||
|
NazaraError("received emscripten fetch onsuccess with unbound request");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<WebRequest>& request = it->second;
|
||||||
|
request->StopClock();
|
||||||
|
request->OnBodyResponse(fetch->data, SafeCast<std::size_t>(fetch->numBytes));
|
||||||
|
|
||||||
|
service->m_finishedRequests.push_back({
|
||||||
|
std::move(request),
|
||||||
|
true
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
attr.onerror = [](emscripten_fetch_t* fetch)
|
||||||
|
{
|
||||||
|
WebService* service = static_cast<WebService*>(fetch->userData);
|
||||||
|
|
||||||
|
auto it = service->m_activeRequests.find(fetch);
|
||||||
|
if (it == service->m_activeRequests.end())
|
||||||
|
{
|
||||||
|
NazaraError("received emscripten fetch onsuccess with unbound request");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<WebRequest>& request = it->second;
|
||||||
|
request->StopClock();
|
||||||
|
|
||||||
|
service->m_finishedRequests.push_back({
|
||||||
|
std::move(request),
|
||||||
|
false
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
attr.userData = this;
|
||||||
|
|
||||||
|
emscripten_fetch_t* handle = request->Prepare(&attr);
|
||||||
|
m_activeRequests.emplace(handle, std::move(request));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
25
xmake.lua
25
xmake.lua
|
|
@ -129,11 +129,17 @@ local modules = {
|
||||||
Deps = {"NazaraCore"},
|
Deps = {"NazaraCore"},
|
||||||
Packages = { "fmt" },
|
Packages = { "fmt" },
|
||||||
Custom = function ()
|
Custom = function ()
|
||||||
if has_config("link_curl") then
|
if not is_plat("wasm") then
|
||||||
add_defines("NAZARA_NETWORK_CURL_LINK")
|
if has_config("link_curl") then
|
||||||
add_packages("libcurl")
|
add_defines("NAZARA_NETWORK_CURL_LINK")
|
||||||
|
add_packages("libcurl")
|
||||||
|
else
|
||||||
|
add_packages("libcurl", { links = {} })
|
||||||
|
end
|
||||||
else
|
else
|
||||||
add_packages("libcurl", { links = {} })
|
add_ldflags("-sFETCH", { public = true })
|
||||||
|
remove_headerfiles("include/Nazara/Network/CurlLibrary.hpp")
|
||||||
|
remove_files("src/Nazara/Network/CurlLibrary.cpp")
|
||||||
end
|
end
|
||||||
|
|
||||||
if is_plat("windows", "mingw") then
|
if is_plat("windows", "mingw") then
|
||||||
|
|
@ -268,10 +274,13 @@ if has_config("joltphysics") then
|
||||||
end
|
end
|
||||||
|
|
||||||
if has_config("network") then
|
if has_config("network") then
|
||||||
if has_config("link_curl") then
|
-- emscripten fetch API is used for WebService on wasm
|
||||||
add_requires("libcurl")
|
if not is_plat("wasm") then
|
||||||
else
|
if has_config("link_curl") then
|
||||||
add_requires("libcurl", { configs = { shared = true }})
|
add_requires("libcurl")
|
||||||
|
else
|
||||||
|
add_requires("libcurl", { configs = { shared = true }})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue