Schedule, execute and manage HTTP requests. More...
Public Member Functions | |
BHttpSession () | |
Construct a new object. | |
BHttpSession (BHttpSession &&) noexcept=delete | |
Move is disabled. | |
BHttpSession (const BHttpSession &) noexcept | |
Create a new BHttpSession object that shares a state with another. | |
~BHttpSession () noexcept | |
Destructor. | |
void | Cancel (const BHttpResult &request) |
Cancel a running request. | |
void | Cancel (int32 identifier) |
Cancel a running request. | |
BHttpResult | Execute (BHttpRequest &&request, BBorrow< BDataIO > target=nullptr, BMessenger observer=BMessenger()) |
Schedule and execute a request. | |
BHttpSession & | operator= (BHttpSession &&) noexcept=delete |
Move is disabled. | |
BHttpSession & | operator= (const BHttpSession &) noexcept |
Copy and use the shared state from another session. | |
void | SetMaxConnectionsPerHost (size_t maxConnections) |
Set the maximum number of connections per host. | |
void | SetMaxHosts (size_t maxConnections) |
Set the maximum number of concurrent hosts that can be connected to. | |
Schedule, execute and manage HTTP requests.
All requests start from a `BHttpSession`. This class has the following jobs:
Objects of the `BHttpSession` class can be shared between different parts of the application. They should be copied, rather than shared using pointers. This is because they have an inner state that is shared between the various objects.
There are specific scenarios for having more than one session, most notably if an application provides services over HTTP whereby a user is identified by cookies, and the application wants to support more than one user account. But in most cases, there will be one instance of the BHttpSession that is shared between various segments of the application.
BPrivate::Network::BHttpSession::BHttpSession | ( | ) |
Construct a new object.
Each newly constructed object will have their own queue for HTTP requests, as well as their own cookies and certificate store.
std::bad_alloc | Unable to allocate resources for the object. |
BRuntimeError | Unable to create semaphores or threads. |
|
noexcept |
Create a new BHttpSession object that shares a state with another.
The internal HTTP queue and context can be shared among multiple objects. You can use the copy constructor to create a new object.
|
deletenoexcept |
Move is disabled.
BHttpSession objects cannot be moved. Because it has a shared internal state, making copies is cheap and it is the only supported method of creating multiple scoped objects with a shared lifetime.
|
noexcept |
Destructor.
The destructor releases the shared internal state of the session object. If there are no more sessions using the shared state, the state is cleaned up.
void BPrivate::Network::BHttpSession::Cancel | ( | const BHttpResult & | request | ) |
Cancel a running request.
See the BHttpSession::Cancel(int32 identifier) method for more details on how this method works.
std::bad_alloc | Error in case memory cannot be allocated. |
void BPrivate::Network::BHttpSession::Cancel | ( | int32 | identifier | ) |
Cancel a running request.
When a request that is scheduled or running is cancelled, its BHttpResult object will be set to the BNetworkRequestError exception with the Cancelled
type.
There are three possible outcomes: 1. If the request is not yet scheduled, then it will never start. The result will be set to the cancelled error state. 2. If the request was started, then processing it will be terminated. The result will be set to the cancelled error state. 3. If the request was already finished, then nothing happens. The result will keep the value it had assigned. The same if the request identifier is invalid.
identifier | The identifier for the request to cancel. |
std::bad_alloc | Error in case memory cannot be allocated. |
BHttpResult BPrivate::Network::BHttpSession::Execute | ( | BHttpRequest && | request, |
BBorrow< BDataIO > | target = nullptr , |
||
BMessenger | observer = BMessenger() |
||
) |
Schedule and execute a request.
request | The (valid) request to move from. |
target | An optional data buffer to write the incoming body of the request to. This can be nullptr if you want to use the default internal storage. If you provide a buffer, it must be wrapped in a BBorrow object. This means that you exclusively borrow the target to this session object. After the request is finished, you can regain usage of the object through the matching BExclusiveBorrow object. Use the BHttpResult::Body() method to synchronize when the target is available again. |
observer | An optional observer that will receive the progress and status messages for this request. |
|
deletenoexcept |
Move is disabled.
BHttpSession objects cannot be moved. Because it has a shared internal state, making copies is cheap and it is the only supported method of creating multiple scoped objects with a shared lifetime.
|
noexcept |
Copy and use the shared state from another session.
The internal HTTP queue and context can be shared among multiple objects. You can use the copy constructor to create a new copy.
This copy assignment operator should be used in very specific instances only, where there is a particular reason to replace an existing session internal session with another. It should not be used in the following case:
void BPrivate::Network::BHttpSession::SetMaxConnectionsPerHost | ( | size_t | maxConnections | ) |
Set the maximum number of connections per host.
A host is identified by the domain name and the port. You can limit the number of concurrent connections to a host by tweaking this value.
The default value is 2 connections per host.
If the value is decreased, any requests that already started will not be affected. The new value will only be applied when any new requests are added.
maxConnections | The maximum number of connections per host. This value must between 1 and INT32_MAX . |
BRuntimeError | In case the maxConnections is invalid. |
void BPrivate::Network::BHttpSession::SetMaxHosts | ( | size_t | maxConnections | ) |
Set the maximum number of concurrent hosts that can be connected to.
A host is identified by the domain name and the port. You can limit the number of concurrent hosts by tweaking this value.
The default value is 10 concurrent hosts.
If the value is decreased, any requests that already started will not be affected. The new value will only be applied when any new requests are added.
maxConnections | The maximum number of hosts. The value must be at least 1. |
BRuntimeError | In case the maxConnections is 0. |