10#ifndef NETWORK_CORE_HTTP_SHARED_H
11#define NETWORK_CORE_HTTP_SHARED_H
15#include <condition_variable>
20class HTTPThreadSafeCallback {
25 Callback(std::unique_ptr<
char[]> data,
size_t length) : data(std::move(data)), length(length), failure(
false) {}
26 Callback() : data(
nullptr), length(0), failure(
true) {}
28 std::unique_ptr<char[]> data;
39 std::lock_guard<std::mutex>
lock(this->
mutex);
40 this->
queue.emplace_back();
48 std::lock_guard<std::mutex>
lock(this->
mutex);
49 this->
queue.emplace_back(std::move(data), length);
59 this->cancelled =
callback->IsCancelled();
61 std::lock_guard<std::mutex>
lock(this->
mutex);
63 for (
auto &item : this->
queue) {
67 this->
callback->OnReceiveData(std::move(item.data), item.length);
82 std::unique_lock<std::mutex>
lock(this->
mutex);
84 while (!(
queue.empty() || condition())) {
94 std::lock_guard<std::mutex>
lock(this->
mutex);
95 return this->
queue.empty();
100 ~HTTPThreadSafeCallback()
102 std::lock_guard<std::mutex>
lock(this->
mutex);
109 std::atomic<bool> cancelled =
false;
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.
std::mutex lock
synchronization for playback status fields