OpenTTD Source 20260304-master-g1baaa74679
HTTPThreadSafeCallback Class Reference

Converts a HTTPCallback to a Thread-Safe variant. More...

#include <http_shared.h>

Data Structures

class  Callback
 Entries on the queue for later handling. More...

Public Member Functions

void OnFailure ()
 Similar to HTTPCallback::OnFailure, but thread-safe.
void OnReceiveData (std::unique_ptr< char[]> data, size_t length)
 Similar to HTTPCallback::OnReceiveData, but thread-safe.
void HandleQueue ()
 Process everything on the queue.
template<typename T>
void WaitTillEmptyOrCondition (T condition)
 Wait till the queue is dequeued, or a condition is met.
bool IsQueueEmpty ()
 Check if the queue is empty.
 HTTPThreadSafeCallback (HTTPCallback *callback)
 ~HTTPThreadSafeCallback ()
 Ensure our queues are emptied while holding a lock.

Data Fields

std::atomic< bool > cancelled = false

Private Attributes

HTTPCallbackcallback
 The callback to send data back on.
std::mutex mutex
 Mutex to protect the queue.
std::vector< Callbackqueue
 Queue of data to send back.
std::condition_variable queue_cv
 Condition variable to wait for the queue to be empty.

Detailed Description

Converts a HTTPCallback to a Thread-Safe variant.

Definition at line 20 of file http_shared.h.

Constructor & Destructor Documentation

◆ HTTPThreadSafeCallback()

HTTPThreadSafeCallback::HTTPThreadSafeCallback ( HTTPCallback * callback)
inline

Definition at line 100 of file http_shared.h.

◆ ~HTTPThreadSafeCallback()

HTTPThreadSafeCallback::~HTTPThreadSafeCallback ( )
inline

Ensure our queues are emptied while holding a lock.

Definition at line 103 of file http_shared.h.

References lock, mutex, queue, and queue_cv.

Member Function Documentation

◆ HandleQueue()

void HTTPThreadSafeCallback::HandleQueue ( )
inline

Process everything on the queue.

Should be called from the Game Thread.

Definition at line 58 of file http_shared.h.

References callback, lock, mutex, queue, and queue_cv.

◆ IsQueueEmpty()

bool HTTPThreadSafeCallback::IsQueueEmpty ( )
inline

Check if the queue is empty.

Returns
true iff the queue is empty.

Definition at line 94 of file http_shared.h.

References lock, mutex, and queue.

◆ OnFailure()

void HTTPThreadSafeCallback::OnFailure ( )
inline

Similar to HTTPCallback::OnFailure, but thread-safe.

Definition at line 37 of file http_shared.h.

References lock, mutex, and queue.

◆ OnReceiveData()

void HTTPThreadSafeCallback::OnReceiveData ( std::unique_ptr< char[]> data,
size_t length )
inline

Similar to HTTPCallback::OnReceiveData, but thread-safe.

We're receiving data.

Parameters
datathe received data, nullptr when all data has been received.
lengththe amount of received data, 0 when all data has been received.
Note
When nullptr is sent the HTTP socket handler is closed/freed.

Definition at line 47 of file http_shared.h.

References lock, mutex, and queue.

◆ WaitTillEmptyOrCondition()

template<typename T>
void HTTPThreadSafeCallback::WaitTillEmptyOrCondition ( T condition)
inline

Wait till the queue is dequeued, or a condition is met.

Parameters
conditionCondition functor.

Definition at line 81 of file http_shared.h.

References lock, mutex, queue, queue_cv, and T.

Field Documentation

◆ callback

HTTPCallback* HTTPThreadSafeCallback::callback
private

The callback to send data back on.

Definition at line 115 of file http_shared.h.

Referenced by HandleQueue().

◆ cancelled

std::atomic<bool> HTTPThreadSafeCallback::cancelled = false

Definition at line 112 of file http_shared.h.

◆ mutex

std::mutex HTTPThreadSafeCallback::mutex
private

Mutex to protect the queue.

Definition at line 116 of file http_shared.h.

Referenced by HandleQueue(), IsQueueEmpty(), OnFailure(), OnReceiveData(), WaitTillEmptyOrCondition(), and ~HTTPThreadSafeCallback().

◆ queue

std::vector<Callback> HTTPThreadSafeCallback::queue
private

Queue of data to send back.

Definition at line 117 of file http_shared.h.

Referenced by HandleQueue(), IsQueueEmpty(), OnFailure(), OnReceiveData(), WaitTillEmptyOrCondition(), and ~HTTPThreadSafeCallback().

◆ queue_cv

std::condition_variable HTTPThreadSafeCallback::queue_cv
private

Condition variable to wait for the queue to be empty.

Definition at line 118 of file http_shared.h.

Referenced by HandleQueue(), WaitTillEmptyOrCondition(), and ~HTTPThreadSafeCallback().


The documentation for this class was generated from the following file: