-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reactor.hpp
198 lines (179 loc) · 5.07 KB
/
Reactor.hpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#pragma once
// c++ utils
#include <functional>
#include <vector>
#include <memory>
#include <thread>
#include <algorithm>
#include <map>
#include <queue>
#include "utils.h"
#include "Channel.hpp"
#include "WorkerInterface.h"
#include "MiniLog.hpp"
#include "EpollWrapper.hpp"
class Reactor;
using SpReactor = std::shared_ptr<Reactor>;
class Reactor : public WorkerInterface, public std::enable_shared_from_this<Reactor>, noncopyable
{
public:
Reactor() = delete;
explicit Reactor(const std::string &);
~Reactor();
bool Work() override;
void SetThreadId(const std::thread::id &) override;
std::string GetName() const { return _name; };
void AddChannel(SpChannel, ChannelEvent_e);
void DelChannel(SpChannel);
void EnableEvents(SpChannel, ChannelEvent_e);
void DisableEvents(SpChannel, ChannelEvent_e);
void PushFunctor(std::function<void(void)>);
private:
void Init();
void wakeup();
bool isInSelfWorkThread() { return std::this_thread::get_id() == _thrdId; }
void handlePendingFunctors();
private:
bool _init;
std::string _name;
std::mutex _pendingMutex;
std::queue<std::function<void(void)>> _pendingFunctors;
SpEpoll _epoll;
bool _running;
std::thread::id _thrdId;
int _wakeUpFd[2];
};
static void wake_up_call_back(SpChannel chan)
{
SpReactor re = std::static_pointer_cast<Reactor>(chan->GetSpPrivData());
int fd = chan->GetSocket();
char one = {};
int n = read(fd, &one, sizeof(one));
if (n != sizeof(one))
{
minilog(LogLevel_e::ERROR, "[%s] wakeup failed!", re->GetName().c_str());
}
else
{
minilog(LogLevel_e::INFO, "[%s] wake up!", re->GetName().c_str());
}
}
Reactor::Reactor(const std::string &name) :
_name(name),
_epoll(CreateSpEpoll())
{
_init = false;
}
Reactor::~Reactor()
{
}
void Reactor::Init()
{
socketpair(AF_UNIX, SOCK_STREAM, 0, _wakeUpFd);
auto wakeUpChannel = CreateSpChannel(_wakeUpFd[1], shared_from_this(), wake_up_call_back, nullptr, nullptr);
_epoll->Add(wakeUpChannel, ChannelEvent_e::IN);
_init = true;
}
bool Reactor::Work()
{
if(!_init){
Init();
}
_epoll->PollOnce(1000);
handlePendingFunctors();
return true;
}
void Reactor::SetThreadId(const std::thread::id &id)
{
_thrdId = id;
}
void Reactor::AddChannel(SpChannel channel, ChannelEvent_e events)
{
PushFunctor([this, channel, events](){
if (_epoll->IsChannelInEpoll(channel))
{
minilog(LogLevel_e::WARRNIG, "[%s] channel(fd = %d) has already in epoll(fd = %d)", _name.c_str(), channel->GetSocket(), this->_epoll->GetEpollFd());
}
else{
minilog(LogLevel_e::DEBUG, "[%s] add channel(fd = %d, events = %d)", _name.c_str(), channel->GetSocket(), channel->GetEvents());
_epoll->Add(channel, events);
}
});
}
void Reactor::DelChannel(SpChannel channel)
{
PushFunctor([this, channel](){
if (!_epoll->IsChannelInEpoll(channel))
{
minilog(LogLevel_e::WARRNIG, "[%s] This channel(fd = %d) is not in epoll(fd = %d)", _name.c_str(), channel->GetSocket(), _epoll->GetEpollFd());
}
else{
minilog(LogLevel_e::DEBUG, "[%s] delete channel(fd = %d, events = %d)", _name.c_str(), channel->GetSocket(), channel->GetEvents());
_epoll->Delete(channel);
}
});
}
void Reactor::EnableEvents(SpChannel channel, ChannelEvent_e events)
{
PushFunctor([this, channel, events](){
if (!_epoll->IsChannelInEpoll(channel))
{
minilog(LogLevel_e::ERROR, "[%s] This channel(fd = %d) is not in epoll(fd = %d)", _name.c_str(), channel->GetSocket(), _epoll->GetEpollFd());
}
else{
ChannelEvent_e oldEvts = channel->GetEvents();
ChannelEvent_e newEvts = static_cast<ChannelEvent_e>(oldEvts | events);
_epoll->Modify(channel, newEvts);
minilog(LogLevel_e::DEBUG, "[%s] enable events %d, channel(fd = %d, events %d -> %d)", _name.c_str(), events, channel->GetSocket(), oldEvts, channel->GetEvents());
}
});
}
void Reactor::DisableEvents(SpChannel channel, ChannelEvent_e events){
PushFunctor([this, channel, events](){
if (!_epoll->IsChannelInEpoll(channel))
{
minilog(LogLevel_e::ERROR, "[%s] This channel(fd = %d) is not in epoll(fd = %d)", _name.c_str(), channel->GetSocket(), _epoll->GetEpollFd());
}
else{
ChannelEvent_e oldEvts = channel->GetEvents();
ChannelEvent_e newEvts = static_cast<ChannelEvent_e>(oldEvts & ~events);
_epoll->Modify(channel, newEvts);
minilog(LogLevel_e::DEBUG, "[%s] disable events %d, channel(fd = %d, events %d -> %d)", _name.c_str(), events, channel->GetSocket(), oldEvts, channel->GetEvents());
}
});
}
void Reactor::PushFunctor(std::function<void(void)> functor)
{
{
std::lock_guard<std::mutex> lock(_pendingMutex);
_pendingFunctors.push(functor);
}
if (!isInSelfWorkThread())
{
wakeup();
}
}
void Reactor::wakeup()
{
char one = '1';
auto n = write(_wakeUpFd[0], &one, sizeof(one));
if (n != sizeof(one))
{
minilog(LogLevel_e::ERROR, "can't wakeup this epoll loop");
}
}
void Reactor::handlePendingFunctors()
{
std::lock_guard<std::mutex> lock(_pendingMutex);
while (_pendingFunctors.size() > 0)
{
auto func = _pendingFunctors.front();
if(func) func();
_pendingFunctors.pop();
}
}
/*--------------- shared_ptr -----------*/
SpReactor CreateSpReactor(const std::string &name)
{
return std::make_shared<Reactor>(name);
}