Skip to content

Commit bce86ed

Browse files
committed
🎨 Update reactor
1 parent 77c363d commit bce86ed

File tree

5 files changed

+14
-31
lines changed

5 files changed

+14
-31
lines changed

code/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int main() {
1010
/* 守护进程 后台运行 */
1111
//daemon(1, 0);
1212
WebServer server(
13-
1316, 3, 5000, true, false, /* 端口 ET模式 timeoutMs Proactor/Reactor(使用异步线程池) 优雅退出 */
13+
1316, 3, 5000, false, /* 端口 ET模式 timeoutMs 优雅退出 */
1414
3306, "root", "root", "webserver", /* Mysql配置 */
1515
100, 6, true, 0, 0); /* 连接池数量 线程池数量 日志开关 日志等级 日志异步队列容量 */
1616
server.Start();

code/pool/threadpool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ThreadPool {
4949
}
5050

5151
template<class F>
52-
void addTask(F&& task) {
52+
void AddTask(F&& task) {
5353
{
5454
std::lock_guard<std::mutex> locker(pool_->mtx);
5555
pool_->tasks.emplace(std::forward<F>(task));

code/server/webserver.cpp

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
using namespace std;
1010

1111
WebServer::WebServer(
12-
int port, int trigMode, int timeoutMS, bool isReactor, bool OptLinger,
13-
int sqlPort, const char* sqlUser, const char* sqlPwd,
14-
const char* dbName, int connPoolNum, int threadNum,
15-
bool openLog, int logLevel, int logQueSize):
16-
port_(port), openLinger_(OptLinger), isReactor_(isReactor),
17-
timeoutMS_(timeoutMS), isClose_(false), timer_(new HeapTimer()),
18-
threadpool_(new ThreadPool(threadNum)), epoller_(new Epoller()) {
19-
12+
int port, int trigMode, int timeoutMS, bool OptLinger,
13+
int sqlPort, const char* sqlUser, const char* sqlPwd,
14+
const char* dbName, int connPoolNum, int threadNum,
15+
bool openLog, int logLevel, int logQueSize):
16+
port_(port), openLinger_(OptLinger), timeoutMS_(timeoutMS), isClose_(false),
17+
timer_(new HeapTimer()), threadpool_(new ThreadPool(threadNum)), epoller_(new Epoller())
18+
{
2019
srcDir_ = getcwd(nullptr, 256);
2120
assert(srcDir_);
2221
strncat(srcDir_, "/resources/", 16);
@@ -32,16 +31,10 @@ WebServer::WebServer(
3231
if(isClose_) { LOG_ERROR("========== Server init error!=========="); }
3332
else {
3433
LOG_INFO("========== Server init ==========");
35-
36-
LOG_INFO("Port:%d, OpenLinger: %s, IO Mode: %s",
37-
port_, OptLinger? "true":"false",
38-
isReactor_?"Reactor":"Proctor");
39-
40-
34+
LOG_INFO("Port:%d, OpenLinger: %s", port_, OptLinger? "true":"false");
4135
LOG_INFO("Listen Mode: %s, OpenConn Mode: %s",
4236
(listenEvent_ & EPOLLET ? "ET": "LT"),
4337
(connEvent_ & EPOLLET ? "ET": "LT"));
44-
4538
LOG_INFO("LogSys level: %d", logLevel);
4639
LOG_INFO("srcDir: %s", HttpConn::srcDir);
4740
LOG_INFO("SqlConnPool num: %d, ThreadPool num: %d", connPoolNum, threadNum);
@@ -97,7 +90,6 @@ void WebServer::Start() {
9790
DealListen_();
9891
}
9992
else if(events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) {
100-
LOG_DEBUG("!!!!!!!EVENT QUIT %d %d %d", events & EPOLLRDHUP, events & EPOLLHUP, EPOLLERR);
10193
assert(users_.count(fd) > 0);
10294
CloseConn_(&users_[fd]);
10395
}
@@ -160,21 +152,13 @@ void WebServer::DealListen_() {
160152
void WebServer::DealRead_(HttpConn* client) {
161153
assert(client);
162154
ExtentTime_(client);
163-
if(isReactor_) {
164-
threadpool_->addTask(std::bind(&WebServer::OnRead_, this, client));
165-
} else {
166-
OnRead_(client);
167-
}
155+
threadpool_->AddTask(std::bind(&WebServer::OnRead_, this, client));
168156
}
169157

170158
void WebServer::DealWrite_(HttpConn* client) {
171159
assert(client);
172160
ExtentTime_(client);
173-
if(isReactor_) {
174-
threadpool_->addTask(std::bind(&WebServer::OnWrite_, this, client));
175-
} else {
176-
OnWrite_(client);
177-
}
161+
threadpool_->AddTask(std::bind(&WebServer::OnWrite_, this, client));
178162
}
179163

180164
void WebServer::ExtentTime_(HttpConn* client) {

code/server/webserver.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
class WebServer {
2727
public:
2828
WebServer(
29-
int port, int trigMode, int timeoutMS, bool isReactor, bool OptLinger,
29+
int port, int trigMode, int timeoutMS, bool OptLinger,
3030
int sqlPort, const char* sqlUser, const char* sqlPwd,
3131
const char* dbName, int connPoolNum, int threadNum,
3232
bool openLog, int logLevel, int logQueSize);
@@ -56,7 +56,6 @@ class WebServer {
5656

5757
int port_;
5858
bool openLinger_;
59-
bool isReactor_;
6059
int timeoutMS_; /* 毫秒MS */
6160
bool isClose_;
6261
int listenFd_;

test/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void TestThreadPool() {
3939
Log::Instance()->init(0, "./testThreadpool", ".log", 5000);
4040
ThreadPool threadpool(6);
4141
for(int i = 0; i < 18; i++) {
42-
threadpool.addTask(std::bind(ThreadLogTask, i % 4, i * 10000));
42+
threadpool.AddTask(std::bind(ThreadLogTask, i % 4, i * 10000));
4343
}
4444
getchar();
4545
}

0 commit comments

Comments
 (0)