OpenTTD Source 20260311-master-g511d3794ce
http_shared.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef NETWORK_CORE_HTTP_SHARED_H
11#define NETWORK_CORE_HTTP_SHARED_H
12
13#include "http.h"
14
15#include <condition_variable>
16#include <mutex>
17#include <vector>
18
21private:
23 class Callback {
24 public:
30 Callback(std::unique_ptr<char[]> data, size_t length) : data(std::move(data)), length(length), failure(false) {}
32 Callback() = default;
33
34 std::unique_ptr<char[]> data{};
35 size_t length = 0;
36 bool failure = true;
37 };
38
39public:
43 void OnFailure()
44 {
45 std::lock_guard<std::mutex> lock(this->mutex);
46 this->queue.emplace_back();
47 }
48
53 void OnReceiveData(std::unique_ptr<char[]> data, size_t length)
54 {
55 std::lock_guard<std::mutex> lock(this->mutex);
56 this->queue.emplace_back(std::move(data), length);
57 }
58
65 {
66 this->cancelled = callback->IsCancelled();
67
68 std::lock_guard<std::mutex> lock(this->mutex);
69
70 for (auto &item : this->queue) {
71 if (item.failure) {
72 this->callback->OnFailure();
73 } else {
74 this->callback->OnReceiveData(std::move(item.data), item.length);
75 }
76 }
77
78 this->queue.clear();
79 this->queue_cv.notify_all();
80 }
81
86 template <typename T>
88 {
89 std::unique_lock<std::mutex> lock(this->mutex);
90
91 while (!(queue.empty() || condition())) {
92 this->queue_cv.wait(lock);
93 }
94 }
95
101 {
102 std::lock_guard<std::mutex> lock(this->mutex);
103 return this->queue.empty();
104 }
105
111
114 {
115 std::lock_guard<std::mutex> lock(this->mutex);
116
117 /* Clear the list and notify explicitly. */
118 queue.clear();
119 queue_cv.notify_all();
120 }
121
122 std::atomic<bool> cancelled = false;
123
124private:
126 std::mutex mutex;
127 std::vector<Callback> queue;
128 std::condition_variable queue_cv;
129};
130
131#endif /* NETWORK_CORE_HTTP_SHARED_H */
Callback(std::unique_ptr< char[]> data, size_t length)
Create the callback.
Definition http_shared.h:30
bool failure
Whether the callback denotes a failure.
Definition http_shared.h:36
size_t length
The length of the data.
Definition http_shared.h:35
std::unique_ptr< char[]> data
The data of the callback.
Definition http_shared.h:34
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.
Definition http_shared.h:43
void OnReceiveData(std::unique_ptr< char[]> data, size_t length)
Similar to HTTPCallback::OnReceiveData, but thread-safe.
Definition http_shared.h:53
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.
Definition http_shared.h:87
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.
Definition http_shared.h:64
#define T
Climate temperate.
Definition engines.h:91
Basic functions to send and receive HTTP packets.
Callback for when the HTTP handler has something to tell us.
Definition http.h:18
std::mutex lock
synchronization for playback status fields
Definition win32_m.cpp:35