19 if (
_response.ReceiveHeader(buffer, size))
67 _resolver = std::make_shared<Asio::TCPResolver>(
service());
70 _timeout = std::make_shared<Asio::Timer>(
service());
72 _promise = std::promise<HTTPResponse>();
78 SetPromiseError(
"Invalid HTTP request!");
79 return _promise.get_future();
87 SetPromiseError(
"Connection failed!");
88 return _promise.get_future();
96 SetPromiseError(
"Failed to send HTTP request!");
97 return _promise.get_future();
102 auto self(this->shared_from_this());
103 auto timeout_handler = [
this,
self](
bool canceled)
113 if (!_timeout->Setup(timeout_handler,
timeout) || !_timeout->WaitAsync())
115 SetPromiseError(
"Failed to setup timeout timer!");
116 return _promise.get_future();
119 return _promise.get_future();
129 SetPromiseError(
"Failed to send HTTP request!");
147 SetPromiseValue(response);
156 SetPromiseError(error);
159 void HTTPClientEx::SetPromiseValue(
const HTTPResponse& response)
161 _promise.set_value(response);
165 void HTTPClientEx::SetPromiseError(
const std::string& error)
167 _promise.set_exception(std::make_exception_ptr(std::runtime_error(error)));
bool IsConnected() const noexcept
Is the client connected?
virtual void onConnected()
Handle client connected notification.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
virtual bool ConnectAsync()
Connect the client (asynchronous)
virtual bool DisconnectAsync()
Disconnect the client (asynchronous)
void onReceivedResponse(const HTTPResponse &response) override
Handle HTTP response received notification.
void onDisconnected() override
Handle client disconnected notification.
void onConnected() override
Handle client connected notification.
std::shared_ptr< Asio::Timer > & timeout() noexcept
Get the timeout check timer.
void onReceivedResponseError(const HTTPResponse &response, const std::string &error) override
Handle HTTP response error notification.
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
HTTPResponse _response
HTTP response.
HTTPRequest _request
HTTP request.
HTTPRequest & request() noexcept
Get the HTTP request.
size_t SendRequest()
Send the current HTTP request (synchronous)
bool SendRequestAsync()
Send the current HTTP request (asynchronous)
virtual void onReceivedResponse(const HTTPResponse &response)
Handle HTTP response received notification.
virtual void onReceivedResponseHeader(const HTTPResponse &response)
Handle HTTP response header received notification.
virtual void onReceivedResponseError(const HTTPResponse &response, const std::string &error)
Handle HTTP response error notification.
void onDisconnected() override
Handle client disconnected notification.
HTTPRequest & Clear()
Clear the HTTP request cache.
bool empty() const noexcept
Is the HTTP request empty?
bool error() const noexcept
Is the HTTP request error flag set?
bool error() const noexcept
Is the HTTP response error flag set?
HTTPResponse & Clear()
Clear the HTTP response cache.
C++ Server project definitions.