12#ifndef NETWORK_CORE_HTTP_SHARED_H
13#define NETWORK_CORE_HTTP_SHARED_H
17#include <condition_variable>
27 Callback(std::unique_ptr<
char[]> data,
size_t length) : data(std::move(data)), length(length), failure(
false) {}
28 Callback() : data(
nullptr), length(0), failure(
true) {}
30 std::unique_ptr<char[]> data;
41 std::lock_guard<std::mutex>
lock(this->
mutex);
42 this->
queue.emplace_back();
50 std::lock_guard<std::mutex>
lock(this->
mutex);
51 this->
queue.emplace_back(std::move(data), length);
63 std::lock_guard<std::mutex>
lock(this->
mutex);
65 for (
auto &item : this->
queue) {
84 std::unique_lock<std::mutex>
lock(this->
mutex);
86 while (!(
queue.empty() || condition())) {
96 std::lock_guard<std::mutex>
lock(this->
mutex);
97 return this->
queue.empty();
104 std::lock_guard<std::mutex>
lock(this->
mutex);
111 std::atomic<bool> cancelled =
false;
Entries on the queue for later handling.
Converts a HTTPCallback to a Thread-Safe variant.
HTTPCallback * callback
The callback to send data back on.
std::mutex mutex
Mutex to protect the queue.
std::condition_variable queue_cv
Condition variable to wait for the queue to be empty.
void OnFailure()
Similar to HTTPCallback::OnFailure, but thread-safe.
void OnReceiveData(std::unique_ptr< char[]> data, size_t length)
Similar to HTTPCallback::OnReceiveData, but thread-safe.
bool IsQueueEmpty()
Check if the queue is empty.
std::vector< Callback > queue
Queue of data to send back.
void WaitTillEmptyOrCondition(T condition)
Wait till the queue is dequeued, or a condition is met.
void HandleQueue()
Process everything on the queue.
Basic functions to send and receive HTTP packets.
Callback for when the HTTP handler has something to tell us.
virtual bool IsCancelled() const =0
Check if there is a request to cancel the transfer.
virtual void OnFailure()=0
An error has occurred and the connection has been closed.
virtual void OnReceiveData(std::unique_ptr< char[]> data, size_t length)=0
We're receiving data.
std::mutex lock
synchronization for playback status fields