10#ifndef NETWORK_CORE_HTTP_SHARED_H
11#define NETWORK_CORE_HTTP_SHARED_H
15#include <condition_variable>
34 std::unique_ptr<char[]>
data{};
45 std::lock_guard<std::mutex>
lock(this->
mutex);
46 this->
queue.emplace_back();
55 std::lock_guard<std::mutex>
lock(this->
mutex);
56 this->
queue.emplace_back(std::move(data), length);
68 std::lock_guard<std::mutex>
lock(this->
mutex);
70 for (
auto &item : this->
queue) {
74 this->
callback->OnReceiveData(std::move(item.data), item.length);
89 std::unique_lock<std::mutex>
lock(this->
mutex);
91 while (!(
queue.empty() || condition())) {
102 std::lock_guard<std::mutex>
lock(this->
mutex);
103 return this->
queue.empty();
115 std::lock_guard<std::mutex>
lock(this->
mutex);
Callback(std::unique_ptr< char[]> data, size_t length)
Create the callback.
bool failure
Whether the callback denotes a failure.
size_t length
The length of the data.
std::unique_ptr< char[]> data
The data of the callback.
Callback()=default
Default constructor for a failed callback.
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.
HTTPThreadSafeCallback(HTTPCallback *callback)
Create the thread safe callback.
~HTTPThreadSafeCallback()
Ensure our queues are emptied while holding a lock.
std::atomic< bool > cancelled
Whether this callback has been cancelled, or not.
void HandleQueue()
Process everything on the queue.
#define T
Climate temperate.
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