Skip to content

Commit

Permalink
Merge pull request markparticle#21 from markparticle/dev_mark
Browse files Browse the repository at this point in the history
🎨 Update reactor
  • Loading branch information
markparticle committed Jun 30, 2020
2 parents afcace7 + bce86ed commit 73c4d67
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 31 deletions.
2 changes: 1 addition & 1 deletion code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main() {
/* 守护进程 后台运行 */
//daemon(1, 0);
WebServer server(
1316, 3, 5000, true, false, /* 端口 ET模式 timeoutMs Proactor/Reactor(使用异步线程池) 优雅退出 */
1316, 3, 5000, false, /* 端口 ET模式 timeoutMs 优雅退出 */
3306, "root", "root", "webserver", /* Mysql配置 */
100, 6, true, 0, 0); /* 连接池数量 线程池数量 日志开关 日志等级 日志异步队列容量 */
server.Start();
Expand Down
2 changes: 1 addition & 1 deletion code/pool/threadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ThreadPool {
}

template<class F>
void addTask(F&& task) {
void AddTask(F&& task) {
{
std::lock_guard<std::mutex> locker(pool_->mtx);
pool_->tasks.emplace(std::forward<F>(task));
Expand Down
36 changes: 10 additions & 26 deletions code/server/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
using namespace std;

WebServer::WebServer(
int port, int trigMode, int timeoutMS, bool isReactor, bool OptLinger,
int sqlPort, const char* sqlUser, const char* sqlPwd,
const char* dbName, int connPoolNum, int threadNum,
bool openLog, int logLevel, int logQueSize):
port_(port), openLinger_(OptLinger), isReactor_(isReactor),
timeoutMS_(timeoutMS), isClose_(false), timer_(new HeapTimer()),
threadpool_(new ThreadPool(threadNum)), epoller_(new Epoller()) {

int port, int trigMode, int timeoutMS, bool OptLinger,
int sqlPort, const char* sqlUser, const char* sqlPwd,
const char* dbName, int connPoolNum, int threadNum,
bool openLog, int logLevel, int logQueSize):
port_(port), openLinger_(OptLinger), timeoutMS_(timeoutMS), isClose_(false),
timer_(new HeapTimer()), threadpool_(new ThreadPool(threadNum)), epoller_(new Epoller())
{
srcDir_ = getcwd(nullptr, 256);
assert(srcDir_);
strncat(srcDir_, "/resources/", 16);
Expand All @@ -32,16 +31,10 @@ WebServer::WebServer(
if(isClose_) { LOG_ERROR("========== Server init error!=========="); }
else {
LOG_INFO("========== Server init ==========");

LOG_INFO("Port:%d, OpenLinger: %s, IO Mode: %s",
port_, OptLinger? "true":"false",
isReactor_?"Reactor":"Proctor");


LOG_INFO("Port:%d, OpenLinger: %s", port_, OptLinger? "true":"false");
LOG_INFO("Listen Mode: %s, OpenConn Mode: %s",
(listenEvent_ & EPOLLET ? "ET": "LT"),
(connEvent_ & EPOLLET ? "ET": "LT"));

LOG_INFO("LogSys level: %d", logLevel);
LOG_INFO("srcDir: %s", HttpConn::srcDir);
LOG_INFO("SqlConnPool num: %d, ThreadPool num: %d", connPoolNum, threadNum);
Expand Down Expand Up @@ -97,7 +90,6 @@ void WebServer::Start() {
DealListen_();
}
else if(events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) {
LOG_DEBUG("!!!!!!!EVENT QUIT %d %d %d", events & EPOLLRDHUP, events & EPOLLHUP, EPOLLERR);
assert(users_.count(fd) > 0);
CloseConn_(&users_[fd]);
}
Expand Down Expand Up @@ -160,21 +152,13 @@ void WebServer::DealListen_() {
void WebServer::DealRead_(HttpConn* client) {
assert(client);
ExtentTime_(client);
if(isReactor_) {
threadpool_->addTask(std::bind(&WebServer::OnRead_, this, client));
} else {
OnRead_(client);
}
threadpool_->AddTask(std::bind(&WebServer::OnRead_, this, client));
}

void WebServer::DealWrite_(HttpConn* client) {
assert(client);
ExtentTime_(client);
if(isReactor_) {
threadpool_->addTask(std::bind(&WebServer::OnWrite_, this, client));
} else {
OnWrite_(client);
}
threadpool_->AddTask(std::bind(&WebServer::OnWrite_, this, client));
}

void WebServer::ExtentTime_(HttpConn* client) {
Expand Down
3 changes: 1 addition & 2 deletions code/server/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class WebServer {
public:
WebServer(
int port, int trigMode, int timeoutMS, bool isReactor, bool OptLinger,
int port, int trigMode, int timeoutMS, bool OptLinger,
int sqlPort, const char* sqlUser, const char* sqlPwd,
const char* dbName, int connPoolNum, int threadNum,
bool openLog, int logLevel, int logQueSize);
Expand Down Expand Up @@ -56,7 +56,6 @@ class WebServer {

int port_;
bool openLinger_;
bool isReactor_;
int timeoutMS_; /* 毫秒MS */
bool isClose_;
int listenFd_;
Expand Down
2 changes: 1 addition & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void TestThreadPool() {
Log::Instance()->init(0, "./testThreadpool", ".log", 5000);
ThreadPool threadpool(6);
for(int i = 0; i < 18; i++) {
threadpool.addTask(std::bind(ThreadLogTask, i % 4, i * 10000));
threadpool.AddTask(std::bind(ThreadLogTask, i % 4, i * 10000));
}
getchar();
}
Expand Down

0 comments on commit 73c4d67

Please sign in to comment.