-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHandler.cpp
More file actions
111 lines (94 loc) · 1.83 KB
/
StringHandler.cpp
File metadata and controls
111 lines (94 loc) · 1.83 KB
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
#include "StringHandler.h"
#include <chrono>
StringHandler::StringHandler() : task_executer(execute, this)
{
}
StringHandler::~StringHandler()
{
task_executer.detach();
// Wait until executer thread exits to avoid 0xC0000005, prob. there is a better way to handle this.
thread_mutex.lock();
thread_mutex.unlock();
}
void StringHandler::execute(StringHandler* i)
{
i->thread_mutex.lock();
while (i->task_executer.joinable())
{
if (i->task_count() > 0)
{
task t = i->get_task();
t.o.f(t.packet);
i->remove_task();
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
i->thread_mutex.unlock();
}
void StringHandler::search(std::string packet)
{
for (ops& f : filters)
{
for (std::string prefix : f.filters)
{
if (packet._Starts_with(prefix))
{
if (f.f != NULL)
add_task(packet, f);
}
}
}
}
void StringHandler::add_filter(std::string name, std::string filter)
{
for (ops& f : filters)
{
if (f.name.compare(name) == 0)
{
f.filters.push_back(filter);
return;
}
}
ops f;
f.name = name;
f.filters.push_back(filter);
filters.push_back(f);
}
void StringHandler::on(std::string name, std::function<void(std::string packet)> callback)
{
// Assign callback function to filter
for (ops& f : filters)
{
if (f.name.compare(name) == 0)
{
f.f = callback;
return;
}
}
ops f;
f.name = name;
f.f = callback;
filters.push_back(f);
}
int StringHandler::task_count()
{
std::lock_guard<std::mutex> m(task_mutex);
return tasks.size();
}
task StringHandler::get_task()
{
std::lock_guard<std::mutex> m(task_mutex);
return tasks.front();
}
void StringHandler::add_task(std::string packet, ops& o)
{
task_mutex.lock();
tasks.push_back(task(packet, o));
task_mutex.unlock();
}
void StringHandler::remove_task()
{
task_mutex.lock();
tasks.pop_front();
task_mutex.unlock();
}