Update: Translations from eints
[openttd-github.git] / src / network / core / http_shared.h
blobec3ae79348baceb2f5d721242427c9e968a06a40
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 <http://www.gnu.org/licenses/>.
6 */
8 /**
9 * @file http_shared.h Shared functions for implementations of HTTP requests.
12 #ifndef NETWORK_CORE_HTTP_SHARED_H
13 #define NETWORK_CORE_HTTP_SHARED_H
15 #include "http.h"
17 #include <condition_variable>
18 #include <mutex>
19 #include <vector>
21 /** Converts a HTTPCallback to a Thread-Safe variant. */
22 class HTTPThreadSafeCallback {
23 private:
24 /** Entries on the queue for later handling. */
25 class Callback {
26 public:
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;
31 size_t length;
32 bool failure;
35 public:
36 /**
37 * Similar to HTTPCallback::OnFailure, but thread-safe.
39 void OnFailure()
41 std::lock_guard<std::mutex> lock(this->mutex);
42 this->queue.emplace_back();
45 /**
46 * Similar to HTTPCallback::OnReceiveData, but thread-safe.
48 void OnReceiveData(std::unique_ptr<char[]> data, size_t length)
50 std::lock_guard<std::mutex> lock(this->mutex);
51 this->queue.emplace_back(std::move(data), length);
54 /**
55 * Process everything on the queue.
57 * Should be called from the Game Thread.
59 void HandleQueue()
61 this->cancelled = callback->IsCancelled();
63 std::lock_guard<std::mutex> lock(this->mutex);
65 for (auto &item : this->queue) {
66 if (item.failure) {
67 this->callback->OnFailure();
68 } else {
69 this->callback->OnReceiveData(std::move(item.data), item.length);
73 this->queue.clear();
74 this->queue_cv.notify_all();
77 /**
78 * Wait till the queue is dequeued, or a condition is met.
79 * @param condition Condition functor.
81 template <typename T>
82 void WaitTillEmptyOrCondition(T condition)
84 std::unique_lock<std::mutex> lock(this->mutex);
86 while (!(queue.empty() || condition())) {
87 this->queue_cv.wait(lock);
91 /**
92 * Check if the queue is empty.
94 bool IsQueueEmpty()
96 std::lock_guard<std::mutex> lock(this->mutex);
97 return this->queue.empty();
100 HTTPThreadSafeCallback(HTTPCallback *callback) : callback(callback) {}
102 ~HTTPThreadSafeCallback()
104 std::lock_guard<std::mutex> lock(this->mutex);
106 /* Clear the list and notify explicitly. */
107 queue.clear();
108 queue_cv.notify_all();
111 std::atomic<bool> cancelled = false;
113 private:
114 HTTPCallback *callback; ///< The callback to send data back on.
115 std::mutex mutex; ///< Mutex to protect the queue.
116 std::vector<Callback> queue; ///< Queue of data to send back.
117 std::condition_variable queue_cv; ///< Condition variable to wait for the queue to be empty.
120 #endif /* NETWORK_CORE_HTTP_SHARED_H */