-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt_thread_t.cpp
73 lines (63 loc) · 2.09 KB
/
crypt_thread_t.cpp
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
//
// Created by macskas on 12/17/20.
//
#include <thread>
#include "log.h"
#include "crypt_thread_t.h"
crypt_thread_t::crypt_thread_t(int myTheadId) {
this->shutdown_requested = false;
this->myThread = new std::thread(crypt_thread_t::loop_proxy, this);
this->thread_id = myTheadId;
}
crypt_thread_t::~crypt_thread_t() {
if (this->myThread) {
delete this->myThread;
this->myThread = nullptr;
}
}
void crypt_thread_t::join() {
this->shutdown_requested = true;
this->cv.notify_one();
debug_sprintf("[%s] join.", __PRETTY_FUNCTION__ );
if (this->myThread) {
this->myThread->join();
}
debug_sprintf("[%s] joined.", __PRETTY_FUNCTION__ );
}
void crypt_thread_t::add(int method, network_client *nc) {
std::unique_lock<std::mutex> lck(mtx);
this->queue_items.push(crypt_queue_item_t(method, nc));
lck.unlock();
this->cv.notify_one();
}
void crypt_thread_t::loop_proxy(class crypt_thread_t *ct) {
ct->loop();
}
void crypt_thread_t::loop() {
debug_sprintf("[%s] started.", __PRETTY_FUNCTION__ );
do {
std::unique_lock<std::mutex> lck(mtx);
cv.wait(lck, [this]{
return (!this->queue_items.empty() || this->shutdown_requested);
});
if (this->shutdown_requested && this->queue_items.empty())
continue;
crypt_queue_item_t queueItem = std::move(this->queue_items.front());
this->queue_items.pop();
lck.unlock();
if (queueItem.method == THREADMANAGER_METHOD_DECRYPT_PACKET) {
if (this->CI) {
queueItem.networkClient->set_CI(this->CI);
queueItem.networkClient->process_queue(this->thread_id);
}
}
lck.lock();
} while (!this->shutdown_requested);
debug_sprintf("[%s] finished.", __PRETTY_FUNCTION__ );
}
void crypt_thread_t::set_CI(struct crypt_instance *myCI) {
this->CI = myCI;
}
struct crypt_instance * crypt_thread_t::get_CI() {
return this->CI;
}