CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
URLSession.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2023 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
12
27
28namespace CeresEngine {
29
34
35 class URLRequest;
36 class URLResponse;
37 class URLSession;
38
40 using URL = URI;
41
44
47
50
52 enum class URLRequestMode {
54 Read = (1u << 0u),
55
57 Write = (1u << 1u),
58
60 Create = (1u << 2u),
61
63 Truncate = (1u << 3u),
64
67
69 Default = 0,
70 };
71
75
76
86 public:
89 struct CachePolicy : StructEnum<CachePolicy> {
90 enum : UnderlyingType {
92 None = 0,
93
96 } raw;
97
99 };
100
103
104 public:
106 const URL url;
107
112
114 CachePolicy cachePolicy = CachePolicy::Default;
115
116 public:
121 explicit IURLRequest(const URL& url, const CachePolicy cachePolicy = CachePolicy::Default) : url(url), cachePolicy(cachePolicy) {}
122
124 explicit IURLRequest(const IURLRequest& request, const URL& url) : url(url), mode(request.mode), cachePolicy(request.cachePolicy) {}
125
128 };
129
132 public:
135 using Poly::Poly;
136
139 CE_EXPLICIT(false) URLRequest(URL url) : Poly(std::move(url)) {}
140
144
149 explicit URLRequest(const URLRequest& request, const URL& url) : Poly(url) {
150 get()->mode = request->mode;
151 get()->cachePolicy = request->cachePolicy;
152 }
153 };
154
243
245 class URLResponse : public Poly<IURLResponse, sizeof(IURLResponse)> {
246 public:
247 using Poly::Poly;
248
249 public:
250 friend std::ostream& operator<<(std::ostream& stream, const URLResponse& response);
251 };
252
255 protected:
258
259 protected:
260 friend class URLSession;
261
263 class Task final {
264 public:
267
270
274
275 // TODO: Implement progress tracking.
276
277 public:
286
289
290 public:
292
298
301
307
310
314 if(baseURL.empty()) {
315 throw URLNotFoundException("No source URL for protocol.");
316 }
317 return subRequest(std::move(baseURL));
318 }
319
320 public: // Request casting
324 template<typename T> const T& as() const { return request.as<T>(); }
325 };
326
327 public:
329 explicit URLProtocol(URLSession& session) noexcept : mSession(session) {}
330
333
334 public:
345
357 [[nodiscard]] virtual URLRequest prepareRequest(URLRequest&& request) { return std::move(request); }
358
370 [[nodiscard]] virtual Async<URLResponse> execute(Task& connection) = 0;
371
374 };
375
378 public:
380
381 public:
384 if(request.is<typename T::Request>()) {
385 return std::move(request);
386 }
387 return typename T::Request(*request);
388 }
389
392 };
393
396
400 private:
404
407
410
411 public:
414
417
418 public:
419 Async<URLResponse> execute(URLRequest request);
420
421 public:
422 template<typename T, typename... Args> T& registerProtocol(Args&&... args) {
423 URLProtocolPtr protocolPtr = ce_unique_new<T>(*this, std::forward<Args>(args)...);
424 URLProtocol& protocol = *protocolPtr;
425 registerProtocol(std::move(protocolPtr));
426 return static_cast<T&>(protocol);
427 }
428
429 template<typename T> void unregisterProtocol() {}
430
431 public:
433 ExecutionContext& getExecutionContext() { return mExecutionContext; }
434
435 private:
437 };
438
446 class URLCache {
447 // TODO: Implement this.
448 };
449
450} // namespace CeresEngine
#define CE_FLAGS_OPERATORS(Enum)
Defines global operators for a Flags<Enum, Storage> implementation.
Definition Flags.hpp:216
#define CE_EXCEPTION_DECL2(Name, Parent)
Definition Exception.hpp:100
#define CE_EXPLICIT(EXPR)
Definition Macros.hpp:413
#define CE_STRUCT_ENUM_DECL(T)
Definition StructEnum.hpp:49
General purpose class used for encapsulating the reading and writing of data from and to various sour...
Definition Stream.hpp:460
A context for function object execution.
Definition ExecutionContext.hpp:90
Definition Exception.hpp:110
A URL load request that is independent of protocol or URL scheme.
Definition URLSession.hpp:85
IURLRequest(const IURLRequest &request, const URL &url)
Creates a new IURLRequest object by copying from another but with a rewritten URL.
Definition URLSession.hpp:124
IURLRequest(const URL &url, const CachePolicy cachePolicy=CachePolicy::Default)
Creates a new URLRequest instance.
Definition URLSession.hpp:121
virtual ~IURLRequest() noexcept=default
Destroys the URLRequest.
URLRequestMode Mode
Determines the mode the request should be performed.
Definition URLSession.hpp:102
const URL url
The URL of the request.
Definition URLSession.hpp:106
The metadata associated with the response to a URL load request, independent of protocol and URL sche...
Definition URLSession.hpp:161
String mimeType
The MIME type of the response.
Definition URLSession.hpp:200
virtual URLDataStream openDataStream()
Opens a new stream that can read and write the data from the response.
Definition URLSession.hpp:238
const URLRequest request
The request that triggered the response.
Definition URLSession.hpp:164
UUID contentID
A ID that uniquely represents a content.
Definition URLSession.hpp:217
IURLResponse(URLRequest request)
Creates a new URLResponse from a request.
Definition URLSession.hpp:222
virtual ~IURLResponse() noexcept=default
Destroys the URLResponse.
UInt64 expectedContentLength
The expected length of the response's content.
Definition URLSession.hpp:174
virtual URLOutputStream openOutputStream()
Opens a new stream that can write the data from the response.
Definition URLSession.hpp:234
String textEncodingName
The name of the text encoding provided by the response's originating source.
Definition URLSession.hpp:207
JSON asJSON()
Opens the stream and reads it as a JSON file.
String suggestedFileName
A suggested filename for the response data.
Definition URLSession.hpp:189
virtual URLInputStream openInputStream()=0
Opens a new stream that can receive the data from the response.
A stream that provides read-only stream functionality.
Definition Stream.hpp:210
A helper class that wraps and protects a value from non-synchronized access.
Definition Threading.hpp:516
A stream that provides write-only stream functionality.
Definition Stream.hpp:307
A pointer type that has value semantics.
Definition Poly.hpp:57
TT & as() &
Casts the hold type to TT.
Definition Poly.hpp:421
friend class Poly
Definition Poly.hpp:58
Definition StructEnum.hpp:18
UInt32 UnderlyingType
Definition StructEnum.hpp:20
Creates a new URLProtocol instance.
Definition URLSession.hpp:377
URLRequest prepareRequest(URLRequest &&request) override
If the request is supported by the protocol, this method is called to perform and transformation that...
Definition URLSession.hpp:383
StringView getName() const noexcept final
Returns a string that identifies the protocol by a name.
Definition URLSession.hpp:391
A Uniform Resource Identifier (URI) is a unique sequence of characters that identifies a logical or p...
Definition URI.hpp:54
An object that maps URL requests to cached response objects.
Definition URLSession.hpp:446
Definition URLSession.hpp:30
Definition URLSession.hpp:33
Definition URLSession.hpp:32
Definition URLSession.hpp:31
An object that manages the task for a URL request.
Definition URLSession.hpp:263
const T & as() const
Gets the request as a concrete type.
Definition URLSession.hpp:324
URLSession & session
A reference to the URLSession that is executing the task.
Definition URLSession.hpp:266
Task(URLSession &session, URLProtocol &protocol, URLRequest &&request)
Creates a new connection.
Definition URLSession.hpp:284
StringView(URL::*)() const URLGetter
Definition URLSession.hpp:291
~Task() noexcept=default
Destroys the URLProtocol Connection.
Async< URLResponse > subRequest(const URLGetter &getter)
Executes a sub-request.
Definition URLSession.hpp:312
URLProtocol & protocol
A reference to the URLProtocol that is executing the request.
Definition URLSession.hpp:269
Async< URLResponse > subRequest(URLRequest baseRequest)
Executes a sub-request.
Async< URLResponse > subRequest(URL url)
Executes a sub-request.
Definition URLSession.hpp:309
URL parseURL(const URLGetter &getter) const noexcept
Definition URLSession.hpp:300
const URLRequest request
A copy of the request object, already transformed by the executing protocol.
Definition URLSession.hpp:273
URL parseURL(const StringView url) const noexcept
Parses a URL and automatically detects if the element uses nested URLs.
The implementation of a URL requester.
Definition URLSession.hpp:254
virtual StringView getName() const noexcept=0
Returns a string that identifies the protocol by a name.
virtual Async< URLResponse > execute(Task &connection)=0
Executes a request.
URLProtocol(URLSession &session) noexcept
Creates a new URLProtocol instance.
Definition URLSession.hpp:329
virtual bool isRequestSupported(const URLRequest &request)=0
Checks if the given request is supported by the protocol.
virtual URLRequest prepareRequest(URLRequest &&request)
If the request is supported by the protocol, this method is called to perform and transformation that...
Definition URLSession.hpp:357
URLSession & mSession
The session that owns this protocol instance.
Definition URLSession.hpp:257
virtual ~URLProtocol() noexcept=default
Destroys the URLProtocol instance.
A URL load request that is independent of protocol or URL scheme.
Definition URLSession.hpp:131
URLRequest(const URLRequest &request, const URL &url)
Creates a new URLRequest instance that is triggered from another.
Definition URLSession.hpp:149
The metadata associated with the response to a URL load request, independent of protocol and URL sche...
Definition URLSession.hpp:245
friend std::ostream & operator<<(std::ostream &stream, const URLResponse &response)
An object that coordinates a group of related, network data transfer tasks.
Definition URLSession.hpp:399
void registerProtocol(URLProtocolPtr &&protocol)
~URLSession() noexcept
Destroys an existing URLSession.
ExecutionContext & mExecutionContext
The execution context to which URL session callbacks will be called from.
Definition URLSession.hpp:403
LockedObject< Vector< URLProtocolPtr > > mProtocols
A vector that contains all registered protocols.
Definition URLSession.hpp:406
ExecutionContext & getExecutionContext()
The execution context to which URL session callbacks will be called from.
Definition URLSession.hpp:433
LockedObject< List< UPtr< URLProtocol::Task > > > mConnections
A list that contains a reference to all active connections.
Definition URLSession.hpp:409
void unregisterProtocol()
Definition URLSession.hpp:429
URLSession(ExecutionContext &executionContext)
Creates a new URLSession instance.
Definition Application.hpp:19
std::unique_ptr< T, Deleter > UPtr
UPtr is a smart pointer that owns and manages another object through a pointer and disposes of that o...
Definition SmartPtr.hpp:28
std::uint64_t UInt64
Definition DataTypes.hpp:26
BasicJSON<> JSON
Definition JSONForward.hpp:40
UPtr< URLProtocol > URLProtocolPtr
A pointer to a URLProtocol.
Definition URLSession.hpp:395
decltype(auto) get(BezierPath::Element &element) noexcept
Decomposes a bezier path element.
Definition BezierPath.hpp:723
cti::continuable< Args... > Async
Defines a non-copyable continuation type which uses the function2 backend for type erasure.
Definition Async.hpp:22
auto move(Vector3 position)
Moves a entity to the given position.
Definition Helpers.hpp:22
URLRequestMode
Determines the mode the request should be performed.
Definition URLSession.hpp:52
@ Write
Requests writing support from the protocool.
@ Create
Creates the file if it doesn't exist.
@ ReadWrite
Requests read and write support from the protocol.
@ Read
Requests reading support from the protocol.
@ Truncate
Truncate the file if it exists.
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668
An enumeration that describes how the URLProtocol implementation should handle caching of this reques...
Definition URLSession.hpp:89
Definition String.hpp:314
Represents a universally unique identifier (UUID).
Definition UUID.hpp:27