-
Notifications
You must be signed in to change notification settings - Fork 3
/
AsyncChannel.h
147 lines (137 loc) · 4.45 KB
/
AsyncChannel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef LIB_OPEN_VPN_ASYNC_CHANNEL_H
#define LIB_OPEN_VPN_ASYNC_CHANNEL_H
#include <queue>
#include <mutex>
#include <future>
#include <atomic>
#include <condition_variable>
#include <memory>
#include <optional>
#include <chrono>
template <class Reader, class Buffer>
class AsyncChannel
{
public:
AsyncChannel(bool startTaskExecutor)
{
if (startTaskExecutor)
tasksThread = std::thread(&AsyncChannel::taskExecutor, this);
}
~AsyncChannel()
{
//Blocks until taskExecutor loop stops. It's not good to block but what can I do?
stop();
if (tasksThread.joinable())
tasksThread.join();
}
//Read call, to be filled with a buffer
void emplace_reader(std::shared_ptr<Reader> reader)
{
{
std::unique_lock<std::mutex> lock{readerMutex};
//std::cout << "emplacing reader with size " << reader->size() << std::endl;
readerFifo.emplace(reader);
}
tasksConditionVariable.notify_all();
}
//Buffer that fills read call
void emplace_buffer(Buffer buffer)
{
{
std::unique_lock<std::mutex> lock{bufferMutex};
bufferFifo.emplace(buffer);
}
tasksConditionVariable.notify_all();
}
//Direct read, without queueing a read handler
std::optional<Buffer> read_all()
{
throw std::runtime_error("read_all deprecated");
{
std::unique_lock<std::mutex> bufferLock{bufferMutex};
if (!bufferFifo.empty())
{
auto b = bufferFifo.front();
bufferFifo.pop();
return b;
}
else
{
return std::nullopt;
}
}
}
//Direct read, without queueing a read handler
bool read_just(size_t just, std::function<void(uint8_t*, uint8_t*)> onConsume)
{
std::unique_lock<std::mutex> bufferLock{bufferMutex};
if (!bufferFifo.empty())
{
//b is a reference
auto b = bufferFifo.front();
//Consumes up to `just` bytes from `b`
size_t n = b.consume(just, onConsume);
if (!b.stillHasData()) {
//we can pop, because we consumed the entire buffer
bufferFifo.pop();
} else {
//no pop, because there is more to consume
}
//returns true because we consumed the buffer
return true;
}
else
{
//returns false because we did not consume the buffer
return false;
}
}
void stop()
{
shouldContinue.store(false);
}
void taskExecutor()
{
while (shouldContinue.load())
{
using namespace std::chrono_literals;
std::unique_lock<std::mutex> lock{tasksMutex};
tasksConditionVariable.wait_for(lock, 35ms);
{
if (!readerFifo.empty() && !bufferFifo.empty())
{
auto r = readerFifo.front();
auto b = bufferFifo.front();
//Tries to consume, from `b`, `r->size()` bytes
size_t amountConsumed = b.consume(r->size(), [&r](const uint8_t* begin, const uint8_t* end){
//The amount consumed from `b`
size_t n = end-begin;
//Fills r with `n` bytes from `b`
r->receive(begin, n);
});
if (!b.stillHasData()) {
//We consumed everything from `b`, we can pop `b` (`r` is always popped)
bufferFifo.pop();
readerFifo.pop();
} else {
//There's more to consume from `b`, we cannot pop `b`
readerFifo.pop();
}
//Deliver the buffer to the reader
r->deliver();
}
}
}
}
private:
//TODO: add size control to these queues so it won't eat all RAM in case reader or buffer stops emplacing
std::queue<Buffer> bufferFifo;
std::queue<std::shared_ptr<Reader>> readerFifo;
std::mutex tasksMutex;
std::thread tasksThread;
std::condition_variable tasksConditionVariable;
std::mutex bufferMutex;
std::mutex readerMutex;
std::atomic<bool> shouldContinue{true};
};
#endif //LIB_OPEN_VPN_ASYNC_CHANNEL_H