OpenTTD Source 20260109-master-g241b5fcdfe
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
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:
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
46 void OnReceiveData(std::unique_ptr<char[]> data, size_t length)
47 {
48 std::lock_guard<std::mutex> lock(this->mutex);
49 this->queue.emplace_back(std::move(data), length);
50 }
51
58 {
59 this->cancelled = callback->IsCancelled();
60
61 std::lock_guard<std::mutex> lock(this->mutex);
62
63 for (auto &item : this->queue) {
64 if (item.failure) {
65 this->callback->OnFailure();
66 } else {
67 this->callback->OnReceiveData(std::move(item.data), item.length);
68 }
69 }
70
71 this->queue.clear();
72 this->queue_cv.notify_all();
73 }
74
79 template <typename T>
80 void WaitTillEmptyOrCondition(T condition)
81 {
82 std::unique_lock<std::mutex> lock(this->mutex);
83
84 while (!(queue.empty() || condition())) {
85 this->queue_cv.wait(lock);
86 }
87 }
88
93 {
94 std::lock_guard<std::mutex> lock(this->mutex);
95 return this->queue.empty();
96 }
97
99
101 {
102 std::lock_guard<std::mutex> lock(this->mutex);
103
104 /* Clear the list and notify explicitly. */
105 queue.clear();
106 queue_cv.notify_all();
107 }
108
109 std::atomic<bool> cancelled = false;
110
111private:
113 std::mutex mutex;
114 std::vector<Callback> queue;
115 std::condition_variable queue_cv;
116};
117
118#endif /* NETWORK_CORE_HTTP_SHARED_H */
Entries on the queue for later handling.
Definition http_shared.h:23
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:46
bool IsQueueEmpty()
Check if the queue is empty.
Definition http_shared.h:92
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:80
void HandleQueue()
Process everything on the queue.
Definition http_shared.h:57
Basic functions to send and receive HTTP packets.
Callback for when the HTTP handler has something to tell us.
Definition http.h:18
virtual bool IsCancelled() const =0
Check if there is a request to cancel the transfer.
virtual void OnFailure()=0
An error has occurred and the connection has been closed.
virtual void OnReceiveData(std::unique_ptr< char[]> data, size_t length)=0
We're receiving data.
std::mutex lock
synchronization for playback status fields
Definition win32_m.cpp:35