forked from nefarius/sm-ext-socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CallbackHandler.cpp
61 lines (46 loc) · 1.38 KB
/
CallbackHandler.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
#include "CallbackHandler.h"
#include <iostream>
#include "Callback.h"
void CallbackHandler::AddCallback(Callback* callback) {
boost::mutex::scoped_lock l(callbackQueueMutex);
if (!callback->IsValid()) {
std::cout << "[SERR] invalid callback (event=" << callback->callbackEvent << ")" << std::endl;
delete callback;
} else {
callbackQueue.push_back(callback);
}
}
void CallbackHandler::RemoveCallbacks(SocketWrapper* sw) {
boost::mutex::scoped_lock l(callbackQueueMutex);
for (std::deque<Callback*>::iterator it=callbackQueue.begin(); it!=callbackQueue.end(); ) {
if ((*it)->socketWrapper == sw) {
/*if (!(*it)->isExecuting)*/ delete *it;
it = callbackQueue.erase(it);
} else {
it++;
}
}
}
void CallbackHandler::ExecuteQueuedCallbacks() {
Callback* cb = FetchFirstCallback();
if (!cb) return;
// cb->isExecuting = true;
cb->Execute();
delete cb;
}
Callback* CallbackHandler::FetchFirstCallback() {
boost::mutex::scoped_lock l(callbackQueueMutex);
if (!callbackQueue.empty()) {
for (std::deque<Callback*>::iterator it=callbackQueue.begin(); it!=callbackQueue.end(); it++) {
Callback* ret = callbackQueue.front();
if (!ret->IsExecutable()) {
std::cout << "[SERR] callback not executable (event=" << ret->callbackEvent << ")" << std::endl;
continue;
}
callbackQueue.erase(it);
return ret;
}
}
return NULL;
}
CallbackHandler callbackHandler;