OpenTTD Source: src/network/core/http_shared.h Source File

OpenTTD Source 14.0-beta3

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 <http://www.gnu.org/licenses/>.
6 */
7
12 #ifndef NETWORK_CORE_HTTP_SHARED_H
13 #define NETWORK_CORE_HTTP_SHARED_H
14
15 #include "http.h"
16
17 #include <condition_variable>
18 #include <mutex>
19 #include <vector>
20
22 class HTTPThreadSafeCallback {
23 private:
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) {}
29
30 std::unique_ptr<char[]> data;
31 size_t length;
32 bool failure;
33 };
34
35 public:
39 void OnFailure()
40 {
41 std::lock_guard<std::mutex> lock(this->mutex);
42 this->queue.emplace_back();
43 }
44
48 void OnReceiveData(std::unique_ptr<char[]> data, size_t length)
49 {
50 std::lock_guard<std::mutex> lock(this->mutex);
51 this->queue.emplace_back(std::move(data), length);
52 }
53
59 void HandleQueue()
60 {
61 this->cancelled = callback->IsCancelled();
62
63 std::lock_guard<std::mutex> lock(this->mutex);
64
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);
70 }
71 }
72
73 this->queue.clear();
74 this->queue_cv.notify_all();
75 }
76
81 template <typename T>
82 void WaitTillEmptyOrCondition(T condition)
83 {
84 std::unique_lock<std::mutex> lock(this->mutex);
85
86 while (!(queue.empty() || condition())) {
87 this->queue_cv.wait(lock);
88 }
89 }
90
94 bool IsQueueEmpty()
95 {
96 std::lock_guard<std::mutex> lock(this->mutex);
97 return this->queue.empty();
98 }
99
100 HTTPThreadSafeCallback(HTTPCallback *callback) : callback(callback) {}
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
113 private:
114 HTTPCallback *callback;
115 std::mutex mutex;
116 std::vector<Callback> queue;
117 std::condition_variable queue_cv;
118 };
119
120 #endif /* NETWORK_CORE_HTTP_SHARED_H */
HTTPCallback::OnReceiveData
virtual void OnReceiveData(std::unique_ptr< char[]> data, size_t length)=0
We're receiving data.
HTTPCallback
Callback for when the HTTP handler has something to tell us.
Definition: http.h:20
HTTPThreadSafeCallback::queue
std::vector< Callback > queue
Queue of data to send back.
Definition: http_shared.h:116
lock
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:35
HTTPThreadSafeCallback::callback
HTTPCallback * callback
The callback to send data back on.
Definition: http_shared.h:114
HTTPThreadSafeCallback::queue_cv
std::condition_variable queue_cv
Condition variable to wait for the queue to be empty.
Definition: http_shared.h:117
HTTPThreadSafeCallback::OnReceiveData
void OnReceiveData(std::unique_ptr< char[]> data, size_t length)
Similar to HTTPCallback::OnReceiveData, but thread-safe.
Definition: http_shared.h:48
HTTPThreadSafeCallback::WaitTillEmptyOrCondition
void WaitTillEmptyOrCondition(T condition)
Wait till the queue is dequeued, or a condition is met.
Definition: http_shared.h:82
HTTPCallback::OnFailure
virtual void OnFailure()=0
An error has occurred and the connection has been closed.
HTTPThreadSafeCallback::HandleQueue
void HandleQueue()
Process everything on the queue.
Definition: http_shared.h:59
HTTPThreadSafeCallback::Callback
Entries on the queue for later handling.
Definition: http_shared.h:25
HTTPThreadSafeCallback::OnFailure
void OnFailure()
Similar to HTTPCallback::OnFailure, but thread-safe.
Definition: http_shared.h:39
HTTPCallback::IsCancelled
virtual bool IsCancelled() const =0
Check if there is a request to cancel the transfer.
HTTPThreadSafeCallback::mutex
std::mutex mutex
Mutex to protect the queue.
Definition: http_shared.h:115
HTTPThreadSafeCallback::IsQueueEmpty
bool IsQueueEmpty()
Check if the queue is empty.
Definition: http_shared.h:94
http.h
HTTPThreadSafeCallback
Converts a HTTPCallback to a Thread-Safe variant.
Definition: http_shared.h:22