OpenTTD Source
20240919-master-gdf0233f4c2
|
Go to the documentation of this file.
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;
virtual void OnReceiveData(std::unique_ptr< char[]> data, size_t length)=0
We're receiving data.
Callback for when the HTTP handler has something to tell us.
std::vector< Callback > queue
Queue of data to send back.
std::mutex lock
synchronization for playback status fields
HTTPCallback * callback
The callback to send data back on.
std::condition_variable queue_cv
Condition variable to wait for the queue to be empty.
void OnReceiveData(std::unique_ptr< char[]> data, size_t length)
Similar to HTTPCallback::OnReceiveData, but thread-safe.
void WaitTillEmptyOrCondition(T condition)
Wait till the queue is dequeued, or a condition is met.
virtual void OnFailure()=0
An error has occurred and the connection has been closed.
void HandleQueue()
Process everything on the queue.
Entries on the queue for later handling.
void OnFailure()
Similar to HTTPCallback::OnFailure, but thread-safe.
virtual bool IsCancelled() const =0
Check if there is a request to cancel the transfer.
std::mutex mutex
Mutex to protect the queue.
bool IsQueueEmpty()
Check if the queue is empty.
Converts a HTTPCallback to a Thread-Safe variant.