OpenTTD Source 20260218-master-g2123fca5ea
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
20class HTTPThreadSafeCallback {
21private:
23 class Callback {
24 public:
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) {}
27
28 std::unique_ptr<char[]> data;
29 size_t length;
30 bool failure;
31 };
32
33public:
37 void OnFailure()
38 {
39 std::lock_guard<std::mutex> lock(this->mutex);
40 this->queue.emplace_back();
41 }
42
47 void OnReceiveData(std::unique_ptr<char[]> data, size_t length)
48 {
49 std::lock_guard<std::mutex> lock(this->mutex);
50 this->queue.emplace_back(std::move(data), length);
51 }
52
59 {
60 this->cancelled = callback->IsCancelled();
61
62 std::lock_guard<std::mutex> lock(this->mutex);
63
64 for (auto &item : this->queue) {
65 if (item.failure) {
66 this->callback->OnFailure();
67 } else {
68 this->callback->OnReceiveData(std::move(item.data), item.length);
69 }
70 }
71
72 this->queue.clear();
73 this->queue_cv.notify_all();
74 }
75
80 template <typename T>
81 void WaitTillEmptyOrCondition(T condition)
82 {
83 std::unique_lock<std::mutex> lock(this->mutex);
84
85 while (!(queue.empty() || condition())) {
86 this->queue_cv.wait(lock);
87 }
88 }
89
95 {
96 std::lock_guard<std::mutex> lock(this->mutex);
97 return this->queue.empty();
98 }
99
101
102 ~HTTPThreadSafeCallback()
103 {
104 std::lock_guard<std::mutex> lock(this->mutex);
105
106 /* Clear the list and notify explicitly. */
107 queue.clear();
108 queue_cv.notify_all();
109 }
110
111 std::atomic<bool> cancelled = false;
112
113private:
115 std::mutex mutex;
116 std::vector<Callback> queue;
117 std::condition_variable queue_cv;
118};
119
120#endif /* NETWORK_CORE_HTTP_SHARED_H */
Converts a HTTPCallback to a Thread-Safe variant.
Definition http_shared.h:20
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:37
void OnReceiveData(std::unique_ptr< char[]> data, size_t length)
Similar to HTTPCallback::OnReceiveData, but thread-safe.
Definition http_shared.h:47
bool IsQueueEmpty()
Check if the queue is empty.
Definition http_shared.h:94
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:81
void HandleQueue()
Process everything on the queue.
Definition http_shared.h:58
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